Skip to content
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
15 changes: 15 additions & 0 deletions op-chain-ops/cmd/celo-migrate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
)
Expand Down Expand Up @@ -98,3 +99,17 @@ func removeBlocks(ldb ethdb.Database, numberHashes []*rawdb.NumberHash) error {

return nil
}

func getHeadHeader(dbpath string) (*types.Header, error) {
db, err := openDBWithoutFreezer(dbpath, true)
if err != nil {
return nil, fmt.Errorf("failed to open database at %q err: %w", dbpath, err)
}
defer db.Close()

headHeader := rawdb.ReadHeadHeader(db)
if headHeader == nil {
return nil, fmt.Errorf("head header not in database at: %s", dbpath)
}
return headHeader, nil
}
21 changes: 19 additions & 2 deletions op-chain-ops/cmd/celo-migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ var (
Usage: "Path to write the genesis JSON file, to be used to sync new nodes",
Required: true,
}
migrationBlockNumberFlag = &cli.Uint64Flag{
Name: "migration-block-number",
Usage: "Specifies the migration block number. If the source db is not synced exactly to the block immediately before this number (i.e. migration-block-number - 1), the migration will fail.",
Required: true,
}
migrationBlockTimeFlag = &cli.Uint64Flag{
Name: "migration-block-time",
Usage: "Specifies a unix timestamp to use for the migration block. If not provided, the current time will be used.",
Expand Down Expand Up @@ -106,6 +111,7 @@ var (
outfileRollupConfigFlag,
outfileGenesisFlag,
migrationBlockTimeFlag,
migrationBlockNumberFlag,
)
)

Expand All @@ -130,6 +136,7 @@ type stateMigrationOptions struct {
type fullMigrationOptions struct {
preMigrationOptions
stateMigrationOptions
migrationBlockNumber uint64
}

func parsePreMigrationOptions(ctx *cli.Context) preMigrationOptions {
Expand Down Expand Up @@ -158,6 +165,7 @@ func parseFullMigrationOptions(ctx *cli.Context) fullMigrationOptions {
return fullMigrationOptions{
preMigrationOptions: parsePreMigrationOptions(ctx),
stateMigrationOptions: parseStateMigrationOptions(ctx),
migrationBlockNumber: ctx.Uint64(migrationBlockNumberFlag.Name),
}
}

Expand Down Expand Up @@ -215,7 +223,16 @@ func runFullMigration(opts fullMigrationOptions) error {

log.Info("Full Migration Started", "oldDBPath", opts.oldDBPath, "newDBPath", opts.newDBPath)

var err error
head, err := getHeadHeader(opts.oldDBPath)
if err != nil {
return fmt.Errorf("failed to get head header: %w", err)
}
if head.Number.Uint64() != opts.migrationBlockNumber-1 {
return fmt.Errorf("old-db head block number not synced to the block immediately before the migration block number: %d != %d", head.Number.Uint64(), opts.migrationBlockNumber-1)
}

log.Info("Source db is synced to correct height", "head", head.Number.Uint64(), "migrationBlock", opts.migrationBlockNumber)

var numAncients uint64
var strayAncientBlocks []*rawdb.NumberHash

Expand All @@ -226,7 +243,7 @@ func runFullMigration(opts fullMigrationOptions) error {
if err = runNonAncientMigration(opts.newDBPath, strayAncientBlocks, opts.batchSize, numAncients); err != nil {
return fmt.Errorf("failed to run non-ancient migration: %w", err)
}
if err := runStateMigration(opts.newDBPath, opts.stateMigrationOptions); err != nil {
if err = runStateMigration(opts.newDBPath, opts.stateMigrationOptions); err != nil {
return fmt.Errorf("failed to run state migration: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion op-chain-ops/cmd/celo-migrate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func applyStateMigrationChanges(config *genesis.DeployConfig, l2Allocs types.Gen
if num == nil {
return nil, fmt.Errorf("cannot find header number for %s", hash)
}
log.Info("Reading chain tip num from database", "number", num)
log.Info("Reading chain tip num from database", "number", *num)

// Grab the full header.
header := rawdb.ReadHeader(ldb, hash, *num)
Expand Down