From e0c2f761cde080548516b34c44f317a18e6d7890 Mon Sep 17 00:00:00 2001 From: Aviv Greenburg Date: Tue, 12 Nov 2024 17:24:27 +0200 Subject: [PATCH] chore(blockifier): add flag run_native --- crates/blockifier/src/blockifier/config.rs | 43 +++++++++++++++++++ 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, 109 insertions(+), 12 deletions(-) diff --git a/crates/blockifier/src/blockifier/config.rs b/crates/blockifier/src/blockifier/config.rs index 3252394a45..030b80d67a 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,44 @@ impl SerializeConfig for ConcurrencyConfig { ]) } } + +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] +pub struct CairoNativeConfig { + pub run_cairo_native: bool, + pub block_compilation: bool, + pub global_contract_cache_size: usize, +} +impl CairoNativeConfig { + pub fn default() -> Self { + Self { + run_cairo_native: false, + block_compilation: false, + global_contract_cache_size: GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST, + } + } +} + +impl SerializeConfig for CairoNativeConfig { + fn dump(&self) -> BTreeMap { + BTreeMap::from_iter([ + ser_param( + "run_cairo_native", + &self.run_cairo_native, + "Enables Cairo native execution.", + ParamPrivacyInput::Public, + ), + ser_param( + "block_compilation", + &self.block_compilation, + "Block Sequencer main program while compiling sierra, for testing.", + ParamPrivacyInput::Public, + ), + ser_param( + "global_contract_cache_size", + &self.global_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 670045fe5f..12365a0ee4 100644 --- a/crates/blockifier/src/state/global_cache.rs +++ b/crates/blockifier/src/state/global_cache.rs @@ -14,7 +14,7 @@ pub type LockedContractClassCache<'a> = MutexGuard<'a, ContractClassLRUCache>; // TODO(Yoni, 1/1/2025): consider defining CachedStateReader. pub struct GlobalContractCache(pub Arc>); -pub const GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST: usize = 100; +pub const GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST: usize = 400; //TODO(AvivG): should be 400 to match w py? 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 23cf2c828b..b1b2d623db 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::{CairoNativeConfig, TransactionExecutorConfig}; use blockifier::blockifier::transaction_executor::{TransactionExecutor, TransactionExecutorError}; use blockifier::bouncer::BouncerConfig; use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses}; @@ -24,7 +24,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, + PyCairoNativeConfig, + 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}; @@ -129,18 +134,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 cairo_native_config: CairoNativeConfig, 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, cairo_native_config, os_config, target_storage_config, py_versioned_constants_overrides))] pub fn create( bouncer_config: PyBouncerConfig, concurrency_config: PyConcurrencyConfig, + cairo_native_config: PyCairoNativeConfig, os_config: PyOsConfig, - global_contract_cache_size: usize, target_storage_config: StorageConfig, py_versioned_constants_overrides: PyVersionedConstantsOverrides, ) -> Self { @@ -160,7 +166,10 @@ impl PyBlockExecutor { versioned_constants, tx_executor: None, storage: Box::new(storage), - global_contract_cache: GlobalContractCache::new(global_contract_cache_size), + cairo_native_config: cairo_native_config.into(), + global_contract_cache: GlobalContractCache::new( + cairo_native_config.global_contract_cache_size, + ), } } @@ -368,16 +377,16 @@ impl PyBlockExecutor { } #[cfg(any(feature = "testing", test))] - #[pyo3(signature = (concurrency_config, os_config, path, max_state_diff_size))] + #[pyo3(signature = (concurrency_config, cairo_native_config, os_config, path, max_state_diff_size))] #[staticmethod] fn create_for_testing( concurrency_config: PyConcurrencyConfig, + cairo_native_config: PyCairoNativeConfig, 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(); @@ -396,7 +405,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), + cairo_native_config: cairo_native_config.into(), + global_contract_cache: GlobalContractCache::new( + cairo_native_config.global_contract_cache_size, + ), } } } @@ -426,6 +438,11 @@ impl PyBlockExecutor { chain_info: ChainInfo::default(), versioned_constants: VersionedConstants::latest_constants().clone(), tx_executor: None, + cairo_native_config: CairoNativeConfig { + run_cairo_native: false, + block_compilation: false, + global_contract_cache_size: GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST, + }, global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST), } } @@ -433,11 +450,18 @@ impl PyBlockExecutor { #[cfg(test)] pub(crate) fn native_create_for_testing( concurrency_config: PyConcurrencyConfig, + cairo_native_config: PyCairoNativeConfig, 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, + cairo_native_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 1bd931d7c1..fd275f640c 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::ContractClass; use starknet_types_core::felt::Felt; use crate::py_block_executor::{PyBlockExecutor, PyOsConfig}; -use crate::py_objects::PyConcurrencyConfig; +use crate::py_objects::{PyCairoNativeConfig, PyConcurrencyConfig}; 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(), + PyCairoNativeConfig::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(), + PyCairoNativeConfig::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..6ce3e1e0e9 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::{CairoNativeConfig, ConcurrencyConfig}; 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 PyCairoNativeConfig { + pub run_cairo_native: bool, + pub block_compilation: bool, + pub global_contract_cache_size: usize, +} + +impl PyCairoNativeConfig { + pub fn default() -> Self { + Self { + run_cairo_native: false, + block_compilation: false, + global_contract_cache_size: GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST, + } + } +} + +impl From for CairoNativeConfig { + fn from(py_cairo_native_config: PyCairoNativeConfig) -> Self { + CairoNativeConfig { + run_cairo_native: py_cairo_native_config.run_cairo_native, + block_compilation: py_cairo_native_config.block_compilation, + global_contract_cache_size: py_cairo_native_config.global_contract_cache_size, + } + } +}