Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
feat: backwards compatibility for calldata_factor
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Gilad Chase committed Feb 18, 2024
1 parent 9c0c99a commit 26ed90c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
38 changes: 37 additions & 1 deletion crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, Value>,
}

impl TryFrom<ResourceParamsRaw> for ResourcesParams {
type Error = VersionedConstantsError;

fn try_from(mut json_data: ResourceParamsRaw) -> Result<Self, Self::Error> {
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.
Expand Down
27 changes: 26 additions & 1 deletion crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down

0 comments on commit 26ed90c

Please sign in to comment.