Skip to content

Commit

Permalink
chore(blockifier): remove syscall_gas_costs from os_constans
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonatan-Starkware committed Jan 2, 2025
1 parent 06e5f87 commit 9d03378
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 150 deletions.
8 changes: 4 additions & 4 deletions crates/blockifier/resources/versioned_constants_0_13_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
"n_memory_holes": 0,
"n_steps": 44
},
"KeccakRound": {
"Keccak": {
"builtin_instance_counter": {
"bitwise_builtin": 6,
"keccak_builtin": 1,
Expand All @@ -305,10 +305,10 @@
"n_memory_holes": 0,
"n_steps": 381
},
"Keccak": {
"n_steps": 0,
"KeccakRound": {
"builtin_instance_counter": {},
"n_memory_holes": 0
"n_memory_holes": 0,
"n_steps": 0
},
"LibraryCall": {
"builtin_instance_counter": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,4 +657,4 @@
10000
]
}
}
}
113 changes: 4 additions & 109 deletions crates/blockifier/resources/versioned_constants_0_13_4.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,112 +116,7 @@
"validate_block_number_rounding": 100,
"validate_timestamp_rounding": 3600
},
"validated": "VALID",
"syscall_gas_costs": {
"call_contract": {
"step_gas_cost": 860,
"range_check": 15
},
"deploy": {
"step_gas_cost": 1128,
"range_check": 18,
"pedersen": 7
},
"emit_event": {
"step_gas_cost": 100,
"range_check": 1
},
"get_block_hash": {
"step_gas_cost": 104,
"range_check": 2
},
"get_execution_info": {
"step_gas_cost": 100,
"range_check": 1
},
"keccak": {
"syscall_base_gas_cost": 1
},
"keccak_round_cost": 180000,
"library_call": {
"step_gas_cost": 836,
"range_check": 15
},
"sha256_process_block": {
"step_gas_cost": 1855,
"range_check": 65,
"bitwise": 1115,
"syscall_base_gas_cost": 1
},
"replace_class": {
"step_gas_cost": 104,
"range_check": 1
},
"secp256k1_add": {
"range_check": 29,
"step_gas_cost": 410
},
"secp256k1_get_point_from_x": {
"memory_hole_gas_cost": 20,
"range_check": 30,
"step_gas_cost": 395
},
"secp256k1_get_xy": {
"memory_hole_gas_cost": 40,
"range_check": 11,
"step_gas_cost": 207
},
"secp256k1_mul": {
"memory_hole_gas_cost": 2,
"range_check": 7045,
"step_gas_cost": 76505
},
"secp256k1_new": {
"memory_hole_gas_cost": 40,
"range_check": 35,
"step_gas_cost": 461
},
"secp256r1_add": {
"range_check": 57,
"step_gas_cost": 593
},
"secp256r1_get_point_from_x": {
"memory_hole_gas_cost": 20,
"range_check": 44,
"step_gas_cost": 514
},
"secp256r1_get_xy": {
"memory_hole_gas_cost": 40,
"range_check": 11,
"step_gas_cost": 209
},
"secp256r1_mul": {
"memory_hole_gas_cost": 2,
"range_check": 13961,
"step_gas_cost": 125344
},
"secp256r1_new": {
"memory_hole_gas_cost": 40,
"range_check": 49,
"step_gas_cost": 580
},
"send_message_to_l1": {
"step_gas_cost": 141,
"range_check": 1
},
"storage_read": {
"step_gas_cost": 100,
"range_check": 1
},
"storage_write": {
"step_gas_cost": 100,
"range_check": 1
},
"get_class_hash_at": {
"step_gas_cost": 100,
"range_check": 1
}
}
"validated": "VALID"
},
"os_resources": {
"execute_syscalls": {
Expand Down Expand Up @@ -317,7 +212,7 @@
"n_memory_holes": 0
},
"KeccakRound": {
"n_steps": 381,
"n_steps": 281,
"builtin_instance_counter": {
"bitwise_builtin": 6,
"keccak_builtin": 1,
Expand All @@ -326,7 +221,7 @@
"n_memory_holes": 0
},
"Keccak": {
"n_steps": 0,
"n_steps": 100,
"builtin_instance_counter": {},
"n_memory_holes": 0
},
Expand Down Expand Up @@ -663,4 +558,4 @@
10000
]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub const REQUIRED_GAS_CALL_CONTRACT_TEST: u64 = 130370;
pub const REQUIRED_GAS_STORAGE_READ_WRITE_TEST: u64 = 26990;
pub const REQUIRED_GAS_GET_CLASS_HASH_AT_TEST: u64 = 17830;
pub const REQUIRED_GAS_LIBRARY_CALL_TEST: u64 = 127970;
pub const REQUIRED_GAS_CALL_CONTRACT_TEST: u64 = 130830;
pub const REQUIRED_GAS_STORAGE_READ_WRITE_TEST: u64 = 26850;
pub const REQUIRED_GAS_GET_CLASS_HASH_AT_TEST: u64 = 17760;
pub const REQUIRED_GAS_LIBRARY_CALL_TEST: u64 = 128430;
pub const REQUIRED_GAS_GET_BLOCK_HASH_TEST: u64 = 15220;
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn no_constructor(runnable_version: RunnableCairo1) {
let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap();
assert_eq!(
deploy_call.execution,
CallExecution { retdata: retdata![], gas_consumed: 155200, ..CallExecution::default() }
CallExecution { retdata: retdata![], gas_consumed: 155600, ..CallExecution::default() }
);

let deployed_contract_address = calculate_contract_address(
Expand Down Expand Up @@ -124,7 +124,7 @@ fn with_constructor(runnable_version: RunnableCairo1) {
let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap();
assert_eq!(
deploy_call.execution,
CallExecution { retdata: retdata![], gas_consumed: 174550, ..CallExecution::default() }
CallExecution { retdata: retdata![], gas_consumed: 174880, ..CallExecution::default() }
);

let constructor_call = &deploy_call.inner_calls[0];
Expand All @@ -136,7 +136,7 @@ fn with_constructor(runnable_version: RunnableCairo1) {
// The test contract constructor returns its first argument.
retdata: retdata![constructor_calldata[0]],
// This reflects the gas cost of storage write syscall.
gas_consumed: 14610,
gas_consumed: 14540,
..CallExecution::default()
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn positive_flow(runnable_version: RunnableCairo1) {
call_info.execution,
CallExecution {
events: vec![OrderedEvent { order: 0, event }],
gas_consumed: 57330,
gas_consumed: 57260,
..Default::default()
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::abi::constants;
use crate::context::ChainInfo;
use crate::execution::call_info::CallExecution;
use crate::execution::entry_point::CallEntryPoint;
use crate::execution::syscalls::syscall_tests::constants::REQUIRED_GAS_GET_BLOCK_HASH_TEST;
use crate::retdata;
use crate::state::cached_state::CachedState;
use crate::state::state_api::State;
Expand All @@ -20,7 +21,9 @@ use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, RunnableCairo1, BALANCE};
use crate::versioned_constants::VersionedConstants;

fn initialize_state(test_contract: FeatureContract) -> (CachedState<DictStateReader>, Felt, Felt) {
pub fn initialize_state(
test_contract: FeatureContract,
) -> (CachedState<DictStateReader>, Felt, Felt) {
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);

Expand Down Expand Up @@ -53,7 +56,10 @@ fn positive_flow(runnable_version: RunnableCairo1) {

assert_eq!(
entry_point_call.clone().execute_directly(&mut state).unwrap().execution,
CallExecution { gas_consumed: 15220, ..CallExecution::from_retdata(retdata![block_hash]) }
CallExecution {
gas_consumed: REQUIRED_GAS_GET_BLOCK_HASH_TEST,
..CallExecution::from_retdata(retdata![block_hash])
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fn test_keccak(runnable_version: RunnableCairo1) {

pretty_assertions::assert_eq!(
entry_point_call.execute_directly(&mut state).unwrap().execution,
CallExecution { gas_consumed: 264240, ..CallExecution::from_retdata(retdata![]) }
CallExecution { gas_consumed: 255947, ..CallExecution::from_retdata(retdata![]) }
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn test_library_call_assert_fails(runnable_version: RunnableCairo1) {
// 'ENTRYPOINT_FAILED'.
felt!("0x454e545259504f494e545f4641494c4544")
]),
gas_consumed: 110980,
gas_consumed: 111580,
failed: true,
..Default::default()
}
Expand Down Expand Up @@ -124,7 +124,7 @@ fn test_nested_library_call(runnable_version: RunnableCairo1) {
class_hash: Some(test_class_hash),
code_address: None,
call_type: CallType::Delegate,
initial_gas: 9999085960,
initial_gas: 9999084760,
..trivial_external_entry_point_new(test_contract)
};
let library_entry_point = CallEntryPoint {
Expand All @@ -139,16 +139,16 @@ fn test_nested_library_call(runnable_version: RunnableCairo1) {
class_hash: Some(test_class_hash),
code_address: None,
call_type: CallType::Delegate,
initial_gas: 9999186940,
initial_gas: 9999186340,
..trivial_external_entry_point_new(test_contract)
};
let storage_entry_point = CallEntryPoint {
calldata: calldata![felt!(key), felt!(value)],
initial_gas: 9998984320,
initial_gas: 9998982660,
..nested_storage_entry_point
};

let storage_entry_point_gas = GasAmount(26990);
let storage_entry_point_gas = GasAmount(26850);

// The default VersionedConstants is used in the execute_directly call bellow.
let tracked_resource = test_contract.get_runnable_class().tracked_resource(
Expand Down Expand Up @@ -177,7 +177,7 @@ fn test_nested_library_call(runnable_version: RunnableCairo1) {
gas_consumed: REQUIRED_GAS_LIBRARY_CALL_TEST,
..CallExecution::default()
},
charged_resources: ChargedResources::from_gas(GasAmount(127970)),
charged_resources: ChargedResources::from_gas(GasAmount(128430)),
inner_calls: vec![nested_storage_call_info],
tracked_resource,
..Default::default()
Expand All @@ -197,7 +197,7 @@ fn test_nested_library_call(runnable_version: RunnableCairo1) {
..Default::default()
};

let main_gas_consumed = 335110;
let main_gas_consumed = 336630;
let expected_call_info = CallInfo {
call: main_entry_point.clone(),
execution: CallExecution {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,51 @@ use starknet_api::{calldata, felt};
use test_case::test_case;

use crate::abi::constants::MAX_POSSIBLE_SIERRA_GAS;
#[cfg(feature = "cairo_native")]
use crate::context::ChainInfo;
use crate::execution::call_info::CallExecution;
use crate::execution::entry_point::CallEntryPoint;
use crate::execution::syscalls::syscall_tests::constants::REQUIRED_GAS_STORAGE_READ_WRITE_TEST;
use crate::execution::syscalls::syscall_tests::constants;
use crate::execution::syscalls::syscall_tests::get_block_hash::initialize_state;
use crate::execution::syscalls::SyscallSelector;
use crate::retdata;
use crate::test_utils::contracts::FeatureContract;
#[cfg(feature = "cairo_native")]
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, RunnableCairo1, BALANCE};
#[cfg(feature = "cairo_native")]
use crate::test_utils::BALANCE;
use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, RunnableCairo1};
use crate::versioned_constants::VersionedConstants;

const SYSCALL_BASE_GAS_COST: u64 = 10000;

#[cfg_attr(feature = "cairo_native", test_case(RunnableCairo1::Native; "Native"))]
#[test_case(RunnableCairo1::Casm; "VM")]
fn test_out_of_gas(runnable_version: RunnableCairo1) {
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1(runnable_version));
let mut state = test_state(&ChainInfo::create_for_testing(), BALANCE, &[(test_contract, 1)]);
let (mut state, block_number, _block_hash) = initialize_state(test_contract);

let key = felt!(1234_u16);
let value = felt!(18_u8);
let calldata = calldata![key, value];
let calldata = calldata![block_number];
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_get_block_hash"),
calldata,
entry_point_selector: selector_from_name("test_storage_read_write"),
initial_gas: REQUIRED_GAS_STORAGE_READ_WRITE_TEST - 1,
initial_gas: constants::REQUIRED_GAS_GET_BLOCK_HASH_TEST - 1,
..trivial_external_entry_point_new(test_contract)
};
let call_info = entry_point_call.execute_directly(&mut state).unwrap();

let gas_costs = &VersionedConstants::create_for_testing().os_constants.gas_costs;
let get_block_hash_gas_cost =
gas_costs.syscalls.get_syscall_gas_cost(&SyscallSelector::GetBlockHash).unwrap();

// We hit the out of gas error right before executing the syscall.
let syscall_required_gas = get_block_hash_gas_cost - SYSCALL_BASE_GAS_COST;
let call_info = entry_point_call.clone().execute_directly(&mut state).unwrap();
assert_eq!(
call_info.execution,
CallExecution {
// 'Out of gas'
retdata: retdata![felt!["0x4f7574206f6620676173"]],
gas_consumed: REQUIRED_GAS_STORAGE_READ_WRITE_TEST - 70,
gas_consumed: constants::REQUIRED_GAS_GET_BLOCK_HASH_TEST - syscall_required_gas,
failed: true,
..Default::default()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn test_secp256k1(runnable_version: RunnableCairo1) {

pretty_assertions::assert_eq!(
entry_point_call.execute_directly(&mut state).unwrap().execution,
CallExecution { gas_consumed: 17043486, ..Default::default() }
CallExecution { gas_consumed: 17042553, ..Default::default() }
);
}

Expand All @@ -45,6 +45,6 @@ fn test_secp256r1(runnable_version: RunnableCairo1) {

pretty_assertions::assert_eq!(
entry_point_call.execute_directly(&mut state).unwrap().execution,
CallExecution { gas_consumed: 27572930, ..Default::default() }
CallExecution { gas_consumed: 27570090, ..Default::default() }
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fn test_sha256(runnable_version: RunnableCairo1) {

pretty_assertions::assert_eq!(
entry_point_call.execute_directly(&mut state).unwrap().execution,
CallExecution { gas_consumed: 891425, ..CallExecution::from_retdata(retdata![]) }
CallExecution { gas_consumed: 882425, ..CallExecution::from_retdata(retdata![]) }
);
}
Loading

0 comments on commit 9d03378

Please sign in to comment.