Skip to content

Commit de4aa37

Browse files
authored
Revert "cmd, core, eth, graphql, trie: no persisted clean trie cache file (ethereum#27525)"
This reverts commit 2c0c010.
1 parent d963280 commit de4aa37

13 files changed

Lines changed: 186 additions & 59 deletions

File tree

cmd/devp2p/internal/ethtest/suite_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@ func setupGeth(stack *node.Node) error {
109109
}
110110

111111
backend, err := eth.New(stack, &ethconfig.Config{
112-
Genesis: &chain.genesis,
113-
NetworkId: chain.genesis.Config.ChainID.Uint64(), // 19763
114-
DatabaseCache: 10,
115-
TrieCleanCache: 10,
116-
TrieDirtyCache: 16,
117-
TrieTimeout: 60 * time.Minute,
118-
SnapshotCache: 10,
112+
Genesis: &chain.genesis,
113+
NetworkId: chain.genesis.Config.ChainID.Uint64(), // 19763
114+
DatabaseCache: 10,
115+
TrieCleanCache: 10,
116+
TrieCleanCacheJournal: "",
117+
TrieCleanCacheRejournal: 60 * time.Minute,
118+
TrieDirtyCache: 16,
119+
TrieTimeout: 60 * time.Minute,
120+
SnapshotCache: 10,
119121
})
120122
if err != nil {
121123
return err

cmd/geth/config.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,6 @@ func deprecated(field string) bool {
276276
return true
277277
case "ethconfig.Config.EWASMInterpreter":
278278
return true
279-
case "ethconfig.Config.TrieCleanCacheJournal":
280-
return true
281-
case "ethconfig.Config.TrieCleanCacheRejournal":
282-
return true
283279
default:
284280
return false
285281
}

cmd/geth/snapshot.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var (
5050
ArgsUsage: "<root>",
5151
Action: pruneState,
5252
Flags: flags.Merge([]cli.Flag{
53+
utils.CacheTrieJournalFlag,
5354
utils.BloomFilterSizeFlag,
5455
}, utils.NetworkFlags, utils.DatabasePathFlags),
5556
Description: `
@@ -159,14 +160,15 @@ block is used.
159160
// Deprecation: this command should be deprecated once the hash-based
160161
// scheme is deprecated.
161162
func pruneState(ctx *cli.Context) error {
162-
stack, _ := makeConfigNode(ctx)
163+
stack, config := makeConfigNode(ctx)
163164
defer stack.Close()
164165

165166
chaindb := utils.MakeChainDatabase(ctx, stack, false)
166167
defer chaindb.Close()
167168

168169
prunerconfig := pruner.Config{
169170
Datadir: stack.ResolvePath(""),
171+
Cachedir: stack.ResolvePath(config.Eth.TrieCleanCacheJournal),
170172
BloomSize: ctx.Uint64(utils.BloomFilterSizeFlag.Name),
171173
}
172174
pruner, err := pruner.NewPruner(chaindb, prunerconfig)

cmd/utils/flags.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,18 @@ var (
410410
Value: 15,
411411
Category: flags.PerfCategory,
412412
}
413+
CacheTrieJournalFlag = &cli.StringFlag{
414+
Name: "cache.trie.journal",
415+
Usage: "Disk journal directory for trie cache to survive node restarts",
416+
Value: ethconfig.Defaults.TrieCleanCacheJournal,
417+
Category: flags.PerfCategory,
418+
}
419+
CacheTrieRejournalFlag = &cli.DurationFlag{
420+
Name: "cache.trie.rejournal",
421+
Usage: "Time interval to regenerate the trie cache journal",
422+
Value: ethconfig.Defaults.TrieCleanCacheRejournal,
423+
Category: flags.PerfCategory,
424+
}
413425
CacheGCFlag = &cli.IntFlag{
414426
Name: "cache.gc",
415427
Usage: "Percentage of cache memory allowance to use for trie pruning (default = 25% full mode, 0% archive mode)",
@@ -1698,6 +1710,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16981710
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheTrieFlag.Name) {
16991711
cfg.TrieCleanCache = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100
17001712
}
1713+
if ctx.IsSet(CacheTrieJournalFlag.Name) {
1714+
cfg.TrieCleanCacheJournal = ctx.String(CacheTrieJournalFlag.Name)
1715+
}
1716+
if ctx.IsSet(CacheTrieRejournalFlag.Name) {
1717+
cfg.TrieCleanCacheRejournal = ctx.Duration(CacheTrieRejournalFlag.Name)
1718+
}
17011719
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheGCFlag.Name) {
17021720
cfg.TrieDirtyCache = ctx.Int(CacheFlag.Name) * ctx.Int(CacheGCFlag.Name) / 100
17031721
}

cmd/utils/flags_legacy.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ var ShowDeprecated = &cli.Command{
3333

3434
var DeprecatedFlags = []cli.Flag{
3535
NoUSBFlag,
36-
CacheTrieJournalFlag,
37-
CacheTrieRejournalFlag,
3836
}
3937

4038
var (
@@ -44,17 +42,6 @@ var (
4442
Usage: "Disables monitoring for and managing USB hardware wallets (deprecated)",
4543
Category: flags.DeprecatedCategory,
4644
}
47-
// (Deprecated June 2023, shown in aliased flags section)
48-
CacheTrieJournalFlag = &cli.StringFlag{
49-
Name: "cache.trie.journal",
50-
Usage: "Disk journal directory for trie cache to survive node restarts",
51-
Category: flags.PerfCategory,
52-
}
53-
CacheTrieRejournalFlag = &cli.DurationFlag{
54-
Name: "cache.trie.rejournal",
55-
Usage: "Time interval to regenerate the trie cache journal",
56-
Category: flags.PerfCategory,
57-
}
5845
)
5946

6047
// showDeprecated displays deprecated flags that will be soon removed from the codebase.

core/blockchain.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ const (
130130
// that's resident in a blockchain.
131131
type CacheConfig struct {
132132
TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory
133+
TrieCleanJournal string // Disk journal for saving clean cache entries.
134+
TrieCleanRejournal time.Duration // Time interval to dump clean cache to disk periodically
133135
TrieCleanNoPrefetch bool // Whether to disable heuristic state prefetching for followup blocks
134136
TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk
135137
TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node)
@@ -236,6 +238,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
236238
// Open trie database with provided config
237239
triedb := trie.NewDatabaseWithConfig(db, &trie.Config{
238240
Cache: cacheConfig.TrieCleanLimit,
241+
Journal: cacheConfig.TrieCleanJournal,
239242
Preimages: cacheConfig.Preimages,
240243
})
241244
// Setup the genesis block, commit the provided genesis specification
@@ -408,6 +411,18 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
408411
bc.wg.Add(1)
409412
go bc.updateFutureBlocks()
410413

414+
// If periodic cache journal is required, spin it up.
415+
if bc.cacheConfig.TrieCleanRejournal > 0 {
416+
if bc.cacheConfig.TrieCleanRejournal < time.Minute {
417+
log.Warn("Sanitizing invalid trie cache journal time", "provided", bc.cacheConfig.TrieCleanRejournal, "updated", time.Minute)
418+
bc.cacheConfig.TrieCleanRejournal = time.Minute
419+
}
420+
bc.wg.Add(1)
421+
go func() {
422+
defer bc.wg.Done()
423+
bc.triedb.SaveCachePeriodically(bc.cacheConfig.TrieCleanJournal, bc.cacheConfig.TrieCleanRejournal, bc.quit)
424+
}()
425+
}
411426
// Rewind the chain in case of an incompatible config upgrade.
412427
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
413428
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
@@ -972,6 +987,11 @@ func (bc *BlockChain) Stop() {
972987
if err := bc.stateCache.TrieDB().Close(); err != nil {
973988
log.Error("Failed to close trie db", "err", err)
974989
}
990+
// Ensure all live cached entries be saved into disk, so that we can skip
991+
// cache warmup when node restarts.
992+
if bc.cacheConfig.TrieCleanJournal != "" {
993+
bc.triedb.SaveCache(bc.cacheConfig.TrieCleanJournal)
994+
}
975995
log.Info("Blockchain stopped")
976996
}
977997

core/state/pruner/pruner.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const (
5757
// Config includes all the configurations for pruning.
5858
type Config struct {
5959
Datadir string // The directory of the state database
60+
Cachedir string // The directory of state clean cache
6061
BloomSize uint64 // The Megabytes of memory allocated to bloom-filter
6162
}
6263

@@ -240,7 +241,7 @@ func (p *Pruner) Prune(root common.Hash) error {
240241
return err
241242
}
242243
if stateBloomRoot != (common.Hash{}) {
243-
return RecoverPruning(p.config.Datadir, p.db)
244+
return RecoverPruning(p.config.Datadir, p.db, p.config.Cachedir)
244245
}
245246
// If the target state root is not specified, use the HEAD-127 as the
246247
// target. The reason for picking it is:
@@ -298,6 +299,12 @@ func (p *Pruner) Prune(root common.Hash) error {
298299
log.Info("Selecting user-specified state as the pruning target", "root", root)
299300
}
300301
}
302+
// Before start the pruning, delete the clean trie cache first.
303+
// It's necessary otherwise in the next restart we will hit the
304+
// deleted state root in the "clean cache" so that the incomplete
305+
// state is picked for usage.
306+
deleteCleanTrieCache(p.config.Cachedir)
307+
301308
// All the state roots of the middle layer should be forcibly pruned,
302309
// otherwise the dangling state will be left.
303310
middleRoots := make(map[common.Hash]struct{})
@@ -335,7 +342,7 @@ func (p *Pruner) Prune(root common.Hash) error {
335342
// pruning can be resumed. What's more if the bloom filter is constructed, the
336343
// pruning **has to be resumed**. Otherwise a lot of dangling nodes may be left
337344
// in the disk.
338-
func RecoverPruning(datadir string, db ethdb.Database) error {
345+
func RecoverPruning(datadir string, db ethdb.Database, trieCachePath string) error {
339346
stateBloomPath, stateBloomRoot, err := findBloomFilter(datadir)
340347
if err != nil {
341348
return err
@@ -371,6 +378,12 @@ func RecoverPruning(datadir string, db ethdb.Database) error {
371378
}
372379
log.Info("Loaded state bloom filter", "path", stateBloomPath)
373380

381+
// Before start the pruning, delete the clean trie cache first.
382+
// It's necessary otherwise in the next restart we will hit the
383+
// deleted state root in the "clean cache" so that the incomplete
384+
// state is picked for usage.
385+
deleteCleanTrieCache(trieCachePath)
386+
374387
// All the state roots of the middle layers should be forcibly pruned,
375388
// otherwise the dangling state will be left.
376389
var (
@@ -484,3 +497,23 @@ func findBloomFilter(datadir string) (string, common.Hash, error) {
484497
}
485498
return stateBloomPath, stateBloomRoot, nil
486499
}
500+
501+
const warningLog = `
502+
503+
WARNING!
504+
505+
The clean trie cache is not found. Please delete it by yourself after the
506+
pruning. Remember don't start the Geth without deleting the clean trie cache
507+
otherwise the entire database may be damaged!
508+
509+
Check the command description "geth snapshot prune-state --help" for more details.
510+
`
511+
512+
func deleteCleanTrieCache(path string) {
513+
if !common.FileExist(path) {
514+
log.Warn(warningLog)
515+
return
516+
}
517+
os.RemoveAll(path)
518+
log.Info("Deleted trie clean cache", "path", path)
519+
}

eth/backend.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
132132
if err != nil {
133133
return nil, err
134134
}
135-
if err := pruner.RecoverPruning(stack.ResolvePath(""), chainDb); err != nil {
135+
if err := pruner.RecoverPruning(stack.ResolvePath(""), chainDb, stack.ResolvePath(config.TrieCleanCacheJournal)); err != nil {
136136
log.Error("Failed to recover state", "error", err)
137137
}
138138
// Transfer mining-related config to the ethash config.
@@ -184,6 +184,8 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
184184
}
185185
cacheConfig = &core.CacheConfig{
186186
TrieCleanLimit: config.TrieCleanCache,
187+
TrieCleanJournal: stack.ResolvePath(config.TrieCleanCacheJournal),
188+
TrieCleanRejournal: config.TrieCleanCacheRejournal,
187189
TrieCleanNoPrefetch: config.NoPrefetch,
188190
TrieDirtyLimit: config.TrieDirtyCache,
189191
TrieDirtyDisabled: config.NoPruning,

eth/ethconfig/config.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,25 @@ var LightClientGPO = gasprice.Config{
5757

5858
// Defaults contains default settings for use on the Ethereum main net.
5959
var Defaults = Config{
60-
SyncMode: downloader.SnapSync,
61-
NetworkId: 1,
62-
TxLookupLimit: 2350000,
63-
LightPeers: 100,
64-
UltraLightFraction: 75,
65-
DatabaseCache: 512,
66-
TrieCleanCache: 154,
67-
TrieDirtyCache: 256,
68-
TrieTimeout: 60 * time.Minute,
69-
SnapshotCache: 102,
70-
FilterLogCacheSize: 32,
71-
Miner: miner.DefaultConfig,
72-
TxPool: legacypool.DefaultConfig,
73-
RPCGasCap: 50000000,
74-
RPCEVMTimeout: 5 * time.Second,
75-
GPO: FullNodeGPO,
76-
RPCTxFeeCap: 1, // 1 ether
60+
SyncMode: downloader.SnapSync,
61+
NetworkId: 1,
62+
TxLookupLimit: 2350000,
63+
LightPeers: 100,
64+
UltraLightFraction: 75,
65+
DatabaseCache: 512,
66+
TrieCleanCache: 154,
67+
TrieCleanCacheJournal: "triecache",
68+
TrieCleanCacheRejournal: 60 * time.Minute,
69+
TrieDirtyCache: 256,
70+
TrieTimeout: 60 * time.Minute,
71+
SnapshotCache: 102,
72+
FilterLogCacheSize: 32,
73+
Miner: miner.DefaultConfig,
74+
TxPool: legacypool.DefaultConfig,
75+
RPCGasCap: 50000000,
76+
RPCEVMTimeout: 5 * time.Second,
77+
GPO: FullNodeGPO,
78+
RPCTxFeeCap: 1, // 1 ether
7779
}
7880

7981
//go:generate go run github.com/fjl/gencodec -type Config -formats toml -out gen_config.go
@@ -122,11 +124,13 @@ type Config struct {
122124
DatabaseCache int
123125
DatabaseFreezer string
124126

125-
TrieCleanCache int
126-
TrieDirtyCache int
127-
TrieTimeout time.Duration
128-
SnapshotCache int
129-
Preimages bool
127+
TrieCleanCache int
128+
TrieCleanCacheJournal string `toml:",omitempty"` // Disk journal directory for trie cache to survive node restarts
129+
TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache
130+
TrieDirtyCache int
131+
TrieTimeout time.Duration
132+
SnapshotCache int
133+
Preimages bool
130134

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

eth/ethconfig/gen_config.go

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

0 commit comments

Comments
 (0)