From da3aa478c50f5cc35f0762962bc1571ff1e3d438 Mon Sep 17 00:00:00 2001 From: Yonatan Iluz Date: Mon, 25 Nov 2024 17:09:01 +0200 Subject: [PATCH] refactor(blockifier): share emit_event and storage_read syscalls --- .../src/execution/native/syscall_handler.rs | 22 +++---------------- .../src/execution/syscalls/hint_processor.rs | 13 ----------- .../blockifier/src/execution/syscalls/mod.rs | 17 ++++---------- .../src/execution/syscalls/syscall_base.rs | 22 +++++++++++++++++++ 4 files changed, 29 insertions(+), 45 deletions(-) diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 0debda771b..1be977b86e 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -30,7 +30,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, OrderedEvent, OrderedL2ToL1Message, Retdata}; +use crate::execution::call_info::{MessageToL1, OrderedL2ToL1Message, Retdata}; use crate::execution::common_hints::ExecutionMode; use crate::execution::contract_class::RunnableContractClass; use crate::execution::entry_point::{ @@ -43,7 +43,6 @@ use crate::execution::errors::EntryPointExecutionError; use crate::execution::execution_utils::execute_deployment; use crate::execution::native::utils::{calculate_resource_bounds, default_tx_v2_info}; use crate::execution::secp; -use crate::execution::syscalls::exceeds_event_size_limit; use crate::execution::syscalls::hint_processor::{ SyscallExecutionError, INVALID_INPUT_LENGTH_ERROR, @@ -441,12 +440,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { let key = StorageKey::try_from(address) .map_err(|e| self.handle_error(remaining_gas, e.into()))?; - let read_result = self.base.state.get_storage_at(self.base.call.storage_address, key); - let value = read_result.map_err(|e| self.handle_error(remaining_gas, e.into()))?; - - self.base.accessed_keys.insert(key); - self.base.read_values.push(value); - + let value = self.base.storage_read(key).map_err(|e| self.handle_error(remaining_gas, e))?; Ok(value) } @@ -480,22 +474,12 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { ) -> SyscallResult<()> { self.pre_execute_syscall(remaining_gas, self.gas_costs().emit_event_gas_cost)?; - let order = self.base.context.n_emitted_events; let event = EventContent { keys: keys.iter().copied().map(EventKey).collect(), data: EventData(data.to_vec()), }; - exceeds_event_size_limit( - self.base.context.versioned_constants(), - self.base.context.n_emitted_events + 1, - &event, - ) - .map_err(|e| self.handle_error(remaining_gas, e.into()))?; - - self.base.events.push(OrderedEvent { order, event }); - self.base.context.n_emitted_events += 1; - + self.base.emit_event(event).map_err(|e| self.handle_error(remaining_gas, e))?; Ok(()) } diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 847292ab25..db6ada14ed 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -14,7 +14,6 @@ use cairo_vm::vm::errors::vm_errors::VirtualMachineError; use cairo_vm::vm::runners::cairo_runner::{ResourceTracker, RunResources}; use cairo_vm::vm::vm_core::VirtualMachine; use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector}; -use starknet_api::state::StorageKey; use starknet_api::transaction::fields::{ AllResourceBounds, Calldata, @@ -64,7 +63,6 @@ use crate::execution::syscalls::{ sha_256_process_block, storage_read, storage_write, - StorageReadResponse, SyscallRequest, SyscallRequestWrapper, SyscallResponse, @@ -631,17 +629,6 @@ impl<'a> SyscallHintProcessor<'a> { Ok(tx_info_start_ptr) } - pub fn get_contract_storage_at( - &mut self, - key: StorageKey, - ) -> SyscallResult { - self.base.accessed_keys.insert(key); - let value = self.base.state.get_storage_at(self.storage_address(), key)?; - self.base.read_values.push(value); - - Ok(StorageReadResponse { value }) - } - pub fn finalize(&mut self) { self.base.finalize(); } diff --git a/crates/blockifier/src/execution/syscalls/mod.rs b/crates/blockifier/src/execution/syscalls/mod.rs index dcef96c904..648e3316df 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -27,7 +27,7 @@ use self::hint_processor::{ SyscallExecutionError, SyscallHintProcessor, }; -use crate::execution::call_info::{MessageToL1, OrderedEvent, OrderedL2ToL1Message}; +use crate::execution::call_info::{MessageToL1, OrderedL2ToL1Message}; use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector; use crate::execution::entry_point::{CallEntryPoint, CallType, ConstructorContext}; use crate::execution::execution_utils::{ @@ -332,17 +332,7 @@ pub fn emit_event( syscall_handler: &mut SyscallHintProcessor<'_>, _remaining_gas: &mut u64, ) -> SyscallResult { - let execution_context = &mut syscall_handler.base.context; - exceeds_event_size_limit( - execution_context.versioned_constants(), - execution_context.n_emitted_events + 1, - &request.content, - )?; - let ordered_event = - OrderedEvent { order: execution_context.n_emitted_events, event: request.content }; - syscall_handler.base.events.push(ordered_event); - execution_context.n_emitted_events += 1; - + syscall_handler.base.emit_event(request.content)?; Ok(EmitEventResponse {}) } @@ -580,7 +570,8 @@ pub fn storage_read( syscall_handler: &mut SyscallHintProcessor<'_>, _remaining_gas: &mut u64, ) -> SyscallResult { - syscall_handler.get_contract_storage_at(request.address) + let value = syscall_handler.base.storage_read(request.address)?; + Ok(StorageReadResponse { value }) } // StorageWrite syscall. diff --git a/crates/blockifier/src/execution/syscalls/syscall_base.rs b/crates/blockifier/src/execution/syscalls/syscall_base.rs index bdfe1185ab..ccdc1ecf0f 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -3,8 +3,10 @@ use std::convert::From; use starknet_api::core::{ClassHash, ContractAddress}; use starknet_api::state::StorageKey; +use starknet_api::transaction::EventContent; 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::common_hints::ExecutionMode; @@ -99,6 +101,13 @@ impl<'state> SyscallHandlerBase<'state> { Ok(self.state.get_storage_at(block_hash_contract_address, key)?) } + pub fn storage_read(&mut self, key: StorageKey) -> SyscallResult { + self.accessed_keys.insert(key); + let value = self.state.get_storage_at(self.call.storage_address, key)?; + self.read_values.push(value); + Ok(value) + } + pub fn storage_write(&mut self, key: StorageKey, value: Felt) -> SyscallResult<()> { let contract_address = self.call.storage_address; @@ -115,6 +124,19 @@ impl<'state> SyscallHandlerBase<'state> { Ok(()) } + pub fn emit_event(&mut self, event: EventContent) -> SyscallResult<()> { + exceeds_event_size_limit( + self.context.versioned_constants(), + self.context.n_emitted_events + 1, + &event, + )?; + let ordered_event = OrderedEvent { order: self.context.n_emitted_events, event }; + self.events.push(ordered_event); + self.context.n_emitted_events += 1; + + Ok(()) + } + pub fn execute_inner_call( &mut self, call: CallEntryPoint,