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

Commit

Permalink
feat: added private versioned constants (#1992)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware authored Jun 23, 2024
1 parent 35f5081 commit 0e13d53
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 15 deletions.
34 changes: 34 additions & 0 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,34 @@ impl VersionedConstants {
) -> Self {
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.
pub fn get_versioned_constants(
versioned_constants_overrides: VersionedConstantsOverrides,
) -> Self {
let VersionedConstantsOverrides {
validate_max_n_steps,
max_recursion_depth,
versioned_constants_base_overrides,
} = 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 }
}
}

impl TryFrom<&Path> for VersionedConstants {
Expand Down Expand Up @@ -690,3 +718,9 @@ pub struct ResourcesByVersion {
pub resources: ResourcesParams,
pub deprecated_resources: ResourcesParams,
}

pub struct VersionedConstantsOverrides {
pub validate_max_n_steps: u32,
pub max_recursion_depth: usize,
pub versioned_constants_base_overrides: Option<VersionedConstants>,
}
20 changes: 20 additions & 0 deletions crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ fn get_json_value_without_defaults() -> serde_json::Value {
json_value_without_defaults
}

/// Assert `versioned_constants_base_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 = DEFAULT_CONSTANTS.clone();
versioned_constants_base_overrides.invoke_tx_max_n_steps += 1;

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()),
});

// Assert the new value is used.
assert_eq!(
result.invoke_tx_max_n_steps,
versioned_constants_base_overrides.invoke_tx_max_n_steps
);
}

#[test]
fn test_default_values() {
let json_value_without_defaults = get_json_value_without_defaults();
Expand Down
2 changes: 2 additions & 0 deletions crates/native_blockifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use py_validator::PyValidator;
use pyo3::prelude::*;
use storage::StorageConfig;

use crate::py_objects::PyVersionedConstantsOverrides;
use crate::py_state_diff::PyStateDiff;
use crate::py_testing_wrappers::{
estimate_casm_hash_computation_resources_for_testing_list,
Expand All @@ -47,6 +48,7 @@ fn native_blockifier(py: Python<'_>, py_module: &PyModule) -> PyResult<()> {
py_module.add_class::<PyBlockExecutor>()?;
py_module.add_class::<PyStateDiff>()?;
py_module.add_class::<PyValidator>()?;
py_module.add_class::<PyVersionedConstantsOverrides>()?;
py_module.add_class::<PyExecutionResources>()?;
py_module.add_class::<StorageConfig>()?;
py_module.add("UndeclaredClassHashError", py.get_type::<UndeclaredClassHashError>())?;
Expand Down
13 changes: 5 additions & 8 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use starknet_api::hash::StarkFelt;
use starknet_api::transaction::Fee;

use crate::errors::{NativeBlockifierError, NativeBlockifierResult};
use crate::py_objects::{PyBouncerConfig, PyConcurrencyConfig};
use crate::py_objects::{PyBouncerConfig, PyConcurrencyConfig, PyVersionedConstantsOverrides};
use crate::py_state_diff::{PyBlockInfo, PyStateDiff};
use crate::py_transaction::{py_tx, PyClassInfo, PY_TX_PARSING_ERR};
use crate::py_utils::{int_to_chain_id, into_block_number_hash_pair, PyFelt};
Expand Down Expand Up @@ -91,23 +91,20 @@ pub struct PyBlockExecutor {
#[pymethods]
impl PyBlockExecutor {
#[new]
#[pyo3(signature = (bouncer_config, concurrency_config, general_config, validate_max_n_steps, max_recursion_depth, global_contract_cache_size, target_storage_config))]
#[pyo3(signature = (bouncer_config, concurrency_config, general_config, global_contract_cache_size, target_storage_config, py_versioned_constants_overrides))]
pub fn create(
bouncer_config: PyBouncerConfig,
concurrency_config: PyConcurrencyConfig,
general_config: PyGeneralConfig,
validate_max_n_steps: u32,
max_recursion_depth: usize,
global_contract_cache_size: usize,
target_storage_config: StorageConfig,
py_versioned_constants_overrides: PyVersionedConstantsOverrides,
) -> Self {
log::debug!("Initializing Block Executor...");
let storage =
PapyrusStorage::new(target_storage_config).expect("Failed to initialize storage.");
let versioned_constants = VersionedConstants::latest_constants_with_overrides(
validate_max_n_steps,
max_recursion_depth,
);
let versioned_constants =
VersionedConstants::get_versioned_constants(py_versioned_constants_overrides.into());
log::debug!("Initialized Block Executor.");

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

// From Rust to Python.
Expand Down Expand Up @@ -31,6 +33,58 @@ impl From<ExecutionResources> for PyExecutionResources {

// From Python to Rust.

#[pyclass]
#[derive(Clone)]
pub struct PyVersionedConstantsOverrides {
pub validate_max_n_steps: u32,
pub max_recursion_depth: usize,
pub versioned_constants_base_overrides: Option<String>,
}

#[pymethods]
impl PyVersionedConstantsOverrides {
#[new]
#[pyo3(signature = (validate_max_n_steps, max_recursion_depth, versioned_constants_base_overrides))]
pub fn create(
validate_max_n_steps: u32,
max_recursion_depth: usize,
versioned_constants_base_overrides: Option<String>,
) -> 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`."))
}
}
}

impl From<PyVersionedConstantsOverrides> for VersionedConstantsOverrides {
fn from(py_versioned_constants_overrides: PyVersionedConstantsOverrides) -> Self {
let PyVersionedConstantsOverrides {
validate_max_n_steps,
max_recursion_depth,
versioned_constants_base_overrides,
} = 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,
}
}
}

#[derive(Clone, Debug, FromPyObject)]
pub struct PyBouncerConfig {
pub full_total_weights_with_keccak: HashMap<String, usize>,
Expand Down
12 changes: 5 additions & 7 deletions crates/native_blockifier/src/py_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use starknet_api::transaction::TransactionHash;

use crate::errors::NativeBlockifierResult;
use crate::py_block_executor::PyGeneralConfig;
use crate::py_objects::PyVersionedConstantsOverrides;
use crate::py_state_diff::PyBlockInfo;
use crate::py_transaction::{py_account_tx, PyClassInfo, PY_TX_PARSING_ERR};
use crate::py_utils::PyFelt;
Expand All @@ -22,24 +23,21 @@ pub struct PyValidator {
#[pymethods]
impl PyValidator {
#[new]
#[pyo3(signature = (general_config, state_reader_proxy, next_block_info, validate_max_n_steps, max_recursion_depth, max_nonce_for_validation_skip))]
#[pyo3(signature = (general_config, state_reader_proxy, next_block_info, max_nonce_for_validation_skip, py_versioned_constants_overrides))]
pub fn create(
general_config: PyGeneralConfig,
state_reader_proxy: &PyAny,
next_block_info: PyBlockInfo,
validate_max_n_steps: u32,
max_recursion_depth: usize,
max_nonce_for_validation_skip: PyFelt,
py_versioned_constants_overrides: PyVersionedConstantsOverrides,
) -> NativeBlockifierResult<Self> {
// Create the state.
let state_reader = PyStateReader::new(state_reader_proxy);
let state = CachedState::new(state_reader);

// Create the block context.
let versioned_constants = VersionedConstants::latest_constants_with_overrides(
validate_max_n_steps,
max_recursion_depth,
);
let versioned_constants =
VersionedConstants::get_versioned_constants(py_versioned_constants_overrides.into());
let block_context = BlockContext::new(
next_block_info.try_into().expect("Failed to convert block info."),
general_config.starknet_os_config.into_chain_info(),
Expand Down

0 comments on commit 0e13d53

Please sign in to comment.