Skip to content

Commit

Permalink
Merge pull request #95 from EspressoSystems/fix/seen-at-foreign-key
Browse files Browse the repository at this point in the history
Ensure seen_at blocks exist before inserting virtual batches
  • Loading branch information
jbearer authored Jan 16, 2024
2 parents 013f146 + b32ccd4 commit 5931dcf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
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

0 comments on commit 5931dcf

Please sign in to comment.