Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit de53232

Browse files
committed
add process genesis block
1 parent 30e1deb commit de53232

File tree

2 files changed

+240
-73
lines changed

2 files changed

+240
-73
lines changed

synchronizer/common/syncinterfaces/mocks/etherman_pre_rollup.go

Lines changed: 166 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

synchronizer/synchronizer.go

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
// SequentialMode is the value for L1SynchronizationMode to run in sequential mode
3434
SequentialMode = "sequential"
3535
maxBatchNumber = ^uint64(0)
36+
maxBlockNumber = ^uint64(0)
3637
timeOfLiveBatchOnCache = 5 * time.Minute
3738
autoDiscoverLxLyBlockNumber = uint64(0)
3839
)
@@ -633,40 +634,36 @@ func (s *ClientSynchronizer) RequestAndProcessRollupGenesisBlock(dbTx pgx.Tx, la
633634
log.Error("error getting rollupInfoByBlockRange after set the genesis: ", err)
634635
return err
635636
}
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)
637639
err = sanityCheckForGenesisBlockRollupInfo(blocks, order)
638640
if err != nil {
639641
return err
640642
}
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)
643645
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)
655647
}
648+
656649
return nil
657650
}
658651

659652
func sanityCheckForGenesisBlockRollupInfo(blocks []etherman.Block, order map[common.Hash][]etherman.Order) error {
660653
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")
663656
}
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+
}
667662
}
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
670667
}
671668

672669
// 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
813810
log.Errorf("error creating db transaction to store block. BlockNumber: %d, error: %v", blocks[i].BlockNumber, err)
814811
return err
815812
}
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)
827814
if err != nil {
815+
log.Error("rollingback BlockNumber: %d, because error internalProcessBlock: ", blocks[i].BlockNumber, err)
828816
// 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)
830817
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)
834821
}
835822
return err
836823
}
837824

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-
}
874825
err = dbTx.Commit(s.ctx)
875826
if err != nil {
876-
// If any goes wrong we ensure that the state is rollbacked
877827
log.Errorf("error committing state to store block. BlockNumber: %d, err: %v", blocks[i].BlockNumber, err)
878828
rollbackErr := dbTx.Rollback(s.ctx)
879829
if rollbackErr != nil {
@@ -886,6 +836,57 @@ func (s *ClientSynchronizer) ProcessBlockRange(blocks []etherman.Block, order ma
886836
return nil
887837
}
888838

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+
889890
func (s *ClientSynchronizer) syncTrustedState(latestSyncedBatch uint64) error {
890891
if s.syncTrustedStateExecutor == nil {
891892
return nil

0 commit comments

Comments
 (0)