Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure seen_at blocks exist before inserting virtual batches #95

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, db
TxHash: ZeroHash,
Coinbase: ZeroAddress,
BlockNumber: block.BlockNumber,
SeenAt: block.BlockNumber,
}
err = s.AddVirtualBatch(ctx, virtualBatch, dbTx)
if err != nil {
Expand Down
80 changes: 46 additions & 34 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,47 +641,18 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
return nil
}

if err := s.ensureBlockExists(dbTx, batchesSeenAtBlock); err != nil {
return err
}

for _, sbatch := range sequencedBatches {
// Ensure the L1 origin for this batch is in the database. Since the L1 origin assigned by
// HotShot is not necessarily the same as an L1 block which we added to the database as a
// result of receiving a contract event, it might not be.
blockNumber := sbatch.BlockNumber
exists, err := s.state.ContainsBlock(s.ctx, blockNumber, dbTx)
if err != nil {
log.Errorf("error fetching L1 block %d from db: %v", blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Fatalf("error rolling back state. BlockNumber: %d, rollbackErr: %s, error: %w", blockNumber, rollbackErr.Error(), err)
}
if err := s.ensureBlockExists(dbTx, blockNumber); err != nil {
return err
}
if !exists {
header, err := s.etherMan.HeaderByNumber(s.ctx, big.NewInt(int64(blockNumber)))
if err != nil {
log.Errorf("error fetching L1 block %d: %v", blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Fatalf("error rolling back state. BlockNumber: %d, rollbackErr: %s, error: %w", blockNumber, rollbackErr.Error(), err)
}
return err
}
b := state.Block{
BlockNumber: blockNumber,
BlockHash: header.Hash(),
ParentHash: header.ParentHash,
ReceivedAt: time.Unix(int64(header.Time), 0),
}
log.Infof("L1 block %d does not already exist, adding to database", blockNumber)
err = s.state.AddBlock(s.ctx, &b, dbTx)
if err != nil {
log.Errorf("error storing block. BlockNumber: %d, error: %w", blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Fatalf("error rolling back state to store block. BlockNumber: %d, rollbackErr: %s, error : %w", blockNumber, rollbackErr.Error(), err)
}
return err
}
}

virtualBatch := state.VirtualBatch{
BatchNumber: sbatch.BatchNumber,
Expand Down Expand Up @@ -796,6 +767,47 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
return nil
}

func (s *ClientSynchronizer) ensureBlockExists(dbTx pgx.Tx, blockNumber uint64) error {
exists, err := s.state.ContainsBlock(s.ctx, blockNumber, dbTx)
if err != nil {
log.Errorf("error fetching L1 block %d from db: %v", blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Fatalf("error rolling back state. BlockNumber: %d, rollbackErr: %s, error: %w", blockNumber, rollbackErr.Error(), err)
}
return err
}
if !exists {
header, err := s.etherMan.HeaderByNumber(s.ctx, big.NewInt(int64(blockNumber)))
if err != nil {
log.Errorf("error fetching L1 block %d: %v", blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Fatalf("error rolling back state. BlockNumber: %d, rollbackErr: %s, error: %w", blockNumber, rollbackErr.Error(), err)
}
return err
}
b := state.Block{
BlockNumber: blockNumber,
BlockHash: header.Hash(),
ParentHash: header.ParentHash,
ReceivedAt: time.Unix(int64(header.Time), 0),
}
log.Infof("L1 block %d does not already exist, adding to database", blockNumber)
err = s.state.AddBlock(s.ctx, &b, dbTx)
if err != nil {
log.Errorf("error storing block. BlockNumber: %d, error: %w", blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Fatalf("error rolling back state to store block. BlockNumber: %d, rollbackErr: %s, error : %w", blockNumber, rollbackErr.Error(), err)
}
return err
}
}

return nil
}

func (s *ClientSynchronizer) processForcedBatch(forcedBatch etherman.ForcedBatch, dbTx pgx.Tx) error {
// Store forced batch into the db
forcedB := state.ForcedBatch{
Expand Down