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

Add continuity script to pre-migration #347

Merged
merged 2 commits into from
Mar 6, 2025
Merged
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
45 changes: 43 additions & 2 deletions op-chain-ops/cmd/celo-migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ var (
Usage: "Path to the db to perform a continuity check on",
Required: true,
}
dbCheckStartFlag = &cli.Uint64Flag{
Name: "start",
Usage: "Block number to start the db check from. If not set, the db check will start from block 0.",
Value: 0,
}
dbCheckFailFastFlag = &cli.BoolFlag{
Name: "fail-fast",
Usage: "Fail fast on the first error encountered. If set, the db check will stop on the first error encountered, otherwise it will continue to check all blocks and print out all errors at the end.",
Expand Down Expand Up @@ -169,6 +174,7 @@ type fullMigrationOptions struct {

type dbCheckOptions struct {
dbPath string
start uint64
batchSize uint64
failFast bool
}
Expand Down Expand Up @@ -209,6 +215,7 @@ func parseDBCheckOptions(ctx *cli.Context) dbCheckOptions {
dbPath: ctx.String(dbCheckPathFlag.Name),
batchSize: ctx.Uint64(batchSizeFlag.Name),
failFast: ctx.Bool(dbCheckFailFastFlag.Name),
start: ctx.Uint64(dbCheckStartFlag.Name),
}
}

Expand Down Expand Up @@ -333,6 +340,11 @@ func runPreMigration(opts preMigrationOptions) ([]*rawdb.NumberHash, uint64, err
}
}

err = runDBCheckFromLastMigrated(opts)
if err != nil {
return nil, 0, fmt.Errorf("failed to run db check from last migrated block: %w", err)
}

var numAncientsNewBefore uint64
var numAncientsNewAfter uint64
var strayAncientBlocks []*rawdb.NumberHash
Expand Down Expand Up @@ -617,8 +629,8 @@ func runDBCheck(opts dbCheckOptions) (err error) {
return nil
}

log.Info("Checking continuity of ancient blocks", "start", 0, "end", lastAncientNumber, "count", lastAncientNumber+1)
if err := checkContinuity(0, lastAncientNumber, func(start, count uint64) (*RLPBlockRange, error) {
log.Info("Checking continuity of ancient blocks", "start", opts.start, "end", lastAncientNumber, "count", lastAncientNumber-opts.start+1)
if err := checkContinuity(opts.start, lastAncientNumber, func(start, count uint64) (*RLPBlockRange, error) {
return loadAncientRange(ancientDB, start, count)
}); err != nil {
return err
Expand All @@ -643,6 +655,35 @@ func runDBCheck(opts dbCheckOptions) (err error) {
return nil
}

func runDBCheckFromLastMigrated(opts preMigrationOptions) (err error) {
newFreezer, err := NewChainFreezer(filepath.Join(opts.newDBPath, "ancient"), "", false)
if err != nil {
return fmt.Errorf("failed to open new freezer: %w", err)
}
numAncientsInNewDB, err := newFreezer.Ancients()
if err != nil {
return fmt.Errorf("failed to get number of ancients in new freezer: %w", err)
}
err = newFreezer.Close()
if err != nil {
return fmt.Errorf("failed to close new freezer: %w", err)
}

var start uint64
if numAncientsInNewDB == 0 {
start = 0
} else {
start = numAncientsInNewDB - 1
}

err = runDBCheck(dbCheckOptions{dbPath: opts.oldDBPath, start: start, batchSize: opts.batchSize, failFast: true})
if err != nil {
return fmt.Errorf("failed to run db continuity check: %w", err)
}

return nil
}

func timer(name string) func() {
start := time.Now()
return func() {
Expand Down