@@ -76,7 +76,7 @@ impl Default for StateUpdateConfig {
76
76
fn default ( ) -> Self {
77
77
Self {
78
78
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 ) ,
80
80
account_transactions : CollectionConfig :: new ( 0 , 3 , 0.0 ) ,
81
81
transactions : CollectionConfig :: new ( 0 , 2 , 0.0 ) ,
82
82
leaf_nullifications : CollectionConfig :: new ( 0 , 3 , 0.0 ) ,
@@ -299,12 +299,12 @@ async fn assert_output_accounts_persisted(
299
299
300
300
if state_update. out_accounts . is_empty ( ) {
301
301
// 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
+ // );
308
308
return Ok ( ( ) ) ;
309
309
}
310
310
@@ -409,50 +409,45 @@ async fn assert_state_tree_root(
409
409
// First, get all leaf nodes from database to verify they match our output accounts
410
410
let leaf_nodes = state_trees:: Entity :: find ( )
411
411
. 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
413
413
. all ( db_conn)
414
414
. await ?;
415
415
416
416
println ! ( "Database Leaf Hashes:" ) ;
417
417
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
+ ) ;
419
423
}
420
424
421
425
// Assert that all our new account hashes are present as leaf nodes in the database
422
426
for account_with_context in & state_update. out_accounts {
423
427
let account_hash = hex:: encode ( & account_with_context. account . hash . 0 ) ;
424
428
let leaf_index = account_with_context. account . leaf_index . 0 ;
425
-
429
+
426
430
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
429
432
} ) ;
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
+ ) ;
434
440
}
435
441
println ! ( "✅ All account hashes verified as leaf nodes in database" ) ;
436
442
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 ;
456
451
reference_tree. append ( & leaf_hash) ?;
457
452
}
458
453
@@ -472,19 +467,26 @@ async fn assert_state_tree_root(
472
467
. filter ( |node| node. level == max_level)
473
468
. collect ( ) ;
474
469
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
+
477
477
let root_node = root_nodes[ 0 ] ;
478
478
let mut db_root_array = [ 0u8 ; 32 ] ;
479
479
db_root_array. copy_from_slice ( & root_node. hash ) ;
480
480
println ! ( "Database root: {}" , hex:: encode( & db_root_array) ) ;
481
481
482
482
assert_eq ! (
483
- reference_root, db_root_array,
483
+ reference_root,
484
+ db_root_array,
484
485
"State tree root mismatch!\n Reference: {}\n Database: {}" ,
485
- hex:: encode( & reference_root) , hex:: encode( & db_root_array)
486
+ hex:: encode( & reference_root) ,
487
+ hex:: encode( & db_root_array)
486
488
) ;
487
-
489
+
488
490
println ! ( "✅ State tree root verification successful!" ) ;
489
491
490
492
Ok ( ( ) )
@@ -566,10 +568,6 @@ async fn test_output_accounts(#[values(DatabaseBackend::Sqlite)] db_backend: Dat
566
568
println ! ( "\n \n config structure test seed {}\n \n " , seed) ;
567
569
let mut rng = StdRng :: seed_from_u64 ( seed) ;
568
570
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 ;
573
571
let mut reference_tree = MerkleTree :: < Poseidon > :: new ( 26 , 0 ) ;
574
572
575
573
// Test that the new config structure works correctly
@@ -580,7 +578,7 @@ async fn test_output_accounts(#[values(DatabaseBackend::Sqlite)] db_backend: Dat
580
578
assert_eq ! ( config. in_accounts. max_entries, 5 ) ;
581
579
assert_eq ! ( config. in_accounts. probability, 0.0 ) ;
582
580
583
- assert_eq ! ( config. out_accounts. min_entries, 1 ) ;
581
+ assert_eq ! ( config. out_accounts. min_entries, 0 ) ;
584
582
assert_eq ! ( config. out_accounts. max_entries, 5 ) ;
585
583
assert_eq ! ( config. out_accounts. probability, 1.0 ) ;
586
584
@@ -589,32 +587,37 @@ async fn test_output_accounts(#[values(DatabaseBackend::Sqlite)] db_backend: Dat
589
587
assert_eq ! ( config. transactions. probability, 0.0 ) ;
590
588
591
589
// 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
+ ) ;
618
609
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
+ }
619
622
println ! ( "Config structure test completed successfully - unified CollectionConfig approach with incremental slot/seq/leaf_index working" ) ;
620
623
}
0 commit comments