From 26ed90c1420c8c9ae524173f292445410cbd66b6 Mon Sep 17 00:00:00 2001 From: Gilad Chase Date: Sun, 18 Feb 2024 09:54:29 +0200 Subject: [PATCH] feat: backwards compatibility for calldata_factor If os resources' inner tx is missing `constant` and `calldata_factor` keys, then: - assume it is a flat `ExecutionResources` instance, and put its contents inside a `constant` key. - initialize calldata_factor as default (all zeros). --- crates/blockifier/src/versioned_constants.rs | 38 ++++++++++++++++++- .../src/versioned_constants_test.rs | 27 ++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index d5db6c8b3b..c447697e5f 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -9,7 +9,7 @@ use indexmap::{IndexMap, IndexSet}; use once_cell::sync::Lazy; use serde::de::Error as DeserializationError; use serde::{Deserialize, Deserializer, Serialize}; -use serde_json::{Number, Value}; +use serde_json::{Map, Number, Value}; use strum::IntoEnumIterator; use thiserror::Error; @@ -490,11 +490,47 @@ pub enum OsConstantsSerdeError { } #[derive(Clone, Debug, Deserialize)] +#[serde(try_from = "ResourceParamsRaw")] pub struct ResourcesParams { pub constant: ExecutionResources, pub calldata_factor: ExecutionResources, } +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +struct ResourceParamsRaw { + #[serde(flatten)] + raw_resource_params_as_dict: Map, +} + +impl TryFrom for ResourcesParams { + type Error = VersionedConstantsError; + + fn try_from(mut json_data: ResourceParamsRaw) -> Result { + let constant_value = json_data.raw_resource_params_as_dict.remove("constant"); + let calldata_factor_value = json_data.raw_resource_params_as_dict.remove("calldata_factor"); + + let (constant, calldata_factor) = match (constant_value, calldata_factor_value) { + (Some(constant), Some(calldata_factor)) => (constant, calldata_factor), + (Some(_), None) => { + return Err(serde_json::Error::custom( + "Malformed JSON: If `constant` is present, then so should `calldata_factor`", + ))?; + } + (None, _) => { + // If `constant` is not found, use the entire map for `constant` and default + // `calldata_factor` + let entire_value = std::mem::take(&mut json_data.raw_resource_params_as_dict); + (Value::Object(entire_value), serde_json::to_value(ExecutionResources::default())?) + } + }; + + Ok(Self { + constant: serde_json::from_value(constant)?, + calldata_factor: serde_json::from_value(calldata_factor)?, + }) + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] struct ValidateRoundingConsts { // Flooring factor for block number in validate mode. diff --git a/crates/blockifier/src/versioned_constants_test.rs b/crates/blockifier/src/versioned_constants_test.rs index 62cdedc108..681ce919ba 100644 --- a/crates/blockifier/src/versioned_constants_test.rs +++ b/crates/blockifier/src/versioned_constants_test.rs @@ -43,7 +43,16 @@ fn test_default_values() { "os_constants": {}, "os_resources": { "execute_syscalls":{}, - "execute_txs_inner": {} + "execute_txs_inner": { + "Declare": { + "builtin_instance_counter": { + "pedersen_builtin": 16, + "range_check_builtin": 63 + }, + "n_memory_holes": 0, + "n_steps": 2839 + } + } }, "vm_resource_fee_cost": {}, "max_recursion_depth": 2 @@ -55,6 +64,22 @@ fn test_default_values() { assert_eq!(versioned_constants.event_size_limit, EventSizeLimit::max()); assert_eq!(versioned_constants.l2_resource_gas_costs, L2ResourceGasCosts::default()); + + // Calldata factor was initialized as 0, and did not affect the expected result, even if + // calldata length is nonzero. + let calldata_length = 2; + let expected_declare_resources = ExecutionResources { + n_steps: 2839, + builtin_instance_counter: HashMap::from([ + ("pedersen_builtin".to_string(), 16), + ("range_check_builtin".to_string(), 63), + ]), + ..Default::default() + }; + assert_eq!( + versioned_constants.os_resources_for_tx_type(&TransactionType::Declare, calldata_length), + expected_declare_resources + ); } #[test]