Skip to content

Commit 23524f8

Browse files
all: disable recording preimage of trie keys (#21402)
* 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 <peterke@gmail.com>
1 parent 6b98580 commit 23524f8

File tree

15 files changed

+110
-48
lines changed

15 files changed

+110
-48
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ var (
113113
utils.CacheGCFlag,
114114
utils.CacheSnapshotFlag,
115115
utils.CacheNoPrefetchFlag,
116+
utils.CachePreimagesFlag,
116117
utils.ListenPortFlag,
117118
utils.MaxPeersFlag,
118119
utils.MaxPendingPeersFlag,

cmd/geth/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
114114
utils.CacheGCFlag,
115115
utils.CacheSnapshotFlag,
116116
utils.CacheNoPrefetchFlag,
117+
utils.CachePreimagesFlag,
117118
},
118119
},
119120
{

cmd/utils/flags.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ var (
383383
Name: "cache.noprefetch",
384384
Usage: "Disable heuristic state prefetch during block import (less CPU and disk IO, more time waiting for data)",
385385
}
386+
CachePreimagesFlag = cli.BoolTFlag{
387+
Name: "cache.preimages",
388+
Usage: "Enable recording the SHA3/keccak preimages of trie keys (default: true)",
389+
}
386390
// Miner settings
387391
MiningEnabledFlag = cli.BoolFlag{
388392
Name: "mine",
@@ -1526,6 +1530,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15261530
if ctx.GlobalIsSet(CacheNoPrefetchFlag.Name) {
15271531
cfg.NoPrefetch = ctx.GlobalBool(CacheNoPrefetchFlag.Name)
15281532
}
1533+
// Read the value from the flag no matter if it's set or not.
1534+
cfg.Preimages = ctx.GlobalBool(CachePreimagesFlag.Name)
1535+
if cfg.NoPruning && !cfg.Preimages {
1536+
cfg.Preimages = true
1537+
log.Info("Enabling recording of key preimages since archive mode is used")
1538+
}
15291539
if ctx.GlobalIsSet(TxLookupLimitFlag.Name) {
15301540
cfg.TxLookupLimit = ctx.GlobalUint64(TxLookupLimitFlag.Name)
15311541
}
@@ -1835,6 +1845,11 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readOnly bool) (chain *core.B
18351845
TrieDirtyDisabled: ctx.GlobalString(GCModeFlag.Name) == "archive",
18361846
TrieTimeLimit: eth.DefaultConfig.TrieTimeout,
18371847
SnapshotLimit: eth.DefaultConfig.SnapshotCache,
1848+
Preimages: ctx.GlobalBool(CachePreimagesFlag.Name),
1849+
}
1850+
if cache.TrieDirtyDisabled && !cache.Preimages {
1851+
cache.Preimages = true
1852+
log.Info("Enabling recording of key preimages since archive mode is used")
18381853
}
18391854
if !ctx.GlobalIsSet(SnapshotFlag.Name) {
18401855
cache.SnapshotLimit = 0 // Disabled

core/blockchain.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ type CacheConfig struct {
129129
TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node)
130130
TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
131131
SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory
132+
Preimages bool // Whether to store preimage of trie key to the disk
132133

133134
SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it
134135
}
@@ -229,11 +230,15 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
229230
badBlocks, _ := lru.New(badBlockLimit)
230231

231232
bc := &BlockChain{
232-
chainConfig: chainConfig,
233-
cacheConfig: cacheConfig,
234-
db: db,
235-
triegc: prque.New(nil),
236-
stateCache: state.NewDatabaseWithCache(db, cacheConfig.TrieCleanLimit, cacheConfig.TrieCleanJournal),
233+
chainConfig: chainConfig,
234+
cacheConfig: cacheConfig,
235+
db: db,
236+
triegc: prque.New(nil),
237+
stateCache: state.NewDatabaseWithConfig(db, &trie.Config{
238+
Cache: cacheConfig.TrieCleanLimit,
239+
Journal: cacheConfig.TrieCleanJournal,
240+
Preimages: cacheConfig.Preimages,
241+
}),
237242
quit: make(chan struct{}),
238243
shouldPreserve: shouldPreserve,
239244
bodyCache: bodyCache,

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig
175175
// We have the genesis block in database(perhaps in ancient database)
176176
// but the corresponding state is missing.
177177
header := rawdb.ReadHeader(db, stored, 0)
178-
if _, err := state.New(header.Root, state.NewDatabaseWithCache(db, 0, ""), nil); err != nil {
178+
if _, err := state.New(header.Root, state.NewDatabaseWithConfig(db, nil), nil); err != nil {
179179
if genesis == nil {
180180
genesis = DefaultGenesisBlock()
181181
}

core/state/database.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,18 @@ type Trie interface {
104104

105105
// NewDatabase creates a backing store for state. The returned database is safe for
106106
// concurrent use, but does not retain any recent trie nodes in memory. To keep some
107-
// historical state in memory, use the NewDatabaseWithCache constructor.
107+
// historical state in memory, use the NewDatabaseWithConfig constructor.
108108
func NewDatabase(db ethdb.Database) Database {
109-
return NewDatabaseWithCache(db, 0, "")
109+
return NewDatabaseWithConfig(db, nil)
110110
}
111111

112-
// NewDatabaseWithCache creates a backing store for state. The returned database
112+
// NewDatabaseWithConfig creates a backing store for state. The returned database
113113
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
114114
// large memory cache.
115-
func NewDatabaseWithCache(db ethdb.Database, cache int, journal string) Database {
115+
func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
116116
csc, _ := lru.New(codeSizeCacheSize)
117117
return &cachingDB{
118-
db: trie.NewDatabaseWithCache(db, cache, journal),
118+
db: trie.NewDatabaseWithConfig(db, config),
119119
codeSizeCache: csc,
120120
codeCache: fastcache.New(codeCacheSize),
121121
}

core/state/state_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ func newStateTest() *stateTest {
4141
}
4242

4343
func TestDump(t *testing.T) {
44-
s := newStateTest()
44+
db := rawdb.NewMemoryDatabase()
45+
sdb, _ := New(common.Hash{}, NewDatabaseWithConfig(db, nil), nil)
46+
s := &stateTest{db: db, state: sdb}
4547

4648
// generate a few entries
4749
obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))

eth/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (h resultHash) Less(i, j int) bool { return bytes.Compare(h[i].Bytes(), h[j
5858

5959
func TestAccountRange(t *testing.T) {
6060
var (
61-
statedb = state.NewDatabase(rawdb.NewMemoryDatabase())
61+
statedb = state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), nil)
6262
state, _ = state.New(common.Hash{}, statedb, nil)
6363
addrs = [AccountRangeMaxResults * 2]common.Address{}
6464
m = map[common.Address]bool{}

eth/api_tracer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (api *PrivateDebugAPI) traceChain(ctx context.Context, start, end *types.Bl
148148

149149
// Ensure we have a valid starting state before doing any work
150150
origin := start.NumberU64()
151-
database := state.NewDatabaseWithCache(api.eth.ChainDb(), 16, "") // Chain tracing will probably start at genesis
151+
database := state.NewDatabaseWithConfig(api.eth.ChainDb(), &trie.Config{Cache: 16, Preimages: true})
152152

153153
if number := start.NumberU64(); number > 0 {
154154
start = api.eth.blockchain.GetBlock(start.ParentHash(), start.NumberU64()-1)
@@ -659,7 +659,7 @@ func (api *PrivateDebugAPI) computeStateDB(block *types.Block, reexec uint64) (*
659659
}
660660
// Otherwise try to reexec blocks until we find a state or reach our limit
661661
origin := block.NumberU64()
662-
database := state.NewDatabaseWithCache(api.eth.ChainDb(), 16, "")
662+
database := state.NewDatabaseWithConfig(api.eth.ChainDb(), &trie.Config{Cache: 16, Preimages: true})
663663

664664
for i := uint64(0); i < reexec; i++ {
665665
block = api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)

eth/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func New(stack *node.Node, config *Config) (*Ethereum, error) {
169169
TrieDirtyDisabled: config.NoPruning,
170170
TrieTimeLimit: config.TrieTimeout,
171171
SnapshotLimit: config.SnapshotCache,
172+
Preimages: config.Preimages,
172173
}
173174
)
174175
eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, chainConfig, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit)

0 commit comments

Comments
 (0)