Skip to content

Commit

Permalink
feat(papyrus_storage): add sierra version to papyrus cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Dec 4, 2024
1 parent 18aa37e commit 2779f87
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 8 deletions.
6 changes: 2 additions & 4 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ impl From<VersionedRunnableCompiledClass> for RunnableCompiledClass {
versioned_runnable_compiled_class: VersionedRunnableCompiledClass,
) -> RunnableCompiledClass {
match versioned_runnable_compiled_class {
VersionedRunnableCompiledClass::Cairo0(runnable_compiled_class) => {
runnable_compiled_class
}
VersionedRunnableCompiledClass::Cairo1((runnable_compiled_class, __)) => {
VersionedRunnableCompiledClass::Cairo0(runnable_compiled_class)
| VersionedRunnableCompiledClass::Cairo1((runnable_compiled_class, _)) => {
runnable_compiled_class
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/state/global_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Clone> GlobalContractCache<T> {
/// Locks the cache for atomic access. Although conceptually shared, writing to this cache is
Expand Down
17 changes: 16 additions & 1 deletion crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,6 +140,7 @@ pub struct PyBlockExecutor {
pub storage: Box<dyn Storage + Send>,
pub contract_class_manager_config: ContractClassManagerConfig,
pub global_contract_cache: GlobalContractCache<RunnableCompiledClass>,
pub global_sierra_version_cache: GlobalContractCache<SierraVersion>,
}

#[pymethods]
Expand Down Expand Up @@ -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,
),
}
}

Expand Down Expand Up @@ -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,
),
}
}
}
Expand All @@ -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),
Expand All @@ -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,
),
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/native_blockifier/src/py_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion crates/papyrus_state_reader/src/papyrus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ pub struct PapyrusReader {
storage_reader: StorageReader,
latest_block: BlockNumber,
global_class_hash_to_class: GlobalContractCache<RunnableCompiledClass>,
#[allow(dead_code)]
// TODO(Aviv): use this cache
global_class_hash_to_sierra_version: GlobalContractCache<SierraVersion>,
}

impl PapyrusReader {
pub fn new(
storage_reader: StorageReader,
latest_block: BlockNumber,
global_class_hash_to_class: GlobalContractCache<RunnableCompiledClass>,
global_class_hash_to_sierra_version: GlobalContractCache<SierraVersion>,
) -> 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<RawPapyrusReader<'_>> {
Expand Down
7 changes: 6 additions & 1 deletion crates/papyrus_state_reader/src/papyrus_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -318,6 +319,7 @@ pub struct BlockBuilderFactory {
pub block_builder_config: BlockBuilderConfig,
pub storage_reader: StorageReader,
pub global_class_hash_to_class: GlobalContractCache<RunnableCompiledClass>,
pub global_class_hash_to_sierra_version: GlobalContractCache<SierraVersion>,
}

impl BlockBuilderFactory {
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_batcher/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
}
}
Expand Down

0 comments on commit 2779f87

Please sign in to comment.