diff --git a/crates/blockifier/src/execution/call_info.rs b/crates/blockifier/src/execution/call_info.rs index 643a459d1e..80ff326a4d 100644 --- a/crates/blockifier/src/execution/call_info.rs +++ b/crates/blockifier/src/execution/call_info.rs @@ -4,6 +4,7 @@ use std::ops::{Add, AddAssign}; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use serde::Serialize; +use starknet_api::block::{BlockHash, BlockNumber}; use starknet_api::core::{ClassHash, ContractAddress, EthAddress}; use starknet_api::execution_resources::GasAmount; use starknet_api::state::StorageKey; @@ -145,6 +146,9 @@ pub struct StorageAccessTracker { pub accessed_storage_keys: HashSet, pub read_class_hash_values: Vec, pub accessed_contract_addresses: HashSet, + // TODO(Aner): add tests for storage tracking of contract 0x1 + pub read_block_hash_values: Vec, + pub accessed_blocks: HashSet, } /// Represents the full effects of executing an entry point, including the inner calls it invoked. diff --git a/crates/blockifier/src/execution/entry_point_execution.rs b/crates/blockifier/src/execution/entry_point_execution.rs index 9a0d685ede..b7e3a262a0 100644 --- a/crates/blockifier/src/execution/entry_point_execution.rs +++ b/crates/blockifier/src/execution/entry_point_execution.rs @@ -10,7 +10,6 @@ use cairo_vm::vm::security::verify_secure_runner; use num_traits::{ToPrimitive, Zero}; use starknet_types_core::felt::Felt; -use super::call_info::StorageAccessTracker; use crate::execution::call_info::{CallExecution, CallInfo, Retdata}; use crate::execution::contract_class::{CompiledClassV1, EntryPointV1, TrackedResource}; use crate::execution::entry_point::{ @@ -388,12 +387,7 @@ pub fn finalize_execution( inner_calls: syscall_handler_base.inner_calls, tracked_resource, resources: vm_resources, - storage_access_tracker: StorageAccessTracker { - storage_read_values: syscall_handler_base.read_values, - accessed_storage_keys: syscall_handler_base.accessed_keys, - read_class_hash_values: syscall_handler_base.read_class_hash_values, - accessed_contract_addresses: syscall_handler_base.accessed_contract_addresses, - }, + storage_access_tracker: syscall_handler_base.storage_access_tracker, }) } diff --git a/crates/blockifier/src/execution/native/entry_point_execution.rs b/crates/blockifier/src/execution/native/entry_point_execution.rs index e401d443b4..b419b20402 100644 --- a/crates/blockifier/src/execution/native/entry_point_execution.rs +++ b/crates/blockifier/src/execution/native/entry_point_execution.rs @@ -1,7 +1,7 @@ use cairo_native::execution_result::ContractExecutionResult; use cairo_native::utils::BuiltinCosts; -use crate::execution::call_info::{CallExecution, CallInfo, Retdata, StorageAccessTracker}; +use crate::execution::call_info::{CallExecution, CallInfo, Retdata}; use crate::execution::contract_class::TrackedResource; use crate::execution::entry_point::{ EntryPointExecutionContext, @@ -97,12 +97,7 @@ fn create_callinfo( }, resources: vm_resources, inner_calls: syscall_handler.base.inner_calls, - storage_access_tracker: StorageAccessTracker { - storage_read_values: syscall_handler.base.read_values, - accessed_storage_keys: syscall_handler.base.accessed_keys, - accessed_contract_addresses: syscall_handler.base.accessed_contract_addresses, - read_class_hash_values: syscall_handler.base.read_class_hash_values, - }, + storage_access_tracker: syscall_handler.base.storage_access_tracker, tracked_resource: TrackedResource::SierraGas, }) } diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index a818540fbb..84d6a2fa92 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -234,6 +234,7 @@ impl<'state> NativeSyscallHandler<'state> { impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> { fn get_block_hash( + // HERE??? &mut self, block_number: u64, remaining_gas: &mut u64, diff --git a/crates/blockifier/src/execution/syscalls/syscall_base.rs b/crates/blockifier/src/execution/syscalls/syscall_base.rs index 5de5129173..a468d9685f 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_base.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_base.rs @@ -1,7 +1,8 @@ /// This file is for sharing common logic between Native and VM syscall implementations. -use std::collections::{hash_map, HashMap, HashSet}; +use std::collections::{hash_map, HashMap}; use std::convert::From; +use starknet_api::block::{BlockHash, BlockNumber}; use starknet_api::core::{calculate_contract_address, ClassHash, ContractAddress}; use starknet_api::state::StorageKey; use starknet_api::transaction::fields::{Calldata, ContractAddressSalt}; @@ -10,7 +11,13 @@ use starknet_types_core::felt::Felt; use super::exceeds_event_size_limit; use crate::abi::constants; -use crate::execution::call_info::{CallInfo, MessageToL1, OrderedEvent, OrderedL2ToL1Message}; +use crate::execution::call_info::{ + CallInfo, + MessageToL1, + OrderedEvent, + OrderedL2ToL1Message, + StorageAccessTracker, +}; use crate::execution::common_hints::ExecutionMode; use crate::execution::entry_point::{ CallEntryPoint, @@ -44,11 +51,7 @@ pub struct SyscallHandlerBase<'state> { pub inner_calls: Vec, // Additional information gathered during execution. - pub read_values: Vec, - pub accessed_keys: HashSet, - pub read_class_hash_values: Vec, - // Accessed addresses by the `get_class_hash_at` syscall. - pub accessed_contract_addresses: HashSet, + pub storage_access_tracker: StorageAccessTracker, // The original storage value of the executed contract. // Should be moved back `context.revert_info` before executing an inner call. @@ -79,16 +82,13 @@ impl<'state> SyscallHandlerBase<'state> { events: Vec::new(), l2_to_l1_messages: Vec::new(), inner_calls: Vec::new(), - read_values: Vec::new(), - accessed_keys: HashSet::new(), - read_class_hash_values: Vec::new(), - accessed_contract_addresses: HashSet::new(), + storage_access_tracker: StorageAccessTracker::default(), original_values, revert_info_idx, } } - pub fn get_block_hash(&self, requested_block_number: u64) -> SyscallResult { + pub fn get_block_hash(&mut self, requested_block_number: u64) -> SyscallResult { // Note: we take the actual block number (and not the rounded one for validate) // in any case; it is consistent with the OS implementation and safe (see `Validate` arm). let current_block_number = self.context.tx_context.block_context.block_info.block_number.0; @@ -123,6 +123,7 @@ impl<'state> SyscallHandlerBase<'state> { } } + self.storage_access_tracker.accessed_blocks.insert(BlockNumber(requested_block_number)); let key = StorageKey::try_from(Felt::from(requested_block_number))?; let block_hash_contract_address = self .context @@ -132,13 +133,15 @@ impl<'state> SyscallHandlerBase<'state> { .os_constants .os_contract_addresses .block_hash_contract_address(); - Ok(self.state.get_storage_at(block_hash_contract_address, key)?) + let block_hash = self.state.get_storage_at(block_hash_contract_address, key)?; + self.storage_access_tracker.read_block_hash_values.push(BlockHash(block_hash)); + Ok(block_hash) } pub fn storage_read(&mut self, key: StorageKey) -> SyscallResult { - self.accessed_keys.insert(key); + self.storage_access_tracker.accessed_storage_keys.insert(key); let value = self.state.get_storage_at(self.call.storage_address, key)?; - self.read_values.push(value); + self.storage_access_tracker.storage_read_values.push(value); Ok(value) } @@ -152,7 +155,7 @@ impl<'state> SyscallHandlerBase<'state> { hash_map::Entry::Occupied(_) => {} } - self.accessed_keys.insert(key); + self.storage_access_tracker.accessed_storage_keys.insert(key); self.state.set_storage_at(contract_address, key, value)?; Ok(()) @@ -168,9 +171,9 @@ impl<'state> SyscallHandlerBase<'state> { execution_mode: ExecutionMode::Validate, }); } - self.accessed_contract_addresses.insert(contract_address); + self.storage_access_tracker.accessed_contract_addresses.insert(contract_address); let class_hash = self.state.get_class_hash_at(contract_address)?; - self.read_class_hash_values.push(class_hash); + self.storage_access_tracker.read_class_hash_values.push(class_hash); Ok(class_hash) } diff --git a/crates/starknet_consensus_orchestrator/resources/central_transaction_execution_info.json b/crates/starknet_consensus_orchestrator/resources/central_transaction_execution_info.json index 53af04bb3b..26e2d4b09b 100644 --- a/crates/starknet_consensus_orchestrator/resources/central_transaction_execution_info.json +++ b/crates/starknet_consensus_orchestrator/resources/central_transaction_execution_info.json @@ -137,6 +137,12 @@ "0x0", "0x1", "0x2" + ], + "read_block_hash_values": [ + "0xdeafbee" + ], + "accessed_blocks": [ + 100 ] }, "tracked_resource": "SierraGas", @@ -165,6 +171,12 @@ "0x0", "0x1", "0x2" + ], + "read_block_hash_values": [ + "0xdeafbee" + ], + "accessed_blocks": [ + 100 ] }, "tracked_resource": "SierraGas" @@ -296,6 +308,12 @@ "0x0", "0x1", "0x2" + ], + "read_block_hash_values": [ + "0xdeafbee" + ], + "accessed_blocks": [ + 100 ] }, "tracked_resource": "SierraGas", @@ -324,6 +342,12 @@ "0x0", "0x1", "0x2" + ], + "read_block_hash_values": [ + "0xdeafbee" + ], + "accessed_blocks": [ + 100 ] }, "tracked_resource": "SierraGas" @@ -461,6 +485,12 @@ "0x0", "0x1", "0x2" + ], + "read_block_hash_values": [ + "0xdeafbee" + ], + "accessed_blocks": [ + 100 ] }, "tracked_resource": "SierraGas", @@ -489,6 +519,12 @@ "0x0", "0x1", "0x2" + ], + "read_block_hash_values": [ + "0xdeafbee" + ], + "accessed_blocks": [ + 100 ] }, "tracked_resource": "SierraGas" diff --git a/crates/starknet_consensus_orchestrator/src/cende/central_objects_test.rs b/crates/starknet_consensus_orchestrator/src/cende/central_objects_test.rs index d553c0ad99..d29a4659c4 100644 --- a/crates/starknet_consensus_orchestrator/src/cende/central_objects_test.rs +++ b/crates/starknet_consensus_orchestrator/src/cende/central_objects_test.rs @@ -46,6 +46,7 @@ use num_bigint::BigUint; use rstest::rstest; use serde_json::Value; use starknet_api::block::{ + BlockHash, BlockInfo, BlockNumber, BlockTimestamp, @@ -491,6 +492,8 @@ fn call_info() -> CallInfo { accessed_storage_keys: HashSet::from([StorageKey::from(1_u128)]), read_class_hash_values: vec![ClassHash(felt!("0x80020000"))], accessed_contract_addresses: HashSet::from([contract_address!("0x1")]), + read_block_hash_values: vec![BlockHash(felt!("0xdeafbee"))], + accessed_blocks: HashSet::from([BlockNumber(100)]), }, } }