From f6538494d761782395a8252de7a14f89bdf80d80 Mon Sep 17 00:00:00 2001 From: meship-starkware Date: Wed, 14 Aug 2024 08:33:03 +0300 Subject: [PATCH] refactor(blockifier): fix gas cost in versioned constants --- .../resources/versioned_constants.json | 66 ++++++++++--------- .../syscalls/syscall_tests/constants.rs | 6 +- .../syscalls/syscall_tests/deploy.rs | 5 +- .../syscalls/syscall_tests/emit_event.rs | 2 +- .../syscalls/syscall_tests/get_block_hash.rs | 2 +- .../syscalls/syscall_tests/library_call.rs | 8 +-- .../syscalls/syscall_tests/replace_class.rs | 2 +- .../execution/syscalls/syscall_tests/secp.rs | 4 +- .../syscall_tests/send_message_to_l1.rs | 2 +- .../syscalls/syscall_tests/sha256.rs | 2 +- .../src/transaction/transactions_test.rs | 6 +- crates/blockifier/src/versioned_constants.rs | 2 + 12 files changed, 59 insertions(+), 48 deletions(-) diff --git a/crates/blockifier/resources/versioned_constants.json b/crates/blockifier/resources/versioned_constants.json index a0dd34a749..9789f0fb80 100644 --- a/crates/blockifier/resources/versioned_constants.json +++ b/crates/blockifier/resources/versioned_constants.json @@ -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, @@ -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 @@ -84,10 +87,12 @@ "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 @@ -95,72 +100,73 @@ "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": { diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/constants.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/constants.rs index 94c378a074..81505355e0 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/constants.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/constants.rs @@ -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; diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs index 6381d4f945..f3517e1f9d 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs @@ -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( @@ -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() } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/emit_event.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/emit_event.rs index cd7a0a3c1e..3913e47014 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/emit_event.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/emit_event.rs @@ -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. diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs index 163526dba9..cb3bdc85db 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs @@ -40,7 +40,7 @@ fn initialize_state(test_contract: FeatureContract) -> (CachedState 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