From 3de09873ea957f0a2a9b07d28f630347f25801dc Mon Sep 17 00:00:00 2001 From: AvivYossef-starkware Date: Tue, 3 Dec 2024 17:31:19 +0200 Subject: [PATCH] feat(papyrus_storage): add sierra version to papyrus cache --- crates/blockifier/src/state/global_cache.rs | 2 ++ .../native_blockifier/src/py_block_executor.rs | 17 ++++++++++++++++- crates/native_blockifier/src/py_objects.rs | 7 ++++++- .../papyrus_state_reader/src/papyrus_state.rs | 11 ++++++++++- .../src/papyrus_state_test.rs | 7 ++++++- crates/papyrus_storage/src/compiled_class.rs | 3 ++- crates/starknet_batcher/src/batcher.rs | 3 +++ crates/starknet_batcher/src/block_builder.rs | 3 +++ crates/starknet_batcher/src/config.rs | 3 +++ 9 files changed, 51 insertions(+), 5 deletions(-) diff --git a/crates/blockifier/src/state/global_cache.rs b/crates/blockifier/src/state/global_cache.rs index f60df02f01..2103b5e6cb 100644 --- a/crates/blockifier/src/state/global_cache.rs +++ b/crates/blockifier/src/state/global_cache.rs @@ -26,6 +26,8 @@ pub enum CachedCairoNative { } pub const GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST: usize = 400; +// TODO(AVIV): Check with Avi if this number makes sense. +pub const GLOBAL_SIERRA_VERSION_CACHE_SIZE_FOR_TEST: usize = 50; 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 d515e3a2da..bb51278764 100644 --- a/crates/native_blockifier/src/py_block_executor.rs +++ b/crates/native_blockifier/src/py_block_executor.rs @@ -21,6 +21,7 @@ use pyo3::types::{PyBytes, PyList}; use pyo3::{FromPyObject, PyAny, Python}; use serde::Serialize; use starknet_api::block::BlockNumber; +use starknet_api::contract_class::SierraVersion; use starknet_api::core::{ChainId, ContractAddress}; use starknet_api::execution_resources::GasVector; use starknet_api::transaction::fields::Fee; @@ -139,6 +140,7 @@ pub struct PyBlockExecutor { pub storage: Box, pub contract_class_manager_config: ContractClassManagerConfig, pub global_contract_cache: GlobalContractCache, + pub global_sierra_version_cache: GlobalContractCache, } #[pymethods] @@ -173,6 +175,9 @@ impl PyBlockExecutor { global_contract_cache: GlobalContractCache::new( contract_class_manager_config.contract_cache_size, ), + global_sierra_version_cache: GlobalContractCache::new( + contract_class_manager_config.contract_cache_size, + ), } } @@ -412,6 +417,9 @@ impl PyBlockExecutor { global_contract_cache: GlobalContractCache::new( contract_class_manager_config.contract_cache_size, ), + global_sierra_version_cache: GlobalContractCache::new( + contract_class_manager_config.sierra_version_cache_size, + ), } } } @@ -428,12 +436,16 @@ impl PyBlockExecutor { self.storage.reader().clone(), next_block_number, self.global_contract_cache.clone(), + self.global_sierra_version_cache.clone(), ) } #[cfg(any(feature = "testing", test))] pub fn create_for_testing_with_storage(storage: impl Storage + Send + 'static) -> Self { - use blockifier::state::global_cache::GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST; + use blockifier::state::global_cache::{ + GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST, + GLOBAL_SIERRA_VERSION_CACHE_SIZE_FOR_TEST, + }; Self { bouncer_config: BouncerConfig::max(), tx_executor_config: TransactionExecutorConfig::create_for_testing(true), @@ -447,6 +459,9 @@ impl PyBlockExecutor { contract_cache_size: GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST, }, global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST), + global_sierra_version_cache: GlobalContractCache::new( + GLOBAL_SIERRA_VERSION_CACHE_SIZE_FOR_TEST, + ), } } diff --git a/crates/native_blockifier/src/py_objects.rs b/crates/native_blockifier/src/py_objects.rs index e61e185010..5254ce9ef8 100644 --- a/crates/native_blockifier/src/py_objects.rs +++ b/crates/native_blockifier/src/py_objects.rs @@ -5,7 +5,10 @@ use std::collections::HashMap; use blockifier::abi::constants; 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::state::global_cache::{ + GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST, + GLOBAL_SIERRA_VERSION_CACHE_SIZE_FOR_TEST, +}; use blockifier::versioned_constants::VersionedConstantsOverrides; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; @@ -157,6 +160,7 @@ pub struct PyContractClassManagerConfig { pub run_cairo_native: bool, pub wait_on_native_compilation: bool, pub contract_cache_size: usize, + pub sierra_version_cache_size: usize, } impl Default for PyContractClassManagerConfig { @@ -165,6 +169,7 @@ impl Default for PyContractClassManagerConfig { run_cairo_native: false, wait_on_native_compilation: false, contract_cache_size: GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST, + sierra_version_cache_size: GLOBAL_SIERRA_VERSION_CACHE_SIZE_FOR_TEST, } } } diff --git a/crates/papyrus_state_reader/src/papyrus_state.rs b/crates/papyrus_state_reader/src/papyrus_state.rs index 8f2a8bb9d8..3e593b1a9b 100644 --- a/crates/papyrus_state_reader/src/papyrus_state.rs +++ b/crates/papyrus_state_reader/src/papyrus_state.rs @@ -26,6 +26,9 @@ pub struct PapyrusReader { storage_reader: StorageReader, latest_block: BlockNumber, global_class_hash_to_class: GlobalContractCache, + #[allow(dead_code)] + // TODO(Aviv): use this cache + global_class_hash_to_sierra_version: GlobalContractCache, } impl PapyrusReader { @@ -33,8 +36,14 @@ impl PapyrusReader { storage_reader: StorageReader, latest_block: BlockNumber, global_class_hash_to_class: GlobalContractCache, + global_class_hash_to_sierra_version: GlobalContractCache, ) -> Self { - Self { storage_reader, latest_block, global_class_hash_to_class } + Self { + storage_reader, + latest_block, + global_class_hash_to_class, + global_class_hash_to_sierra_version, + } } fn reader(&self) -> StateResult> { diff --git a/crates/papyrus_state_reader/src/papyrus_state_test.rs b/crates/papyrus_state_reader/src/papyrus_state_test.rs index 0f398f00af..ab4d93b77f 100644 --- a/crates/papyrus_state_reader/src/papyrus_state_test.rs +++ b/crates/papyrus_state_reader/src/papyrus_state_test.rs @@ -3,7 +3,11 @@ use blockifier::execution::call_info::CallExecution; use blockifier::execution::entry_point::CallEntryPoint; use blockifier::retdata; use blockifier::state::cached_state::CachedState; -use blockifier::state::global_cache::{GlobalContractCache, GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST}; +use blockifier::state::global_cache::{ + GlobalContractCache, + GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST, + GLOBAL_SIERRA_VERSION_CACHE_SIZE_FOR_TEST, +}; use blockifier::state::state_api::StateReader; use blockifier::test_utils::contracts::FeatureContract; use blockifier::test_utils::{trivial_external_entry_point_new, CairoVersion}; @@ -50,6 +54,7 @@ fn test_entry_point_with_papyrus_state() -> papyrus_storage::StorageResult<()> { storage_reader, block_number, GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST), + GlobalContractCache::new(GLOBAL_SIERRA_VERSION_CACHE_SIZE_FOR_TEST), ); let mut state = CachedState::from(papyrus_reader); diff --git a/crates/papyrus_storage/src/compiled_class.rs b/crates/papyrus_storage/src/compiled_class.rs index ce427d157b..14ae9199a3 100644 --- a/crates/papyrus_storage/src/compiled_class.rs +++ b/crates/papyrus_storage/src/compiled_class.rs @@ -63,7 +63,8 @@ use crate::{FileHandlers, MarkerKind, MarkersTable, OffsetKind, StorageResult, S pub trait CasmStorageReader { /// Returns the Cairo assembly of a class given its Sierra class hash. fn get_casm(&self, class_hash: &ClassHash) -> StorageResult>; - /// Returns the Cairo assembly of a class and its sierra contract class given its Sierra class hash. + /// Returns the Cairo assembly of a class and its sierra contract class given its Sierra class + /// hash. fn get_casm_and_sierra( &self, class_hash: &ClassHash, diff --git a/crates/starknet_batcher/src/batcher.rs b/crates/starknet_batcher/src/batcher.rs index 320681a756..2d28817071 100644 --- a/crates/starknet_batcher/src/batcher.rs +++ b/crates/starknet_batcher/src/batcher.rs @@ -372,6 +372,9 @@ pub fn create_batcher(config: BatcherConfig, mempool_client: SharedMempoolClient block_builder_config: config.block_builder_config.clone(), storage_reader: storage_reader.clone(), global_class_hash_to_class: GlobalContractCache::new(config.global_contract_cache_size), + global_class_hash_to_sierra_version: GlobalContractCache::new( + config.global_sierra_version_cache_size, + ), }); let storage_reader = Arc::new(storage_reader); let storage_writer = Box::new(storage_writer); diff --git a/crates/starknet_batcher/src/block_builder.rs b/crates/starknet_batcher/src/block_builder.rs index b900f35d9c..33f4114ed8 100644 --- a/crates/starknet_batcher/src/block_builder.rs +++ b/crates/starknet_batcher/src/block_builder.rs @@ -34,6 +34,7 @@ use starknet_api::block::{ BlockTimestamp, NonzeroGasPrice, }; +use starknet_api::contract_class::SierraVersion; use starknet_api::core::ContractAddress; use starknet_api::executable_transaction::Transaction; use starknet_api::transaction::TransactionHash; @@ -318,6 +319,7 @@ pub struct BlockBuilderFactory { pub block_builder_config: BlockBuilderConfig, pub storage_reader: StorageReader, pub global_class_hash_to_class: GlobalContractCache, + pub global_class_hash_to_sierra_version: GlobalContractCache, } impl BlockBuilderFactory { @@ -351,6 +353,7 @@ impl BlockBuilderFactory { self.storage_reader.clone(), block_metadata.height, self.global_class_hash_to_class.clone(), + self.global_class_hash_to_sierra_version.clone(), ); let executor = TransactionExecutor::pre_process_and_create( diff --git a/crates/starknet_batcher/src/config.rs b/crates/starknet_batcher/src/config.rs index d36329e733..a909d9b99f 100644 --- a/crates/starknet_batcher/src/config.rs +++ b/crates/starknet_batcher/src/config.rs @@ -15,6 +15,7 @@ pub struct BatcherConfig { pub input_stream_content_buffer_size: usize, pub block_builder_config: BlockBuilderConfig, pub global_contract_cache_size: usize, + pub global_sierra_version_cache_size: usize, pub max_l1_handler_txs_per_block_proposal: usize, } @@ -76,6 +77,8 @@ impl Default for BatcherConfig { input_stream_content_buffer_size: 400, block_builder_config: BlockBuilderConfig::default(), global_contract_cache_size: 400, + // TODO(Aviv): set a more reasonable default value. + global_sierra_version_cache_size: 50, max_l1_handler_txs_per_block_proposal: 3, } }