Skip to content

Commit 221f96e

Browse files
committed
add iters
1 parent bdbc31d commit 221f96e

File tree

1 file changed

+74
-71
lines changed

1 file changed

+74
-71
lines changed

tests/integration_tests/persist_state_update_test.rs

Lines changed: 74 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Default for StateUpdateConfig {
7676
fn default() -> Self {
7777
Self {
7878
in_accounts: CollectionConfig::new(0, 5, 0.0),
79-
out_accounts: CollectionConfig::new(1, 5, 1.0),
79+
out_accounts: CollectionConfig::new(0, 5, 1.0),
8080
account_transactions: CollectionConfig::new(0, 3, 0.0),
8181
transactions: CollectionConfig::new(0, 2, 0.0),
8282
leaf_nullifications: CollectionConfig::new(0, 3, 0.0),
@@ -299,12 +299,12 @@ async fn assert_output_accounts_persisted(
299299

300300
if state_update.out_accounts.is_empty() {
301301
// If no accounts expected, verify table is empty
302-
let account_count = accounts::Entity::find().count(db_conn).await?;
303-
assert_eq!(
304-
account_count, 0,
305-
"Expected no accounts in database, but found {}",
306-
account_count
307-
);
302+
// let account_count = accounts::Entity::find().count(db_conn).await?;
303+
// assert_eq!(
304+
// account_count, 0,
305+
// "Expected no accounts in database, but found {}",
306+
// account_count
307+
// );
308308
return Ok(());
309309
}
310310

@@ -409,50 +409,45 @@ async fn assert_state_tree_root(
409409
// First, get all leaf nodes from database to verify they match our output accounts
410410
let leaf_nodes = state_trees::Entity::find()
411411
.filter(state_trees::Column::Tree.eq(tree_pubkey_bytes.clone()))
412-
.filter(state_trees::Column::Level.eq(0i64)) // Leaf level
412+
.filter(state_trees::Column::Level.eq(0i64)) // Leaf level
413413
.all(db_conn)
414414
.await?;
415415

416416
println!("Database Leaf Hashes:");
417417
for leaf in &leaf_nodes {
418-
println!(" Hash({}) at leaf_idx={:?}", hex::encode(&leaf.hash), leaf.leaf_idx);
418+
println!(
419+
" Hash({}) at leaf_idx={:?}",
420+
hex::encode(&leaf.hash),
421+
leaf.leaf_idx
422+
);
419423
}
420424

421425
// Assert that all our new account hashes are present as leaf nodes in the database
422426
for account_with_context in &state_update.out_accounts {
423427
let account_hash = hex::encode(&account_with_context.account.hash.0);
424428
let leaf_index = account_with_context.account.leaf_index.0;
425-
429+
426430
let found_leaf = leaf_nodes.iter().find(|leaf| {
427-
leaf.leaf_idx == Some(leaf_index as i64) &&
428-
hex::encode(&leaf.hash) == account_hash
431+
leaf.leaf_idx == Some(leaf_index as i64) && hex::encode(&leaf.hash) == account_hash
429432
});
430-
431-
assert!(found_leaf.is_some(),
432-
"Account hash {} at leaf_index {} not found in database leaf nodes",
433-
account_hash, leaf_index);
433+
434+
assert!(
435+
found_leaf.is_some(),
436+
"Account hash {} at leaf_index {} not found in database leaf nodes",
437+
account_hash,
438+
leaf_index
439+
);
434440
}
435441
println!("✅ All account hashes verified as leaf nodes in database");
436442

437-
// Construct reference tree from output accounts directly
438-
// Find the maximum leaf index to determine tree size needed
439-
let max_leaf_idx = state_update.out_accounts
440-
.iter()
441-
.map(|acc| acc.account.leaf_index.0)
442-
.max()
443-
.unwrap_or(0);
444-
445-
println!("Constructing reference tree up to leaf index {}", max_leaf_idx);
446-
447-
// Append leaves to reference tree in the correct positions
448-
// Fill with zero hashes for missing leaves, actual account hashes for present ones
449-
for i in 0..=max_leaf_idx {
450-
let leaf_hash = state_update.out_accounts
451-
.iter()
452-
.find(|acc| acc.account.leaf_index.0 == i)
453-
.map(|acc| acc.account.hash.0)
454-
.unwrap_or([0u8; 32]); // Zero hash for missing leaves
455-
443+
// Append only the new leaves from current state update to reference tree
444+
println!(
445+
"Appending {} new leaves from current state update",
446+
state_update.out_accounts.len()
447+
);
448+
449+
for account_with_context in &state_update.out_accounts {
450+
let leaf_hash = account_with_context.account.hash.0;
456451
reference_tree.append(&leaf_hash)?;
457452
}
458453

@@ -472,19 +467,26 @@ async fn assert_state_tree_root(
472467
.filter(|node| node.level == max_level)
473468
.collect();
474469

475-
assert_eq!(root_nodes.len(), 1, "Expected exactly 1 root node, found {}", root_nodes.len());
476-
470+
assert_eq!(
471+
root_nodes.len(),
472+
1,
473+
"Expected exactly 1 root node, found {}",
474+
root_nodes.len()
475+
);
476+
477477
let root_node = root_nodes[0];
478478
let mut db_root_array = [0u8; 32];
479479
db_root_array.copy_from_slice(&root_node.hash);
480480
println!("Database root: {}", hex::encode(&db_root_array));
481481

482482
assert_eq!(
483-
reference_root, db_root_array,
483+
reference_root,
484+
db_root_array,
484485
"State tree root mismatch!\nReference: {}\nDatabase: {}",
485-
hex::encode(&reference_root), hex::encode(&db_root_array)
486+
hex::encode(&reference_root),
487+
hex::encode(&db_root_array)
486488
);
487-
489+
488490
println!("✅ State tree root verification successful!");
489491

490492
Ok(())
@@ -566,10 +568,6 @@ async fn test_output_accounts(#[values(DatabaseBackend::Sqlite)] db_backend: Dat
566568
println!("\n\nconfig structure test seed {}\n\n", seed);
567569
let mut rng = StdRng::seed_from_u64(seed);
568570

569-
// Initialize reference Merkle tree for state tree root verification
570-
let tree_info =
571-
TreeInfo::get(TEST_TREE_PUBKEY_STR).expect("Test tree should exist in QUEUE_TREE_MAPPING");
572-
let tree_height = tree_info.height as usize;
573571
let mut reference_tree = MerkleTree::<Poseidon>::new(26, 0);
574572

575573
// Test that the new config structure works correctly
@@ -580,7 +578,7 @@ async fn test_output_accounts(#[values(DatabaseBackend::Sqlite)] db_backend: Dat
580578
assert_eq!(config.in_accounts.max_entries, 5);
581579
assert_eq!(config.in_accounts.probability, 0.0);
582580

583-
assert_eq!(config.out_accounts.min_entries, 1);
581+
assert_eq!(config.out_accounts.min_entries, 0);
584582
assert_eq!(config.out_accounts.max_entries, 5);
585583
assert_eq!(config.out_accounts.probability, 1.0);
586584

@@ -589,32 +587,37 @@ async fn test_output_accounts(#[values(DatabaseBackend::Sqlite)] db_backend: Dat
589587
assert_eq!(config.transactions.probability, 0.0);
590588

591589
// Test that we can create a state update with incremental values
592-
let slot = 1000;
593-
let base_seq = 500;
594-
let base_leaf_index = 100;
595-
let simple_state_update =
596-
get_rnd_state_update(&mut rng, &config, slot, base_seq, base_leaf_index);
597-
println!("simple_state_update {:?}", simple_state_update);
598-
599-
// Persist the simple state update
600-
let result = persist_state_update_and_commit(&setup.db_conn, simple_state_update.clone()).await;
601-
602-
// Should complete successfully
603-
assert!(
604-
result.is_ok(),
605-
"Failed to persist simple state update: {:?}",
606-
result.err()
607-
);
608-
609-
// Assert that all output accounts were persisted correctly
610-
assert_output_accounts_persisted(&setup.db_conn, &simple_state_update)
611-
.await
612-
.expect("Failed to verify output accounts persistence");
613-
614-
// Assert that state tree root matches reference implementation
615-
assert_state_tree_root(&setup.db_conn, &mut reference_tree, &simple_state_update)
616-
.await
617-
.expect("Failed to verify state tree root");
590+
let mut base_seq = 500;
591+
let mut base_leaf_index = 0;
592+
let num_iters = 1000;
593+
for slot in 0..num_iters {
594+
println!("iter {}", slot);
595+
let simple_state_update =
596+
get_rnd_state_update(&mut rng, &config, slot, base_seq, base_leaf_index);
597+
println!("simple_state_update {:?}", simple_state_update);
598+
599+
// Persist the simple state update
600+
let result =
601+
persist_state_update_and_commit(&setup.db_conn, simple_state_update.clone()).await;
602+
603+
// Should complete successfully
604+
assert!(
605+
result.is_ok(),
606+
"Failed to persist simple state update: {:?}",
607+
result.err()
608+
);
618609

610+
// Assert that all output accounts were persisted correctly
611+
assert_output_accounts_persisted(&setup.db_conn, &simple_state_update)
612+
.await
613+
.expect("Failed to verify output accounts persistence");
614+
615+
// Assert that state tree root matches reference implementation
616+
assert_state_tree_root(&setup.db_conn, &mut reference_tree, &simple_state_update)
617+
.await
618+
.expect("Failed to verify state tree root");
619+
base_seq += simple_state_update.out_accounts.len() as u64;
620+
base_leaf_index += simple_state_update.out_accounts.len() as u64;
621+
}
619622
println!("Config structure test completed successfully - unified CollectionConfig approach with incremental slot/seq/leaf_index working");
620623
}

0 commit comments

Comments
 (0)