Skip to content

Commit

Permalink
chore(blockifier): add ContractClassManagerConfig (#2092)
Browse files Browse the repository at this point in the history
  • Loading branch information
avivg-starkware authored Nov 27, 2024
1 parent 18e06a7 commit bbd1aef
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 12 deletions.
44 changes: 44 additions & 0 deletions crates/blockifier/src/blockifier/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};

use crate::state::global_cache::GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST;

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct TransactionExecutorConfig {
pub concurrency_config: ConcurrencyConfig,
Expand Down Expand Up @@ -61,3 +63,45 @@ impl SerializeConfig for ConcurrencyConfig {
])
}
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ContractClassManagerConfig {
pub run_cairo_native: bool,
pub wait_on_native_compilation: bool,
pub contract_cache_size: usize,
}

impl Default for ContractClassManagerConfig {
fn default() -> Self {
Self {
run_cairo_native: false,
wait_on_native_compilation: false,
contract_cache_size: GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST,
}
}
}

impl SerializeConfig for ContractClassManagerConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([
ser_param(
"run_cairo_native",
&self.run_cairo_native,
"Enables Cairo native execution.",
ParamPrivacyInput::Public,
),
ser_param(
"wait_on_native_compilation",
&self.wait_on_native_compilation,
"Block Sequencer main program while compiling sierra, for testing.",
ParamPrivacyInput::Public,
),
ser_param(
"contract_cache_size",
&self.contract_cache_size,
"The size of the global contract cache.",
ParamPrivacyInput::Public,
),
])
}
}
2 changes: 1 addition & 1 deletion crates/blockifier/src/state/global_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum CachedCairoNative {
CompilationFailed,
}

pub const GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST: usize = 100;
pub const GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST: usize = 400;

impl<T: Clone> GlobalContractCache<T> {
/// Locks the cache for atomic access. Although conceptually shared, writing to this cache is
Expand Down
42 changes: 33 additions & 9 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use blockifier::abi::constants as abi_constants;
use blockifier::blockifier::config::TransactionExecutorConfig;
use blockifier::blockifier::config::{ContractClassManagerConfig, TransactionExecutorConfig};
use blockifier::blockifier::transaction_executor::{TransactionExecutor, TransactionExecutorError};
use blockifier::bouncer::BouncerConfig;
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
Expand All @@ -25,7 +25,12 @@ use starknet_api::transaction::fields::Fee;
use starknet_types_core::felt::Felt;

use crate::errors::{NativeBlockifierError, NativeBlockifierResult};
use crate::py_objects::{PyBouncerConfig, PyConcurrencyConfig, PyVersionedConstantsOverrides};
use crate::py_objects::{
PyBouncerConfig,
PyConcurrencyConfig,
PyContractClassManagerConfig,
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 @@ -130,18 +135,19 @@ pub struct PyBlockExecutor {
pub tx_executor: Option<TransactionExecutor<PapyrusReader>>,
/// `Send` trait is required for `pyclass` compatibility as Python objects must be threadsafe.
pub storage: Box<dyn Storage + Send>,
pub contract_class_manager_config: ContractClassManagerConfig,
pub global_contract_cache: GlobalContractCache<RunnableCompiledClass>,
}

#[pymethods]
impl PyBlockExecutor {
#[new]
#[pyo3(signature = (bouncer_config, concurrency_config, os_config, global_contract_cache_size, target_storage_config, py_versioned_constants_overrides))]
#[pyo3(signature = (bouncer_config, concurrency_config, contract_class_manager_config, os_config, target_storage_config, py_versioned_constants_overrides))]
pub fn create(
bouncer_config: PyBouncerConfig,
concurrency_config: PyConcurrencyConfig,
contract_class_manager_config: PyContractClassManagerConfig,
os_config: PyOsConfig,
global_contract_cache_size: usize,
target_storage_config: StorageConfig,
py_versioned_constants_overrides: PyVersionedConstantsOverrides,
) -> Self {
Expand All @@ -161,7 +167,10 @@ impl PyBlockExecutor {
versioned_constants,
tx_executor: None,
storage: Box::new(storage),
global_contract_cache: GlobalContractCache::new(global_contract_cache_size),
contract_class_manager_config: contract_class_manager_config.into(),
global_contract_cache: GlobalContractCache::new(
contract_class_manager_config.contract_cache_size,
),
}
}

Expand Down Expand Up @@ -369,16 +378,16 @@ impl PyBlockExecutor {
}

#[cfg(any(feature = "testing", test))]
#[pyo3(signature = (concurrency_config, os_config, path, max_state_diff_size))]
#[pyo3(signature = (concurrency_config, contract_class_manager_config, os_config, path, max_state_diff_size))]
#[staticmethod]
fn create_for_testing(
concurrency_config: PyConcurrencyConfig,
contract_class_manager_config: PyContractClassManagerConfig,
os_config: PyOsConfig,
path: std::path::PathBuf,
max_state_diff_size: usize,
) -> Self {
use blockifier::bouncer::BouncerWeights;
use blockifier::state::global_cache::GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST;
// TODO(Meshi, 01/01/2025): Remove this once we fix all python tests that re-declare cairo0
// contracts.
let mut versioned_constants = VersionedConstants::latest_constants().clone();
Expand All @@ -397,7 +406,10 @@ impl PyBlockExecutor {
chain_info: os_config.into_chain_info(),
versioned_constants,
tx_executor: None,
global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
contract_class_manager_config: contract_class_manager_config.into(),
global_contract_cache: GlobalContractCache::new(
contract_class_manager_config.contract_cache_size,
),
}
}
}
Expand Down Expand Up @@ -427,18 +439,30 @@ impl PyBlockExecutor {
chain_info: ChainInfo::default(),
versioned_constants: VersionedConstants::latest_constants().clone(),
tx_executor: None,
contract_class_manager_config: ContractClassManagerConfig {
run_cairo_native: false,
wait_on_native_compilation: false,
contract_cache_size: GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST,
},
global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
}
}

#[cfg(test)]
pub(crate) fn native_create_for_testing(
concurrency_config: PyConcurrencyConfig,
contract_class_manager_config: PyContractClassManagerConfig,
os_config: PyOsConfig,
path: std::path::PathBuf,
max_state_diff_size: usize,
) -> Self {
Self::create_for_testing(concurrency_config, os_config, path, max_state_diff_size)
Self::create_for_testing(
concurrency_config,
contract_class_manager_config,
os_config,
path,
max_state_diff_size,
)
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/native_blockifier/src/py_block_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use starknet_api::state::SierraContractClass;
use starknet_types_core::felt::Felt;

use crate::py_block_executor::{PyBlockExecutor, PyOsConfig};
use crate::py_objects::PyConcurrencyConfig;
use crate::py_objects::{PyConcurrencyConfig, PyContractClassManagerConfig};
use crate::py_state_diff::{PyBlockInfo, PyStateDiff};
use crate::py_utils::PyFelt;
use crate::test_utils::MockStorage;
Expand All @@ -39,6 +39,7 @@ fn global_contract_cache_update() {
let temp_storage_path = tempfile::tempdir().unwrap().into_path();
let mut block_executor = PyBlockExecutor::create_for_testing(
PyConcurrencyConfig::default(),
PyContractClassManagerConfig::default(),
PyOsConfig::default(),
temp_storage_path,
4000,
Expand Down Expand Up @@ -119,6 +120,7 @@ fn global_contract_cache_update_large_contract() {
let temp_storage_path = tempfile::tempdir().unwrap().into_path();
let mut block_executor = PyBlockExecutor::native_create_for_testing(
Default::default(),
PyContractClassManagerConfig::default(),
Default::default(),
temp_storage_path,
4000,
Expand Down
30 changes: 29 additions & 1 deletion crates/native_blockifier/src/py_objects.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;

use blockifier::abi::constants;
use blockifier::blockifier::config::ConcurrencyConfig;
use blockifier::blockifier::config::{ConcurrencyConfig, ContractClassManagerConfig};
use blockifier::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount, HashMapWrapper};
use blockifier::state::global_cache::GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST;
use blockifier::versioned_constants::VersionedConstantsOverrides;
use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
Expand Down Expand Up @@ -148,3 +149,30 @@ impl From<PyConcurrencyConfig> for ConcurrencyConfig {
}
}
}

#[derive(Debug, Clone, Copy, FromPyObject)]
pub struct PyContractClassManagerConfig {
pub run_cairo_native: bool,
pub wait_on_native_compilation: bool,
pub contract_cache_size: usize,
}

impl Default for PyContractClassManagerConfig {
fn default() -> Self {
Self {
run_cairo_native: false,
wait_on_native_compilation: false,
contract_cache_size: GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST,
}
}
}

impl From<PyContractClassManagerConfig> for ContractClassManagerConfig {
fn from(py_contract_class_manager_config: PyContractClassManagerConfig) -> Self {
ContractClassManagerConfig {
run_cairo_native: py_contract_class_manager_config.run_cairo_native,
wait_on_native_compilation: py_contract_class_manager_config.wait_on_native_compilation,
contract_cache_size: py_contract_class_manager_config.contract_cache_size,
}
}
}

0 comments on commit bbd1aef

Please sign in to comment.