Skip to content

Commit 66e0ede

Browse files
committed
apollo_batcher: thread native classes whitelist from create block builder to manager
1 parent 05acbeb commit 66e0ede

File tree

8 files changed

+76
-23
lines changed

8 files changed

+76
-23
lines changed

crates/apollo_batcher/src/batcher.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ impl Batcher {
354354
.block_builder_config
355355
.proposer_idle_detection_delay_millis,
356356
},
357+
self.config
358+
.static_config
359+
.contract_class_manager_config
360+
.cairo_native_run_config
361+
.native_classes_whitelist
362+
.clone(),
357363
Box::new(tx_provider),
358364
Some(output_tx_sender),
359365
Some(candidate_tx_sender),
@@ -442,6 +448,12 @@ impl Batcher {
442448
.block_builder_config
443449
.proposer_idle_detection_delay_millis,
444450
},
451+
self.config
452+
.static_config
453+
.contract_class_manager_config
454+
.cairo_native_run_config
455+
.native_classes_whitelist
456+
.clone(),
445457
Box::new(tx_provider),
446458
None,
447459
None,

crates/apollo_batcher/src/batcher_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ fn mock_create_builder_for_validate_block(
292292
build_block_result: BlockBuilderResult<BlockExecutionArtifacts>,
293293
) {
294294
block_builder_factory.expect_create_block_builder().times(1).return_once(
295-
|_, _, tx_provider, _, _, _, _| {
295+
|_, _, _, tx_provider, _, _, _, _| {
296296
let block_builder = FakeValidateBlockBuilder {
297297
tx_provider,
298298
build_block_result: Some(build_block_result),
@@ -317,7 +317,7 @@ fn mock_create_builder_for_propose_block(
317317
build_block_result: BlockBuilderResult<BlockExecutionArtifacts>,
318318
) {
319319
block_builder_factory.expect_create_block_builder().times(1).return_once(
320-
move |_, _, tx_provider, output_content_sender, _, _, _| {
320+
move |_, _, _, tx_provider, output_content_sender, _, _, _| {
321321
let block_builder = FakeProposeBlockBuilder {
322322
output_content_sender: output_content_sender.unwrap(),
323323
output_txs,

crates/apollo_batcher/src/block_builder.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use apollo_state_reader::apollo_state::{ApolloReader, ClassReader};
1717
use apollo_storage::StorageReader;
1818
use async_trait::async_trait;
1919
use blockifier::blockifier::concurrent_transaction_executor::ConcurrentTransactionExecutor;
20+
use blockifier::blockifier::config::NativeClassesWhitelist;
2021
use blockifier::blockifier::transaction_executor::{
2122
BlockExecutionSummary,
2223
CompiledClassHashesForMigration,
@@ -759,6 +760,7 @@ pub trait BlockBuilderFactoryTrait: Send + Sync {
759760
&self,
760761
block_metadata: BlockMetadata,
761762
execution_params: BlockBuilderExecutionParams,
763+
native_classes_whitelist: NativeClassesWhitelist,
762764
tx_provider: Box<dyn TransactionProvider>,
763765
output_content_sender: Option<
764766
tokio::sync::mpsc::UnboundedSender<InternalConsensusTransaction>,
@@ -782,6 +784,7 @@ impl BlockBuilderFactory {
782784
fn preprocess_and_create_transaction_executor(
783785
&self,
784786
block_metadata: BlockMetadata,
787+
native_classes_whitelist: NativeClassesWhitelist,
785788
runtime: tokio::runtime::Handle,
786789
) -> BlockBuilderResult<ConcurrentTransactionExecutor<ApolloStateReaderAndContractManager>>
787790
{
@@ -804,9 +807,10 @@ impl BlockBuilderFactory {
804807
let class_reader = Some(ClassReader { reader: self.class_manager_client.clone(), runtime });
805808
let apollo_reader =
806809
ApolloReader::new_with_class_reader(self.storage_reader.clone(), height, class_reader);
807-
let state_reader = StateReaderAndContractManager::new(
810+
let state_reader = StateReaderAndContractManager::new_with_native_classes_whitelist(
808811
apollo_reader,
809812
self.contract_class_manager.clone(),
813+
native_classes_whitelist,
810814
Some(BATCHER_CLASS_CACHE_METRICS),
811815
);
812816

@@ -827,6 +831,7 @@ impl BlockBuilderFactoryTrait for BlockBuilderFactory {
827831
&self,
828832
block_metadata: BlockMetadata,
829833
execution_params: BlockBuilderExecutionParams,
834+
native_classes_whitelist: NativeClassesWhitelist,
830835
tx_provider: Box<dyn TransactionProvider>,
831836
output_content_sender: Option<
832837
tokio::sync::mpsc::UnboundedSender<InternalConsensusTransaction>,
@@ -835,7 +840,11 @@ impl BlockBuilderFactoryTrait for BlockBuilderFactory {
835840
pre_confirmed_tx_sender: Option<PreconfirmedTxSender>,
836841
runtime: tokio::runtime::Handle,
837842
) -> BlockBuilderResult<(Box<dyn BlockBuilderTrait>, AbortSignalSender)> {
838-
let executor = self.preprocess_and_create_transaction_executor(block_metadata, runtime)?;
843+
let executor = self.preprocess_and_create_transaction_executor(
844+
block_metadata,
845+
native_classes_whitelist,
846+
runtime,
847+
)?;
839848
let (abort_signal_sender, abort_signal_receiver) = tokio::sync::oneshot::channel();
840849
let transaction_converter = TransactionConverter::new(
841850
self.class_manager_client.clone(),

crates/blockifier/src/state/contract_class_manager.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod trivial_class_manager {
1010
use starknet_api::class_cache::GlobalContractCache;
1111
use starknet_api::core::{ClassHash, CompiledClassHash};
1212

13-
use crate::blockifier::config::ContractClassManagerConfig;
13+
use crate::blockifier::config::{ContractClassManagerConfig, NativeClassesWhitelist};
1414
use crate::execution::contract_class::RunnableCompiledClass;
1515
use crate::state::global_cache::{CompiledClasses, RawClassCache};
1616

@@ -33,7 +33,11 @@ pub mod trivial_class_manager {
3333
}
3434
}
3535

36-
pub fn get_runnable(&self, class_hash: &ClassHash) -> Option<RunnableCompiledClass> {
36+
pub fn get_runnable(
37+
&self,
38+
class_hash: &ClassHash,
39+
_native_classes_whitelist: &NativeClassesWhitelist,
40+
) -> Option<RunnableCompiledClass> {
3741
Some(self.class_cache.get(class_hash)?.to_runnable())
3842
}
3943

crates/blockifier/src/state/native_class_manager.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ use starknet_api::core::{ClassHash, CompiledClassHash};
1313
use starknet_api::state::SierraContractClass;
1414
use thiserror::Error;
1515

16-
use crate::blockifier::config::{CairoNativeRunConfig, ContractClassManagerConfig};
16+
use crate::blockifier::config::{
17+
CairoNativeRunConfig,
18+
ContractClassManagerConfig,
19+
NativeClassesWhitelist,
20+
};
1721
use crate::execution::contract_class::{CompiledClassV1, RunnableCompiledClass};
1822
use crate::execution::native::contract_class::NativeCompiledClassV1;
1923
use crate::metrics::NATIVE_COMPILATION_ERROR;
@@ -117,7 +121,11 @@ impl NativeClassManager {
117121
}
118122

119123
/// Returns the runnable compiled class for the given class hash, if it exists in class_cache.
120-
pub fn get_runnable(&self, class_hash: &ClassHash) -> Option<RunnableCompiledClass> {
124+
pub fn get_runnable(
125+
&self,
126+
class_hash: &ClassHash,
127+
native_classes_whitelist: &NativeClassesWhitelist,
128+
) -> Option<RunnableCompiledClass> {
121129
let cached_class = self.class_cache.get(class_hash)?;
122130

123131
let cached_class = match cached_class {
@@ -131,7 +139,7 @@ impl NativeClassManager {
131139
cached_class
132140
}
133141
CompiledClasses::V1Native(CachedCairoNative::Compiled(native))
134-
if !self.run_class_with_cairo_native(class_hash) =>
142+
if !native_classes_whitelist.contains(class_hash) =>
135143
{
136144
CompiledClasses::V1(native.casm(), Arc::new(SierraContractClass::default()))
137145
}
@@ -219,11 +227,6 @@ impl NativeClassManager {
219227
self.cairo_native_run_config.wait_on_native_compilation
220228
}
221229

222-
/// Determines if a contract should run with cairo native based on the whitelist.
223-
pub fn run_class_with_cairo_native(&self, class_hash: &ClassHash) -> bool {
224-
self.cairo_native_run_config.native_classes_whitelist.contains(class_hash)
225-
}
226-
227230
/// Clears the contract class_cache.
228231
pub fn clear(&mut self) {
229232
self.class_cache.clear();

crates/blockifier/src/state/native_class_manager_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn test_native_classes_whitelist(
254254
wait_on_native_compilation: true,
255255
panic_on_compilation_failure: true,
256256
channel_size: TEST_CHANNEL_SIZE,
257-
native_classes_whitelist: whitelist,
257+
native_classes_whitelist: whitelist.clone(),
258258
};
259259
let manager = NativeClassManager::create_for_testing(native_config);
260260

@@ -264,12 +264,12 @@ fn test_native_classes_whitelist(
264264

265265
match allow_run_native {
266266
true => assert_matches!(
267-
manager.get_runnable(&class_hash),
267+
manager.get_runnable(&class_hash, &whitelist),
268268
Some(RunnableCompiledClass::V1Native(_))
269269
),
270270
false => {
271271
assert_matches!(
272-
manager.get_runnable(&class_hash).unwrap(),
272+
manager.get_runnable(&class_hash, &whitelist).unwrap(),
273273
RunnableCompiledClass::V1(_)
274274
)
275275
}

crates/blockifier/src/state/state_reader_and_contract_manager.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use starknet_api::core::{ClassHash, CompiledClassHash};
22
use starknet_types_core::felt::Felt;
33

4+
use crate::blockifier::config::NativeClassesWhitelist;
45
use crate::execution::contract_class::RunnableCompiledClass;
56
use crate::metrics::CacheMetrics;
67
use crate::state::contract_class_manager::ContractClassManager;
@@ -33,6 +34,7 @@ impl<T: FetchCompiledClasses + ?Sized> FetchCompiledClasses for Box<T> {
3334
pub struct StateReaderAndContractManager<S: FetchCompiledClasses> {
3435
pub state_reader: S,
3536
contract_class_manager: ContractClassManager,
37+
native_classes_whitelist: NativeClassesWhitelist,
3638
class_cache_metrics: Option<CacheMetrics>,
3739
}
3840

@@ -42,7 +44,21 @@ impl<S: FetchCompiledClasses> StateReaderAndContractManager<S> {
4244
contract_class_manager: ContractClassManager,
4345
class_cache_metrics: Option<CacheMetrics>,
4446
) -> Self {
45-
Self { state_reader, contract_class_manager, class_cache_metrics }
47+
Self::new_with_native_classes_whitelist(
48+
state_reader,
49+
contract_class_manager,
50+
NativeClassesWhitelist::All,
51+
class_cache_metrics,
52+
)
53+
}
54+
55+
pub fn new_with_native_classes_whitelist(
56+
state_reader: S,
57+
contract_class_manager: ContractClassManager,
58+
native_classes_whitelist: NativeClassesWhitelist,
59+
class_cache_metrics: Option<CacheMetrics>,
60+
) -> Self {
61+
Self { state_reader, contract_class_manager, native_classes_whitelist, class_cache_metrics }
4662
}
4763
}
4864

@@ -51,7 +67,9 @@ impl<S: FetchCompiledClasses> StateReaderAndContractManager<S> {
5167
&self,
5268
class_hash: ClassHash,
5369
) -> StateResult<RunnableCompiledClass> {
54-
if let Some(runnable_class) = self.contract_class_manager.get_runnable(&class_hash) {
70+
if let Some(runnable_class) =
71+
self.contract_class_manager.get_runnable(&class_hash, &self.native_classes_whitelist)
72+
{
5573
match &runnable_class {
5674
RunnableCompiledClass::V0(_) => {}
5775
_ => {
@@ -72,8 +90,10 @@ impl<S: FetchCompiledClasses> StateReaderAndContractManager<S> {
7290
let compiled_class = self.state_reader.get_compiled_classes(class_hash)?;
7391
self.contract_class_manager.set_and_compile(class_hash, compiled_class.clone());
7492
// Access the cache again in case the class was compiled.
75-
let runnable_class =
76-
self.contract_class_manager.get_runnable(&class_hash).unwrap_or_else(|| {
93+
let runnable_class = self
94+
.contract_class_manager
95+
.get_runnable(&class_hash, &self.native_classes_whitelist)
96+
.unwrap_or_else(|| {
7797
// Edge case that should not be happen if the cache size is big enough.
7898
// TODO(Yoni): consider having an atomic set-and-get.
7999
log::error!("Class is missing immediately after being cached.");

crates/blockifier/src/state/state_reader_and_contract_manager_test.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use starknet_api::core::ClassHash;
1313

1414
#[cfg(not(feature = "cairo_native"))]
1515
use crate::blockifier::config::CairoNativeRunConfig;
16-
use crate::blockifier::config::ContractClassManagerConfig;
16+
use crate::blockifier::config::{ContractClassManagerConfig, NativeClassesWhitelist};
1717
use crate::execution::contract_class::RunnableCompiledClass;
1818
use crate::state::contract_class_manager::ContractClassManager;
1919
#[cfg(not(feature = "cairo_native"))]
@@ -80,7 +80,12 @@ fn test_get_compiled_class_without_native_in_cache(
8080
build_reader_and_declare_contract(test_contract.into(), contract_manager_config);
8181

8282
// Sanity check - the class manager's cache is empty.
83-
assert!(state_reader.contract_class_manager.get_runnable(&test_class_hash).is_none());
83+
assert!(
84+
state_reader
85+
.contract_class_manager
86+
.get_runnable(&test_class_hash, &NativeClassesWhitelist::All)
87+
.is_none()
88+
);
8489

8590
let compiled_class = state_reader.get_compiled_class(test_class_hash).unwrap();
8691

0 commit comments

Comments
 (0)