@@ -33,6 +33,7 @@ const (
33
33
// SequentialMode is the value for L1SynchronizationMode to run in sequential mode
34
34
SequentialMode = "sequential"
35
35
maxBatchNumber = ^ uint64 (0 )
36
+ maxBlockNumber = ^ uint64 (0 )
36
37
timeOfLiveBatchOnCache = 5 * time .Minute
37
38
autoDiscoverLxLyBlockNumber = uint64 (0 )
38
39
)
@@ -633,40 +634,36 @@ func (s *ClientSynchronizer) RequestAndProcessRollupGenesisBlock(dbTx pgx.Tx, la
633
634
log .Error ("error getting rollupInfoByBlockRange after set the genesis: " , err )
634
635
return err
635
636
}
636
- // Check that the response is the expected. It should be 1 block with 2 orders
637
+ // Check that the response is the expected. It should be 1 block with ForkID event
638
+ log .Debugf ("SanityCheck for genesis block (%d) events: %+v" , lastEthBlockSynced .BlockNumber , order )
637
639
err = sanityCheckForGenesisBlockRollupInfo (blocks , order )
638
640
if err != nil {
639
641
return err
640
642
}
641
- forkId := s . state . GetForkIDByBlockNumber ( blocks [ 0 ]. BlockNumber )
642
- err = s .l1EventProcessors . Process ( s . ctx , actions . ForkIdType ( forkId ), etherman. Order { Name : etherman . ForkIDsOrder , Pos : 0 }, & blocks [0 ], dbTx )
643
+ log . Infof ( "Processing genesis block %d orders: %+v" , lastEthBlockSynced . BlockNumber , order )
644
+ err = s .internalProcessBlock ( maxBlockNumber , blocks [ 0 ], order [ blocks [0 ]. BlockHash ], false , dbTx )
643
645
if err != nil {
644
- log .Error ("error storing genesis forkID: " , err )
645
- return err
646
- }
647
- if len (blocks [0 ].SequencedBatches ) != 0 {
648
- batchSequence := l1event_orders .GetSequenceFromL1EventOrder (etherman .InitialSequenceBatchesOrder , & blocks [0 ], 0 )
649
- forkId = s .state .GetForkIDByBatchNumber (batchSequence .FromBatchNumber )
650
- err = s .l1EventProcessors .Process (s .ctx , actions .ForkIdType (forkId ), etherman.Order {Name : etherman .InitialSequenceBatchesOrder , Pos : 0 }, & blocks [0 ], dbTx )
651
- if err != nil {
652
- log .Error ("error storing initial tx (batch 1): " , err )
653
- return err
654
- }
646
+ log .Errorf ("error processinge events on genesis block %d: err:%w" , lastEthBlockSynced .BlockNumber , err )
655
647
}
648
+
656
649
return nil
657
650
}
658
651
659
652
func sanityCheckForGenesisBlockRollupInfo (blocks []etherman.Block , order map [common.Hash ][]etherman.Order ) error {
660
653
if len (blocks ) != 1 || len (order ) < 1 || len (order [blocks [0 ].BlockHash ]) < 1 {
661
- log .Errorf ("error getting rollupInfoByBlockRange after set the genesis. Expected 1 block with 2 orders" )
662
- return fmt .Errorf ("error getting rollupInfoByBlockRange after set the genesis. Expected 1 block with 2 orders" )
654
+ log .Errorf ("error getting rollupInfoByBlockRange after set the genesis. Expected 1 block with minimum 2 orders" )
655
+ return fmt .Errorf ("error getting rollupInfoByBlockRange after set the genesis. Expected 1 block with minimum 2 orders" )
663
656
}
664
- if order [blocks [0 ].BlockHash ][0 ].Name != etherman .ForkIDsOrder {
665
- log .Errorf ("error getting rollupInfoByBlockRange after set the genesis. Expected ForkIDsOrder, got %s" , order [blocks [0 ].BlockHash ][0 ].Name )
666
- return fmt .Errorf ("error getting rollupInfoByBlockRange after set the genesis. Expected ForkIDsOrder" )
657
+ // The genesis block implies 1 ForkID event
658
+ for _ , value := range order [blocks [0 ].BlockHash ] {
659
+ if value .Name == etherman .ForkIDsOrder {
660
+ return nil
661
+ }
667
662
}
668
-
669
- return nil
663
+ err := fmt .Errorf ("events on genesis block (%d) need a ForkIDsOrder event but this block got %+v" ,
664
+ blocks [0 ].BlockNumber , order [blocks [0 ].BlockHash ])
665
+ log .Error (err .Error ())
666
+ return err
670
667
}
671
668
672
669
// This function syncs the node from a specific block to the latest
@@ -813,67 +810,20 @@ func (s *ClientSynchronizer) ProcessBlockRange(blocks []etherman.Block, order ma
813
810
log .Errorf ("error creating db transaction to store block. BlockNumber: %d, error: %v" , blocks [i ].BlockNumber , err )
814
811
return err
815
812
}
816
- b := state.Block {
817
- BlockNumber : blocks [i ].BlockNumber ,
818
- BlockHash : blocks [i ].BlockHash ,
819
- ParentHash : blocks [i ].ParentHash ,
820
- ReceivedAt : blocks [i ].ReceivedAt ,
821
- }
822
- if blocks [i ].BlockNumber <= finalizedBlockNumber {
823
- b .Checked = true
824
- }
825
- // Add block information
826
- err = s .state .AddBlock (s .ctx , & b , dbTx )
813
+ err = s .internalProcessBlock (finalizedBlockNumber , blocks [i ], order [blocks [i ].BlockHash ], true , dbTx )
827
814
if err != nil {
815
+ log .Error ("rollingback BlockNumber: %d, because error internalProcessBlock: " , blocks [i ].BlockNumber , err )
828
816
// If any goes wrong we ensure that the state is rollbacked
829
- log .Errorf ("error storing block. BlockNumber: %d, error: %v" , blocks [i ].BlockNumber , err )
830
817
rollbackErr := dbTx .Rollback (s .ctx )
831
- if rollbackErr != nil {
832
- log .Errorf ("error rolling back state to store block. BlockNumber: %d, rollbackErr: %s, error : %v" , blocks [i ].BlockNumber , rollbackErr .Error (), err )
833
- return rollbackErr
818
+ if rollbackErr != nil && ! errors . Is ( rollbackErr , pgx . ErrTxClosed ) {
819
+ log .Warnf ("error rolling back state to store block. BlockNumber: %d, rollbackErr: %s, error : %v" , blocks [i ].BlockNumber , rollbackErr .Error (), err )
820
+ return fmt . Errorf ( "error rollback BlockNumber: %d: err:%w original error:%w" , blocks [ i ]. BlockNumber , rollbackErr , err )
834
821
}
835
822
return err
836
823
}
837
824
838
- for _ , element := range order [blocks [i ].BlockHash ] {
839
- batchSequence := l1event_orders .GetSequenceFromL1EventOrder (element .Name , & blocks [i ], element .Pos )
840
- var forkId uint64
841
- if batchSequence != nil {
842
- forkId = s .state .GetForkIDByBatchNumber (batchSequence .FromBatchNumber )
843
- log .Debug ("EventOrder: " , element .Name , ". Batch Sequence: " , batchSequence , "forkId: " , forkId )
844
- } else {
845
- forkId = s .state .GetForkIDByBlockNumber (blocks [i ].BlockNumber )
846
- log .Debug ("EventOrder: " , element .Name , ". BlockNumber: " , blocks [i ].BlockNumber , ". forkId: " , forkId )
847
- }
848
- forkIdTyped := actions .ForkIdType (forkId )
849
- // Process event received from l1
850
- err := s .l1EventProcessors .Process (s .ctx , forkIdTyped , element , & blocks [i ], dbTx )
851
- if err != nil {
852
- log .Error ("error: " , err )
853
- // If any goes wrong we ensure that the state is rollbacked
854
- rollbackErr := dbTx .Rollback (s .ctx )
855
- if rollbackErr != nil && ! errors .Is (rollbackErr , pgx .ErrTxClosed ) {
856
- log .Warnf ("error rolling back state to store block. BlockNumber: %d, rollbackErr: %s, error : %v" , blocks [i ].BlockNumber , rollbackErr .Error (), err )
857
- return rollbackErr
858
- }
859
- return err
860
- }
861
- }
862
- log .Debug ("Checking FlushID to commit L1 data to db" )
863
- err = s .checkFlushID (dbTx )
864
- if err != nil {
865
- // If any goes wrong we ensure that the state is rollbacked
866
- log .Errorf ("error checking flushID. Error: %v" , err )
867
- rollbackErr := dbTx .Rollback (s .ctx )
868
- if rollbackErr != nil {
869
- log .Errorf ("error rolling back state. RollbackErr: %s, Error : %v" , rollbackErr .Error (), err )
870
- return rollbackErr
871
- }
872
- return err
873
- }
874
825
err = dbTx .Commit (s .ctx )
875
826
if err != nil {
876
- // If any goes wrong we ensure that the state is rollbacked
877
827
log .Errorf ("error committing state to store block. BlockNumber: %d, err: %v" , blocks [i ].BlockNumber , err )
878
828
rollbackErr := dbTx .Rollback (s .ctx )
879
829
if rollbackErr != nil {
@@ -886,6 +836,57 @@ func (s *ClientSynchronizer) ProcessBlockRange(blocks []etherman.Block, order ma
886
836
return nil
887
837
}
888
838
839
+ // internalProcessBlock process one iteration of events and stores the information in the db
840
+ func (s * ClientSynchronizer ) internalProcessBlock (finalizedBlockNumber uint64 , blocks etherman.Block , order []etherman.Order , addBlock bool , dbTx pgx.Tx ) error {
841
+ var err error
842
+ // New info has to be included into the db using the state
843
+ if addBlock {
844
+ b := state.Block {
845
+ BlockNumber : blocks .BlockNumber ,
846
+ BlockHash : blocks .BlockHash ,
847
+ ParentHash : blocks .ParentHash ,
848
+ ReceivedAt : blocks .ReceivedAt ,
849
+ }
850
+ if blocks .BlockNumber <= finalizedBlockNumber {
851
+ b .Checked = true
852
+ }
853
+ // Add block information
854
+ log .Debugf ("Storing block. Block: %s" , b .String ())
855
+ err = s .state .AddBlock (s .ctx , & b , dbTx )
856
+ if err != nil {
857
+ log .Errorf ("error storing block. BlockNumber: %d, error: %v" , blocks .BlockNumber , err )
858
+ return err
859
+ }
860
+ }
861
+
862
+ for _ , element := range order {
863
+ batchSequence := l1event_orders .GetSequenceFromL1EventOrder (element .Name , & blocks , element .Pos )
864
+ var forkId uint64
865
+ if batchSequence != nil {
866
+ forkId = s .state .GetForkIDByBatchNumber (batchSequence .FromBatchNumber )
867
+ log .Debug ("EventOrder: " , element .Name , ". Batch Sequence: " , batchSequence , "forkId: " , forkId )
868
+ } else {
869
+ forkId = s .state .GetForkIDByBlockNumber (blocks .BlockNumber )
870
+ log .Debug ("EventOrder: " , element .Name , ". BlockNumber: " , blocks .BlockNumber , "forkId: " , forkId )
871
+ }
872
+ forkIdTyped := actions .ForkIdType (forkId )
873
+ // Process event received from l1
874
+ err := s .l1EventProcessors .Process (s .ctx , forkIdTyped , element , & blocks , dbTx )
875
+ if err != nil {
876
+ log .Error ("1EventProcessors.Process error: " , err )
877
+ return err
878
+ }
879
+ }
880
+ log .Debug ("Checking FlushID to commit L1 data to db" )
881
+ err = s .checkFlushID (dbTx )
882
+ if err != nil {
883
+ log .Errorf ("error checking flushID. Error: %v" , err )
884
+ return err
885
+ }
886
+
887
+ return nil
888
+ }
889
+
889
890
func (s * ClientSynchronizer ) syncTrustedState (latestSyncedBatch uint64 ) error {
890
891
if s .syncTrustedStateExecutor == nil {
891
892
return nil
0 commit comments