@@ -16,6 +16,7 @@ use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
1616use zksync_node_test_utils:: { create_l1_batch, create_l2_block} ;
1717use zksync_types:: {
1818 block:: { L2BlockHasher , L2BlockHeader } ,
19+ commitment:: L1BatchCommitmentArtifacts ,
1920 ProtocolVersion ,
2021} ;
2122use zksync_web3_decl:: jsonrpsee:: core:: ClientError as RpcError ;
@@ -56,6 +57,13 @@ async fn seal_l1_batch(storage: &mut Connection<'_, Core>, number: u32, hash: H2
5657 . set_l1_batch_hash ( L1BatchNumber ( number) , hash)
5758 . await
5859 . unwrap ( ) ;
60+ let mut commitment_artifact = L1BatchCommitmentArtifacts :: default ( ) ;
61+ commitment_artifact. commitment_hash . commitment = hash;
62+ storage
63+ . blocks_dal ( )
64+ . save_l1_batch_commitment_artifacts ( L1BatchNumber ( number) , & commitment_artifact)
65+ . await
66+ . unwrap ( )
5967}
6068
6169/// Tests the binary search algorithm.
@@ -87,6 +95,7 @@ impl From<RpcErrorKind> for RpcError {
8795struct MockMainNodeClient {
8896 l2_block_hashes : BTreeMap < L2BlockNumber , H256 > ,
8997 l1_batch_root_hashes : BTreeMap < L1BatchNumber , Result < H256 , MissingData > > ,
98+ l1_batch_commitment : BTreeMap < L1BatchNumber , Result < H256 , MissingData > > ,
9099 error_kind : Arc < Mutex < Option < RpcErrorKind > > > ,
91100}
92101
@@ -136,10 +145,16 @@ impl MainNodeClient for MockMainNodeClient {
136145 let Ok ( state_hash) = state_hash else {
137146 return Ok ( Err ( MissingData :: Batch ) ) ;
138147 } ;
148+ let commitment = self
149+ . l1_batch_commitment
150+ . get ( & number)
151+ . copied ( )
152+ . transpose ( )
153+ . unwrap_or_default ( ) ;
139154
140155 Ok ( Ok ( L1BatchHashedData {
141156 root_hash : state_hash,
142- commitment : state_hash ,
157+ commitment,
143158 } ) )
144159 }
145160}
@@ -204,6 +219,9 @@ async fn normal_reorg_function(snapshot_recovery: bool, with_transient_errors: b
204219 client
205220 . l1_batch_root_hashes
206221 . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. root_hash ) ) ;
222+ client
223+ . l1_batch_commitment
224+ . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. commitment ) ) ;
207225 }
208226
209227 let l1_batch_numbers = if snapshot_recovery {
@@ -223,6 +241,9 @@ async fn normal_reorg_function(snapshot_recovery: bool, with_transient_errors: b
223241 client
224242 . l1_batch_root_hashes
225243 . insert ( L1BatchNumber ( number) , Ok ( l1_batch_hash) ) ;
244+ client
245+ . l1_batch_commitment
246+ . insert ( L1BatchNumber ( 0 ) , Ok ( l1_batch_hash) ) ;
226247 ( number, l2_block_hash, l1_batch_hash)
227248 } )
228249 . collect ( ) ;
@@ -298,6 +319,9 @@ async fn reorg_is_detected_on_batch_hash_mismatch() {
298319 client
299320 . l1_batch_root_hashes
300321 . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. root_hash ) ) ;
322+ client
323+ . l1_batch_commitment
324+ . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. commitment ) ) ;
301325
302326 let l2_block_hash = H256 :: from_low_u64_be ( 23 ) ;
303327 client
@@ -306,12 +330,19 @@ async fn reorg_is_detected_on_batch_hash_mismatch() {
306330 client
307331 . l1_batch_root_hashes
308332 . insert ( L1BatchNumber ( 1 ) , Ok ( H256 :: repeat_byte ( 1 ) ) ) ;
333+ client
334+ . l1_batch_commitment
335+ . insert ( L1BatchNumber ( 1 ) , Ok ( H256 :: repeat_byte ( 1 ) ) ) ;
336+
309337 client
310338 . l2_block_hashes
311339 . insert ( L2BlockNumber ( 2 ) , l2_block_hash) ;
312340 client
313341 . l1_batch_root_hashes
314342 . insert ( L1BatchNumber ( 2 ) , Ok ( H256 :: repeat_byte ( 2 ) ) ) ;
343+ client
344+ . l1_batch_commitment
345+ . insert ( L1BatchNumber ( 2 ) , Ok ( H256 :: repeat_byte ( 2 ) ) ) ;
315346
316347 let mut detector = create_mock_detector ( client, pool. clone ( ) ) ;
317348
@@ -353,6 +384,9 @@ async fn reorg_is_detected_on_l2_block_hash_mismatch() {
353384 client
354385 . l1_batch_root_hashes
355386 . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. root_hash ) ) ;
387+ client
388+ . l1_batch_commitment
389+ . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. commitment ) ) ;
356390
357391 let l2_block_hash = H256 :: from_low_u64_be ( 23 ) ;
358392 client
@@ -361,6 +395,10 @@ async fn reorg_is_detected_on_l2_block_hash_mismatch() {
361395 client
362396 . l1_batch_root_hashes
363397 . insert ( L1BatchNumber ( 1 ) , Ok ( H256 :: repeat_byte ( 1 ) ) ) ;
398+ client
399+ . l1_batch_commitment
400+ . insert ( L1BatchNumber ( 1 ) , Ok ( H256 :: repeat_byte ( 1 ) ) ) ;
401+
364402 client
365403 . l2_block_hashes
366404 . insert ( L2BlockNumber ( 2 ) , l2_block_hash) ;
@@ -429,6 +467,9 @@ async fn reorg_is_detected_on_historic_batch_hash_mismatch(
429467 client
430468 . l1_batch_root_hashes
431469 . insert ( L1BatchNumber ( earliest_l1_batch_number) , Ok ( H256 :: zero ( ) ) ) ;
470+ client
471+ . l1_batch_commitment
472+ . insert ( L1BatchNumber ( earliest_l1_batch_number) , Ok ( H256 :: zero ( ) ) ) ;
432473
433474 let l2_block_and_l1_batch_hashes = l1_batch_numbers. clone ( ) . map ( |number| {
434475 let mut l2_block_hash = H256 :: from_low_u64_be ( number. into ( ) ) ;
@@ -439,6 +480,9 @@ async fn reorg_is_detected_on_historic_batch_hash_mismatch(
439480 client
440481 . l1_batch_root_hashes
441482 . insert ( L1BatchNumber ( number) , Ok ( l1_batch_hash) ) ;
483+ client
484+ . l1_batch_commitment
485+ . insert ( L1BatchNumber ( number) , Ok ( l1_batch_hash) ) ;
442486
443487 if number > last_correct_batch {
444488 l2_block_hash = H256 :: zero ( ) ;
@@ -515,6 +559,10 @@ async fn detector_errors_on_earliest_batch_hash_mismatch() {
515559 client
516560 . l1_batch_root_hashes
517561 . insert ( L1BatchNumber ( 0 ) , Ok ( H256 :: zero ( ) ) ) ;
562+ client
563+ . l1_batch_commitment
564+ . insert ( L1BatchNumber ( 0 ) , Ok ( H256 :: zero ( ) ) ) ;
565+
518566 client
519567 . l2_block_hashes
520568 . insert ( L2BlockNumber ( 0 ) , H256 :: zero ( ) ) ;
@@ -533,6 +581,10 @@ async fn detector_errors_on_earliest_batch_hash_mismatch_with_snapshot_recovery(
533581 client
534582 . l1_batch_root_hashes
535583 . insert ( L1BatchNumber ( 3 ) , Ok ( H256 :: zero ( ) ) ) ;
584+ client
585+ . l1_batch_commitment
586+ . insert ( L1BatchNumber ( 3 ) , Ok ( H256 :: zero ( ) ) ) ;
587+
536588 client
537589 . l2_block_hashes
538590 . insert ( L2BlockNumber ( 3 ) , H256 :: zero ( ) ) ;
@@ -576,6 +628,10 @@ async fn reorg_is_detected_without_waiting_for_main_node_to_catch_up() {
576628 client
577629 . l1_batch_root_hashes
578630 . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. root_hash ) ) ;
631+ client
632+ . l1_batch_commitment
633+ . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. commitment ) ) ;
634+
579635 for number in 1 ..3 {
580636 client
581637 . l2_block_hashes
@@ -590,6 +646,9 @@ async fn reorg_is_detected_without_waiting_for_main_node_to_catch_up() {
590646 client
591647 . l1_batch_root_hashes
592648 . insert ( L1BatchNumber ( 3 ) , Ok ( H256 :: repeat_byte ( 0xff ) ) ) ;
649+ client
650+ . l1_batch_commitment
651+ . insert ( L1BatchNumber ( 3 ) , Ok ( H256 :: repeat_byte ( 0xff ) ) ) ;
593652
594653 let mut detector = create_mock_detector ( client, pool) ;
595654 assert_matches ! (
@@ -599,9 +658,10 @@ async fn reorg_is_detected_without_waiting_for_main_node_to_catch_up() {
599658}
600659
601660/// Tests the worst-case scenario w.r.t. L1 batch root hashes: *all* root hashes match locally and on the main node, only L2 block hashes diverge.
602- #[ test_casing( 3 , [ 2 , 5 , 8 ] ) ]
661+ // #[test_casing(3, [2, 5, 8])]
603662#[ tokio:: test]
604- async fn reorg_is_detected_based_on_l2_block_hashes ( last_correct_l1_batch : u32 ) {
663+ async fn reorg_is_detected_based_on_l2_block_hashes ( ) {
664+ let last_correct_l1_batch = 5 ;
605665 const L1_BATCH_COUNT : u32 = 10 ;
606666
607667 assert ! ( last_correct_l1_batch < L1_BATCH_COUNT ) ;
@@ -616,6 +676,9 @@ async fn reorg_is_detected_based_on_l2_block_hashes(last_correct_l1_batch: u32)
616676 client
617677 . l1_batch_root_hashes
618678 . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. root_hash ) ) ;
679+ client
680+ . l1_batch_commitment
681+ . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. commitment ) ) ;
619682 for number in 1 ..L1_BATCH_COUNT {
620683 let l2_block_hash = H256 :: from_low_u64_le ( number. into ( ) ) ;
621684 store_l2_block ( & mut storage, number, l2_block_hash) . await ;
@@ -633,6 +696,9 @@ async fn reorg_is_detected_based_on_l2_block_hashes(last_correct_l1_batch: u32)
633696 client
634697 . l1_batch_root_hashes
635698 . insert ( L1BatchNumber ( number) , Ok ( l1_batch_root_hash) ) ;
699+ client
700+ . l1_batch_commitment
701+ . insert ( L1BatchNumber ( number) , Ok ( l1_batch_root_hash) ) ;
636702 }
637703 drop ( storage) ;
638704
@@ -651,19 +717,19 @@ async fn reorg_is_detected_based_on_l2_block_hashes(last_correct_l1_batch: u32)
651717
652718#[ derive( Debug ) ]
653719struct SlowMainNode {
654- l1_batch_root_hash_call_count : Arc < AtomicUsize > ,
655- l1_batch_commitment_call_count : Arc < AtomicUsize > ,
720+ l1_batch_data_count : Arc < AtomicUsize > ,
656721 delay_call_count : usize ,
657722 genesis_root_hash : H256 ,
723+ genesis_commitment : H256 ,
658724}
659725
660726impl SlowMainNode {
661- fn new ( genesis_root_hash : H256 , delay_call_count : usize ) -> Self {
727+ fn new ( genesis_root_hash : H256 , genesis_commitment : H256 , delay_call_count : usize ) -> Self {
662728 Self {
663- l1_batch_root_hash_call_count : Arc :: default ( ) ,
664- l1_batch_commitment_call_count : Arc :: default ( ) ,
729+ l1_batch_data_count : Arc :: default ( ) ,
665730 delay_call_count,
666731 genesis_root_hash,
732+ genesis_commitment,
667733 }
668734 }
669735}
@@ -693,13 +759,11 @@ impl MainNodeClient for SlowMainNode {
693759 if number > L1BatchNumber ( 0 ) {
694760 return Ok ( Err ( MissingData :: Batch ) ) ;
695761 }
696- let count = self
697- . l1_batch_commitment_call_count
698- . fetch_add ( 1 , Ordering :: Relaxed ) ;
762+ let count = self . l1_batch_data_count . fetch_add ( 1 , Ordering :: Relaxed ) ;
699763 Ok ( if count >= self . delay_call_count {
700764 Ok ( L1BatchHashedData {
701765 root_hash : Some ( self . genesis_root_hash ) ,
702- commitment : Some ( self . genesis_root_hash ) ,
766+ commitment : Some ( self . genesis_commitment ) ,
703767 } )
704768 } else {
705769 Err ( MissingData :: RootHash )
@@ -716,8 +780,8 @@ async fn detector_waits_for_state_hash_on_main_node() {
716780 . unwrap ( ) ;
717781 drop ( storage) ;
718782
719- let client = SlowMainNode :: new ( genesis_batch. root_hash , 5 ) ;
720- let l1_batch_root_hash_call_count = client. l1_batch_root_hash_call_count . clone ( ) ;
783+ let client = SlowMainNode :: new ( genesis_batch. root_hash , genesis_batch . commitment , 5 ) ;
784+ let l1_batch_root_hash_call_count = client. l1_batch_data_count . clone ( ) ;
721785 let mut detector = create_mock_detector ( client, pool) ;
722786 let ( _stop_sender, stop_receiver) = watch:: channel ( false ) ;
723787 detector. run_once ( stop_receiver) . await . unwrap ( ) ;
0 commit comments