From bbd1aef2735d45964dcee2e273d4205741d97e26 Mon Sep 17 00:00:00 2001 From: avivg-starkware Date: Wed, 27 Nov 2024 20:52:11 +0200 Subject: [PATCH] chore(blockifier): add ContractClassManagerConfig (#2092) --- crates/blockifier/src/blockifier/config.rs | 44 +++++++++++++++++++ crates/blockifier/src/state/global_cache.rs | 2 +- .../src/py_block_executor.rs | 42 ++++++++++++++---- .../src/py_block_executor_test.rs | 4 +- crates/native_blockifier/src/py_objects.rs | 30 ++++++++++++- 5 files changed, 110 insertions(+), 12 deletions(-) diff --git a/crates/blockifier/src/blockifier/config.rs b/crates/blockifier/src/blockifier/config.rs index 3252394a45..bc8b5628ce 100644 --- a/crates/blockifier/src/blockifier/config.rs +++ b/crates/blockifier/src/blockifier/config.rs @@ -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, @@ -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 { + 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, + ), + ]) + } +} diff --git a/crates/blockifier/src/state/global_cache.rs b/crates/blockifier/src/state/global_cache.rs index f26d4ab39d..46534a1839 100644 --- a/crates/blockifier/src/state/global_cache.rs +++ b/crates/blockifier/src/state/global_cache.rs @@ -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 GlobalContractCache { /// Locks the cache for atomic access. Although conceptually shared, writing to this cache is diff --git a/crates/native_blockifier/src/py_block_executor.rs b/crates/native_blockifier/src/py_block_executor.rs index 0cefe8026d..d2668edb0b 100644 --- a/crates/native_blockifier/src/py_block_executor.rs +++ b/crates/native_blockifier/src/py_block_executor.rs @@ -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}; @@ -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}; @@ -130,18 +135,19 @@ pub struct PyBlockExecutor { pub tx_executor: Option>, /// `Send` trait is required for `pyclass` compatibility as Python objects must be threadsafe. pub storage: Box, + pub contract_class_manager_config: ContractClassManagerConfig, pub global_contract_cache: GlobalContractCache, } #[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 { @@ -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, + ), } } @@ -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(); @@ -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, + ), } } } @@ -427,6 +439,11 @@ 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), } } @@ -434,11 +451,18 @@ impl PyBlockExecutor { #[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, + ) } } diff --git a/crates/native_blockifier/src/py_block_executor_test.rs b/crates/native_blockifier/src/py_block_executor_test.rs index 2a87bec4fc..d223df4657 100644 --- a/crates/native_blockifier/src/py_block_executor_test.rs +++ b/crates/native_blockifier/src/py_block_executor_test.rs @@ -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; @@ -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, @@ -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, diff --git a/crates/native_blockifier/src/py_objects.rs b/crates/native_blockifier/src/py_objects.rs index 8212a3fd3a..8e54cff2f4 100644 --- a/crates/native_blockifier/src/py_objects.rs +++ b/crates/native_blockifier/src/py_objects.rs @@ -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; @@ -148,3 +149,30 @@ impl From 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 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, + } + } +}