diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 8dde577c954..83ce272aecb 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -97,7 +97,6 @@ impl<'state> NativeSyscallHandler<'state> { // Handles gas related logic when executing a syscall. Required because Native calls the // syscalls directly unlike the VM where the `execute_syscall` method perform this operation // first. - #[allow(dead_code)] fn substract_syscall_gas_cost( &mut self, remaining_gas: &mut u128, @@ -185,20 +184,46 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { fn storage_read( &mut self, _address_domain: u32, - _address: Felt, - _remaining_gas: &mut u128, + address: Felt, + remaining_gas: &mut u128, ) -> SyscallResult { - todo!("Implement storage_read syscall."); + self.substract_syscall_gas_cost( + remaining_gas, + SyscallSelector::StorageRead, + self.context.gas_costs().storage_read_gas_cost, + )?; + + let key = StorageKey::try_from(address).map_err(|e| encode_str_as_felts(&e.to_string()))?; + + let read_result = self.state.get_storage_at(self.call.storage_address, key); + let value = read_result.map_err(|e| encode_str_as_felts(&e.to_string()))?; + + self.accessed_keys.insert(key); + self.read_values.push(value); + + Ok(value) } fn storage_write( &mut self, _address_domain: u32, - _address: Felt, - _value: Felt, - _remaining_gas: &mut u128, + address: Felt, + value: Felt, + remaining_gas: &mut u128, ) -> SyscallResult<()> { - todo!("Implement storage_write syscall."); + self.substract_syscall_gas_cost( + remaining_gas, + SyscallSelector::StorageWrite, + self.context.gas_costs().storage_write_gas_cost, + )?; + + let key = StorageKey::try_from(address).map_err(|e| encode_str_as_felts(&e.to_string()))?; + self.accessed_keys.insert(key); + + let write_result = self.state.set_storage_at(self.call.storage_address, key, value); + write_result.map_err(|e| encode_str_as_felts(&e.to_string()))?; + + Ok(()) } fn emit_event( diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/storage_read_write.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/storage_read_write.rs index dbc85fff6ba..bebde966a44 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/storage_read_write.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/storage_read_write.rs @@ -13,6 +13,10 @@ use crate::test_utils::contracts::FeatureContract; use crate::test_utils::initial_test_state::test_state; use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE}; +#[cfg_attr( + feature = "cairo_native", + test_case(FeatureContract::TestContract(CairoVersion::Native), 27290; "Native") +)] #[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), REQUIRED_GAS_STORAGE_READ_WRITE_TEST; "VM")] fn test_storage_read_write(test_contract: FeatureContract, expected_gas: u64) { let chain_info = &ChainInfo::create_for_testing();