Skip to content

Commit d87e217

Browse files
apollo_integration_tests: add shared proof-flow infrastructure (#13760)
1 parent d3164c7 commit d87e217

8 files changed

Lines changed: 138 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_infra_utils/src/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub enum TestIdentifier {
3434
RevertedL1HandlerTx,
3535
InfraUnitTests,
3636
PositiveFlowIntegrationTest,
37+
ProofFlowIntegrationTest,
3738
RestartFlowIntegrationTest,
3839
RestartServiceMultipleNodesFlowIntegrationTest,
3940
RestartServiceSingleNodeFlowIntegrationTest,

crates/apollo_integration_tests/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ apollo_batcher.workspace = true
2020
apollo_batcher_config.workspace = true
2121
apollo_class_manager = { workspace = true, features = ["testing"] }
2222
apollo_class_manager_config.workspace = true
23+
apollo_committer = { workspace = true, features = ["testing"] }
2324
apollo_committer_config.workspace = true
2425
apollo_config.workspace = true
2526
apollo_config_manager_config = { workspace = true, features = ["testing"] }
@@ -75,6 +76,8 @@ serde.workspace = true
7576
serde_json.workspace = true
7677
starknet-types-core.workspace = true
7778
starknet_api.workspace = true
79+
starknet_committer.workspace = true
80+
starknet_patricia_storage.workspace = true
7881
static_assertions.workspace = true
7982
strum.workspace = true
8083
tempfile.workspace = true

crates/apollo_integration_tests/src/flow_test_setup.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use tokio::sync::Mutex;
5757
use tracing::{debug, instrument, Instrument};
5858
use url::Url;
5959

60-
use crate::state_reader::{StorageTestHandles, StorageTestSetup};
60+
use crate::state_reader::{PresetTestContracts, StorageTestHandles, StorageTestSetup};
6161
use crate::utils::{
6262
create_consensus_manager_configs_from_network_configs,
6363
create_mempool_p2p_configs,
@@ -248,7 +248,7 @@ impl FlowSequencerSetup {
248248
) -> Self {
249249
let path = None;
250250
let StorageTestSetup { storage_config, storage_handles } =
251-
StorageTestSetup::new(accounts, &chain_info, path);
251+
StorageTestSetup::new(accounts, &chain_info, path, PresetTestContracts::new(), None);
252252

253253
let (recorder_url, _join_handle) =
254254
spawn_local_success_recorder(available_ports.get_next_port());
@@ -285,6 +285,7 @@ impl FlowSequencerSetup {
285285
block_max_capacity_gas,
286286
validator_id,
287287
allow_bootstrap_txs,
288+
true,
288289
);
289290
let num_l1_txs = u64::try_from(NUM_L1_TRANSACTIONS).unwrap();
290291
node_config.l1_gas_price_scraper_config.as_mut().unwrap().number_of_blocks_for_mean =

crates/apollo_integration_tests/src/integration_test_manager.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use mempool_test_utils::starknet_api_test_utils::{
3535
use papyrus_base_layer::ethereum_base_layer_contract::EthereumBaseLayerConfig;
3636
use papyrus_base_layer::test_utils::anvil_mine_blocks;
3737
use starknet_api::block::{BlockHash, BlockNumber};
38-
use starknet_api::core::{ChainId, Nonce};
38+
use starknet_api::core::{ChainId, Nonce, StateDiffCommitment};
3939
use starknet_api::execution_resources::GasAmount;
4040
use starknet_api::rpc_transaction::RpcTransaction;
4141
use starknet_api::state::SierraContractClass;
@@ -62,7 +62,7 @@ use crate::node_component_configs::{
6262
create_hybrid_component_configs,
6363
};
6464
use crate::sequencer_simulator_utils::SequencerSimulator;
65-
use crate::state_reader::StorageTestHandles;
65+
use crate::state_reader::{proof_flow_chain_info, PresetTestContracts, StorageTestHandles};
6666
use crate::storage::{get_integration_test_storage, CustomPaths};
6767
use crate::utils::{
6868
create_consensus_manager_configs_from_network_configs,
@@ -735,7 +735,7 @@ impl IntegrationTestManager {
735735
///
736736
/// The function verifies the initial state, runs the test with the given number of
737737
/// transactions, waits for execution to complete, and then verifies the final state.
738-
async fn test_and_verify(
738+
pub async fn test_and_verify(
739739
&mut self,
740740
test_scenario: impl TestScenario,
741741
sender_account: AccountId,
@@ -1242,7 +1242,9 @@ async fn get_sequencer_setup_configs(
12421242
}
12431243

12441244
info!("Creating node configurations.");
1245-
let chain_info = ChainInfo::create_for_testing();
1245+
let is_proof_flow = matches!(test_unique_id, TestIdentifier::ProofFlowIntegrationTest);
1246+
let chain_info =
1247+
if is_proof_flow { proof_flow_chain_info() } else { ChainInfo::create_for_testing() };
12461248
let accounts = tx_generator.accounts();
12471249
let component_configs_len = node_component_configs.len();
12481250

@@ -1319,11 +1321,15 @@ async fn get_sequencer_setup_configs(
13191321
let validator_id = set_validator_id(&mut consensus_manager_config, node_index);
13201322
let chain_info = chain_info.clone();
13211323

1324+
let preset_test_contracts = PresetTestContracts::new();
1325+
let initial_state_diff_commitment = is_proof_flow.then(StateDiffCommitment::default);
13221326
let storage_setup = get_integration_test_storage(
13231327
node_index,
13241328
custom_paths.clone(),
13251329
accounts.to_vec(),
13261330
&chain_info,
1331+
preset_test_contracts,
1332+
initial_state_diff_commitment,
13271333
);
13281334

13291335
// Per node, create the executables constituting it.
@@ -1348,6 +1354,7 @@ async fn get_sequencer_setup_configs(
13481354
block_max_capacity_gas(),
13491355
validator_id,
13501356
ALLOW_BOOTSTRAP_TXS,
1357+
!is_proof_flow,
13511358
);
13521359

13531360
let base_app_config = DeploymentBaseAppConfig::new(config, config_pointers_map);

crates/apollo_integration_tests/src/state_reader.rs

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ use apollo_class_manager::{ClassStorage, FsClassStorage};
66
use apollo_class_manager_config::config::FsClassStorageConfig;
77
use apollo_proof_manager::test_utils::FsProofStorageBuilderForTesting;
88
use apollo_proof_manager_config::config::ProofManagerConfig;
9+
use apollo_storage::block_hash::BlockHashStorageWriter;
910
use apollo_storage::body::BodyStorageWriter;
1011
use apollo_storage::class::ClassStorageWriter;
1112
use apollo_storage::compiled_class::CasmStorageWriter;
13+
use apollo_storage::global_root::GlobalRootStorageWriter;
14+
use apollo_storage::global_root_marker::GlobalRootMarkerStorageWriter;
1215
use apollo_storage::header::HeaderStorageWriter;
1316
use apollo_storage::partial_block_hash::PartialBlockHashComponentsStorageWriter;
1417
use apollo_storage::state::StateStorageWriter;
@@ -24,6 +27,7 @@ use mempool_test_utils::starknet_api_test_utils::{AccountTransactionGenerator, C
2427
use starknet_api::abi::abi_utils::get_fee_token_var_address;
2528
use starknet_api::block::{
2629
BlockBody,
30+
BlockHash,
2731
BlockHeader,
2832
BlockHeaderWithoutHash,
2933
BlockNumber,
@@ -32,6 +36,7 @@ use starknet_api::block::{
3236
GasPricePerToken,
3337
};
3438
use 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;
6773
use tempfile::TempDir;
6874

6975
use crate::storage::StorageExecutablePaths;
76+
use crate::utils::seed_committer_offset;
7077

7178
pub type TempDirHandlePair = (TempDir, TempDir);
7279
type 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+
262289
impl 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

345383
fn 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

455512
fn 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+
}

crates/apollo_integration_tests/src/storage.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use std::path::{Path, PathBuf};
22

33
use blockifier::context::ChainInfo;
44
use mempool_test_utils::starknet_api_test_utils::AccountTransactionGenerator;
5+
use starknet_api::core::StateDiffCommitment;
56

67
use crate::executable_setup::NodeExecutableId;
78
use crate::state_reader::{
9+
PresetTestContracts,
810
StorageTestConfig,
911
StorageTestSetup,
1012
BATCHER_DB_PATH_SUFFIX,
@@ -117,13 +119,20 @@ pub fn get_integration_test_storage(
117119
custom_paths: Option<CustomPaths>,
118120
accounts: Vec<AccountTransactionGenerator>,
119121
chain_info: &ChainInfo,
122+
preset_test_contracts: PresetTestContracts,
123+
initial_state_diff_commitment: Option<StateDiffCommitment>,
120124
) -> StorageTestSetup {
121125
let storage_exec_paths = custom_paths.as_ref().and_then(|paths| {
122126
paths.get_db_base().map(|db_base| StorageExecutablePaths::new(db_base, node_index))
123127
});
124128

125-
let StorageTestSetup { mut storage_config, storage_handles } =
126-
StorageTestSetup::new(accounts, chain_info, storage_exec_paths);
129+
let StorageTestSetup { mut storage_config, storage_handles } = StorageTestSetup::new(
130+
accounts,
131+
chain_info,
132+
storage_exec_paths,
133+
preset_test_contracts,
134+
initial_state_diff_commitment,
135+
);
127136

128137
// Allow overriding the path with a custom prefix for Docker mode in system tests.
129138
if let Some(paths) = custom_paths {

0 commit comments

Comments
 (0)