@@ -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 ! (
@@ -616,6 +675,9 @@ async fn reorg_is_detected_based_on_l2_block_hashes(last_correct_l1_batch: u32)
616675 client
617676 . l1_batch_root_hashes
618677 . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. root_hash ) ) ;
678+ client
679+ . l1_batch_commitment
680+ . insert ( L1BatchNumber ( 0 ) , Ok ( genesis_batch. commitment ) ) ;
619681 for number in 1 ..L1_BATCH_COUNT {
620682 let l2_block_hash = H256 :: from_low_u64_le ( number. into ( ) ) ;
621683 store_l2_block ( & mut storage, number, l2_block_hash) . await ;
@@ -633,6 +695,9 @@ async fn reorg_is_detected_based_on_l2_block_hashes(last_correct_l1_batch: u32)
633695 client
634696 . l1_batch_root_hashes
635697 . insert ( L1BatchNumber ( number) , Ok ( l1_batch_root_hash) ) ;
698+ client
699+ . l1_batch_commitment
700+ . insert ( L1BatchNumber ( number) , Ok ( l1_batch_root_hash) ) ;
636701 }
637702 drop ( storage) ;
638703
@@ -651,19 +716,19 @@ async fn reorg_is_detected_based_on_l2_block_hashes(last_correct_l1_batch: u32)
651716
652717#[ derive( Debug ) ]
653718struct SlowMainNode {
654- l1_batch_root_hash_call_count : Arc < AtomicUsize > ,
655- l1_batch_commitment_call_count : Arc < AtomicUsize > ,
719+ l1_batch_data_count : Arc < AtomicUsize > ,
656720 delay_call_count : usize ,
657721 genesis_root_hash : H256 ,
722+ genesis_commitment : H256 ,
658723}
659724
660725impl SlowMainNode {
661- fn new ( genesis_root_hash : H256 , delay_call_count : usize ) -> Self {
726+ fn new ( genesis_root_hash : H256 , genesis_commitment : H256 , delay_call_count : usize ) -> Self {
662727 Self {
663- l1_batch_root_hash_call_count : Arc :: default ( ) ,
664- l1_batch_commitment_call_count : Arc :: default ( ) ,
728+ l1_batch_data_count : Arc :: default ( ) ,
665729 delay_call_count,
666730 genesis_root_hash,
731+ genesis_commitment,
667732 }
668733 }
669734}
@@ -693,13 +758,11 @@ impl MainNodeClient for SlowMainNode {
693758 if number > L1BatchNumber ( 0 ) {
694759 return Ok ( Err ( MissingData :: Batch ) ) ;
695760 }
696- let count = self
697- . l1_batch_commitment_call_count
698- . fetch_add ( 1 , Ordering :: Relaxed ) ;
761+ let count = self . l1_batch_data_count . fetch_add ( 1 , Ordering :: Relaxed ) ;
699762 Ok ( if count >= self . delay_call_count {
700763 Ok ( L1BatchHashedData {
701764 root_hash : Some ( self . genesis_root_hash ) ,
702- commitment : Some ( self . genesis_root_hash ) ,
765+ commitment : Some ( self . genesis_commitment ) ,
703766 } )
704767 } else {
705768 Err ( MissingData :: RootHash )
@@ -716,8 +779,8 @@ async fn detector_waits_for_state_hash_on_main_node() {
716779 . unwrap ( ) ;
717780 drop ( storage) ;
718781
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 ( ) ;
782+ let client = SlowMainNode :: new ( genesis_batch. root_hash , genesis_batch . commitment , 5 ) ;
783+ let l1_batch_root_hash_call_count = client. l1_batch_data_count . clone ( ) ;
721784 let mut detector = create_mock_detector ( client, pool) ;
722785 let ( _stop_sender, stop_receiver) = watch:: channel ( false ) ;
723786 detector. run_once ( stop_receiver) . await . unwrap ( ) ;
0 commit comments