Skip to content

Commit

Permalink
refactor(blockifier): move get_block_hash_base to syscallHandlerBase (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored Nov 25, 2024
1 parent 19f082e commit 1e38b56
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 39 deletions.
9 changes: 5 additions & 4 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<syscall_base::SyscallHandlerBase<'state>>,
pub base: Box<SyscallHandlerBase<'state>>,

// It is set if an unrecoverable error happens during syscall execution
pub unrecoverable_error: Option<SyscallExecutionError>,
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -237,7 +238,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
) -> SyscallResult<Felt> {
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)),
}
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -64,7 +65,6 @@ use crate::execution::syscalls::{
sha_256_process_block,
storage_read,
storage_write,
syscall_base,
StorageReadResponse,
StorageWriteResponse,
SyscallRequest,
Expand Down Expand Up @@ -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<syscall_base::SyscallHandlerBase<'a>>,
pub base: Box<SyscallHandlerBase<'a>>,

// VM-specific fields.
pub syscall_counter: SyscallCounter,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,7 @@ pub fn get_block_hash(
syscall_handler: &mut SyscallHintProcessor<'_>,
_remaining_gas: &mut u64,
) -> SyscallResult<GetBlockHashResponse> {
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 })
}

Expand Down
51 changes: 24 additions & 27 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,7 +32,7 @@ pub struct SyscallHandlerBase<'state> {

// Additional information gathered during execution.
pub read_values: Vec<Felt>,
pub accessed_keys: HashSet<StorageKey, RandomState>,
pub accessed_keys: HashSet<StorageKey>,
pub read_class_hash_values: Vec<ClassHash>,
// Accessed addresses by the `get_class_hash_at` syscall.
pub accessed_contract_addresses: HashSet<ContractAddress>,
Expand Down Expand Up @@ -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<Felt> {
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<Felt> {
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)?)
}
}

0 comments on commit 1e38b56

Please sign in to comment.