From b2351da9d6f571cbc1fef36ebcc5f21bafb12f22 Mon Sep 17 00:00:00 2001 From: gary rong Date: Wed, 18 Nov 2020 17:51:33 +0800 Subject: [PATCH] all: disable recording preimage of trie keys (#21402) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * cmd, core, eth, light, trie: disable recording preimage by default * core, eth: fix unit tests * core: fix import * all: change to nopreimage * cmd, core, eth, trie: use cache.preimages flag * cmd: enable preimages for archive node * cmd/utils, trie: simplify preimage tracking a bit * core: fix linter Co-authored-by: Péter Szilágyi # Conflicts: # cmd/geth/main.go # cmd/geth/usage.go # cmd/utils/flags.go # core/blockchain.go # core/genesis.go # core/state/database.go # core/state/state_test.go # eth/api_test.go # eth/api_tracer.go # eth/backend.go # light/postprocess.go # trie/secure_trie.go # turbo/trie/database.go --- cmd/utils/flags.go | 2 ++ eth/config.go | 1 + eth/gen_config.go | 6 ++++++ turbo/trie/database.go | 15 ++++++++++----- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 730cd04bb..10e713609 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1488,9 +1488,11 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { cfg.BlocksToPrune = ctx.GlobalUint64(GCModeBlockToPruneFlag.Name) cfg.PruningTimeout = ctx.GlobalDuration(GCModeTickTimeout.Name) + // Read the value from the flag no matter if it's set or not. cfg.DownloadOnly = ctx.GlobalBoolT(DownloadOnlyFlag.Name) cfg.EnableDebugProtocol = ctx.GlobalBool(DebugProtocolFlag.Name) + log.Info("Enabling recording of key preimages since archive mode is used") cfg.ArchiveSyncInterval = ctx.GlobalInt(ArchiveSyncInterval.Name) diff --git a/eth/config.go b/eth/config.go index e3f059518..15e445bbf 100644 --- a/eth/config.go +++ b/eth/config.go @@ -172,6 +172,7 @@ type Config struct { TrieDirtyCache int TrieTimeout time.Duration SnapshotCache int + Preimages bool // Mining options Miner miner.Config diff --git a/eth/gen_config.go b/eth/gen_config.go index 542015bee..2533b2a8d 100644 --- a/eth/gen_config.go +++ b/eth/gen_config.go @@ -43,6 +43,7 @@ func (c Config) MarshalTOML() (interface{}, error) { TrieDirtyCache int TrieTimeout time.Duration SnapshotCache int + Preimages bool Miner miner.Config Ethash ethash.Config TxPool core.TxPoolConfig @@ -81,6 +82,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.TrieDirtyCache = c.TrieDirtyCache enc.TrieTimeout = c.TrieTimeout enc.SnapshotCache = c.SnapshotCache + enc.Preimages = c.Preimages enc.Miner = c.Miner enc.Ethash = c.Ethash enc.TxPool = c.TxPool @@ -124,6 +126,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { TrieDirtyCache *int TrieTimeout *time.Duration SnapshotCache *int + Preimages *bool Miner *miner.Config Ethash *ethash.Config TxPool *core.TxPoolConfig @@ -217,6 +220,9 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.SnapshotCache != nil { c.SnapshotCache = *dec.SnapshotCache } + if dec.Preimages != nil { + c.Preimages = *dec.Preimages + } if dec.Miner != nil { c.Miner = *dec.Miner } diff --git a/turbo/trie/database.go b/turbo/trie/database.go index db6b4fb9d..0a31e24fb 100644 --- a/turbo/trie/database.go +++ b/turbo/trie/database.go @@ -208,16 +208,16 @@ func NewDatabase(diskdb ethdb.Database) *Database { return NewDatabaseWithCache(diskdb, 0, "") } -// NewDatabaseWithCache creates a new trie database to store ephemeral trie content +// NewDatabaseWithConfig creates a new trie database to store ephemeral trie content // before its written out to disk or garbage collected. It also acts as a read cache // for nodes loaded from disk. func NewDatabaseWithCache(diskdb ethdb.Database, cache int, journal string) *Database { var cleans *fastcache.Cache - if cache > 0 { - if journal == "" { - cleans = fastcache.New(cache * 1024 * 1024) + if config != nil && config.Cache > 0 { + if config.Journal == "" { + cleans = fastcache.New(config.Cache * 1024 * 1024) } else { - cleans = fastcache.LoadFromFileOrNew(journal, cache*1024*1024) + cleans = fastcache.LoadFromFileOrNew(config.Journal, config.Cache*1024*1024) } } return &Database{ @@ -250,6 +250,11 @@ func (db *Database) InsertBlob(hash common.Hash, blob []byte) { // // Note, this method assumes that the database's lock is held! func (db *Database) insertPreimage(hash common.Hash, preimage []byte) { + // Short circuit if preimage collection is disabled + if db.preimages == nil { + return + } + // Track the preimage if a yet unknown one if _, ok := db.preimages[hash]; ok { return }