Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(blockifier): fix gas cost in versioned constants #428

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 36 additions & 30 deletions crates/blockifier/resources/versioned_constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@
"block_hash_contract_address": 1,
"call_contract_gas_cost": {
"entry_point_gas_cost": 1,
"step_gas_cost": 10,
"syscall_base_gas_cost": 1
"step_gas_cost": 827,
"range_check_gas_cost": 15

},
"constructor_entry_point_selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
"default_entry_point_selector": 0,
"deploy_gas_cost": {
"entry_point_gas_cost": 1,
"step_gas_cost": 200,
"syscall_base_gas_cost": 1
"step_gas_cost": 1097,
"range_check_gas_cost": 18,
"pedersen_gas_cost": 7
},
"emit_event_gas_cost": {
"step_gas_cost": 10,
"syscall_base_gas_cost": 1
"step_gas_cost": 100,
"range_check_gas_cost": 1
},
"entry_point_gas_cost": {
"entry_point_initial_budget": 1,
Expand All @@ -64,12 +66,13 @@
"step_gas_cost": 100
},
"get_block_hash_gas_cost": {
"step_gas_cost": 50,
"syscall_base_gas_cost": 1
"step_gas_cost": 104,
"range_check_gas_cost": 2

},
"get_execution_info_gas_cost": {
"step_gas_cost": 10,
"syscall_base_gas_cost": 1
"step_gas_cost": 100,
"range_check_gas_cost": 1
},
"initial_gas_cost": {
"step_gas_cost": 100000000
Expand All @@ -84,83 +87,86 @@
"l2_gas": "L2_GAS",
"l2_gas_index": 1,
"library_call_gas_cost": {
"call_contract_gas_cost": 1
"entry_point_gas_cost": 1,
"step_gas_cost": 818,
"range_check_gas_cost": 15
},
"sha256_process_block_gas_cost": {
"step_gas_cost": 1852,
"step_gas_cost": 1855,
"range_check_gas_cost": 65,
"bitwise_builtin_gas_cost": 1115,
"syscall_base_gas_cost": 1
},
"memory_hole_gas_cost": 10,
"nop_entry_point_offset": -1,
"range_check_gas_cost": 70,
"pedersen_gas_cost": 4130,
"bitwise_builtin_gas_cost": 594,
"replace_class_gas_cost": {
"step_gas_cost": 50,
"syscall_base_gas_cost": 1
"step_gas_cost": 100,
"range_check_gas_cost": 1
},
"secp256k1_add_gas_cost": {
"range_check_gas_cost": 29,
"step_gas_cost": 406
"step_gas_cost": 410
},
"secp256k1_get_point_from_x_gas_cost": {
"memory_hole_gas_cost": 20,
"range_check_gas_cost": 30,
"step_gas_cost": 391
"step_gas_cost": 395
},
"secp256k1_get_xy_gas_cost": {
"memory_hole_gas_cost": 40,
"range_check_gas_cost": 11,
"step_gas_cost": 239
"step_gas_cost": 207
},
"secp256k1_mul_gas_cost": {
"memory_hole_gas_cost": 2,
"range_check_gas_cost": 7045,
"step_gas_cost": 76501
"step_gas_cost": 76505
},
"secp256k1_new_gas_cost": {
"memory_hole_gas_cost": 40,
"range_check_gas_cost": 35,
"step_gas_cost": 475
"step_gas_cost": 461
},
"secp256r1_add_gas_cost": {
"range_check_gas_cost": 57,
"step_gas_cost": 589
"step_gas_cost": 593
},
"secp256r1_get_point_from_x_gas_cost": {
"memory_hole_gas_cost": 20,
"range_check_gas_cost": 44,
"step_gas_cost": 510
"step_gas_cost": 514
},
"secp256r1_get_xy_gas_cost": {
"memory_hole_gas_cost": 40,
"range_check_gas_cost": 11,
"step_gas_cost": 241
"step_gas_cost": 209
},
"secp256r1_mul_gas_cost": {
"memory_hole_gas_cost": 2,
"range_check_gas_cost": 13961,
"step_gas_cost": 125340
"step_gas_cost": 125344
},
"secp256r1_new_gas_cost": {
"memory_hole_gas_cost": 40,
"range_check_gas_cost": 49,
"step_gas_cost": 594
"step_gas_cost": 580
},
"send_message_to_l1_gas_cost": {
"step_gas_cost": 50,
"syscall_base_gas_cost": 1
"step_gas_cost": 141,
"range_check_gas_cost": 1
},
"sierra_array_len_bound": 4294967296,
"step_gas_cost": 100,
"storage_read_gas_cost": {
"step_gas_cost": 50,
"syscall_base_gas_cost": 1
"step_gas_cost": 100,
"range_check_gas_cost": 1
},
"storage_write_gas_cost": {
"step_gas_cost": 50,
"syscall_base_gas_cost": 1
"step_gas_cost": 100,
"range_check_gas_cost": 1
},
"stored_block_hash_buffer": 10,
"syscall_base_gas_cost": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub const REQUIRED_GAS_CALL_CONTRACT_TEST: u64 = 105680;
pub const REQUIRED_GAS_STORAGE_READ_WRITE_TEST: u64 = 27150;
pub const REQUIRED_GAS_LIBRARY_CALL_TEST: u64 = REQUIRED_GAS_CALL_CONTRACT_TEST;
pub const REQUIRED_GAS_CALL_CONTRACT_TEST: u64 = 168570;
pub const REQUIRED_GAS_STORAGE_READ_WRITE_TEST: u64 = 17290;
pub const REQUIRED_GAS_LIBRARY_CALL_TEST: u64 = 167670;
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn no_constructor_nonempty_calldata(deployer_contract: FeatureContract) {
));
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 10140;"VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 5210;"VM")]
fn with_constructor(deployer_contract: FeatureContract, expected_gas: u64) {
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1);
let mut state = test_state(
Expand Down Expand Up @@ -108,13 +108,16 @@ fn with_constructor(deployer_contract: FeatureContract, expected_gas: u64) {
deployer_contract.get_instance_address(0),
)
.unwrap();
// Note, this is the call info of the constructor call (inner call).
let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap().inner_calls[0];

assert_eq!(deploy_call.call.storage_address, contract_address);
assert_eq!(
deploy_call.execution,
CallExecution {
// The test contract constructor returns its first argument.
retdata: retdata![constructor_calldata[0]],
// This reflects the gas cost of storage write syscall.
gas_consumed: expected_gas,
..CallExecution::default()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const DATA: [Felt; 3] = [
];
const N_EMITTED_EVENTS: [Felt; 1] = [Felt::from_hex_unchecked("0x1")];

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 49860; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 48930; "VM")]
fn positive_flow(test_contract: FeatureContract, expected_gas: u64) {
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why the conversion
// works.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn initialize_state(test_contract: FeatureContract) -> (CachedState<DictStateRea
(state, block_number, block_hash)
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 9680; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 5220; "VM")]
fn positive_flow(test_contract: FeatureContract, expected_gas: u64) {
let (mut state, block_number, block_hash) = initialize_state(test_contract);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn test_library_call_assert_fails(test_contract: FeatureContract) {
assert!(err.to_string().contains("x != y"));
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 276880; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 472710; "VM")]
fn test_nested_library_call(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 Expand Up @@ -113,7 +113,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
class_hash: Some(test_class_hash),
code_address: None,
call_type: CallType::Delegate,
initial_gas: 9999745020,
initial_gas: 9999601320,
..trivial_external_entry_point_new(test_contract)
};
let library_entry_point = CallEntryPoint {
Expand All @@ -128,12 +128,12 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
class_hash: Some(test_class_hash),
code_address: None,
call_type: CallType::Delegate,
initial_gas: 9999823550,
initial_gas: 9999751700,
..trivial_external_entry_point_new(test_contract)
};
let storage_entry_point = CallEntryPoint {
calldata: calldata![felt!(key), felt!(value)],
initial_gas: 9999656870,
initial_gas: 9999451180,
..nested_storage_entry_point
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn cairo0_class_hash(test_contract: FeatureContract) {
assert!(error.contains("Cannot replace V1 class hash with V0 class hash"));
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 9750; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 4820; "VM")]
fn positive_flow(test_contract: FeatureContract, gas_consumed: u64) {
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1);
let empty_contract_cairo0 = FeatureContract::Empty(CairoVersion::Cairo0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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::Cairo1), 17032670; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 17022270; "VM")]
fn test_secp256k1(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 All @@ -27,7 +27,7 @@ fn test_secp256k1(test_contract: FeatureContract, expected_gas: u64) {
);
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 27582260; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 27570060; "VM")]
fn test_secp256r1(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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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::Cairo1), 22990; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 22160; "VM")]
fn test_send_message_to_l1(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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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::Cairo1), 893590; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 893890; "VM")]
fn test_sha256(test_contract: FeatureContract, gas_consumed: u64) {
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ fn add_kzg_da_resources_to_resources_mapping(
},
validate_gas_consumed: 4740, // The gas consumption results from parsing the input
// arguments.
execute_gas_consumed: 88160,
inner_call_initial_gas: 9999707100,
execute_gas_consumed: 159980,
inner_call_initial_gas: 9999635280,
},
CairoVersion::Cairo1)]
fn test_invoke_tx(
Expand Down Expand Up @@ -1840,7 +1840,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
},
execution: CallExecution {
retdata: Retdata(vec![value]),
gas_consumed: 11750,
gas_consumed: 6820,
..Default::default()
},
resources: ExecutionResources {
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ impl<'de> Deserialize<'de> for OsResources {
#[derive(Debug, Default, Deserialize)]
pub struct GasCosts {
pub step_gas_cost: u64,
// Range check has a hard-coded cost higher than its proof percentage to avoid the overhead of
// retrieving its price from the table.
pub range_check_gas_cost: u64,
pub memory_hole_gas_cost: u64,
// An estimation of the initial gas for a transaction to run with. This solution is
Expand Down
Loading