diff --git a/crates/blockifier/resources/versioned_constants_0_13_0.json b/crates/blockifier/resources/versioned_constants_0_13_0.json index f6f4e769991..445923cf444 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_0.json +++ b/crates/blockifier/resources/versioned_constants_0_13_0.json @@ -73,14 +73,16 @@ "default_entry_point_selector": 0, "stored_block_hash_buffer": 10, "step_gas_cost": 100, - "range_check_gas_cost": 70, - "keccak_builtin_gas_cost": 0, - "pedersen_gas_cost": 0, - "bitwise_builtin_gas_cost": 0, - "ecop_gas_cost": 0, - "poseidon_gas_cost": 0, - "add_mod_gas_cost": 0, - "mul_mod_gas_cost": 0, + "builtin_gas_costs": { + "range_check_gas_cost": 70, + "keccak_builtin_gas_cost": 0, + "pedersen_gas_cost": 0, + "bitwise_builtin_gas_cost": 0, + "ecop_gas_cost": 0, + "poseidon_gas_cost": 0, + "add_mod_gas_cost": 0, + "mul_mod_gas_cost": 0 + }, "memory_hole_gas_cost": 10, "os_contract_addresses": { "block_hash_contract_address": 1, @@ -619,4 +621,4 @@ 1000 ] } -} +} \ No newline at end of file diff --git a/crates/blockifier/resources/versioned_constants_0_13_1.json b/crates/blockifier/resources/versioned_constants_0_13_1.json index 22cf25a5fc3..17f8681c4c2 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_1.json @@ -73,14 +73,16 @@ "default_entry_point_selector": 0, "stored_block_hash_buffer": 10, "step_gas_cost": 100, - "range_check_gas_cost": 70, - "keccak_builtin_gas_cost": 0, - "pedersen_gas_cost": 0, - "bitwise_builtin_gas_cost": 0, - "ecop_gas_cost": 0, - "poseidon_gas_cost": 0, - "add_mod_gas_cost": 0, - "mul_mod_gas_cost": 0, + "builtin_gas_costs": { + "range_check_gas_cost": 70, + "keccak_builtin_gas_cost": 0, + "pedersen_gas_cost": 0, + "bitwise_builtin_gas_cost": 0, + "ecop_gas_cost": 0, + "poseidon_gas_cost": 0, + "add_mod_gas_cost": 0, + "mul_mod_gas_cost": 0 + }, "os_contract_addresses": { "block_hash_contract_address": 1, "alias_contract_address": 2, @@ -655,4 +657,4 @@ 10000 ] } -} +} \ No newline at end of file diff --git a/crates/blockifier/resources/versioned_constants_0_13_1_1.json b/crates/blockifier/resources/versioned_constants_0_13_1_1.json index 9acd90d76a4..3119f0987d3 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_1_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_1_1.json @@ -73,14 +73,16 @@ "default_entry_point_selector": 0, "stored_block_hash_buffer": 10, "step_gas_cost": 100, - "range_check_gas_cost": 70, - "keccak_builtin_gas_cost": 0, - "pedersen_gas_cost": 0, - "bitwise_builtin_gas_cost": 0, - "ecop_gas_cost": 0, - "poseidon_gas_cost": 0, - "add_mod_gas_cost": 0, - "mul_mod_gas_cost": 0, + "builtin_gas_costs": { + "range_check_gas_cost": 70, + "keccak_builtin_gas_cost": 0, + "pedersen_gas_cost": 0, + "bitwise_builtin_gas_cost": 0, + "ecop_gas_cost": 0, + "poseidon_gas_cost": 0, + "add_mod_gas_cost": 0, + "mul_mod_gas_cost": 0 + }, "memory_hole_gas_cost": 10, "os_contract_addresses": { "block_hash_contract_address": 1, @@ -655,4 +657,4 @@ 10000 ] } -} +} \ No newline at end of file diff --git a/crates/blockifier/resources/versioned_constants_0_13_2.json b/crates/blockifier/resources/versioned_constants_0_13_2.json index 591db4907f4..87260a7e50a 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_2.json +++ b/crates/blockifier/resources/versioned_constants_0_13_2.json @@ -93,14 +93,16 @@ "alias_contract_address": 2, "reserved_contract_address": 3 }, - "range_check_gas_cost": 70, - "keccak_builtin_gas_cost": 0, - "pedersen_gas_cost": 0, - "bitwise_builtin_gas_cost": 594, - "ecop_gas_cost": 0, - "poseidon_gas_cost": 0, - "add_mod_gas_cost": 0, - "mul_mod_gas_cost": 0, + "builtin_gas_costs": { + "range_check_gas_cost": 70, + "keccak_builtin_gas_cost": 0, + "pedersen_gas_cost": 0, + "bitwise_builtin_gas_cost": 594, + "ecop_gas_cost": 0, + "poseidon_gas_cost": 0, + "add_mod_gas_cost": 0, + "mul_mod_gas_cost": 0 + }, "sierra_array_len_bound": 4294967296, "step_gas_cost": 100, "stored_block_hash_buffer": 10, @@ -662,4 +664,4 @@ 10000 ] } -} +} \ No newline at end of file diff --git a/crates/blockifier/resources/versioned_constants_0_13_2_1.json b/crates/blockifier/resources/versioned_constants_0_13_2_1.json index 596edfb47bb..05bcdc4f429 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_2_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_2_1.json @@ -93,14 +93,16 @@ "alias_contract_address": 2, "reserved_contract_address": 3 }, - "range_check_gas_cost": 70, - "keccak_builtin_gas_cost": 0, - "pedersen_gas_cost": 0, - "bitwise_builtin_gas_cost": 594, - "ecop_gas_cost": 0, - "poseidon_gas_cost": 0, - "add_mod_gas_cost": 0, - "mul_mod_gas_cost": 0, + "builtin_gas_costs": { + "range_check_gas_cost": 70, + "keccak_builtin_gas_cost": 0, + "pedersen_gas_cost": 0, + "bitwise_builtin_gas_cost": 594, + "ecop_gas_cost": 0, + "poseidon_gas_cost": 0, + "add_mod_gas_cost": 0, + "mul_mod_gas_cost": 0 + }, "sierra_array_len_bound": 4294967296, "step_gas_cost": 100, "stored_block_hash_buffer": 10, @@ -662,4 +664,4 @@ 10000 ] } -} +} \ No newline at end of file diff --git a/crates/blockifier/resources/versioned_constants_0_13_3.json b/crates/blockifier/resources/versioned_constants_0_13_3.json index 596edfb47bb..bb7e3e8dcc0 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_3.json +++ b/crates/blockifier/resources/versioned_constants_0_13_3.json @@ -93,14 +93,16 @@ "alias_contract_address": 2, "reserved_contract_address": 3 }, - "range_check_gas_cost": 70, - "keccak_builtin_gas_cost": 0, - "pedersen_gas_cost": 0, - "bitwise_builtin_gas_cost": 594, - "ecop_gas_cost": 0, - "poseidon_gas_cost": 0, - "add_mod_gas_cost": 0, - "mul_mod_gas_cost": 0, + "builtin_gas_costs": { + "range_check_gas_cost": 70, + "keccak_builtin_gas_cost": 0, + "pedersen_gas_cost": 0, + "bitwise_builtin_gas_cost": 594, + "ecop_gas_cost": 0, + "poseidon_gas_cost": 0, + "add_mod_gas_cost": 0, + "mul_mod_gas_cost": 0 + }, "sierra_array_len_bound": 4294967296, "step_gas_cost": 100, "stored_block_hash_buffer": 10, diff --git a/crates/blockifier/resources/versioned_constants_0_13_4.json b/crates/blockifier/resources/versioned_constants_0_13_4.json index b3b709595e8..afa8e1f4fec 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_4.json +++ b/crates/blockifier/resources/versioned_constants_0_13_4.json @@ -91,14 +91,16 @@ "alias_contract_address": 2, "reserved_contract_address": 3 }, - "range_check_gas_cost": 70, - "keccak_builtin_gas_cost": 136189, - "pedersen_gas_cost": 4050, - "bitwise_builtin_gas_cost": 583, - "ecop_gas_cost": 4085, - "poseidon_gas_cost": 491, - "add_mod_gas_cost": 230, - "mul_mod_gas_cost": 604, + "builtin_gas_costs": { + "range_check_gas_cost": 70, + "keccak_builtin_gas_cost": 136189, + "pedersen_gas_cost": 4050, + "bitwise_builtin_gas_cost": 583, + "ecop_gas_cost": 4085, + "poseidon_gas_cost": 491, + "add_mod_gas_cost": 230, + "mul_mod_gas_cost": 604 + }, "sierra_array_len_bound": 4294967296, "step_gas_cost": 100, "stored_block_hash_buffer": 10, @@ -665,4 +667,4 @@ 10000 ] } -} +} \ No newline at end of file diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index dfa8be57efb..3ee145edb9a 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -644,6 +644,18 @@ impl SyscallGasCosts { pub struct BaseGasCosts { pub step_gas_cost: u64, pub memory_hole_gas_cost: u64, + // An estimation of the initial gas for a transaction to run with. This solution is + // temporary and this value will be deduced from the transaction's fields. + pub default_initial_gas_cost: u64, + // Compiler gas costs. + pub entry_point_initial_budget: u64, + pub syscall_base_gas_cost: u64, + // OS gas costs. + pub transaction_gas_cost: u64, +} + +#[derive(Debug, Default, Deserialize)] +pub struct BuiltinGasCosts { // 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, @@ -655,35 +667,28 @@ pub struct BaseGasCosts { pub poseidon_gas_cost: u64, pub add_mod_gas_cost: u64, pub mul_mod_gas_cost: u64, - // An estimation of the initial gas for a transaction to run with. This solution is - // temporary and this value will be deduced from the transaction's fields. - pub default_initial_gas_cost: u64, - // Compiler gas costs. - pub entry_point_initial_budget: u64, - pub syscall_base_gas_cost: u64, - // OS gas costs. - pub transaction_gas_cost: u64, } /// Gas cost constants. For more documentation see in core/os/constants.cairo. #[derive(Debug, Default, Deserialize)] pub struct GasCosts { pub base: BaseGasCosts, + pub builtins: BuiltinGasCosts, pub syscalls: SyscallGasCosts, } impl GasCosts { pub fn get_builtin_gas_cost(&self, builtin: &BuiltinName) -> Result { let gas_cost = match *builtin { - BuiltinName::range_check => self.base.range_check_gas_cost, - BuiltinName::pedersen => self.base.pedersen_gas_cost, - BuiltinName::bitwise => self.base.bitwise_builtin_gas_cost, - BuiltinName::ec_op => self.base.ecop_gas_cost, - BuiltinName::keccak => self.base.keccak_builtin_gas_cost, - BuiltinName::poseidon => self.base.poseidon_gas_cost, - BuiltinName::range_check96 => self.base.range_check_gas_cost, - BuiltinName::add_mod => self.base.add_mod_gas_cost, - BuiltinName::mul_mod => self.base.mul_mod_gas_cost, + BuiltinName::range_check => self.builtins.range_check_gas_cost, + BuiltinName::pedersen => self.builtins.pedersen_gas_cost, + BuiltinName::bitwise => self.builtins.bitwise_builtin_gas_cost, + BuiltinName::ec_op => self.builtins.ecop_gas_cost, + BuiltinName::keccak => self.builtins.keccak_builtin_gas_cost, + BuiltinName::poseidon => self.builtins.poseidon_gas_cost, + BuiltinName::range_check96 => self.builtins.range_check_gas_cost, + BuiltinName::add_mod => self.builtins.add_mod_gas_cost, + BuiltinName::mul_mod => self.builtins.mul_mod_gas_cost, BuiltinName::segment_arena => return Err(GasCostsError::VirtualBuiltin), BuiltinName::output | BuiltinName::ecdsa => { return Err(GasCostsError::UnsupportedBuiltinInCairo1 { builtin: *builtin }); @@ -712,7 +717,7 @@ impl OsConstants { // not used by the blockifier but included for transparency. These constanst will be ignored // during the creation of the struct containing the gas costs. - const ADDITIONAL_FIELDS: [&'static str; 30] = [ + const ADDITIONAL_FIELDS: [&'static str; 31] = [ "block_hash_contract_address", "constructor_entry_point_selector", "default_entry_point_selector", @@ -743,6 +748,7 @@ impl OsConstants { "validate_rounding_consts", "validated", "syscall_gas_costs", + "builtin_gas_costs" ]; } @@ -754,7 +760,7 @@ impl TryFrom<&OsConstantsRawJson> for GasCosts { let base: BaseGasCosts = serde_json::from_value(base_value)?; let syscalls_value: Value = serde_json::to_value(&raw_json_data.parse_syscalls(&base)?)?; let syscalls: SyscallGasCosts = serde_json::from_value(syscalls_value)?; - Ok(GasCosts { base, syscalls }) + Ok(GasCosts { base, builtins, syscalls }) } } @@ -839,6 +845,27 @@ impl OsConstantsRawJson { Ok(gas_costs) } + fn parse_builtin(&self) -> Result, OsConstantsSerdeError> { + let mut gas_costs = IndexMap::new(); + let builtins: IndexMap = serde_json::from_value( + (self.raw_json_file_as_dict.get("builtin_gas_costs").unwrap()).clone(), + )?; + for (key, value) in builtins { + let mut cost = 0; + match value{ + Value::Number(n) => { + cost = n.as_u64().ok_or_else(|| OsConstantsSerdeError::OutOfRange { + key: key.to_string(), + value: n.clone(), + })?; + gas_costs.insert(key.to_string(), cost); + } + _ => return Err(OsConstantsSerdeError::UnhandledValueType(value.clone())), + } + } + Ok(gas_costs) + } + /// Recursively adds a key to gas costs, calculating its value after processing any nested keys. // Invariant: there is no circular dependency between key definitions. fn recursive_add_to_base(