Skip to content

Commit

Permalink
feat(blockifier): implement get_clash_at syscall for native
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Nov 18, 2024
1 parent 6ba1b90 commit e25d2f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,8 @@ fn create_callinfo(
inner_calls: syscall_handler.inner_calls,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
// TODO(Aviv): The syscall is not supported here yet.
// Currently, `accessed_contract_addresses` and `read_class_hash_values` are initialized
// as empty. Support for handling accessed storage keys via syscalls should be implemented.
accessed_contract_addresses: Default::default(),
read_class_hash_values: Default::default(),
accessed_contract_addresses: syscall_handler.accessed_contract_addresses,
read_class_hash_values: syscall_handler.read_class_hash_values,
tracked_resource: TrackedResource::SierraGas,
})
}
25 changes: 22 additions & 3 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub struct NativeSyscallHandler<'state> {
// Additional information gathered during execution.
pub read_values: Vec<Felt>,
pub accessed_keys: HashSet<StorageKey, RandomState>,
pub read_class_hash_values: Vec<ClassHash>,
// Accessed addresses by the `get_class_hash_at` syscall.
pub accessed_contract_addresses: HashSet<ContractAddress>,

// It is set if an unrecoverable error happens during syscall execution
pub unrecoverable_error: Option<SyscallExecutionError>,
Expand All @@ -98,6 +101,8 @@ impl<'state> NativeSyscallHandler<'state> {
inner_calls: Vec::new(),
read_values: Vec::new(),
accessed_keys: HashSet::new(),
read_class_hash_values: Vec::new(),
accessed_contract_addresses: HashSet::new(),
unrecoverable_error: None,
}
}
Expand Down Expand Up @@ -297,10 +302,24 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {

fn get_class_hash_at(
&mut self,
_contract_address: Felt,
_remaining_gas: &mut u128,
contract_address: Felt,
remaining_gas: &mut u128,
) -> SyscallResult<Felt> {
todo!()
self.pre_execute_syscall(
remaining_gas,
self.context.gas_costs().get_class_hash_at_gas_cost,
)?;
let request = ContractAddress::try_from(contract_address)
.map_err(|err| self.handle_error(remaining_gas, err.into()))?;
self.accessed_contract_addresses.insert(request);

let class_hash = self
.state
.get_class_hash_at(request)
.map_err(|err| self.handle_error(remaining_gas, err.into()))?;
self.read_class_hash_values.push(class_hash);

Ok(class_hash.0)
}

fn get_execution_info_v2(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE}
/// 4. Execution fails if `address` has a different `class_hash`.
/// 5. Execution succeeds and returns `class_hash` = 0 if `address` is absent.
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), REQUIRED_GAS_GET_CLASS_HASH_AT_TEST; "VM")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native), 17830; "Native"))
]
fn test_get_class_hash_at(test_contract: FeatureContract, expected_gas: u64) {
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
Expand Down

0 comments on commit e25d2f0

Please sign in to comment.