Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(starknet_integration_tests): add sierra contract class to dummy state #2467

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
9 changes: 9 additions & 0 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ impl CairoVersion {
Self::Native => panic!("There is no other version for native"),
}
}

pub fn is_cairo0(&self) -> bool {
match self {
Self::Cairo0 => true,
Self::Cairo1 => false,
#[cfg(feature = "cairo_native")]
Self::Native => false,
}
}
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down
4 changes: 4 additions & 0 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ impl Contract {
self.contract.cairo_version()
}

pub fn sierra(&self) -> SierraContractClass {
self.contract.get_sierra()
}

pub fn raw_class(&self) -> String {
self.contract.get_raw_class()
}
Expand Down
24 changes: 22 additions & 2 deletions crates/starknet_integration_tests/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use starknet_api::block::{
};
use starknet_api::core::{ChainId, ClassHash, ContractAddress, Nonce, SequencerContractAddress};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::{StorageKey, ThinStateDiff};
use starknet_api::state::{SierraContractClass, StorageKey, ThinStateDiff};
use starknet_api::transaction::fields::Fee;
use starknet_api::{contract_address, felt};
use starknet_client::reader::PendingData;
Expand Down Expand Up @@ -123,6 +123,7 @@ fn initialize_papyrus_test_state(

let contract_classes_to_retrieve =
test_defined_accounts.into_iter().chain(default_test_contracts).chain([erc20_contract]);
let sierra_vec: Vec<_> = prepare_sierra_classes(contract_classes_to_retrieve.clone());
let (cairo0_contract_classes, cairo1_contract_classes) =
prepare_compiled_contract_classes(contract_classes_to_retrieve);

Expand All @@ -131,6 +132,7 @@ fn initialize_papyrus_test_state(
state_diff,
&cairo0_contract_classes,
&cairo1_contract_classes,
&sierra_vec,
)
}

Expand Down Expand Up @@ -159,6 +161,15 @@ fn prepare_state_diff(
state_diff_builder.build()
}

fn prepare_sierra_classes(
contract_classes_to_retrieve: impl Iterator<Item = Contract>,
) -> Vec<(ClassHash, SierraContractClass)> {
contract_classes_to_retrieve
.filter(|contract| !contract.cairo_version().is_cairo0())
.map(|contract| (contract.class_hash(), contract.sierra()))
.collect()
}

fn prepare_compiled_contract_classes(
contract_classes_to_retrieve: impl Iterator<Item = Contract>,
) -> ContractClassesMap {
Expand Down Expand Up @@ -191,6 +202,7 @@ fn write_state_to_papyrus_storage(
state_diff: ThinStateDiff,
cairo0_contract_classes: &[(ClassHash, DeprecatedContractClass)],
cairo1_contract_classes: &[(ClassHash, CasmContractClass)],
cairo1_sierra: &[(ClassHash, SierraContractClass)],
) {
let block_number = BlockNumber(0);
let block_header = test_block_header(block_number);
Expand All @@ -202,14 +214,22 @@ fn write_state_to_papyrus_storage(
for (class_hash, casm) in cairo1_contract_classes {
write_txn = write_txn.append_casm(class_hash, casm).unwrap();
}

write_txn
.append_header(block_number, &block_header)
.unwrap()
.append_body(block_number, BlockBody::default())
.unwrap()
.append_state_diff(block_number, state_diff)
.unwrap()
.append_classes(block_number, &[], &cairo0_contract_classes)
.append_classes(
block_number,
&(cairo1_sierra
.iter()
.map(|(class_hash, sierra)| (*class_hash, sierra))
.collect::<Vec<(ClassHash, &SierraContractClass)>>()),
&cairo0_contract_classes,
)
.unwrap()
.commit()
.unwrap();
Expand Down
Loading