Skip to content

Commit c5fbc6e

Browse files
committed
all: disable recording preimage of trie keys ethereum#21402
1 parent ae70b5d commit c5fbc6e

File tree

12 files changed

+112
-48
lines changed

12 files changed

+112
-48
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ func NewXDCSimulatedBackend(alloc types.GenesisAlloc, gasLimit uint64, chainConf
138138
return lendingServ
139139
}
140140

141-
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, consensus, vm.Config{})
141+
cacheConfig := &core.CacheConfig{
142+
TrieCleanLimit: 256,
143+
TrieDirtyLimit: 256,
144+
TrieTimeLimit: 5 * time.Minute,
145+
Preimages: true,
146+
}
147+
blockchain, _ := core.NewBlockChain(database, cacheConfig, genesis.Config, consensus, vm.Config{})
142148

143149
backend := &SimulatedBackend{
144150
database: database,

cmd/XDC/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ var (
100100
utils.CacheGCFlag,
101101
utils.CachePrefetchFlag,
102102
//utils.TrieCacheGenFlag,
103+
utils.CachePreimagesFlag,
103104
utils.CacheLogSizeFlag,
104105
utils.FDLimitFlag,
105106
utils.CryptoKZGFlag,

cmd/utils/flags.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ var (
308308
Usage: "Enable heuristic state prefetch during block import",
309309
Category: flags.PerfCategory,
310310
}
311+
CachePreimagesFlag = &cli.BoolFlag{
312+
Name: "cache-preimages",
313+
Usage: "Enable recording the SHA3/keccak preimages of trie keys (default: true)",
314+
Value: true,
315+
Category: flags.PerfCategory,
316+
}
311317
CacheLogSizeFlag = &cli.IntFlag{
312318
Name: "cache-blocklogs",
313319
Aliases: []string{"cache.blocklogs"},
@@ -1513,7 +1519,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
15131519
}
15141520
cfg.NoPruning = ctx.String(GCModeFlag.Name) == "archive"
15151521
cfg.NoPrefetch = !ctx.Bool(CachePrefetchFlag.Name)
1516-
1522+
// Read the value from the flag no matter if it's set or not.
1523+
cfg.Preimages = ctx.Bool(CachePreimagesFlag.Name)
1524+
if cfg.NoPruning && !cfg.Preimages {
1525+
cfg.Preimages = true
1526+
log.Info("Enabling recording of key preimages since archive mode is used")
1527+
}
15171528
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheTrieFlag.Name) {
15181529
cfg.TrieCleanCache = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100
15191530
}
@@ -1780,6 +1791,11 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (chain *core.B
17801791
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
17811792
TrieDirtyDisabled: ctx.String(GCModeFlag.Name) == "archive",
17821793
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
1794+
Preimages: ctx.Bool(CachePreimagesFlag.Name),
1795+
}
1796+
if cache.TrieDirtyDisabled && !cache.Preimages {
1797+
cache.Preimages = true
1798+
log.Info("Enabling recording of key preimages since archive mode is used")
17831799
}
17841800
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheTrieFlag.Name) {
17851801
cache.TrieCleanLimit = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100

core/blockchain.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ type CacheConfig struct {
135135
TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk
136136
TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node)
137137
TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
138+
Preimages bool // Whether to store preimage of trie key to the disk
138139
}
139140

140141
type ResultProcessBlock struct {
@@ -238,11 +239,14 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
238239
}
239240

240241
bc := &BlockChain{
241-
chainConfig: chainConfig,
242-
cacheConfig: cacheConfig,
243-
db: db,
244-
triegc: prque.New[int64, common.Hash](nil),
245-
stateCache: state.NewDatabaseWithCache(db, cacheConfig.TrieCleanLimit),
242+
chainConfig: chainConfig,
243+
cacheConfig: cacheConfig,
244+
db: db,
245+
triegc: prque.New[int64, common.Hash](nil),
246+
stateCache: state.NewDatabaseWithConfig(db, &trie.Config{
247+
Cache: cacheConfig.TrieCleanLimit,
248+
Preimages: cacheConfig.Preimages,
249+
}),
246250
quit: make(chan struct{}),
247251
chainmu: syncx.NewClosableMutex(),
248252
bodyCache: lru.NewCache[common.Hash, *types.Body](bodyCacheLimit),

core/state/database.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ type Trie interface {
102102
}
103103

104104
// NewDatabase creates a backing store for state. The returned database is safe for
105-
// concurrent use and retains a few recent expanded trie nodes in memory. To keep
106-
// more historical state in memory, use the NewDatabaseWithCache constructor.
105+
// concurrent use, but does not retain any recent trie nodes in memory. To keep some
106+
// historical state in memory, use the NewDatabaseWithConfig constructor.
107107
func NewDatabase(db ethdb.Database) Database {
108-
return NewDatabaseWithCache(db, 0)
108+
return NewDatabaseWithConfig(db, nil)
109109
}
110110

111-
// NewDatabase creates a backing store for state. The returned database is safe for
112-
// concurrent use and retains both a few recent expanded trie nodes in memory, as
113-
// well as a lot of collapsed RLP trie nodes in a large memory cache.
114-
func NewDatabaseWithCache(db ethdb.Database, cache int) Database {
111+
// NewDatabaseWithConfig creates a backing store for state. The returned database
112+
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
113+
// large memory cache.
114+
func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
115115
return &cachingDB{
116-
db: trie.NewDatabaseWithCache(db, cache),
116+
db: trie.NewDatabaseWithConfig(db, config),
117117
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
118118
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
119119
}

eth/api_tracer.go

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

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

182182
if number := start.NumberU64(); number > 0 {
183183
start = api.eth.blockchain.GetBlock(start.ParentHash(), start.NumberU64()-1)
@@ -549,7 +549,7 @@ func (api *PrivateDebugAPI) computeStateDB(block *types.Block, reexec uint64) (*
549549
}
550550
// Otherwise try to reexec blocks until we find a state or reach our limit
551551
origin := block.NumberU64()
552-
database := state.NewDatabaseWithCache(api.eth.ChainDb(), 16)
552+
database := state.NewDatabaseWithConfig(api.eth.ChainDb(), &trie.Config{Cache: 16, Preimages: true})
553553

554554
for i := uint64(0); i < reexec; i++ {
555555
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
@@ -181,6 +181,7 @@ func New(stack *node.Node, config *ethconfig.Config, XDCXServ *XDCx.XDCX, lendin
181181
TrieDirtyLimit: config.TrieDirtyCache,
182182
TrieDirtyDisabled: config.NoPruning,
183183
TrieTimeLimit: config.TrieTimeout,
184+
Preimages: config.Preimages,
184185
}
185186
)
186187
if eth.chainConfig.XDPoS != nil {

eth/ethconfig/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ type Config struct {
118118
TrieCleanCache int
119119
TrieDirtyCache int
120120
TrieTimeout time.Duration
121+
Preimages bool
121122

122123
// This is the number of blocks for which logs will be cached in the filter system.
123124
FilterLogCacheSize int

eth/ethconfig/gen_config.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eth/state_accessor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (eth *Ethereum) stateAtBlock(block *types.Block, reexec uint64, base *state
5858

5959
// Create an ephemeral trie.Database for isolating the live one. Otherwise
6060
// the internal junks created by tracing will be persisted into the disk.
61-
database = state.NewDatabaseWithCache(eth.chainDb, 16)
61+
database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16, Preimages: true})
6262
// If we didn't check the dirty database, do check the clean one, otherwise
6363
// we would rewind past a persisted block (specific corner case is chain
6464
// tracing from the genesis).

0 commit comments

Comments
 (0)