diff --git a/crates/blockifier/resources/versioned_constants_0_13_0.json b/crates/blockifier/resources/versioned_constants_0_13_0.json index b5a7203236f..065897e0050 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 + }, "ecdsa_gas_cost": 0, "memory_hole_gas_cost": 10, "os_contract_addresses": { @@ -620,4 +622,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 cb05b58cb7b..12146738451 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 + }, "ecdsa_gas_cost": 0, "os_contract_addresses": { "block_hash_contract_address": 1, @@ -656,4 +658,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 4f9f5885cf4..20e66761bb9 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 + }, "ecdsa_gas_cost": 0, "memory_hole_gas_cost": 10, "os_contract_addresses": { @@ -656,4 +658,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 8ecabbdd7fb..6325aaa5c2c 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 + }, "ecdsa_gas_cost": 0, "replace_class_gas_cost": { "step_gas_cost": 50, @@ -719,4 +721,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 14e277d82e3..77d5554708e 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 + }, "ecdsa_gas_cost": 0, "replace_class_gas_cost": { "step_gas_cost": 50, @@ -719,4 +721,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 14e277d82e3..77d5554708e 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 + }, "ecdsa_gas_cost": 0, "replace_class_gas_cost": { "step_gas_cost": 50, @@ -719,4 +721,4 @@ 10000 ] } -} +} \ No newline at end of file diff --git a/crates/blockifier/resources/versioned_constants_0_13_4.json b/crates/blockifier/resources/versioned_constants_0_13_4.json index d29f93a2b60..900e9d42a97 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 + }, "ecdsa_gas_cost": 10561, "replace_class_gas_cost": { "step_gas_cost": 104, @@ -722,4 +724,4 @@ 10000 ] } -} +} \ No newline at end of file diff --git a/crates/blockifier/src/execution/entry_point_execution.rs b/crates/blockifier/src/execution/entry_point_execution.rs index cdb9c8c7dcb..2286c944aac 100644 --- a/crates/blockifier/src/execution/entry_point_execution.rs +++ b/crates/blockifier/src/execution/entry_point_execution.rs @@ -196,12 +196,12 @@ fn prepare_program_extra_data( // Create the builtin cost segment, the builtin order should be the same as the price builtin // array in the os in compiled_class.cairo in load_compiled_class_facts. let builtin_price_array = [ - gas_costs.base.pedersen_gas_cost, - gas_costs.base.bitwise_builtin_gas_cost, - gas_costs.base.ecop_gas_cost, - gas_costs.base.poseidon_gas_cost, - gas_costs.base.add_mod_gas_cost, - gas_costs.base.mul_mod_gas_cost, + gas_costs.builtins.pedersen_gas_cost, + gas_costs.builtins.bitwise_builtin_gas_cost, + gas_costs.builtins.ecop_gas_cost, + gas_costs.builtins.poseidon_gas_cost, + gas_costs.builtins.add_mod_gas_cost, + gas_costs.builtins.mul_mod_gas_cost, ]; let data = builtin_price_array diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index 1bf86ff6df6..a5d6364e9b9 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -645,6 +645,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, @@ -671,22 +683,23 @@ pub struct BaseGasCosts { #[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::ecdsa => self.base.ecdsa_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::ecdsa => self.builtins.ecdsa_gas_cost, BuiltinName::segment_arena => return Err(GasCostsError::VirtualBuiltin), BuiltinName::output => { return Err(GasCostsError::UnsupportedBuiltinInCairo1 { builtin: *builtin }); @@ -715,7 +728,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", @@ -746,6 +759,7 @@ impl OsConstants { "validate_rounding_consts", "validated", "syscall_gas_costs", + "builtin_gas_costs" ]; } @@ -755,9 +769,11 @@ impl TryFrom<&OsConstantsRawJson> for GasCosts { fn try_from(raw_json_data: &OsConstantsRawJson) -> Result { let base_value: Value = serde_json::to_value(&raw_json_data.parse_base()?)?; let base: BaseGasCosts = serde_json::from_value(base_value)?; - let syscalls_value: Value = serde_json::to_value(&raw_json_data.parse_syscalls(&base)?)?; + let builtins_value: Value = serde_json::to_value(&raw_json_data.parse_builtin()?)?; + let builtins: BuiltinGasCosts = serde_json::from_value(builtins_value)?; + let syscalls_value: Value = serde_json::to_value(&raw_json_data.parse_syscalls(&base, &builtins)?)?; let syscalls: SyscallGasCosts = serde_json::from_value(syscalls_value)?; - Ok(GasCosts { base, syscalls }) + Ok(GasCosts { base, builtins, syscalls }) } } @@ -830,14 +846,35 @@ impl OsConstantsRawJson { fn parse_syscalls( &self, - base: &BaseGasCosts, + base: &BaseGasCosts, builtins: &BuiltinGasCosts, ) -> Result, OsConstantsSerdeError> { let mut gas_costs = IndexMap::new(); let syscalls: IndexMap = serde_json::from_value( (self.raw_json_file_as_dict.get("syscall_gas_costs").unwrap()).clone(), )?; for (key, value) in syscalls { - self.add_to_syscalls(&key, &value, &mut gas_costs, base)?; + self.add_to_syscalls(&key, &value, &mut gas_costs, base, builtins)?; + } + 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) } @@ -910,6 +947,7 @@ impl OsConstantsRawJson { value: &Value, syscalls: &mut IndexMap, base: &BaseGasCosts, + builtins: &BuiltinGasCosts, ) -> Result<(), OsConstantsSerdeError> { let mut cost = 0; match value { @@ -918,15 +956,15 @@ impl OsConstantsRawJson { let inner_value = match inner_key.as_str() { "step_gas_cost" => base.step_gas_cost, "memory_hole_gas_cost" => base.memory_hole_gas_cost, - "range_check_gas_cost" => base.range_check_gas_cost, - "keccak_builtin_gas_cost" => base.keccak_builtin_gas_cost, - "pedersen_gas_cost" => base.pedersen_gas_cost, - "bitwise_builtin_gas_cost" => base.bitwise_builtin_gas_cost, - "ecop_gas_cost" => base.ecop_gas_cost, - "poseidon_gas_cost" => base.poseidon_gas_cost, - "add_mod_gas_cost" => base.add_mod_gas_cost, - "mul_mod_gas_cost" => base.mul_mod_gas_cost, - "ecdsa_gas_cost" => base.ecdsa_gas_cost, + "range_check_gas_cost" => builtins.range_check_gas_cost, + "keccak_builtin_gas_cost" => builtins.keccak_builtin_gas_cost, + "pedersen_gas_cost" => builtins.pedersen_gas_cost, + "bitwise_builtin_gas_cost" => builtins.bitwise_builtin_gas_cost, + "ecop_gas_cost" => builtins.ecop_gas_cost, + "poseidon_gas_cost" => builtins.poseidon_gas_cost, + "add_mod_gas_cost" => builtins.add_mod_gas_cost, + "mul_mod_gas_cost" => builtins.mul_mod_gas_cost, + "ecdsa_gas_cost" => builtins.ecdsa_gas_cost, "default_initial_gas_cost" => base.default_initial_gas_cost, "entry_point_initial_budget" => base.entry_point_initial_budget, "syscall_base_gas_cost" => base.syscall_base_gas_cost,