Skip to content

Commit

Permalink
check for gaps in block numbers and throw if found during migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alecps committed Dec 10, 2024
1 parent 5374bb9 commit ab0343e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions op-chain-ops/cmd/celo-migrate/ancients.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func readAncientBlocks(ctx context.Context, freezer *rawdb.Freezer, startBlock,
func transformBlocks(ctx context.Context, in <-chan RLPBlockRange, out chan<- RLPBlockRange) error {
// Transform blocks from the in channel and send them to the out channel
defer close(out)

prevBlockNumber := uint64(0)

for blockRange := range in {
select {
case <-ctx.Done():
Expand All @@ -152,6 +155,11 @@ func transformBlocks(ctx context.Context, in <-chan RLPBlockRange, out chan<- RL
for i := range blockRange.hashes {
blockNumber := blockRange.start + uint64(i)

if blockNumber != 0 && blockNumber != prevBlockNumber+1 {
return fmt.Errorf("Gap found between ancient blocks numbered %d and %d. Please delete the target directory and repeat the migration with an uncorrupted source directory.", prevBlockNumber, blockNumber)
}
prevBlockNumber = blockNumber

newHeader, err := transformHeader(blockRange.headers[i])
if err != nil {
return fmt.Errorf("can't transform header: %w", err)
Expand Down
7 changes: 7 additions & 0 deletions op-chain-ops/cmd/celo-migrate/non-ancients.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,18 @@ func migrateNonAncientsDb(newDB ethdb.Database, lastBlock, numAncients, batchSiz
}
}

prevBlockNumber := uint64(numAncients - 1)

for i := numAncients; i <= lastBlock; i += batchSize {
numbersHash := rawdb.ReadAllHashesInRange(newDB, i, i+batchSize-1)

log.Info("Processing Block Range", "process", "non-ancients", "from", i, "to(inclusve)", i+batchSize-1, "count", len(numbersHash))
for _, numberHash := range numbersHash {
if numberHash.Number != 0 && numberHash.Number != prevBlockNumber+1 {
return 0, fmt.Errorf("Gap found between non-ancient blocks numbered %d and %d. Please delete the target directory and repeat the migration with an uncorrupted source directory.", prevBlockNumber, numberHash.Number)
}
prevBlockNumber = numberHash.Number

if err := migrateNonAncientBlock(numberHash.Number, numberHash.Hash, newDB); err != nil {
return 0, err
}
Expand Down

0 comments on commit ab0343e

Please sign in to comment.