Skip to content

Commit

Permalink
feat(blockifier): add read & write syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Oct 14, 2024
1 parent d73a18c commit fc9b733
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
54 changes: 35 additions & 19 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ use std::collections::HashSet;
use std::hash::RandomState;

use cairo_native::starknet::{
ExecutionInfo,
ExecutionInfoV2,
Secp256k1Point,
Secp256r1Point,
StarknetSyscallHandler,
SyscallResult,
U256,
ExecutionInfo, ExecutionInfoV2, Secp256k1Point, Secp256r1Point, StarknetSyscallHandler,
SyscallResult, U256,
};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::{ContractAddress, EntryPointSelector};
Expand Down Expand Up @@ -93,7 +88,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,
Expand All @@ -105,10 +99,8 @@ impl<'state> NativeSyscallHandler<'state> {

if *remaining_gas < required_gas {
// Out of gas failure.
return Err(vec![
Felt::from_hex(OUT_OF_GAS_ERROR)
.expect("Failed to parse OUT_OF_GAS_ERROR hex string"),
]);
return Err(vec![Felt::from_hex(OUT_OF_GAS_ERROR)
.expect("Failed to parse OUT_OF_GAS_ERROR hex string")]);
}

*remaining_gas -= required_gas;
Expand Down Expand Up @@ -175,20 +167,44 @@ 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<Felt> {
todo!("Implement storage_read syscall.");
self.substract_syscall_gas_cost(
remaining_gas,
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.contract_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,
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.contract_address, key, value);
write_result.map_err(|e| encode_str_as_felts(&e.to_string()))?;

Ok(())
}

fn emit_event(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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};

#[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();
Expand Down
4 changes: 2 additions & 2 deletions scripts/dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ function compile_cairo_native_runtime() {
}

function main() {
# [ "$(uname)" = "Linux" ] && install_essential_deps_linux
# setup_llvm_deps
[ "$(uname)" = "Linux" ] && install_essential_deps_linux
setup_llvm_deps
echo "LLVM dependencies installed successfully."

compile_cairo_native_runtime
Expand Down

0 comments on commit fc9b733

Please sign in to comment.