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 26, 2024
1 parent 42db010 commit 3f4595d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
15 changes: 4 additions & 11 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<U256> {
Expand Down
9 changes: 1 addition & 8 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,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 @@ -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::{
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3f4595d

Please sign in to comment.