diff --git a/crates/blockifier/resources/versioned_constants.json b/crates/blockifier/resources/versioned_constants.json index 0ec0570725..33caf67c3d 100644 --- a/crates/blockifier/resources/versioned_constants.json +++ b/crates/blockifier/resources/versioned_constants.json @@ -1,4 +1,10 @@ { + "gateway": { + "max_calldata_length": 4000, + "max_contract_bytecode_size": 61440 + }, + "invoke_tx_max_n_steps": 3000000, + "max_recursion_depth": 50, "os_resources": { "execute_syscalls": { "CallContract": { @@ -249,7 +255,7 @@ } } }, - "starknet_os_constants": { + "os_constants": { "nop_entry_point_offset": -1, "entry_point_type_external": 0, "entry_point_type_l1_handler": 1, @@ -393,12 +399,6 @@ "l1_gas_index": 0, "l2_gas_index": 1 }, - "gateway": { - "max_calldata_length": 4000, - "max_contract_bytecode_size": 61440 - }, - "invoke_tx_max_n_steps": 3000000, - "max_recursion_depth": 50, "validate_max_n_steps": 1000000, "vm_resource_fee_cost": { "bitwise_builtin": 0.32, diff --git a/crates/blockifier/src/execution/entry_point.rs b/crates/blockifier/src/execution/entry_point.rs index 90dd6e5b8f..27fd4b2d4d 100644 --- a/crates/blockifier/src/execution/entry_point.rs +++ b/crates/blockifier/src/execution/entry_point.rs @@ -286,7 +286,7 @@ impl EntryPointExecutionContext { .map(|call_info| call_info.vm_resources.n_steps) .unwrap_or_default(); - let overhead_steps = self.versioned_constants().resources_for_tx_type(tx_type).n_steps; + let overhead_steps = self.versioned_constants().os_resources_for_tx_type(tx_type).n_steps; self.subtract_steps(validate_steps + overhead_steps) } diff --git a/crates/blockifier/src/fee/gas_usage.rs b/crates/blockifier/src/fee/gas_usage.rs index dcfd6750cc..cba5dda244 100644 --- a/crates/blockifier/src/fee/gas_usage.rs +++ b/crates/blockifier/src/fee/gas_usage.rs @@ -194,7 +194,7 @@ pub fn estimate_minimal_l1_gas( tx: &AccountTransaction, ) -> TransactionPreValidationResult { // TODO(Dori, 1/8/2023): Give names to the constant VM step estimates and regression-test them. - let os_steps_for_type = versioned_constants.resources_for_tx_type(&tx.tx_type()).n_steps; + let os_steps_for_type = versioned_constants.os_resources_for_tx_type(&tx.tx_type()).n_steps; let gas_cost: usize = match tx { // We consider the following state changes: sender balance update (storage update) + nonce // increment (contract modification) (we exclude the sequencer balance update and the ERC20 diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index bef370f1c1..a49451b9a4 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -49,7 +49,7 @@ pub struct VersionedConstants { // Cairo OS constants. // Note: if loaded from a json file, there are some assumptions made on its structure. // See the struct's docstring for more details. - starknet_os_constants: Arc, + os_constants: Arc, } impl VersionedConstants { @@ -61,7 +61,7 @@ impl VersionedConstants { /// Returns the initial gas of any transaction to run with. pub fn tx_initial_gas(&self) -> u64 { - let os_consts = &self.starknet_os_constants; + let os_consts = &self.os_constants; os_consts.gas_costs["initial_gas_cost"] - os_consts.gas_costs["transaction_gas_cost"] } @@ -71,26 +71,26 @@ impl VersionedConstants { } pub fn gas_cost(&self, name: &str) -> u64 { - match self.starknet_os_constants.gas_costs.get(name) { + match self.os_constants.gas_costs.get(name) { Some(&cost) => cost, - None if StarknetOSConstants::ALLOWED_GAS_COST_NAMES.contains(&name) => { + None if OSConstants::ALLOWED_GAS_COST_NAMES.contains(&name) => { panic!( - "{} is present in `StarknetOSConstants::GAS_COSTS` but not in `self`; was \ - validation skipped?", + "{} is present in `OSConstants::GAS_COSTS` but not in `self`; was validation \ + skipped?", name ) } None => { panic!( - "Only gas costs listed in `StarknetOsConstants::GAS_COSTS` should be \ - requested, got: {}", + "Only gas costs listed in `OsConstants::GAS_COSTS` should be requested, got: \ + {}", name ) } } } - pub fn resources_for_tx_type(&self, tx_type: &TransactionType) -> &VmExecutionResources { + pub fn os_resources_for_tx_type(&self, tx_type: &TransactionType) -> &VmExecutionResources { self.os_resources.resources_for_tx_type(tx_type) } @@ -130,7 +130,7 @@ impl TryFrom<&Path> for VersionedConstants { } } -#[derive(Debug, Deserialize, Clone, Default)] +#[derive(Clone, Debug, Default, Deserialize)] // Serde trick for adding validations via a customr deserializer, without forgoing the derive. // See: https://github.com/serde-rs/serde/issues/1220. #[serde(remote = "Self")] @@ -217,7 +217,6 @@ impl<'de> Deserialize<'de> for OsResources { ]); let all_resources = os_resources.execute_syscalls.values().chain(os_resources.execute_txs_inner.values()); - for resources in all_resources { for builtin_name in resources.builtin_instance_counter.keys() { if !known_builtin_names.contains(builtin_name.as_str()) { @@ -233,7 +232,7 @@ impl<'de> Deserialize<'de> for OsResources { } // Below, serde first deserializes the json into a regular IndexMap wrapped by the newtype -// `StarknetOSConstantsRawJSON`, then calls the `try_from` of the newtype, which handles the +// `OSConstantsRawJSON`, then calls the `try_from` of the newtype, which handles the // conversion into actual values. // Assumption: if the json has a value that contains the expression "FOO * 2", then the key `FOO` // must appear before this value in the JSON. @@ -245,12 +244,12 @@ impl<'de> Deserialize<'de> for OsResources { // TODO: consider encoding the * and + operations inside the json file, instead of hardcoded below // in the `try_from`. #[derive(Debug, Clone, Default, Serialize, Deserialize)] -#[serde(try_from = "StarknetOSConstantsRawJSON")] -pub struct StarknetOSConstants { +#[serde(try_from = "OSConstantsRawJSON")] +pub struct OSConstants { gas_costs: IndexMap, } -impl StarknetOSConstants { +impl OSConstants { // See documentation in core/os/constants.cairo. const ALLOWED_GAS_COST_NAMES: [&'static str; 31] = [ "step_gas_cost", @@ -309,10 +308,10 @@ impl StarknetOSConstants { } } -impl TryFrom for StarknetOSConstants { +impl TryFrom for OSConstants { type Error = StarknetConstantsSerdeError; - fn try_from(intermediate: StarknetOSConstantsRawJSON) -> Result { + fn try_from(intermediate: OSConstantsRawJSON) -> Result { let mut gas_costs = IndexMap::new(); let gas_cost_whitelist: IndexSet<_> = @@ -356,7 +355,7 @@ impl TryFrom for StarknetOSConstants { } } - let os_constants = StarknetOSConstants { gas_costs }; + let os_constants = OSConstants { gas_costs }; // Skip validation in testing: to test validation run validate manually. #[cfg(not(any(feature = "testing", test)))] @@ -369,7 +368,7 @@ impl TryFrom for StarknetOSConstants { // Intermediate representation of the JSON file in order to make the deserialization easier, using a // regular the try_from. #[derive(Debug, Deserialize)] -struct StarknetOSConstantsRawJSON { +struct OSConstantsRawJSON { #[serde(flatten)] regular: IndexMap, } diff --git a/crates/blockifier/src/versioned_constants_test.rs b/crates/blockifier/src/versioned_constants_test.rs index fde2322982..e104c8bb76 100644 --- a/crates/blockifier/src/versioned_constants_test.rs +++ b/crates/blockifier/src/versioned_constants_test.rs @@ -19,8 +19,9 @@ fn test_successful_parsing() { "ignore the gas string": "GAS!", "I look like a gas cost but my name is all wrong": 0 }"#; - let starknet_os_constants: Arc = serde_json::from_str(json_data).unwrap(); - let versioned_constants = VersionedConstants { starknet_os_constants, ..Default::default() }; + let os_constants: Arc = serde_json::from_str(json_data).unwrap(); + let versioned_constants = + VersionedConstants { os_constants, ..Default::default() }; assert_eq!(versioned_constants.gas_cost("step_gas_cost"), 2); assert_eq!(versioned_constants.gas_cost("entry_point_initial_budget"), 6); // step_gas_cost * 3. @@ -29,7 +30,7 @@ fn test_successful_parsing() { assert_eq!(versioned_constants.gas_cost("entry_point_gas_cost"), 34); // Only the 3 values asserted against should be present, the rest are ignored. - assert_eq!(versioned_constants.starknet_os_constants.gas_costs.len(), 3); + assert_eq!(versioned_constants.os_constants.gas_costs.len(), 3); } #[test] @@ -50,7 +51,7 @@ fn test_string_inside_composed_field() { } fn check_constants_serde_error(json_data: &str, expected_error_message: &str) { - let error = serde_json::from_str::(json_data).unwrap_err(); + let error = serde_json::from_str::(json_data).unwrap_err(); assert_eq!(error.to_string(), expected_error_message); }