Skip to content

Commit

Permalink
refactor(blockifier): share send_message code
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Nov 27, 2024
1 parent f8826d7 commit 6ed9532
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
14 changes: 4 additions & 10 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use starknet_api::transaction::fields::{Calldata, ContractAddressSalt};
use starknet_api::transaction::{EventContent, EventData, EventKey, L2ToL1Payload};
use starknet_types_core::felt::Felt;

use crate::execution::call_info::{MessageToL1, OrderedL2ToL1Message, Retdata};
use crate::execution::call_info::{MessageToL1, Retdata};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext};
use crate::execution::errors::EntryPointExecutionError;
Expand Down Expand Up @@ -436,17 +436,11 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
) -> SyscallResult<()> {
self.pre_execute_syscall(remaining_gas, self.gas_costs().send_message_to_l1_gas_cost)?;

let order = self.base.context.n_sent_messages_to_l1;
let to_address = EthAddress::try_from(to_address)
.map_err(|e| self.handle_error(remaining_gas, e.into()))?;
self.base.l2_to_l1_messages.push(OrderedL2ToL1Message {
order,
message: MessageToL1 { to_address, payload: L2ToL1Payload(payload.to_vec()) },
});

self.base.context.n_sent_messages_to_l1 += 1;
.map_err(|err| self.handle_error(remaining_gas, err.into()))?;
let message = MessageToL1 { to_address, payload: L2ToL1Payload(payload.to_vec()) };

Ok(())
self.base.send_message_to_l1(message).map_err(|err| self.handle_error(remaining_gas, err))
}

fn keccak(&mut self, input: &[u64], remaining_gas: &mut u64) -> SyscallResult<U256> {
Expand Down
11 changes: 2 additions & 9 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use self::hint_processor::{
SyscallExecutionError,
SyscallHintProcessor,
};
use crate::execution::call_info::{MessageToL1, OrderedL2ToL1Message};
use crate::execution::call_info::MessageToL1;
use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use crate::execution::entry_point::{CallEntryPoint, CallType};
use crate::execution::execution_utils::{
Expand Down Expand Up @@ -487,14 +487,7 @@ pub fn send_message_to_l1(
syscall_handler: &mut SyscallHintProcessor<'_>,
_remaining_gas: &mut u64,
) -> SyscallResult<SendMessageToL1Response> {
let execution_context = &mut syscall_handler.base.context;
let ordered_message_to_l1 = OrderedL2ToL1Message {
order: execution_context.n_sent_messages_to_l1,
message: request.message,
};
syscall_handler.base.l2_to_l1_messages.push(ordered_message_to_l1);
execution_context.n_sent_messages_to_l1 += 1;

syscall_handler.base.send_message_to_l1(request.message)?;
Ok(SendMessageToL1Response {})
}

Expand Down
11 changes: 10 additions & 1 deletion crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use starknet_types_core::felt::Felt;

use super::exceeds_event_size_limit;
use crate::abi::constants;
use crate::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message};
use crate::execution::call_info::{CallInfo, MessageToL1, OrderedEvent, OrderedL2ToL1Message};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::entry_point::{
CallEntryPoint,
Expand Down Expand Up @@ -201,6 +201,15 @@ impl<'state> SyscallHandlerBase<'state> {
Ok((deployed_contract_address, call_info))
}

pub fn send_message_to_l1(&mut self, message: MessageToL1) -> SyscallResult<()> {
let ordered_message_to_l1 =
OrderedL2ToL1Message { order: self.context.n_sent_messages_to_l1, message };
self.l2_to_l1_messages.push(ordered_message_to_l1);
self.context.n_sent_messages_to_l1 += 1;

Ok(())
}

pub fn execute_inner_call(
&mut self,
call: CallEntryPoint,
Expand Down

0 comments on commit 6ed9532

Please sign in to comment.