From d30e22a1ba5785b8d68a1188a64691f967e69293 Mon Sep 17 00:00:00 2001 From: Tzahi Date: Wed, 25 Dec 2024 11:15:10 +0200 Subject: [PATCH] fix(blockifier): move max sierra_gas constants under os_constants (#2908) --- .../resources/versioned_constants_0_13_0.json | 6 +- .../resources/versioned_constants_0_13_1.json | 6 +- .../versioned_constants_0_13_1_1.json | 6 +- .../resources/versioned_constants_0_13_2.json | 6 +- .../versioned_constants_0_13_2_1.json | 6 +- .../resources/versioned_constants_0_13_3.json | 6 +- .../resources/versioned_constants_0_13_4.json | 6 +- .../src/transaction/account_transaction.rs | 7 ++- .../transaction/account_transactions_test.rs | 6 +- .../src/transaction/transactions_test.rs | 19 +++--- crates/blockifier/src/versioned_constants.rs | 60 +++++++++++++++---- 11 files changed, 90 insertions(+), 44 deletions(-) diff --git a/crates/blockifier/resources/versioned_constants_0_13_0.json b/crates/blockifier/resources/versioned_constants_0_13_0.json index c5446f8dfc..476b29e7eb 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_0.json +++ b/crates/blockifier/resources/versioned_constants_0_13_0.json @@ -4,7 +4,6 @@ "max_contract_bytecode_size": 61440 }, "invoke_tx_max_n_steps": 3000000, - "execute_max_sierra_gas": 10000000000, "max_recursion_depth": 50, "segment_arena_cells": true, "disable_cairo0_redeclaration": false, @@ -71,6 +70,8 @@ "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "transfer_entry_point_selector": "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e", "default_entry_point_selector": 0, + "validate_max_sierra_gas": 10000000000, + "execute_max_sierra_gas": 10000000000, "stored_block_hash_buffer": 10, "step_gas_cost": 100, "builtin_gas_costs": { @@ -563,7 +564,6 @@ } }, "validate_max_n_steps": 1000000, - "validate_max_sierra_gas": 10000000000, "min_sierra_version_for_sierra_gas": "100.0.0", "vm_resource_fee_cost": { "builtins": { @@ -617,4 +617,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 5ec4657573..ef4e01eca8 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_1.json @@ -9,7 +9,6 @@ "max_contract_bytecode_size": 81920 }, "invoke_tx_max_n_steps": 4000000, - "execute_max_sierra_gas": 10000000000, "deprecated_l2_resource_gas_costs": { "gas_per_data_felt": [ 128, @@ -71,6 +70,8 @@ "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "transfer_entry_point_selector": "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e", "default_entry_point_selector": 0, + "validate_max_sierra_gas": 10000000000, + "execute_max_sierra_gas": 10000000000, "stored_block_hash_buffer": 10, "step_gas_cost": 100, "builtin_gas_costs": { @@ -599,7 +600,6 @@ } }, "validate_max_n_steps": 1000000, - "validate_max_sierra_gas": 10000000000, "min_sierra_version_for_sierra_gas": "100.0.0", "vm_resource_fee_cost": { "builtins": { @@ -653,4 +653,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 c8bdb2e7db..f5873e51f3 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_1_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_1_1.json @@ -9,7 +9,6 @@ "max_contract_bytecode_size": 81920 }, "invoke_tx_max_n_steps": 4000000, - "execute_max_sierra_gas": 10000000000, "deprecated_l2_resource_gas_costs": { "gas_per_data_felt": [ 128, @@ -71,6 +70,8 @@ "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "transfer_entry_point_selector": "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e", "default_entry_point_selector": 0, + "validate_max_sierra_gas": 10000000000, + "execute_max_sierra_gas": 10000000000, "stored_block_hash_buffer": 10, "step_gas_cost": 100, "builtin_gas_costs": { @@ -599,7 +600,6 @@ } }, "validate_max_n_steps": 1000000, - "validate_max_sierra_gas": 10000000000, "min_sierra_version_for_sierra_gas": "100.0.0", "vm_resource_fee_cost": { "builtins": { @@ -653,4 +653,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 b9102fb596..7a5b29b4d9 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_2.json +++ b/crates/blockifier/resources/versioned_constants_0_13_2.json @@ -9,7 +9,6 @@ "max_contract_bytecode_size": 81920 }, "invoke_tx_max_n_steps": 10000000, - "execute_max_sierra_gas": 10000000000, "deprecated_l2_resource_gas_costs": { "gas_per_data_felt": [ 128, @@ -73,6 +72,7 @@ "error_entry_point_failed": "ENTRYPOINT_FAILED", "error_entry_point_not_found": "ENTRYPOINT_NOT_FOUND", "execute_entry_point_selector": "0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad", + "execute_max_sierra_gas": 10000000000, "fee_transfer_gas_cost": { "entry_point_initial_budget": 1, "step_gas_cost": 600 @@ -113,6 +113,7 @@ "validate_declare_entry_point_selector": "0x289da278a8dc833409cabfdad1581e8e7d40e42dcaed693fa4008dcdb4963b3", "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "validate_entry_point_selector": "0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775", + "validate_max_sierra_gas": 10000000000, "validate_rounding_consts": { "validate_block_number_rounding": 100, "validate_timestamp_rounding": 3600 @@ -605,7 +606,6 @@ } }, "validate_max_n_steps": 1000000, - "validate_max_sierra_gas": 10000000000, "min_sierra_version_for_sierra_gas": "100.0.0", "vm_resource_fee_cost": { "builtins": { @@ -659,4 +659,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 b083459e3b..0b492a399a 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_2_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_2_1.json @@ -9,7 +9,6 @@ "max_contract_bytecode_size": 81920 }, "invoke_tx_max_n_steps": 10000000, - "execute_max_sierra_gas": 10000000000, "deprecated_l2_resource_gas_costs": { "gas_per_data_felt": [ 128, @@ -73,6 +72,7 @@ "error_entry_point_failed": "ENTRYPOINT_FAILED", "error_entry_point_not_found": "ENTRYPOINT_NOT_FOUND", "execute_entry_point_selector": "0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad", + "execute_max_sierra_gas": 10000000000, "fee_transfer_gas_cost": { "entry_point_initial_budget": 1, "step_gas_cost": 600 @@ -113,6 +113,7 @@ "validate_declare_entry_point_selector": "0x289da278a8dc833409cabfdad1581e8e7d40e42dcaed693fa4008dcdb4963b3", "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "validate_entry_point_selector": "0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775", + "validate_max_sierra_gas": 10000000000, "validate_rounding_consts": { "validate_block_number_rounding": 100, "validate_timestamp_rounding": 3600 @@ -605,7 +606,6 @@ } }, "validate_max_n_steps": 1000000, - "validate_max_sierra_gas": 10000000000, "min_sierra_version_for_sierra_gas": "100.0.0", "vm_resource_fee_cost": { "builtins": { @@ -659,4 +659,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 b083459e3b..0b492a399a 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_3.json +++ b/crates/blockifier/resources/versioned_constants_0_13_3.json @@ -9,7 +9,6 @@ "max_contract_bytecode_size": 81920 }, "invoke_tx_max_n_steps": 10000000, - "execute_max_sierra_gas": 10000000000, "deprecated_l2_resource_gas_costs": { "gas_per_data_felt": [ 128, @@ -73,6 +72,7 @@ "error_entry_point_failed": "ENTRYPOINT_FAILED", "error_entry_point_not_found": "ENTRYPOINT_NOT_FOUND", "execute_entry_point_selector": "0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad", + "execute_max_sierra_gas": 10000000000, "fee_transfer_gas_cost": { "entry_point_initial_budget": 1, "step_gas_cost": 600 @@ -113,6 +113,7 @@ "validate_declare_entry_point_selector": "0x289da278a8dc833409cabfdad1581e8e7d40e42dcaed693fa4008dcdb4963b3", "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "validate_entry_point_selector": "0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775", + "validate_max_sierra_gas": 10000000000, "validate_rounding_consts": { "validate_block_number_rounding": 100, "validate_timestamp_rounding": 3600 @@ -605,7 +606,6 @@ } }, "validate_max_n_steps": 1000000, - "validate_max_sierra_gas": 10000000000, "min_sierra_version_for_sierra_gas": "100.0.0", "vm_resource_fee_cost": { "builtins": { @@ -659,4 +659,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 b000fd3e15..adb97a953f 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_4.json +++ b/crates/blockifier/resources/versioned_constants_0_13_4.json @@ -9,7 +9,6 @@ "max_contract_bytecode_size": 81920 }, "invoke_tx_max_n_steps": 10000000, - "execute_max_sierra_gas": 1000000000, "deprecated_l2_resource_gas_costs": { "gas_per_data_felt": [ 128, @@ -73,6 +72,7 @@ "error_entry_point_failed": "ENTRYPOINT_FAILED", "error_entry_point_not_found": "ENTRYPOINT_NOT_FOUND", "execute_entry_point_selector": "0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad", + "execute_max_sierra_gas": 1000000000, "default_initial_gas_cost": { "step_gas_cost": 100000000 }, @@ -111,6 +111,7 @@ "validate_declare_entry_point_selector": "0x289da278a8dc833409cabfdad1581e8e7d40e42dcaed693fa4008dcdb4963b3", "validate_deploy_entry_point_selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895", "validate_entry_point_selector": "0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775", + "validate_max_sierra_gas": 100000000, "validate_rounding_consts": { "validate_block_number_rounding": 100, "validate_timestamp_rounding": 3600 @@ -607,7 +608,6 @@ } }, "validate_max_n_steps": 1000000, - "validate_max_sierra_gas": 100000000, "min_sierra_version_for_sierra_gas": "1.6.0", "vm_resource_fee_cost": { "builtins": { @@ -661,4 +661,4 @@ 10000 ] } -} \ No newline at end of file +} diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 4261c41066..469a8faf87 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -377,8 +377,9 @@ impl AccountTransaction { if self.execution_flags.validate { let limit_steps_by_resources = self.execution_flags.charge_fee; // TODO(Aner): cap the gas for validation. - let remaining_validation_gas = &mut remaining_gas - .limit_usage(tx_context.block_context.versioned_constants.validate_max_sierra_gas); + let remaining_validation_gas = &mut remaining_gas.limit_usage( + tx_context.block_context.versioned_constants.os_constants.validate_max_sierra_gas, + ); Ok(self .validate_tx(state, tx_context, remaining_validation_gas, limit_steps_by_resources)? .inspect(|call_info| { @@ -539,7 +540,7 @@ impl AccountTransaction { let execute_call_info: Option; if matches!(&self.tx, Transaction::DeployAccount(_)) { // Handle `DeployAccount` transactions separately, due to different order of things. - // Also, the execution context required form the `DeployAccount` execute phase is + // Also, the execution context required for the `DeployAccount` execute phase is // validation context. let mut execution_context = EntryPointExecutionContext::new_validate( tx_context.clone(), diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index 8e24a3a2f4..e09f3948a7 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -1718,7 +1718,10 @@ fn test_initial_gas( let validate_call_info = &transaction_ex_info.validate_call_info.unwrap(); let validate_initial_gas = validate_call_info.call.initial_gas; - assert_eq!(validate_initial_gas, block_context.versioned_constants.validate_max_sierra_gas.0); + assert_eq!( + validate_initial_gas, + block_context.versioned_constants.os_constants.validate_max_sierra_gas.0 + ); let validate_gas_consumed = validate_call_info.execution.gas_consumed; assert!(validate_gas_consumed > 0, "New Cairo1 contract should consume gas."); @@ -1728,6 +1731,7 @@ fn test_initial_gas( // minus the gas consumed by validate. Need to add 1 as the check is strictly less than. let mut prev_initial_gas = block_context .versioned_constants + .os_constants .execute_max_sierra_gas .min(user_gas_bound - GasAmount(validate_gas_consumed) + GasAmount(1)) .0; diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 4675cb4cb7..bd78fb3214 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -266,7 +266,7 @@ fn expected_validate_call_info( TrackedResource::SierraGas => { user_initial_gas .unwrap_or(initial_gas_amount_from_block_context(None)) - .min(VERSIONED_CONSTANTS.validate_max_sierra_gas) + .min(VERSIONED_CONSTANTS.os_constants.validate_max_sierra_gas) .0 } }, @@ -533,7 +533,7 @@ fn test_invoke_tx( sender_address, account_cairo_version, tracked_resource, - Some(initial_gas.min(versioned_constants.validate_max_sierra_gas)), + Some(initial_gas.min(versioned_constants.os_constants.validate_max_sierra_gas)), ); // Build expected execute call info. @@ -541,6 +541,7 @@ fn test_invoke_tx( let expected_validated_call = expected_validate_call_info.as_ref().unwrap().call.clone(); let expected_initial_execution_gas = versioned_constants + .os_constants .execute_max_sierra_gas .min(initial_gas - GasAmount(expected_arguments.validate_gas_consumed)) .0; @@ -1834,7 +1835,7 @@ fn test_deploy_account_tx( let expected_execute_initial_gas = user_initial_gas // Note that in the case of deploy account, the initial gas in "execute" is limited by // max_validation_sierra_gas. - .min(versioned_constants.validate_max_sierra_gas); + .min(versioned_constants.os_constants.validate_max_sierra_gas); let expected_execute_call_info = Some(CallInfo { call: CallEntryPoint { class_hash: Some(account_class_hash), @@ -2354,7 +2355,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) { storage_address: contract_address, caller_address: ContractAddress::default(), call_type: CallType::Call, - initial_gas: block_context.versioned_constants.execute_max_sierra_gas.0, + initial_gas: block_context.versioned_constants.os_constants.execute_max_sierra_gas.0, }, execution: CallExecution { retdata: Retdata(vec![value]), @@ -2633,7 +2634,7 @@ fn test_balance_print() { resource_bounds: create_gas_amount_bounds_with_default_price( GasVector{ l1_gas: GasAmount(1652), - l2_gas: versioned_constants.validate_max_sierra_gas + GasAmount(1234567), + l2_gas: versioned_constants.os_constants.validate_max_sierra_gas + GasAmount(1234567), l1_data_gas: GasAmount(0), } ), @@ -2691,7 +2692,7 @@ fn test_invoke_max_sierra_gas_validate_execute( let expected_validate_initial_gas = match account_tracked_resource { TrackedResource::CairoSteps => VERSIONED_CONSTANTS.infinite_gas_for_vm_mode(), TrackedResource::SierraGas => { - versioned_constants.validate_max_sierra_gas.min(user_initial_gas).0 + versioned_constants.os_constants.validate_max_sierra_gas.min(user_initial_gas).0 } }; @@ -2703,6 +2704,7 @@ fn test_invoke_max_sierra_gas_validate_execute( TrackedResource::CairoSteps => VERSIONED_CONSTANTS.infinite_gas_for_vm_mode(), TrackedResource::SierraGas => { versioned_constants + .os_constants .execute_max_sierra_gas .min( user_initial_gas @@ -2751,7 +2753,7 @@ fn test_invoke_max_sierra_gas_validate_execute( resource_bounds: create_gas_amount_bounds_with_default_price( GasVector{ l1_gas: GasAmount(2203), - l2_gas: versioned_constants.validate_max_sierra_gas + GasAmount(1234567), + l2_gas: versioned_constants.os_constants.validate_max_sierra_gas + GasAmount(1234567), l1_data_gas: GasAmount(0), } ), @@ -2809,7 +2811,7 @@ fn test_deploy_max_sierra_gas_validate_execute( let actual_execute_initial_gas = actual_execution_info.execute_call_info.as_ref().unwrap().call.initial_gas; let expected_execute_initial_gas = - versioned_constants.validate_max_sierra_gas.min(user_initial_gas).0; + versioned_constants.os_constants.validate_max_sierra_gas.min(user_initial_gas).0; assert_eq!(actual_execute_initial_gas, expected_execute_initial_gas); let actual_validate_initial_gas = @@ -2818,6 +2820,7 @@ fn test_deploy_max_sierra_gas_validate_execute( TrackedResource::CairoSteps => VERSIONED_CONSTANTS.infinite_gas_for_vm_mode(), TrackedResource::SierraGas => { versioned_constants + .os_constants .validate_max_sierra_gas .min( user_initial_gas diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index c5ab944d72..e08bbce507 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -180,12 +180,10 @@ pub struct VersionedConstants { // Limits. pub tx_event_limits: EventLimits, pub invoke_tx_max_n_steps: u32, - pub execute_max_sierra_gas: GasAmount, pub deprecated_l2_resource_gas_costs: ArchivalDataGasCosts, pub archival_data_gas_costs: ArchivalDataGasCosts, pub max_recursion_depth: usize, pub validate_max_n_steps: u32, - pub validate_max_sierra_gas: GasAmount, pub min_sierra_version_for_sierra_gas: SierraVersion, // BACKWARD COMPATIBILITY: If true, the segment_arena builtin instance counter will be // multiplied by 3. This offsets a bug in the old vm where the counter counted the number of @@ -253,15 +251,18 @@ impl VersionedConstants { /// Default initial gas amount when L2 gas is not provided. pub fn initial_gas_no_user_l2_bound(&self) -> GasAmount { - (self.execute_max_sierra_gas.checked_add(self.validate_max_sierra_gas)) - .expect("The default initial gas cost should be less than the maximum gas amount.") + (self + .os_constants + .execute_max_sierra_gas + .checked_add(self.os_constants.validate_max_sierra_gas)) + .expect("The default initial gas cost should be less than the maximum gas amount.") } /// Returns the maximum gas amount according to the given mode. pub fn sierra_gas_limit(&self, mode: &ExecutionMode) -> GasAmount { match mode { - ExecutionMode::Validate => self.validate_max_sierra_gas, - ExecutionMode::Execute => self.execute_max_sierra_gas, + ExecutionMode::Validate => self.os_constants.validate_max_sierra_gas, + ExecutionMode::Execute => self.os_constants.execute_max_sierra_gas, } } @@ -728,13 +729,15 @@ pub struct OsConstants { pub gas_costs: GasCosts, pub validate_rounding_consts: ValidateRoundingConsts, pub os_contract_addresses: OsContractAddresses, + pub validate_max_sierra_gas: GasAmount, + pub execute_max_sierra_gas: GasAmount, } impl OsConstants { // List of os constants to be ignored // during the creation of the struct containing the base gas costs. - const ADDITIONAL_FIELDS: [&'static str; 30] = [ + const ADDITIONAL_FIELDS: [&'static str; 32] = [ "builtin_gas_costs", "constructor_entry_point_selector", "default_entry_point_selector", @@ -748,6 +751,7 @@ impl OsConstants { "error_entry_point_not_found", "error_out_of_gas", "execute_entry_point_selector", + "execute_max_sierra_gas", "l1_gas", "l1_gas_index", "l1_handler_version", @@ -763,6 +767,7 @@ impl OsConstants { "validate_declare_entry_point_selector", "validate_deploy_entry_point_selector", "validate_entry_point_selector", + "validate_max_sierra_gas", "validate_rounding_consts", "validated", ]; @@ -790,8 +795,29 @@ impl TryFrom for OsConstants { let gas_costs = GasCosts::try_from(&raw_json_data)?; let validate_rounding_consts = raw_json_data.validate_rounding_consts; let os_contract_addresses = raw_json_data.os_contract_addresses; - let os_constants = - OsConstants { gas_costs, validate_rounding_consts, os_contract_addresses }; + let key = "validate_max_sierra_gas"; + let validate_max_sierra_gas = GasAmount(serde_json::from_value( + raw_json_data + .raw_json_file_as_dict + .get(key) + .ok_or_else(|| OsConstantsSerdeError::KeyNotFoundInFile(key.to_string()))? + .clone(), + )?); + let key = "execute_max_sierra_gas"; + let execute_max_sierra_gas = GasAmount(serde_json::from_value( + raw_json_data + .raw_json_file_as_dict + .get(key) + .ok_or_else(|| OsConstantsSerdeError::KeyNotFoundInFile(key.to_string()))? + .clone(), + )?); + let os_constants = OsConstants { + gas_costs, + validate_rounding_consts, + os_contract_addresses, + validate_max_sierra_gas, + execute_max_sierra_gas, + }; Ok(os_constants) } } @@ -856,8 +882,13 @@ impl OsConstantsRawJson { builtins: &BuiltinGasCosts, ) -> Result, OsConstantsSerdeError> { let mut gas_costs = IndexMap::new(); + let key = "syscall_gas_costs"; let syscalls: IndexMap = serde_json::from_value( - (self.raw_json_file_as_dict.get("syscall_gas_costs").unwrap()).clone(), + (self + .raw_json_file_as_dict + .get(key) + .ok_or_else(|| OsConstantsSerdeError::KeyNotFoundInFile(key.to_string()))?) + .clone(), )?; for (key, value) in syscalls { self.add_to_syscalls(&key, &value, &mut gas_costs, base, builtins)?; @@ -867,8 +898,13 @@ impl OsConstantsRawJson { fn parse_builtin(&self) -> Result, OsConstantsSerdeError> { let mut gas_costs = IndexMap::new(); + let key = "builtin_gas_costs"; let builtins: IndexMap = serde_json::from_value( - (self.raw_json_file_as_dict.get("builtin_gas_costs").unwrap()).clone(), + (self + .raw_json_file_as_dict + .get(key) + .ok_or_else(|| OsConstantsSerdeError::KeyNotFoundInFile(key.to_string()))?) + .clone(), )?; for (key, value) in builtins { match value { @@ -1023,6 +1059,8 @@ pub enum OsConstantsSerdeError { InvalidFactorFormat(Value), #[error("Unknown key '{inner_key}' used to create value for '{key}'")] KeyNotFound { key: String, inner_key: String }, + #[error("Key'{0}' is not found")] + KeyNotFoundInFile(String), #[error("Value {value} for key '{key}' is out of range and cannot be cast into u64")] OutOfRange { key: String, value: Number }, #[error(