Skip to content

Commit ca983aa

Browse files
chore(blockifier): create unit tests for contract_class_manager (#2768)
1 parent aeef918 commit ca983aa

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

crates/blockifier/src/state/contract_class_manager.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ use crate::state::global_cache::CachedCairoNative;
2929
use crate::state::global_cache::{CachedCasm, ContractCaches};
3030
pub const DEFAULT_COMPILATION_REQUEST_CHANNEL_SIZE: usize = 2000;
3131

32+
#[cfg(all(test, feature = "cairo_native"))]
33+
#[path = "contract_class_manager_test.rs"]
34+
mod contract_class_manager_test;
3235
/// Represents a request to compile a sierra contract class to a native compiled class.
3336
///
3437
/// # Fields:
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::sync::mpsc::sync_channel;
2+
use std::sync::Arc;
3+
4+
use rstest::rstest;
5+
6+
use crate::blockifier::config::CairoNativeRunConfig;
7+
use crate::execution::contract_class::{CompiledClassV1, RunnableCompiledClass};
8+
use crate::state::contract_class_manager::{CompilationRequest, ContractClassManager};
9+
use crate::state::global_cache::{ContractCaches, GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST};
10+
use crate::test_utils::contracts::FeatureContract;
11+
use crate::test_utils::{CairoVersion, RunnableCairo1};
12+
13+
const TEST_CHANNEL_SIZE: usize = 10;
14+
15+
fn get_casm(test_contract: FeatureContract) -> CompiledClassV1 {
16+
match test_contract.get_runnable_class() {
17+
RunnableCompiledClass::V1(casm) => casm,
18+
_ => panic!("Expected CompiledClassV1"),
19+
}
20+
}
21+
22+
fn create_test_request_from_contract(test_contract: FeatureContract) -> CompilationRequest {
23+
let class_hash = test_contract.get_class_hash();
24+
let sierra = Arc::new(test_contract.get_sierra());
25+
let casm = get_casm(test_contract);
26+
27+
(class_hash, sierra, casm)
28+
}
29+
30+
fn create_test_request() -> CompilationRequest {
31+
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1(RunnableCairo1::Casm));
32+
create_test_request_from_contract(test_contract)
33+
}
34+
35+
#[rstest]
36+
#[case::run_native_while_waiting(true, true)]
37+
#[case::run_native_without_waiting(true, false)]
38+
#[case::run_without_native(false, false)]
39+
fn test_start(#[case] run_cairo_native: bool, #[case] wait_on_native_compilation: bool) {
40+
let native_config =
41+
CairoNativeRunConfig { run_cairo_native, wait_on_native_compilation, ..Default::default() };
42+
let manager = ContractClassManager::create_for_testing(native_config);
43+
44+
assert_eq!(manager.cairo_native_run_config, native_config);
45+
if run_cairo_native {
46+
if wait_on_native_compilation {
47+
assert!(
48+
manager.sender.is_none(),
49+
"Sender should be None - the compilation worker is not used."
50+
);
51+
assert!(
52+
manager.compiler.is_some(),
53+
"Compiler should be Some - compilation is not offloaded to the compilation worker."
54+
);
55+
} else {
56+
assert!(
57+
manager.sender.is_some(),
58+
"Sender should be Some - the compilation worker is used."
59+
);
60+
assert!(
61+
manager.compiler.is_none(),
62+
"Compiler should be None - compilation is offloaded to the compilation worker."
63+
);
64+
}
65+
} else {
66+
assert!(manager.sender.is_none(), "Sender should be None- Cairo native is disabled.");
67+
assert!(manager.compiler.is_none(), "Compiler should be None - Cairo native is disabled.");
68+
}
69+
}
70+
71+
#[test]
72+
#[should_panic(expected = "Compilation request channel is closed.")]
73+
fn test_send_compilation_request_channel_disconnected() {
74+
// We use the channel to send native compilation requests.
75+
let native_config = CairoNativeRunConfig {
76+
run_cairo_native: true,
77+
wait_on_native_compilation: false,
78+
channel_size: TEST_CHANNEL_SIZE,
79+
};
80+
let (sender, receiver) = sync_channel(native_config.channel_size);
81+
let manager = ContractClassManager {
82+
cairo_native_run_config: native_config,
83+
contract_caches: ContractCaches::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
84+
sender: Some(sender),
85+
compiler: None,
86+
};
87+
// Disconnect the channel by dropping the receiver.
88+
drop(receiver);
89+
90+
// Sending request with a disconnected channel should panic.
91+
let request = create_test_request();
92+
manager.send_compilation_request(request);
93+
}

crates/blockifier/src/state/global_cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl CachedCasm {
3131

3232
#[cfg(feature = "cairo_native")]
3333
#[derive(Debug, Clone)]
34+
#[cfg_attr(test, derive(PartialEq))]
3435
pub enum CachedCairoNative {
3536
Compiled(NativeCompiledClassV1),
3637
CompilationFailed,

crates/blockifier/src/test_utils/struct_impls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContract
1919
use starknet_api::execution_resources::GasAmount;
2020
use starknet_api::test_utils::{TEST_ERC20_CONTRACT_ADDRESS, TEST_ERC20_CONTRACT_ADDRESS2};
2121

22+
use crate::blockifier::config::{CairoNativeRunConfig, ContractClassManagerConfig};
2223
use crate::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount};
2324
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext};
2425
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
@@ -33,6 +34,7 @@ use crate::execution::entry_point::{
3334
};
3435
#[cfg(feature = "cairo_native")]
3536
use crate::execution::native::contract_class::NativeCompiledClassV1;
37+
use crate::state::contract_class_manager::ContractClassManager;
3638
use crate::state::state_api::State;
3739
use crate::test_utils::{get_raw_contract_class, update_json_value};
3840
use crate::transaction::objects::{
@@ -182,6 +184,15 @@ impl CallExecution {
182184
}
183185
}
184186

187+
impl ContractClassManager {
188+
pub fn create_for_testing(native_config: CairoNativeRunConfig) -> Self {
189+
let config = ContractClassManagerConfig {
190+
cairo_native_run_config: native_config,
191+
..Default::default()
192+
};
193+
ContractClassManager::start(config)
194+
}
195+
}
185196
// Contract loaders.
186197

187198
// TODO(Noa): Consider using PathBuf.

0 commit comments

Comments
 (0)