Skip to content

Commit

Permalink
chore(blockifier, native_blockifier): updated versioned constants (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware authored Aug 26, 2024
1 parent ddc8f51 commit 29f90d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 61 deletions.
29 changes: 9 additions & 20 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,32 +224,21 @@ impl VersionedConstants {
Self { validate_max_n_steps, max_recursion_depth, ..Self::latest_constants().clone() }
}

// TODO(Amos, 1/8/2024): Remove the explicit `validate_max_n_steps` & `max_recursion_depth`,
// they should be part of the general override.
/// `versioned_constants_base_overrides` are used if they are provided, otherwise the latest
/// versioned constants are used. `validate_max_n_steps` & `max_recursion_depth` override both.
/// Returns the latest versioned constants after applying the given overrides.
pub fn get_versioned_constants(
versioned_constants_overrides: VersionedConstantsOverrides,
) -> Self {
let VersionedConstantsOverrides {
validate_max_n_steps,
max_recursion_depth,
versioned_constants_base_overrides,
invoke_tx_max_n_steps,
} = versioned_constants_overrides;
let base_overrides = match versioned_constants_base_overrides {
Some(versioned_constants_base_overrides) => {
log::debug!(
"Using provided `versioned_constants_base_overrides` (with additional \
overrides)."
);
versioned_constants_base_overrides
}
None => {
log::debug!("Using latest versioned constants (with additional overrides).");
Self::latest_constants().clone()
}
};
Self { validate_max_n_steps, max_recursion_depth, ..base_overrides }
Self {
validate_max_n_steps,
max_recursion_depth,
invoke_tx_max_n_steps,
..Self::latest_constants().clone()
}
}
}

Expand Down Expand Up @@ -746,5 +735,5 @@ pub struct ResourcesByVersion {
pub struct VersionedConstantsOverrides {
pub validate_max_n_steps: u32,
pub max_recursion_depth: usize,
pub versioned_constants_base_overrides: Option<VersionedConstants>,
pub invoke_tx_max_n_steps: u32,
}
27 changes: 14 additions & 13 deletions crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,25 @@ fn get_json_value_without_defaults() -> serde_json::Value {
json_value_without_defaults
}

/// Assert `versioned_constants_base_overrides` are used when provided.
/// Assert versioned constants overrides are used when provided.
#[test]
fn test_versioned_constants_base_overrides() {
// Create a versioned constants copy with a modified value for `invoke_tx_max_n_steps`.
let mut versioned_constants_base_overrides = VERSIONED_CONSTANTS_LATEST.clone();
versioned_constants_base_overrides.invoke_tx_max_n_steps += 1;
fn test_versioned_constants_overrides() {
let versioned_constants = VERSIONED_CONSTANTS_LATEST.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;

// Create a versioned constants copy with overriden values.
let result = VersionedConstants::get_versioned_constants(VersionedConstantsOverrides {
validate_max_n_steps: versioned_constants_base_overrides.validate_max_n_steps,
max_recursion_depth: versioned_constants_base_overrides.max_recursion_depth,
versioned_constants_base_overrides: Some(versioned_constants_base_overrides.clone()),
validate_max_n_steps: updated_validate_max_n_steps,
max_recursion_depth: updated_max_recursion_depth,
invoke_tx_max_n_steps: updated_invoke_tx_max_n_steps,
});

// Assert the new value is used.
assert_eq!(
result.invoke_tx_max_n_steps,
versioned_constants_base_overrides.invoke_tx_max_n_steps
);
// Assert the new values are used.
assert_eq!(result.invoke_tx_max_n_steps, updated_invoke_tx_max_n_steps);
assert_eq!(result.validate_max_n_steps, updated_validate_max_n_steps);
assert_eq!(result.max_recursion_depth, updated_max_recursion_depth);
}

#[test]
Expand Down
35 changes: 7 additions & 28 deletions crates/native_blockifier/src/py_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::collections::HashMap;
use blockifier::abi::constants;
use blockifier::blockifier::config::ConcurrencyConfig;
use blockifier::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount, HashMapWrapper};
use blockifier::versioned_constants::{VersionedConstants, VersionedConstantsOverrides};
use blockifier::versioned_constants::VersionedConstantsOverrides;
use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

use crate::errors::{
Expand Down Expand Up @@ -50,30 +49,19 @@ impl From<ExecutionResources> for PyExecutionResources {
pub struct PyVersionedConstantsOverrides {
pub validate_max_n_steps: u32,
pub max_recursion_depth: usize,
pub versioned_constants_base_overrides: Option<String>,
pub invoke_tx_max_n_steps: u32,
}

#[pymethods]
impl PyVersionedConstantsOverrides {
#[new]
#[pyo3(signature = (validate_max_n_steps, max_recursion_depth, versioned_constants_base_overrides))]
#[pyo3(signature = (validate_max_n_steps, max_recursion_depth, invoke_tx_max_n_steps))]
pub fn create(
validate_max_n_steps: u32,
max_recursion_depth: usize,
versioned_constants_base_overrides: Option<String>,
invoke_tx_max_n_steps: u32,
) -> Self {
Self { validate_max_n_steps, max_recursion_depth, versioned_constants_base_overrides }
}

#[staticmethod]
pub fn assert_versioned_consts_load_successfully(
versioned_constants_str: &str,
) -> PyResult<()> {
if serde_json::from_str::<VersionedConstants>(versioned_constants_str).is_ok() {
Ok(())
} else {
Err(PyValueError::new_err("Failed to parse `versioned_constants_str`."))
}
Self { validate_max_n_steps, max_recursion_depth, invoke_tx_max_n_steps }
}
}

Expand All @@ -82,18 +70,9 @@ impl From<PyVersionedConstantsOverrides> for VersionedConstantsOverrides {
let PyVersionedConstantsOverrides {
validate_max_n_steps,
max_recursion_depth,
versioned_constants_base_overrides,
invoke_tx_max_n_steps,
} = py_versioned_constants_overrides;
let base_overrides =
versioned_constants_base_overrides.map(|versioned_constants_base_overrides| {
serde_json::from_str(&versioned_constants_base_overrides)
.expect("Versioned constants JSON file is malformed.")
});
Self {
validate_max_n_steps,
max_recursion_depth,
versioned_constants_base_overrides: base_overrides,
}
Self { validate_max_n_steps, max_recursion_depth, invoke_tx_max_n_steps }
}
}

Expand Down

0 comments on commit 29f90d3

Please sign in to comment.