From 6069d8294e2e52d6a18c8e9661302e15ddaaf9f8 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Wed, 19 Oct 2022 08:20:39 +0200 Subject: [PATCH] cmd/utils: enable snapshot generation in import-mode (#25990) This PR fixes a regression causing snapshots not to be generated in "geth --import" mode. It also fixes the geth export command to be truly readonly, and adds a new test for geth export. --- cmd/geth/chaincmd.go | 4 ++-- cmd/geth/exportcmd_test.go | 45 ++++++++++++++++++++++++++++++++++++++ cmd/utils/flags.go | 10 +++++---- core/genesis.go | 6 ++++- 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 cmd/geth/exportcmd_test.go diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index c89f73616..48b21ddbf 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -252,7 +252,7 @@ func importChain(ctx *cli.Context) error { stack, _ := makeConfigNode(ctx) defer stack.Close() - chain, db := utils.MakeChain(ctx, stack) + chain, db := utils.MakeChain(ctx, stack, false) defer db.Close() // Start periodically gathering memory profiles @@ -327,7 +327,7 @@ func exportChain(ctx *cli.Context) error { stack, _ := makeConfigNode(ctx) defer stack.Close() - chain, _ := utils.MakeChain(ctx, stack) + chain, _ := utils.MakeChain(ctx, stack, true) start := time.Now() var err error diff --git a/cmd/geth/exportcmd_test.go b/cmd/geth/exportcmd_test.go new file mode 100644 index 000000000..bbf08d820 --- /dev/null +++ b/cmd/geth/exportcmd_test.go @@ -0,0 +1,45 @@ +// Copyright 2022 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import ( + "bytes" + "fmt" + "os" + "testing" + + "github.com/ethereum/go-ethereum/common" +) + +// TestExport does a basic test of "geth export", exporting the test-genesis. +func TestExport(t *testing.T) { + outfile := fmt.Sprintf("%v/testExport.out", os.TempDir()) + defer os.Remove(outfile) + geth := runGeth(t, "--datadir", initGeth(t), "export", outfile) + geth.WaitExit() + if have, want := geth.ExitStatus(), 0; have != want { + t.Errorf("exit error, have %d want %d", have, want) + } + have, err := os.ReadFile(outfile) + if err != nil { + t.Fatal(err) + } + want := common.FromHex("0xf9026bf90266a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a08758259b018f7bce3d2be2ddb62f325eaeea0a0c188cf96623eab468a4413e03a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000180837a12008080b875000000000000000000000000000000000000000000000000000000000000000002f0d131f1f97aef08aec6e3291b957d9efe71050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000880000000000000000c0c0") + if !bytes.Equal(have, want) { + t.Fatalf("wrong content exported") + } +} diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 4c4dc9df1..dafa2dd80 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -2217,10 +2217,10 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis { } // MakeChain creates a chain manager from set command line flags. -func MakeChain(ctx *cli.Context, stack *node.Node) (*core.BlockChain, ethdb.Database) { +func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockChain, ethdb.Database) { var ( gspec = MakeGenesis(ctx) - chainDb = MakeChainDatabase(ctx, stack, false) // TODO(rjl493456442) support read-only database + chainDb = MakeChainDatabase(ctx, stack, readonly) ) cliqueConfig, err := core.LoadCliqueConfig(chainDb, gspec) if err != nil { @@ -2250,8 +2250,10 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (*core.BlockChain, ethdb.Data if !ctx.Bool(SnapshotFlag.Name) { cache.SnapshotLimit = 0 // Disabled } - // Disable snapshot generation/wiping by default - cache.SnapshotNoBuild = true + // If we're in readonly, do not bother generating snapshot data. + if readonly { + cache.SnapshotNoBuild = true + } if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheTrieFlag.Name) { cache.TrieCleanLimit = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100 diff --git a/core/genesis.go b/core/genesis.go index 8b8551479..b5f844724 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -358,6 +358,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override rawdb.WriteChainConfig(db, stored, newcfg) return newcfg, stored, nil } + storedData, _ := json.Marshal(storedcfg) // Special case: if a private network is being used (no genesis and also no // mainnet hash in the database), we must not apply the `configOrDefault` // chain config as that would be AllProtocolChanges (applying any new fork @@ -377,7 +378,10 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override if compatErr != nil && *height != 0 && compatErr.RewindTo != 0 { return newcfg, stored, compatErr } - rawdb.WriteChainConfig(db, stored, newcfg) + // Don't overwrite if the old is identical to the new + if newData, _ := json.Marshal(newcfg); !bytes.Equal(storedData, newData) { + rawdb.WriteChainConfig(db, stored, newcfg) + } return newcfg, stored, nil }