Skip to content

Commit

Permalink
refactor(blockifier): share get_block_hash syscall code between native
Browse files Browse the repository at this point in the history
and casm
  • Loading branch information
meship-starkware committed Nov 17, 2024
1 parent 86461e3 commit 913d484
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion crates/blockifier/cairo_native
Submodule cairo_native updated 72 files
+19 −0 .github/ISSUE_TEMPLATE/daily_failure.md
+36 −0 .github/actions/install-linux-deps/action.yml
+2 −6 .github/workflows/bench-hyperfine.yml
+175 −0 .github/workflows/daily.yml
+150 −0 .github/workflows/starknet-blocks.yml
+64 −63 Cargo.lock
+3 −2 Cargo.toml
+2 −1 Makefile
+18 −82 benches/benches.rs
+17 −0 benches/compile_time.rs
+54 −3 benches/libfuncs.rs
+15 −1 docs/sierra.md
+8 −0 examples/erc20.rs
+8 −0 examples/starknet.rs
+8 −3 programs/benches/factorial_2M.c
+8 −3 programs/benches/fib_2M.c
+8 −3 programs/benches/logistic_map.c
+42 −48 programs/compile_benches/dijkstra.cairo
+8 −17 programs/compile_benches/extended_euclidean_algorithm.cairo
+78 −27 programs/compile_benches/fast_power.cairo
+0 −285 programs/compile_benches/sha256.cairo
+539 −0 programs/compile_benches/sha512.cairo
+1 −1 runtime/Cargo.toml
+28 −1 runtime/src/lib.rs
+10 −38 scripts/bench-hyperfine.sh
+51 −0 scripts/cmp_state_dumps.sh
+30 −0 scripts/diff-check.sh
+2 −9 src/arch/aarch64.rs
+2 −9 src/arch/x86_64.rs
+1 −0 src/bin/utils/mod.rs
+13 −12 src/cache/aot.rs
+10 −8 src/cache/jit.rs
+20 −11 src/compiler.rs
+107 −0 src/error.rs
+81 −32 src/executor.rs
+51 −3 src/executor/aot.rs
+328 −46 src/executor/contract.rs
+23 −2 src/executor/jit.rs
+2 −1 src/ffi.rs
+13 −8 src/libfuncs.rs
+28 −17 src/libfuncs/array.rs
+43 −10 src/libfuncs/bounded_int.rs
+4 −1 src/libfuncs/cast.rs
+5 −3 src/libfuncs/circuit.rs
+10 −5 src/libfuncs/const.rs
+2 −2 src/libfuncs/debug.rs
+9 −4 src/libfuncs/enum.rs
+9 −2 src/libfuncs/felt252.rs
+2 −2 src/libfuncs/felt252_dict_entry.rs
+3 −2 src/libfuncs/function_call.rs
+125 −33 src/libfuncs/gas.rs
+181 −0 src/libfuncs/int_range.rs
+456 −602 src/libfuncs/starknet.rs
+676 −1,507 src/libfuncs/starknet/secp256.rs
+40 −7 src/metadata/gas.rs
+44 −0 src/metadata/runtime_bindings.rs
+43 −2 src/starknet.rs
+8 −0 src/starknet_stub.rs
+24 −9 src/types.rs
+19 −24 src/types/array.rs
+6 −2 src/types/builtin_costs.rs
+46 −0 src/types/int_range.rs
+110 −2 src/utils.rs
+87 −1 src/utils/block_ext.rs
+65 −3 src/values.rs
+6 −6 tests/alexandria/Scarb.lock
+4 −4 tests/alexandria/Scarb.toml
+33 −17 tests/common.rs
+13 −2 tests/tests/starknet/keccak.rs
+6 −1 tests/tests/starknet/programs/syscalls.cairo
+8 −0 tests/tests/starknet/secp256.rs
+28 −0 tests/tests/starknet/syscalls.rs
19 changes: 3 additions & 16 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use starknet_api::transaction::fields::{Calldata, ContractAddressSalt};
use starknet_api::transaction::{EventContent, EventData, EventKey, L2ToL1Payload};
use starknet_types_core::felt::Felt;

use crate::abi::constants;
use crate::execution::call_info::{
CallInfo,
MessageToL1,
Expand All @@ -50,10 +49,10 @@ use crate::execution::native::utils::{calculate_resource_bounds, default_tx_v2_i
use crate::execution::syscalls::exceeds_event_size_limit;
use crate::execution::syscalls::hint_processor::{
SyscallExecutionError,
BLOCK_NUMBER_OUT_OF_RANGE_ERROR,
INVALID_INPUT_LENGTH_ERROR,
OUT_OF_GAS_ERROR,
};
use crate::execution::syscalls::syscall_base::get_block_hash_base;
use crate::state::state_api::State;
use crate::transaction::objects::TransactionInfo;

Expand Down Expand Up @@ -280,21 +279,9 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {

let current_block_number =
self.context.tx_context.block_context.block_info().block_number.0;
if current_block_number < constants::STORED_BLOCK_HASH_BUFFER
|| block_number > current_block_number - constants::STORED_BLOCK_HASH_BUFFER
{
// `panic` is unreachable in this case, also this is covered by tests so we can safely
// unwrap
let out_of_range_felt = Felt::from_hex(BLOCK_NUMBER_OUT_OF_RANGE_ERROR)
.expect("Converting BLOCK_NUMBER_OUT_OF_RANGE_ERROR to Felt should not fail.");
let error = SyscallExecutionError::SyscallError { error_data: vec![out_of_range_felt] };
return Err(self.handle_error(remaining_gas, error));
}

let key = StorageKey::try_from(Felt::from(block_number))
.map_err(|e| self.handle_error(remaining_gas, e.into()))?;
let block_hash_contract_address =
ContractAddress::try_from(Felt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS))
let (key, block_hash_contract_address) =
get_block_hash_base(current_block_number, block_number)
.map_err(|e| self.handle_error(remaining_gas, e.into()))?;

match self.state.get_storage_at(block_hash_contract_address, key) {
Expand Down
18 changes: 4 additions & 14 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ use self::hint_processor::{
EmitEventError,
SyscallExecutionError,
SyscallHintProcessor,
BLOCK_NUMBER_OUT_OF_RANGE_ERROR,
};
use crate::abi::constants;
use crate::execution::call_info::{MessageToL1, OrderedEvent, OrderedL2ToL1Message};
use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use crate::execution::entry_point::{CallEntryPoint, CallType, ConstructorContext};
Expand All @@ -41,16 +39,17 @@ use crate::execution::execution_utils::{
ReadOnlySegment,
};
use crate::execution::syscalls::hint_processor::{INVALID_INPUT_LENGTH_ERROR, OUT_OF_GAS_ERROR};
use crate::execution::syscalls::syscall_base::{get_block_hash_base, SyscallResult};
use crate::transaction::account_transaction::is_cairo1;
use crate::versioned_constants::{EventLimits, VersionedConstants};

pub mod hint_processor;
mod secp;
pub mod syscall_base;

#[cfg(test)]
pub mod syscall_tests;

pub type SyscallResult<T> = Result<T, SyscallExecutionError>;
pub type WriteResponseResult = SyscallResult<()>;

pub type SyscallSelector = DeprecatedSyscallSelector;
Expand Down Expand Up @@ -403,17 +402,8 @@ pub fn get_block_hash(
let current_block_number =
syscall_handler.context.tx_context.block_context.block_info.block_number.0;

if current_block_number < constants::STORED_BLOCK_HASH_BUFFER
|| requested_block_number > current_block_number - constants::STORED_BLOCK_HASH_BUFFER
{
let out_of_range_error =
Felt::from_hex(BLOCK_NUMBER_OUT_OF_RANGE_ERROR).map_err(SyscallExecutionError::from)?;
return Err(SyscallExecutionError::SyscallError { error_data: vec![out_of_range_error] });
}

let key = StorageKey::try_from(Felt::from(requested_block_number))?;
let block_hash_contract_address =
ContractAddress::try_from(Felt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS))?;
let (key, block_hash_contract_address) =
get_block_hash_base(current_block_number, requested_block_number)?;
let block_hash =
BlockHash(syscall_handler.state.get_storage_at(block_hash_contract_address, key)?);
Ok(GetBlockHashResponse { block_hash })
Expand Down
29 changes: 29 additions & 0 deletions crates/blockifier/src/execution/syscalls/syscall_base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use starknet_api::core::ContractAddress;
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;

use crate::abi::constants;
use crate::execution::syscalls::hint_processor::{
SyscallExecutionError,
BLOCK_NUMBER_OUT_OF_RANGE_ERROR,
};

pub type SyscallResult<T> = Result<T, SyscallExecutionError>;

pub fn get_block_hash_base(
current_block_number: u64,
requested_block_number: u64,
) -> SyscallResult<(StorageKey, ContractAddress)> {
if current_block_number < constants::STORED_BLOCK_HASH_BUFFER
|| requested_block_number > current_block_number - constants::STORED_BLOCK_HASH_BUFFER
{
let out_of_range_error =
Felt::from_hex(BLOCK_NUMBER_OUT_OF_RANGE_ERROR).map_err(SyscallExecutionError::from)?;
return Err(SyscallExecutionError::SyscallError { error_data: vec![out_of_range_error] });
}

let key = StorageKey::try_from(Felt::from(requested_block_number))?;
let block_hash_contract_address =
ContractAddress::try_from(Felt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS))?;
Ok((key, block_hash_contract_address))
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,10 @@ fn negative_flow_block_number_out_of_range(test_contract: FeatureContract) {
..trivial_external_entry_point_new(test_contract)
};

let call_result = entry_point_call.execute_directly(&mut state);
let (actual_error_msg, expected_error_msg) = (
format_panic_data(&call_result.unwrap().execution.retdata.0),
"0x426c6f636b206e756d626572206f7574206f662072616e6765 ('Block number out of range')",
let call_info = entry_point_call.execute_directly(&mut state).unwrap();
assert!(call_info.execution.failed);
assert_eq!(
format_panic_data(&call_info.execution.retdata.0),
"0x426c6f636b206e756d626572206f7574206f662072616e6765 ('Block number out of range')"
);

assert_eq!(actual_error_msg, expected_error_msg);
}

0 comments on commit 913d484

Please sign in to comment.