Skip to content

Commit

Permalink
feat(blockifier): explicit names for versioned constants JSONs
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <[email protected]>
  • Loading branch information
dorimedini-starkware committed Sep 26, 2024
1 parent 1242d9d commit a225951
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl GasCosts {
pub fn create_for_testing_from_subset(subset_of_os_constants: &str) -> Self {
let subset_of_os_constants: Value = serde_json::from_str(subset_of_os_constants).unwrap();
let mut os_constants: Value =
serde_json::from_str::<Value>(VERSIONED_CONSTANTS_LATEST_JSON)
serde_json::from_str::<Value>(VERSIONED_CONSTANTS_LATEST_JSON.as_str())
.unwrap()
.get("os_constants")
.unwrap()
Expand Down
50 changes: 35 additions & 15 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};
use std::io;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock};
use std::{fs, io};

use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
Expand Down Expand Up @@ -31,13 +31,21 @@ pub mod test;

/// Auto-generate getters for listed versioned constants versions.
macro_rules! define_versioned_constants {
($(($variant:ident, $path_to_json:expr)),* $(,)?) => {
($(($variant:ident, $path_to_json:expr)),*, $latest_variant:ident) => {
/// Enum of all the Starknet versions supporting versioned constants.
#[derive(Clone, Debug, EnumCount, EnumIter, Hash, Eq, PartialEq)]
pub enum StarknetVersion {
$($variant,)*
}

impl StarknetVersion {
pub fn path_to_versioned_constants_json(&self) -> &'static str {
match self {
$(StarknetVersion::$variant => $path_to_json,)*
}
}
}

// Static (lazy) instances of the versioned constants.
// For internal use only; for access to a static instance use the `StarknetVersion` enum.
paste! {
Expand All @@ -63,16 +71,34 @@ macro_rules! define_versioned_constants {
}
}
}

impl VersionedConstants {
/// Get the constants that shipped with the current version of the Blockifier.
/// To use custom constants, initialize the struct from a file using `try_from`.
pub fn latest_constants() -> &'static Self {
Self::get(StarknetVersion::$latest_variant)
}
}

pub static VERSIONED_CONSTANTS_LATEST_JSON: LazyLock<String> = LazyLock::new(|| {
let latest_variant = StarknetVersion::$latest_variant;
let path_to_json: PathBuf = [
env!("CARGO_MANIFEST_DIR"), "src", latest_variant.path_to_versioned_constants_json()
].iter().collect();
fs::read_to_string(path_to_json.clone())
.expect(&format!("Failed to read file {}.", path_to_json.display()))
});
};
}

define_versioned_constants! {
(V0_13_0, "../resources/versioned_constants_13_0.json"),
(V0_13_1, "../resources/versioned_constants_13_1.json"),
(V0_13_1_1, "../resources/versioned_constants_13_1_1.json"),
(V0_13_2, "../resources/versioned_constants_13_2.json"),
(V0_13_2_1, "../resources/versioned_constants_13_2_1.json"),
(Latest, "../resources/versioned_constants.json"),
(V0_13_0, "../resources/versioned_constants_0_13_0.json"),
(V0_13_1, "../resources/versioned_constants_0_13_1.json"),
(V0_13_1_1, "../resources/versioned_constants_0_13_1_1.json"),
(V0_13_2, "../resources/versioned_constants_0_13_2.json"),
(V0_13_2_1, "../resources/versioned_constants_0_13_2_1.json"),
(V0_13_3, "../resources/versioned_constants_0_13_3.json"),
V0_13_3
}

pub type ResourceCost = Ratio<u128>;
Expand Down Expand Up @@ -150,12 +176,6 @@ impl VersionedConstants {
version.into()
}

/// Get the constants that shipped with the current version of the Blockifier.
/// To use custom constants, initialize the struct from a file using `try_from`.
pub fn latest_constants() -> &'static Self {
Self::get(StarknetVersion::Latest)
}

/// Converts from L1 gas price to L2 gas price with **upward rounding**.
pub fn convert_l1_to_l2_gas_price_round_up(&self, l1_gas_price: u128) -> u128 {
*(self.l1_to_l2_gas_price_ratio() * l1_gas_price).ceil().numer()
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn test_successful_gas_costs_parsing() {
/// Assert versioned constants overrides are used when provided.
#[test]
fn test_versioned_constants_overrides() {
let versioned_constants = VERSIONED_CONSTANTS_LATEST.clone();
let versioned_constants = VersionedConstants::latest_constants().clone();
let updated_invoke_tx_max_n_steps = versioned_constants.invoke_tx_max_n_steps + 1;
let updated_validate_max_n_steps = versioned_constants.validate_max_n_steps + 1;
let updated_max_recursion_depth = versioned_constants.max_recursion_depth + 1;
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ fn get_versioned_constants(
} else if version == STARKNET_VERSION_O_13_2 {
BlockifierStarknetVersion::V0_13_2
} else {
BlockifierStarknetVersion::Latest
BlockifierStarknetVersion::V0_13_3
};
VersionedConstants::get(blockifier_starknet_version)
}
Expand Down

0 comments on commit a225951

Please sign in to comment.