@@ -6,9 +6,12 @@ use apollo_class_manager::{ClassStorage, FsClassStorage};
66use apollo_class_manager_config:: config:: FsClassStorageConfig ;
77use apollo_proof_manager:: test_utils:: FsProofStorageBuilderForTesting ;
88use apollo_proof_manager_config:: config:: ProofManagerConfig ;
9+ use apollo_storage:: block_hash:: BlockHashStorageWriter ;
910use apollo_storage:: body:: BodyStorageWriter ;
1011use apollo_storage:: class:: ClassStorageWriter ;
1112use apollo_storage:: compiled_class:: CasmStorageWriter ;
13+ use apollo_storage:: global_root:: GlobalRootStorageWriter ;
14+ use apollo_storage:: global_root_marker:: GlobalRootMarkerStorageWriter ;
1215use apollo_storage:: header:: HeaderStorageWriter ;
1316use apollo_storage:: partial_block_hash:: PartialBlockHashComponentsStorageWriter ;
1417use apollo_storage:: state:: StateStorageWriter ;
@@ -24,6 +27,7 @@ use mempool_test_utils::starknet_api_test_utils::{AccountTransactionGenerator, C
2427use starknet_api:: abi:: abi_utils:: get_fee_token_var_address;
2528use starknet_api:: block:: {
2629 BlockBody ,
30+ BlockHash ,
2731 BlockHeader ,
2832 BlockHeaderWithoutHash ,
2933 BlockNumber ,
@@ -32,6 +36,7 @@ use starknet_api::block::{
3236 GasPricePerToken ,
3337} ;
3438use starknet_api:: block_hash:: block_hash_calculator:: {
39+ calculate_block_hash,
3540 BlockHeaderCommitments ,
3641 PartialBlockHashComponents ,
3742} ;
@@ -42,6 +47,7 @@ use starknet_api::core::{
4247 ClassHash ,
4348 ContractAddress ,
4449 EventCommitment ,
50+ GlobalRoot ,
4551 Nonce ,
4652 ReceiptCommitment ,
4753 SequencerContractAddress ,
@@ -67,6 +73,7 @@ use strum::IntoEnumIterator;
6773use tempfile:: TempDir ;
6874
6975use crate :: storage:: StorageExecutablePaths ;
76+ use crate :: utils:: seed_committer_offset;
7077
7178pub type TempDirHandlePair = ( TempDir , TempDir ) ;
7279type ContractClassesMap = (
@@ -154,8 +161,9 @@ impl StorageTestSetup {
154161 test_defined_accounts : Vec < AccountTransactionGenerator > ,
155162 chain_info : & ChainInfo ,
156163 storage_exec_paths : Option < StorageExecutablePaths > ,
164+ preset_test_contracts : PresetTestContracts ,
165+ initial_state_diff_commitment : Option < StateDiffCommitment > ,
157166 ) -> Self {
158- let preset_test_contracts = PresetTestContracts :: new ( ) ;
159167 // TODO(yair): Avoid cloning.
160168 let classes = TestClasses :: new ( & test_defined_accounts, preset_test_contracts. clone ( ) ) ;
161169
@@ -172,6 +180,7 @@ impl StorageTestSetup {
172180 & test_defined_accounts,
173181 preset_test_contracts. clone ( ) ,
174182 & classes,
183+ initial_state_diff_commitment,
175184 ) ;
176185
177186 let state_sync_db_path =
@@ -190,6 +199,7 @@ impl StorageTestSetup {
190199 & test_defined_accounts,
191200 preset_test_contracts,
192201 & classes,
202+ initial_state_diff_commitment,
193203 ) ;
194204
195205 let fs_class_storage_db_path =
@@ -232,6 +242,17 @@ impl StorageTestSetup {
232242 storage_exec_paths. as_ref ( ) . map ( |p| p. get_committer_path_with_db_suffix ( ) ) ;
233243 let ( committer_db_path, committer_storage_handle) =
234244 create_dir_for_testing ( committer_db_path) ;
245+ if initial_state_diff_commitment. is_some ( ) {
246+ let seed_db_path = committer_db_path. clone ( ) ;
247+ std:: thread:: spawn ( move || {
248+ tokio:: runtime:: Builder :: new_current_thread ( )
249+ . build ( )
250+ . unwrap ( )
251+ . block_on ( seed_committer_offset ( seed_db_path, BlockNumber ( 1 ) ) ) ;
252+ } )
253+ . join ( )
254+ . unwrap ( ) ;
255+ }
235256 Self {
236257 storage_config : StorageTestConfig :: new (
237258 batcher_storage_config,
@@ -254,11 +275,17 @@ impl StorageTestSetup {
254275}
255276
256277#[ derive( Clone ) ]
257- struct PresetTestContracts {
278+ pub struct PresetTestContracts {
258279 pub default_test_contracts : Vec < Contract > ,
259280 pub erc20_contract : Contract ,
260281}
261282
283+ impl Default for PresetTestContracts {
284+ fn default ( ) -> Self {
285+ Self :: new ( )
286+ }
287+ }
288+
262289impl PresetTestContracts {
263290 pub fn new ( ) -> Self {
264291 let into_contract = |contract : FeatureContract | Contract {
@@ -336,10 +363,21 @@ fn initialize_papyrus_test_state(
336363 test_defined_accounts : & [ AccountTransactionGenerator ] ,
337364 preset_test_contracts : PresetTestContracts ,
338365 classes : & TestClasses ,
366+ initial_state_diff_commitment : Option < StateDiffCommitment > ,
339367) {
340368 let state_diff = prepare_state_diff ( chain_info, test_defined_accounts, & preset_test_contracts) ;
341369
342- write_state_to_apollo_storage ( storage_writer, state_diff, classes)
370+ write_state_to_apollo_storage (
371+ storage_writer,
372+ state_diff,
373+ classes,
374+ initial_state_diff_commitment,
375+ ) ;
376+ }
377+
378+ /// Global root that matches the proof flow fixtures.
379+ fn integration_test_genesis_global_root ( ) -> GlobalRoot {
380+ GlobalRoot ( felt ! ( "0x68fed6a062d385db8d1dd9096060e822196feb311fc3bb4f0018635461ca85e" ) )
343381}
344382
345383fn prepare_state_diff (
@@ -410,7 +448,12 @@ fn write_state_to_apollo_storage(
410448 storage_writer : & mut StorageWriter ,
411449 state_diff : ThinStateDiff ,
412450 classes : & TestClasses ,
451+ initial_state_diff_commitment : Option < StateDiffCommitment > ,
413452) {
453+ let is_proof_flow = initial_state_diff_commitment. is_some ( ) ;
454+ let state_diff_commitment =
455+ initial_state_diff_commitment. unwrap_or_else ( || calculate_state_diff_hash ( & state_diff) ) ;
456+
414457 let block_number = BlockNumber ( 0 ) ;
415458 let block_header = test_block_header ( block_number, state_diff. len ( ) ) ;
416459 let TestClasses { cairo0_contract_classes, cairo1_contract_classes } = classes;
@@ -422,10 +465,7 @@ fn write_state_to_apollo_storage(
422465 l1_data_gas_price : block_header. block_header_without_hash . l1_data_gas_price ,
423466 l2_gas_price : block_header. block_header_without_hash . l2_gas_price ,
424467 sequencer : block_header. block_header_without_hash . sequencer ,
425- header_commitments : BlockHeaderCommitments {
426- state_diff_commitment : calculate_state_diff_hash ( & state_diff) ,
427- ..Default :: default ( )
428- } ,
468+ header_commitments : BlockHeaderCommitments { state_diff_commitment, ..Default :: default ( ) } ,
429469 ..Default :: default ( )
430470 } ;
431471
@@ -437,7 +477,7 @@ fn write_state_to_apollo_storage(
437477 sierras. push ( ( * class_hash, sierra) ) ;
438478 }
439479
440- write_txn
480+ write_txn = write_txn
441481 . append_header ( block_number, & block_header)
442482 . unwrap ( )
443483 . append_body ( block_number, BlockBody :: default ( ) )
@@ -447,9 +487,26 @@ fn write_state_to_apollo_storage(
447487 . append_classes ( block_number, & sierras, & cairo0_contract_classes)
448488 . unwrap ( )
449489 . set_partial_block_hash_components ( & block_number, & partial_block_hash)
450- . unwrap ( )
451- . commit ( )
452490 . unwrap ( ) ;
491+
492+ if is_proof_flow {
493+ let global_root = integration_test_genesis_global_root ( ) ;
494+ let genesis_block_hash =
495+ calculate_block_hash ( & partial_block_hash, global_root, BlockHash :: GENESIS_PARENT_HASH )
496+ . expect (
497+ "Integration test genesis block hash must be computable from seeded \
498+ components.",
499+ ) ;
500+ write_txn = write_txn
501+ . set_global_root ( & block_number, global_root)
502+ . unwrap ( )
503+ . checked_increment_global_root_marker ( block_number)
504+ . unwrap ( )
505+ . set_block_hash ( & block_number, genesis_block_hash)
506+ . unwrap ( ) ;
507+ }
508+
509+ write_txn. commit ( ) . unwrap ( ) ;
453510}
454511
455512fn test_block_header ( block_number : BlockNumber , state_diff_length : usize ) -> BlockHeader {
@@ -606,3 +663,10 @@ impl<'a> ThinStateDiffBuilder<'a> {
606663 }
607664 }
608665}
666+
667+ pub fn proof_flow_chain_info ( ) -> ChainInfo {
668+ let mut chain_info = ChainInfo :: create_for_testing ( ) ;
669+ chain_info. fee_token_addresses . strk_fee_token_address =
670+ contract_address ! ( "0x4ff17bf76a1c6cebb82601a43bcab4f9650aea543c44f28e8863f8b624e4b58" ) ;
671+ chain_info
672+ }
0 commit comments