diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 96b21952b9..48ab901ece 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -43,18 +43,19 @@ 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, OUT_OF_GAS_ERROR, }; -use crate::execution::syscalls::{exceeds_event_size_limit, syscall_base}; +use crate::execution::syscalls::syscall_base::SyscallHandlerBase; use crate::state::state_api::State; use crate::transaction::objects::TransactionInfo; use crate::versioned_constants::GasCosts; pub struct NativeSyscallHandler<'state> { - pub base: Box>, + pub base: Box>, // It is set if an unrecoverable error happens during syscall execution pub unrecoverable_error: Option, @@ -67,7 +68,7 @@ impl<'state> NativeSyscallHandler<'state> { context: &'state mut EntryPointExecutionContext, ) -> NativeSyscallHandler<'state> { NativeSyscallHandler { - base: Box::new(syscall_base::SyscallHandlerBase::new(call, state, context)), + base: Box::new(SyscallHandlerBase::new(call, state, context)), unrecoverable_error: None, } } @@ -237,7 +238,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { ) -> SyscallResult { self.pre_execute_syscall(remaining_gas, self.gas_costs().get_block_hash_gas_cost)?; - match syscall_base::get_block_hash_base(self.base.context, block_number, self.base.state) { + match self.base.get_block_hash(block_number) { Ok(value) => Ok(value), Err(e) => Err(self.handle_error(remaining_gas, e)), } diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index bddf0bb67f..3e3af676bb 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -51,6 +51,7 @@ use crate::execution::syscalls::secp::{ secp256r1_new, SecpHintProcessor, }; +use crate::execution::syscalls::syscall_base::SyscallHandlerBase; use crate::execution::syscalls::{ call_contract, deploy, @@ -65,7 +66,6 @@ use crate::execution::syscalls::{ sha_256_process_block, storage_read, storage_write, - syscall_base, StorageReadResponse, StorageWriteResponse, SyscallRequest, @@ -215,7 +215,7 @@ pub const INVALID_ARGUMENT: &str = /// Executes Starknet syscalls (stateful protocol hints) during the execution of an entry point /// call. pub struct SyscallHintProcessor<'a> { - pub base: Box>, + pub base: Box>, // VM-specific fields. pub syscall_counter: SyscallCounter, @@ -247,7 +247,7 @@ impl<'a> SyscallHintProcessor<'a> { read_only_segments: ReadOnlySegments, ) -> Self { SyscallHintProcessor { - base: Box::new(syscall_base::SyscallHandlerBase::new(call, state, context)), + base: Box::new(SyscallHandlerBase::new(call, state, context)), syscall_counter: SyscallCounter::default(), read_only_segments, syscall_ptr: initial_syscall_ptr, diff --git a/crates/blockifier/src/execution/syscalls/mod.rs b/crates/blockifier/src/execution/syscalls/mod.rs index 690c27374f..720d684ec0 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -390,11 +390,7 @@ pub fn get_block_hash( syscall_handler: &mut SyscallHintProcessor<'_>, _remaining_gas: &mut u64, ) -> SyscallResult { - let block_hash = BlockHash(syscall_base::get_block_hash_base( - syscall_handler.base.context, - request.block_number.0, - syscall_handler.base.state, - )?); + let block_hash = BlockHash(syscall_handler.base.get_block_hash(request.block_number.0)?); Ok(GetBlockHashResponse { block_hash }) } diff --git a/crates/blockifier/src/execution/syscalls/syscall_base.rs b/crates/blockifier/src/execution/syscalls/syscall_base.rs index b561561b8e..e06badbd13 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -71,33 +71,31 @@ impl<'state> SyscallHandlerBase<'state> { original_values, } } -} -pub fn get_block_hash_base( - context: &EntryPointExecutionContext, - requested_block_number: u64, - state: &dyn State, -) -> SyscallResult { - let execution_mode = context.execution_mode; - if execution_mode == ExecutionMode::Validate { - return Err(SyscallExecutionError::InvalidSyscallInExecutionMode { - syscall_name: "get_block_hash".to_string(), - execution_mode, - }); - } + pub fn get_block_hash(&self, requested_block_number: u64) -> SyscallResult { + let execution_mode = self.context.execution_mode; + if execution_mode == ExecutionMode::Validate { + return Err(SyscallExecutionError::InvalidSyscallInExecutionMode { + syscall_name: "get_block_hash".to_string(), + execution_mode, + }); + } - let current_block_number = context.tx_context.block_context.block_info.block_number.0; + let current_block_number = self.context.tx_context.block_context.block_info.block_number.0; - if current_block_number < constants::STORED_BLOCK_HASH_BUFFER - || requested_block_number > current_block_number - constants::STORED_BLOCK_HASH_BUFFER - { - let out_of_range_error = Felt::from_hex(BLOCK_NUMBER_OUT_OF_RANGE_ERROR) - .expect("Converting BLOCK_NUMBER_OUT_OF_RANGE_ERROR to Felt should not fail."); - return Err(SyscallExecutionError::SyscallError { error_data: vec![out_of_range_error] }); - } + if current_block_number < constants::STORED_BLOCK_HASH_BUFFER + || requested_block_number > current_block_number - constants::STORED_BLOCK_HASH_BUFFER + { + let out_of_range_error = Felt::from_hex(BLOCK_NUMBER_OUT_OF_RANGE_ERROR) + .expect("Converting BLOCK_NUMBER_OUT_OF_RANGE_ERROR to Felt should not fail."); + return Err(SyscallExecutionError::SyscallError { + error_data: vec![out_of_range_error], + }); + } - let key = StorageKey::try_from(Felt::from(requested_block_number))?; - let block_hash_contract_address = - ContractAddress::try_from(Felt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS))?; - Ok(state.get_storage_at(block_hash_contract_address, key)?) + let key = StorageKey::try_from(Felt::from(requested_block_number))?; + let block_hash_contract_address = + ContractAddress::try_from(Felt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS))?; + Ok(self.state.get_storage_at(block_hash_contract_address, key)?) + } }