Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Feb 2, 2025
1 parent 3da9a71 commit 5a80804
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 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
2 changes: 2 additions & 0 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ pub fn finalize_execution(
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,
read_block_hash_values: syscall_handler_base.read_block_hash_values,
accessed_blocks: syscall_handler_base.accessed_blocks,
},
})
}
Expand Down
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
14 changes: 11 additions & 3 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::collections::{hash_map, HashMap, HashSet};
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 Down Expand Up @@ -49,7 +50,9 @@ pub struct SyscallHandlerBase<'state> {
pub read_class_hash_values: Vec<ClassHash>,
// Accessed addresses by the `get_class_hash_at` syscall.
pub accessed_contract_addresses: HashSet<ContractAddress>,

pub read_block_hash_values: Vec<BlockHash>,
// Accessed block hashes by the `get_block_hash` syscall.
pub accessed_blocks: HashSet<BlockNumber>,
// The original storage value of the executed contract.
// Should be moved back `context.revert_info` before executing an inner call.
pub original_values: HashMap<StorageKey, Felt>,
Expand Down Expand Up @@ -83,12 +86,14 @@ impl<'state> SyscallHandlerBase<'state> {
accessed_keys: HashSet::new(),
read_class_hash_values: Vec::new(),
accessed_contract_addresses: HashSet::new(),
read_block_hash_values: Vec::new(),
accessed_blocks: HashSet::new(),
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 +128,7 @@ impl<'state> SyscallHandlerBase<'state> {
}
}

self.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,7 +138,9 @@ 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.read_block_hash_values.push(BlockHash(block_hash));
Ok(block_hash)
}

pub fn storage_read(&mut self, key: StorageKey) -> SyscallResult<Felt> {
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 5a80804

Please sign in to comment.