From f5266f3e8f00ff52daec34a9e19c9b1cc1823b58 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 6 Dec 2024 18:43:03 +0100 Subject: [PATCH] cmd/{geth,utils}: read genesis block from db in chain import Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> --- cmd/geth/chaincmd.go | 8 +++++++- cmd/utils/flags.go | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index f6dc1cf4bf..842d1ffe04 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -290,7 +290,13 @@ func importChain(ctx *cli.Context) error { stack, _ := makeConfigNode(ctx) defer stack.Close() - chain, db := utils.MakeChain(ctx, stack, false) + db := utils.MakeChainDatabase(ctx, stack, false) + + // if the error is not nil, no genesis is present in the db and so the default + // behavior of creating the genesis block applies. + genesis, _ := core.ReadGenesis(db) + + chain, db := utils.MakeChainWithGenesisBlockAndChainDB(genesis, db, ctx, stack, false) defer db.Close() // Start periodically gathering memory profiles diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 9f309286bd..7ed6aae168 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -2138,6 +2138,10 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh gspec = MakeGenesis(ctx) chainDb = MakeChainDatabase(ctx, stack, readonly) ) + return MakeChainWithGenesisBlockAndChainDB(gspec, chainDb, ctx, stack, readonly) +} + +func MakeChainWithGenesisBlockAndChainDB(gspec *core.Genesis, chainDb ethdb.Database, ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockChain, ethdb.Database) { config, err := core.LoadChainConfig(chainDb, gspec) if err != nil { Fatalf("%v", err)