Skip to content
Merged
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: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/apollo_gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ blockifier.workspace = true
blockifier_test_utils = { workspace = true, optional = true }
cairo-lang-starknet-classes.workspace = true
clap.workspace = true
lazy_static.workspace = true
mempool_test_utils.workspace = true
num-rational.workspace = true
reqwest.workspace = true
Expand Down
32 changes: 11 additions & 21 deletions crates/apollo_gateway/src/state_reader_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use apollo_class_manager_types::{
ClassManagerClientResult,
Expand All @@ -21,7 +21,6 @@ use blockifier::state::state_api::{StateReader, StateResult};
use blockifier::state::state_api_test_utils::assert_eq_state_result;
use blockifier::state::state_reader_and_contract_manager::StateReaderAndContractManager;
use blockifier::test_utils::initial_test_state::state_reader_and_contract_manager_for_testing;
use lazy_static::lazy_static;
use mockall::predicate;
use rstest::rstest;
use starknet_api::block::{
Expand All @@ -43,6 +42,8 @@ use starknet_api::{class_hash, contract_address, felt, nonce, storage_key};
use crate::state_reader::{GatewayStateReaderWithCompiledClasses, MempoolStateReader};
use crate::sync_state_reader::SyncStateReader;

static DUMMY_CLASS_HASH: LazyLock<ClassHash> = LazyLock::new(|| class_hash!(2_u32));

fn sync_state_reader_and_contract_manager(
state_sync_client: SharedStateSyncClient,
class_manager_client: SharedClassManagerClient,
Expand Down Expand Up @@ -150,28 +151,28 @@ const CACHED_EXPECTATION: GetCompiledClassTestExpectation = GetCompiledClassTest
fn cairo_1_declared_scenario() -> GetCompiledClassTestScenario {
GetCompiledClassTestScenario {
expectations: GetCompiledClassTestExpectation {
get_executable_result: Ok(Some(DUMMY_CONTRACT_CLASS.clone())),
get_executable_result: Ok(Some(ContractClass::test_casm_contract_class())),
n_calls_to_get_executable: 1,
get_sierra_result: Ok(Some(SierraContractClass::default())),
n_calls_to_get_sierra: 1,
is_class_declared_at_result: Some(Ok(true)),
is_cairo_1_class_declared_at_result: None,
},
expected_result: Ok(DUMMY_COMPILED_CLASS.clone()),
expected_result: Ok(RunnableCompiledClass::test_casm_contract_class()),
}
}

fn cairo_0_declared_scenario() -> GetCompiledClassTestScenario {
GetCompiledClassTestScenario {
expectations: GetCompiledClassTestExpectation {
get_executable_result: Ok(Some(DUMMY_CONTRACT_CLASS_V0.clone())),
get_executable_result: Ok(Some(ContractClass::test_deprecated_casm_contract_class())),
n_calls_to_get_executable: 1,
get_sierra_result: Ok(None), // Cairo 0 doesn't use Sierra.
n_calls_to_get_sierra: 0,
is_class_declared_at_result: Some(Ok(true)),
is_cairo_1_class_declared_at_result: None,
},
expected_result: Ok(DUMMY_COMPILED_CLASS_V0.clone()),
expected_result: Ok(RunnableCompiledClass::test_deprecated_casm_contract_class()),
}
}

Expand All @@ -195,7 +196,7 @@ fn cached_cairo_1_declared_scenario() -> GetCompiledClassTestScenario {
is_cairo_1_class_declared_at_result: Some(Ok(true)), // Verification call.
..CACHED_EXPECTATION
},
expected_result: Ok(DUMMY_COMPILED_CLASS.clone()),
expected_result: Ok(RunnableCompiledClass::test_casm_contract_class()),
}
}

Expand All @@ -205,7 +206,7 @@ fn cached_cairo_0_declared_scenario() -> GetCompiledClassTestScenario {
is_cairo_1_class_declared_at_result: None, // Not called for Cairo 0.
..CACHED_EXPECTATION
},
expected_result: Ok(DUMMY_COMPILED_CLASS_V0.clone()),
expected_result: Ok(RunnableCompiledClass::test_deprecated_casm_contract_class()),
}
}

Expand All @@ -222,8 +223,8 @@ fn cached_but_verification_failed_after_reorg_scenario() -> GetCompiledClassTest
fn not_declared_but_in_manager_scenario() -> GetCompiledClassTestScenario {
GetCompiledClassTestScenario {
expectations: GetCompiledClassTestExpectation {
get_executable_result: Ok(Some(DUMMY_CONTRACT_CLASS.clone())), /* In manager but not
* declared. */
get_executable_result: Ok(Some(ContractClass::test_casm_contract_class())), /* In manager but not
* declared. */
n_calls_to_get_executable: 0, // Not called since not declared.
get_sierra_result: Ok(Some(SierraContractClass::default())),
n_calls_to_get_sierra: 0, // Not called since not declared.
Expand Down Expand Up @@ -420,17 +421,6 @@ async fn test_get_class_hash_at() {
assert_eq!(result, expected_result);
}

lazy_static! {
static ref DUMMY_CLASS_HASH: ClassHash = class_hash!("0x2");
static ref DUMMY_CONTRACT_CLASS: ContractClass = ContractClass::test_casm_contract_class();
static ref DUMMY_CONTRACT_CLASS_V0: ContractClass =
ContractClass::test_deprecated_casm_contract_class();
static ref DUMMY_COMPILED_CLASS: RunnableCompiledClass =
DUMMY_CONTRACT_CLASS.clone().try_into().unwrap();
static ref DUMMY_COMPILED_CLASS_V0: RunnableCompiledClass =
DUMMY_CONTRACT_CLASS_V0.clone().try_into().unwrap();
}

// TODO(Arni): Check if any test cases here should move to the tests of
// `StateReaderAndContractManager`.
#[rstest]
Expand Down
16 changes: 6 additions & 10 deletions crates/apollo_gateway/src/sync_state_reader_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use apollo_class_manager_types::{
ClassManagerClientResult,
Expand All @@ -12,7 +12,6 @@ use blockifier::execution::contract_class::RunnableCompiledClass;
use blockifier::state::errors::StateError;
use blockifier::state::state_api::{StateReader, StateResult};
use blockifier::state::state_api_test_utils::assert_eq_state_result;
use lazy_static::lazy_static;
use mockall::predicate;
use rstest::rstest;
use starknet_api::block::{
Expand All @@ -33,6 +32,8 @@ use starknet_api::{class_hash, contract_address, felt, nonce, storage_key};
use crate::state_reader::MempoolStateReader;
use crate::sync_state_reader::SyncStateReader;

static DUMMY_CLASS_HASH: LazyLock<ClassHash> = LazyLock::new(|| class_hash!(2_u32));

#[tokio::test]
async fn test_get_block_info() {
let mut mock_state_sync_client = MockStateSyncClient::new();
Expand Down Expand Up @@ -196,21 +197,16 @@ async fn test_get_class_hash_at() {
assert_eq!(result, expected_result);
}

lazy_static! {
static ref DUMMY_CLASS_HASH: ClassHash = class_hash!("0x2");
static ref DUMMY_CONTRACT_CLASS: ContractClass = ContractClass::test_casm_contract_class();
}

#[rstest]
#[case::class_declared(
Ok(Some(DUMMY_CONTRACT_CLASS.clone())),
Ok(Some(ContractClass::test_casm_contract_class())),
1,
Ok(true),
Ok(DUMMY_CONTRACT_CLASS.clone().try_into().unwrap()),
Ok(RunnableCompiledClass::test_casm_contract_class()),
*DUMMY_CLASS_HASH,
)]
#[case::class_not_declared_but_in_class_manager(
Ok(Some(DUMMY_CONTRACT_CLASS.clone())),
Ok(Some(ContractClass::test_casm_contract_class())),
0,
Ok(false),
Err(StateError::UndeclaredClassHash(*DUMMY_CLASS_HASH)),
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod contracts;
pub mod dict_state_reader;
pub mod execution;
pub mod initial_test_state;
pub mod l1_handler;
pub mod prices;
Expand All @@ -8,6 +9,7 @@ pub mod syscall;
#[cfg(test)]
pub mod test_templates;
pub mod transfers_generator;

use std::collections::HashMap;
use std::slice::Iter;
use std::sync::LazyLock;
Expand Down
21 changes: 21 additions & 0 deletions crates/blockifier/src/test_utils/execution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::sync::LazyLock;

use starknet_api::contract_class::ContractClass;

use crate::execution::contract_class::RunnableCompiledClass;

static TEST_CASM_CONTRACT_CLASS: LazyLock<RunnableCompiledClass> =
LazyLock::new(|| ContractClass::test_casm_contract_class().try_into().unwrap());

static TEST_DEPRECATED_CASM_CONTRACT_CLASS: LazyLock<RunnableCompiledClass> =
LazyLock::new(|| ContractClass::test_deprecated_casm_contract_class().try_into().unwrap());

impl RunnableCompiledClass {
pub fn test_casm_contract_class() -> Self {
TEST_CASM_CONTRACT_CLASS.clone()
}

pub fn test_deprecated_casm_contract_class() -> Self {
TEST_DEPRECATED_CASM_CONTRACT_CLASS.clone()
}
}
82 changes: 45 additions & 37 deletions crates/starknet_api/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,47 +234,55 @@ pub trait TestingTxArgs {
fn get_executable_tx(&self) -> AccountTransaction;
}

static TEST_CASM_CONTRACT_CLASS: LazyLock<ContractClass> = LazyLock::new(|| {
let default_casm = CasmContractClass {
prime: Default::default(),
compiler_version: Default::default(),
bytecode: vec![
BigUintAsHex { value: BigUint::from(1_u8) },
BigUintAsHex { value: BigUint::from(1_u8) },
BigUintAsHex { value: BigUint::from(1_u8) },
],
bytecode_segment_lengths: Default::default(),
hints: Default::default(),
pythonic_hints: Default::default(),
entry_points_by_type: Default::default(),
};
ContractClass::V1((default_casm, SierraVersion::default()))
});

static TEST_DEPRECATED_CASM_CONTRACT_CLASS: LazyLock<ContractClass> = LazyLock::new(|| {
let default_deprecated_casm = DeprecatedContractClass {
abi: None,
program: Program {
attributes: serde_json::Value::Null,
builtins: serde_json::Value::Array(vec![]),
compiler_version: serde_json::Value::Null,
data: serde_json::Value::Array(vec![]),
debug_info: serde_json::Value::Null,
hints: serde_json::Value::Object(serde_json::Map::new()),
identifiers: serde_json::Value::Object(serde_json::Map::new()),
main_scope: serde_json::Value::String("__main__".to_string()),
prime: serde_json::Value::String(
"0x800000000000011000000000000000000000000000000000000000000000001".to_string(),
),
reference_manager: serde_json::Value::Object({
let mut map = serde_json::Map::new();
map.insert("references".to_string(), serde_json::Value::Array(vec![]));
map
}),
},
entry_points_by_type: Default::default(),
};
ContractClass::V0(default_deprecated_casm)
});

impl ContractClass {
pub fn test_casm_contract_class() -> Self {
let default_casm = CasmContractClass {
prime: Default::default(),
compiler_version: Default::default(),
bytecode: vec![
BigUintAsHex { value: BigUint::from(1_u8) },
BigUintAsHex { value: BigUint::from(1_u8) },
BigUintAsHex { value: BigUint::from(1_u8) },
],
bytecode_segment_lengths: Default::default(),
hints: Default::default(),
pythonic_hints: Default::default(),
entry_points_by_type: Default::default(),
};
ContractClass::V1((default_casm, SierraVersion::default()))
TEST_CASM_CONTRACT_CLASS.clone()
}

pub fn test_deprecated_casm_contract_class() -> Self {
let default_deprecated_casm = DeprecatedContractClass {
abi: None,
program: Program {
attributes: serde_json::Value::Null,
builtins: serde_json::Value::Array(vec![]),
compiler_version: serde_json::Value::Null,
data: serde_json::Value::Array(vec![]),
debug_info: serde_json::Value::Null,
hints: serde_json::Value::Object(serde_json::Map::new()),
identifiers: serde_json::Value::Object(serde_json::Map::new()),
main_scope: serde_json::Value::String("__main__".to_string()),
prime: serde_json::Value::String(
"0x800000000000011000000000000000000000000000000000000000000000001".to_string(),
),
reference_manager: serde_json::Value::Object({
let mut map = serde_json::Map::new();
map.insert("references".to_string(), serde_json::Value::Array(vec![]));
map
}),
},
entry_points_by_type: Default::default(),
};
ContractClass::V0(default_deprecated_casm)
TEST_DEPRECATED_CASM_CONTRACT_CLASS.clone()
}
}
Loading