Skip to content

Commit

Permalink
build(blockifier): track access to contract 0x1
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Feb 2, 2025
1 parent 3da9a71 commit 08a3cf5
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 32 deletions.
4 changes: 4 additions & 0 deletions crates/blockifier/src/execution/call_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,6 +146,9 @@ pub struct StorageAccessTracker {
pub accessed_storage_keys: HashSet<StorageKey>,
pub read_class_hash_values: Vec<ClassHash>,
pub accessed_contract_addresses: HashSet<ContractAddress>,
// TODO(Aner): add tests for storage tracking of contract 0x1
pub read_block_hash_values: Vec<BlockHash>,
pub accessed_blocks: HashSet<BlockNumber>,
}

/// Represents the full effects of executing an entry point, including the inner calls it invoked.
Expand Down
8 changes: 1 addition & 7 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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,
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
})
}
1 change: 1 addition & 0 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 21 additions & 18 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -44,11 +51,7 @@ pub struct SyscallHandlerBase<'state> {
pub inner_calls: Vec<CallInfo>,

// Additional information gathered during execution.
pub read_values: Vec<Felt>,
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>,
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.
Expand Down Expand Up @@ -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<Felt> {
pub fn get_block_hash(&mut self, requested_block_number: u64) -> SyscallResult<Felt> {
// 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;
Expand Down Expand Up @@ -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
Expand All @@ -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<Felt> {
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)
}

Expand All @@ -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(())
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
"0x0",
"0x1",
"0x2"
],
"read_block_hash_values": [
"0xdeafbee"
],
"accessed_blocks": [
100
]
},
"tracked_resource": "SierraGas",
Expand Down Expand Up @@ -165,6 +171,12 @@
"0x0",
"0x1",
"0x2"
],
"read_block_hash_values": [
"0xdeafbee"
],
"accessed_blocks": [
100
]
},
"tracked_resource": "SierraGas"
Expand Down Expand Up @@ -296,6 +308,12 @@
"0x0",
"0x1",
"0x2"
],
"read_block_hash_values": [
"0xdeafbee"
],
"accessed_blocks": [
100
]
},
"tracked_resource": "SierraGas",
Expand Down Expand Up @@ -324,6 +342,12 @@
"0x0",
"0x1",
"0x2"
],
"read_block_hash_values": [
"0xdeafbee"
],
"accessed_blocks": [
100
]
},
"tracked_resource": "SierraGas"
Expand Down Expand Up @@ -461,6 +485,12 @@
"0x0",
"0x1",
"0x2"
],
"read_block_hash_values": [
"0xdeafbee"
],
"accessed_blocks": [
100
]
},
"tracked_resource": "SierraGas",
Expand Down Expand Up @@ -489,6 +519,12 @@
"0x0",
"0x1",
"0x2"
],
"read_block_hash_values": [
"0xdeafbee"
],
"accessed_blocks": [
100
]
},
"tracked_resource": "SierraGas"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use num_bigint::BigUint;
use rstest::rstest;
use serde_json::Value;
use starknet_api::block::{
BlockHash,
BlockInfo,
BlockNumber,
BlockTimestamp,
Expand Down Expand Up @@ -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)]),
},
}
}
Expand Down

0 comments on commit 08a3cf5

Please sign in to comment.