From a447ab4a5104a41266dea29a16abd2fbbea156b3 Mon Sep 17 00:00:00 2001 From: Yonatan Iluz Date: Mon, 25 Nov 2024 10:54:33 +0200 Subject: [PATCH] refactor(blockifier): move get_block_hash_base to syscallHandlerBase --- .../src/execution/native/syscall_handler.rs | 9 ++-- .../src/execution/syscalls/hint_processor.rs | 6 +-- .../blockifier/src/execution/syscalls/mod.rs | 6 +-- .../src/execution/syscalls/syscall_base.rs | 51 +++++++++---------- 4 files changed, 33 insertions(+), 39 deletions(-) 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 7cd5237c5e..81d91cf1b4 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -50,6 +50,7 @@ use crate::execution::syscalls::secp::{ secp256r1_new, SecpHintProcessor, }; +use crate::execution::syscalls::syscall_base::SyscallHandlerBase; use crate::execution::syscalls::{ call_contract, deploy, @@ -64,7 +65,6 @@ use crate::execution::syscalls::{ sha_256_process_block, storage_read, storage_write, - syscall_base, StorageReadResponse, StorageWriteResponse, SyscallRequest, @@ -214,7 +214,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, @@ -246,7 +246,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 7e9ce0f3e1..407e7eec38 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -389,11 +389,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..8781b507a6 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -1,6 +1,5 @@ use std::collections::{HashMap, HashSet}; use std::convert::From; -use std::hash::RandomState; use starknet_api::core::{ClassHash, ContractAddress}; use starknet_api::state::StorageKey; @@ -33,7 +32,7 @@ pub struct SyscallHandlerBase<'state> { // Additional information gathered during execution. pub read_values: Vec, - pub accessed_keys: HashSet, + pub accessed_keys: HashSet, pub read_class_hash_values: Vec, // Accessed addresses by the `get_class_hash_at` syscall. pub accessed_contract_addresses: HashSet, @@ -71,33 +70,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)?) + } }