From 3f4595d954b4b173d8377ad6f1e68552e923c0c0 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 | 15 ++++----------- crates/blockifier/src/execution/syscalls/mod.rs | 9 +-------- .../src/execution/syscalls/syscall_base.rs | 11 ++++++++++- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 0c0869d5355..53e1c290fee 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -30,9 +30,8 @@ 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::contract_class::RunnableContractClass; use crate::execution::entry_point::{ CallEntryPoint, CallType, @@ -470,17 +469,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 13d4dd117a9..b5a55e0acde 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -513,14 +513,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 54bc82d1ce7..a5cd212ef24 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -8,7 +8,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, EntryPointExecutionContext}; use crate::execution::syscalls::hint_processor::{ @@ -159,6 +159,15 @@ impl<'state> SyscallHandlerBase<'state> { Ok(()) } + 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,