Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/blockifier_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ jobs:
- uses: actions/checkout@v6
with:
ref: ${{ github.base_ref }}
fetch-depth: 0

- uses: ./.github/actions/bootstrap
with:
Expand Down
17 changes: 16 additions & 1 deletion crates/blockifier/src/blockifier/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl SerializeConfig for WorkerPoolConfig {

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Validate)]
pub struct ContractClassManagerConfig {
#[validate(nested)]
pub cairo_native_run_config: CairoNativeRunConfig,
pub contract_cache_size: usize,
#[validate(nested)]
Expand Down Expand Up @@ -241,7 +242,8 @@ impl NativeClassesWhitelist {
}
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Validate)]
#[validate(schema(function = "validate_run_cairo_native"))]
pub struct CairoNativeRunConfig {
pub run_cairo_native: bool,
pub wait_on_native_compilation: bool,
Expand Down Expand Up @@ -303,3 +305,16 @@ impl SerializeConfig for CairoNativeRunConfig {
])
}
}

fn validate_run_cairo_native(
cairo_native_run_config: &CairoNativeRunConfig,
) -> Result<(), validator::ValidationError> {
if cairo_native_run_config.wait_on_native_compilation
&& !cairo_native_run_config.run_cairo_native
{
return Err(validator::ValidationError::new(
"`wait_on_native_compilation` requires `run_cairo_native` to be enabled",
));
}
Ok(())
}
19 changes: 18 additions & 1 deletion crates/blockifier/src/blockifier/config_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use rstest::rstest;
use serde_json;
use starknet_api::class_hash;
use validator::Validate;

use crate::blockifier::config::NativeClassesWhitelist;
use crate::blockifier::config::{CairoNativeRunConfig, NativeClassesWhitelist};

#[rstest]
#[case::all(NativeClassesWhitelist::All)]
Expand All @@ -14,3 +15,19 @@ fn test_native_classes_whitelist_serializes_and_back(#[case] value: NativeClasse
let deserialized: NativeClassesWhitelist = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized, value);
}

#[rstest]
#[case(true, true, true)]
#[case(true, false, true)]
#[case(false, true, false)]
#[case(false, false, true)]
fn test_validate_run_cairo_native(
#[case] run_cairo_native: bool,
#[case] wait_on_native_compilation: bool,
#[case] should_be_valid: bool,
) {
let config =
CairoNativeRunConfig { run_cairo_native, wait_on_native_compilation, ..Default::default() };
let result = config.validate();
assert_eq!(result.is_ok(), should_be_valid, "unexpected validation result {result:?}");
}
5 changes: 2 additions & 3 deletions crates/blockifier/src/state/native_class_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use starknet_api::class_cache::GlobalContractCache;
use starknet_api::core::{ClassHash, CompiledClassHash};
use starknet_api::state::SierraContractClass;
use thiserror::Error;
use validator::Validate;

use crate::blockifier::config::{
CairoNativeRunConfig,
Expand Down Expand Up @@ -68,6 +69,7 @@ impl NativeClassManager {
/// 2. `config.run_cairo_native` is `false`.
/// 3. `config.wait_on_native_compilation` is `true`.
pub fn start(config: ContractClassManagerConfig) -> NativeClassManager {
config.validate().expect("Invalid contract class manager config");
// TODO(Avi, 15/12/2024): Add the size of the channel to the config.
let class_cache = RawClassCache::new(config.contract_cache_size);
let compiled_class_hash_v2_cache = GlobalContractCache::new(config.contract_cache_size);
Expand Down Expand Up @@ -130,8 +132,6 @@ impl NativeClassManager {
if let CompiledClasses::V1(..) = cached_class {
// When `wait_on_native_compilation` is active, all V1 classes should have been compiled
// to native synchronously. A V1 cache entry indicates a pipeline bug.
// TODO(Yoni): make sure `wait_on_native_compilation` cannot be set to true while
// `run_cairo_native` is false.
assert!(
!self.wait_on_native_compilation(),
"Manager did not wait on native compilation."
Expand Down Expand Up @@ -162,7 +162,6 @@ impl NativeClassManager {
CompiledClasses::V1(compiled_class_v1, sierra_contract_class) => {
// TODO(Yoni): instead of these two flag, use an enum.
if self.wait_on_native_compilation() {
assert!(self.run_cairo_native(), "Native compilation is disabled.");
let compiler = self.compiler.as_ref().expect("Compiler not available.");
// After this point, the Native class should be cached and available through
// `get_runnable` access.
Expand Down
25 changes: 22 additions & 3 deletions crates/blockifier/src/state/native_class_manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use rstest::rstest;
use starknet_api::class_cache::GlobalContractCache;
use starknet_api::core::ClassHash;

use crate::blockifier::config::{CairoNativeRunConfig, NativeClassesWhitelist};
use crate::blockifier::config::{
CairoNativeRunConfig,
ContractClassManagerConfig,
NativeClassesWhitelist,
};
use crate::execution::contract_class::{CompiledClassV1, RunnableCompiledClass};
use crate::state::global_cache::{
CachedCairoNative,
Expand All @@ -29,6 +33,23 @@ use crate::test_utils::contracts::FeatureContractTrait;

const TEST_CHANNEL_SIZE: usize = 10;

#[rstest]
#[should_panic(expected = "Invalid contract class manager config:")]
#[case::invalid_cairo_native_run_config(ContractClassManagerConfig {
cairo_native_run_config: CairoNativeRunConfig {
run_cairo_native: false,
wait_on_native_compilation: true,
..Default::default()
},
..Default::default()
})]
fn test_start_invalid_contract_class_manager_config_panics(
#[case] config: ContractClassManagerConfig,
) {
// This should panic after failing validation.
let _manager = NativeClassManager::start(config.clone());
}

fn get_casm(test_contract: FeatureContract) -> CompiledClassV1 {
match test_contract.get_runnable_class() {
RunnableCompiledClass::V1(casm) => casm,
Expand Down Expand Up @@ -101,8 +122,6 @@ fn test_start(#[case] run_cairo_native: bool, #[case] wait_on_native_compilation
#[rstest]
#[case::run_native_while_waiting(true, true)]
#[case::run_native_without_waiting(true, false)]
#[should_panic(expected = "Native compilation is disabled.")]
#[case::run_without_native(false, true)]
#[case::run_without_native(false, false)]
fn test_set_and_compile(
#[case] run_cairo_native: bool,
Expand Down
Loading