From 6ed9532cc6347a03443cfacaece94aac3915f34c Mon Sep 17 00:00:00 2001 From: Yonatan Iluz Date: Tue, 26 Nov 2024 17:07:14 +0200 Subject: [PATCH] refactor(blockifier): share send_message code --- .../src/execution/native/syscall_handler.rs | 14 ++++---------- crates/blockifier/src/execution/syscalls/mod.rs | 11 ++--------- .../src/execution/syscalls/syscall_base.rs | 11 ++++++++++- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index d0cabe9c10..5412a62d79 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -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; @@ -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 { diff --git a/crates/blockifier/src/execution/syscalls/mod.rs b/crates/blockifier/src/execution/syscalls/mod.rs index d902bcbfd6..1ea040b7d1 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -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::{ @@ -487,14 +487,7 @@ pub fn send_message_to_l1( syscall_handler: &mut SyscallHintProcessor<'_>, _remaining_gas: &mut u64, ) -> SyscallResult { - 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 {}) } diff --git a/crates/blockifier/src/execution/syscalls/syscall_base.rs b/crates/blockifier/src/execution/syscalls/syscall_base.rs index 5ca3fa8675..ade9cb398c 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -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, @@ -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,