Skip to content

Commit 86e494c

Browse files
committed
triedb/pathdb: enable trienode history
1 parent 60dbb64 commit 86e494c

File tree

16 files changed

+317
-106
lines changed

16 files changed

+317
-106
lines changed

cmd/geth/chaincmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ if one is set. Otherwise it prints the genesis from the datadir.`,
117117
utils.LogNoHistoryFlag,
118118
utils.LogExportCheckpointsFlag,
119119
utils.StateHistoryFlag,
120+
utils.TrienodeHistoryFlag,
120121
}, utils.DatabaseFlags, debug.Flags),
121122
Before: func(ctx *cli.Context) error {
122123
flags.MigrateGlobalFlags(ctx)

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ var (
9191
utils.LogNoHistoryFlag,
9292
utils.LogExportCheckpointsFlag,
9393
utils.StateHistoryFlag,
94+
utils.TrienodeHistoryFlag,
9495
utils.LightKDFFlag,
9596
utils.EthRequiredBlocksFlag,
9697
utils.LegacyWhitelistFlag, // deprecated

cmd/utils/flags.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ var (
282282
Value: ethconfig.Defaults.StateHistory,
283283
Category: flags.StateCategory,
284284
}
285+
TrienodeHistoryFlag = &cli.Int64Flag{
286+
Name: "history.trienode",
287+
Usage: "Number of recent blocks to retain trienode history for, only relevant in state.scheme=path (default/negative = disabled, 0 = entire chain)",
288+
Value: ethconfig.Defaults.TrienodeHistory,
289+
Category: flags.StateCategory,
290+
}
285291
TransactionHistoryFlag = &cli.Uint64Flag{
286292
Name: "history.transactions",
287293
Usage: "Number of recent blocks to maintain transactions index for (default = about one year, 0 = entire chain)",
@@ -1663,6 +1669,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16631669
if ctx.IsSet(StateHistoryFlag.Name) {
16641670
cfg.StateHistory = ctx.Uint64(StateHistoryFlag.Name)
16651671
}
1672+
if ctx.IsSet(TrienodeHistoryFlag.Name) {
1673+
cfg.TrienodeHistory = ctx.Int64(TrienodeHistoryFlag.Name)
1674+
}
16661675
if ctx.IsSet(StateSchemeFlag.Name) {
16671676
cfg.StateScheme = ctx.String(StateSchemeFlag.Name)
16681677
}
@@ -2234,15 +2243,16 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
22342243
Fatalf("%v", err)
22352244
}
22362245
options := &core.BlockChainConfig{
2237-
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2238-
NoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2239-
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2240-
ArchiveMode: ctx.String(GCModeFlag.Name) == "archive",
2241-
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2242-
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2243-
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2244-
StateScheme: scheme,
2245-
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2246+
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2247+
NoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2248+
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2249+
ArchiveMode: ctx.String(GCModeFlag.Name) == "archive",
2250+
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2251+
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2252+
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2253+
StateScheme: scheme,
2254+
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2255+
TrienodeHistory: ctx.Int64(TrienodeHistoryFlag.Name),
22462256
// Disable transaction indexing/unindexing.
22472257
TxLookupLimit: -1,
22482258

core/blockchain.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ type BlockChainConfig struct {
175175
// If set to 0, all state histories across the entire chain will be retained;
176176
StateHistory uint64
177177

178+
// Number of blocks from the chain head for which trienode histories are retained.
179+
// If set to 0, all trienode histories across the entire chain will be retained;
180+
// If set to -1, no trienode history will be retained;
181+
TrienodeHistory int64
182+
178183
// State snapshot related options
179184
SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory
180185
SnapshotNoBuild bool // Whether the background generation is allowed
@@ -249,6 +254,7 @@ func (cfg *BlockChainConfig) triedbConfig(isVerkle bool) *triedb.Config {
249254
if cfg.StateScheme == rawdb.PathScheme {
250255
config.PathDB = &pathdb.Config{
251256
StateHistory: cfg.StateHistory,
257+
TrienodeHistory: cfg.TrienodeHistory,
252258
EnableStateIndexing: cfg.ArchiveMode,
253259
TrieCleanSize: cfg.TrieCleanLimit * 1024 * 1024,
254260
StateCleanSize: cfg.SnapshotLimit * 1024 * 1024,

core/rawdb/ancient_utils.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ func inspectFreezers(db ethdb.Database) ([]freezerInfo, error) {
105105
}
106106
infos = append(infos, info)
107107

108+
case MerkleTrienodeFreezerName, VerkleTrienodeFreezerName:
109+
datadir, err := db.AncientDatadir()
110+
if err != nil {
111+
return nil, err
112+
}
113+
f, err := NewTrienodeFreezer(datadir, freezer == VerkleTrienodeFreezerName, true)
114+
if err != nil {
115+
continue // might be possible the trienode freezer is not existent
116+
}
117+
defer f.Close()
118+
119+
info, err := inspect(freezer, trienodeFreezerTableConfigs, f)
120+
if err != nil {
121+
return nil, err
122+
}
123+
infos = append(infos, info)
124+
108125
default:
109126
return nil, fmt.Errorf("unknown freezer, supported ones: %v", freezers)
110127
}

eth/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
230230
SnapshotLimit: config.SnapshotCache,
231231
Preimages: config.Preimages,
232232
StateHistory: config.StateHistory,
233+
TrienodeHistory: config.TrienodeHistory,
233234
StateScheme: scheme,
234235
ChainHistoryMode: config.HistoryMode,
235236
TxLookupLimit: int64(min(config.TransactionHistory, math.MaxInt64)),

eth/ethconfig/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var Defaults = Config{
5656
TransactionHistory: 2350000,
5757
LogHistory: 2350000,
5858
StateHistory: params.FullImmutabilityThreshold,
59+
TrienodeHistory: -1,
5960
DatabaseCache: 512,
6061
TrieCleanCache: 154,
6162
TrieDirtyCache: 256,
@@ -105,6 +106,7 @@ type Config struct {
105106
LogNoHistory bool `toml:",omitempty"` // No log search index is maintained.
106107
LogExportCheckpoints string // export log index checkpoints to file
107108
StateHistory uint64 `toml:",omitempty"` // The maximum number of blocks from head whose state histories are reserved.
109+
TrienodeHistory int64 `toml:",omitempty"` // Number of blocks from the chain head for which trienode histories are retained
108110

109111
// State scheme represents the scheme used to store ethereum states and trie
110112
// nodes on top. It can be 'hash', 'path', or none which means use the scheme

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.

triedb/pathdb/buffer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (b *buffer) size() uint64 {
132132

133133
// flush persists the in-memory dirty trie node into the disk if the configured
134134
// memory threshold is reached. Note, all data must be written atomically.
135-
func (b *buffer) flush(root common.Hash, db ethdb.KeyValueStore, freezer ethdb.AncientWriter, progress []byte, nodesCache, statesCache *fastcache.Cache, id uint64, postFlush func()) {
135+
func (b *buffer) flush(root common.Hash, db ethdb.KeyValueStore, freezers []ethdb.AncientWriter, progress []byte, nodesCache, statesCache *fastcache.Cache, id uint64, postFlush func()) {
136136
if b.done != nil {
137137
panic("duplicated flush operation")
138138
}
@@ -165,7 +165,10 @@ func (b *buffer) flush(root common.Hash, db ethdb.KeyValueStore, freezer ethdb.A
165165
//
166166
// This step is crucial to guarantee that the corresponding state history remains
167167
// available for state rollback.
168-
if freezer != nil {
168+
for _, freezer := range freezers {
169+
if freezer == nil {
170+
continue
171+
}
169172
if err := freezer.SyncAncient(); err != nil {
170173
b.flushErr = err
171174
return

triedb/pathdb/config.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var (
5353
// Defaults contains default settings for Ethereum mainnet.
5454
var Defaults = &Config{
5555
StateHistory: params.FullImmutabilityThreshold,
56+
TrienodeHistory: -1,
5657
EnableStateIndexing: false,
5758
TrieCleanSize: defaultTrieCleanSize,
5859
StateCleanSize: defaultStateCleanSize,
@@ -61,14 +62,16 @@ var Defaults = &Config{
6162

6263
// ReadOnly is the config in order to open database in read only mode.
6364
var ReadOnly = &Config{
64-
ReadOnly: true,
65-
TrieCleanSize: defaultTrieCleanSize,
66-
StateCleanSize: defaultStateCleanSize,
65+
ReadOnly: true,
66+
TrienodeHistory: -1,
67+
TrieCleanSize: defaultTrieCleanSize,
68+
StateCleanSize: defaultStateCleanSize,
6769
}
6870

6971
// Config contains the settings for database.
7072
type Config struct {
7173
StateHistory uint64 // Number of recent blocks to maintain state history for, 0: full chain
74+
TrienodeHistory int64 // Number of recent blocks to maintain trienode history for, 0: full chain, negative: disable
7275
EnableStateIndexing bool // Whether to enable state history indexing for external state access
7376
TrieCleanSize int // Maximum memory allowance (in bytes) for caching clean trie data
7477
StateCleanSize int // Maximum memory allowance (in bytes) for caching clean state data
@@ -108,6 +111,13 @@ func (c *Config) fields() []interface{} {
108111
} else {
109112
list = append(list, "state-history", fmt.Sprintf("last %d blocks", c.StateHistory))
110113
}
114+
if c.TrienodeHistory >= 0 {
115+
if c.TrienodeHistory == 0 {
116+
list = append(list, "trie-history", "entire chain")
117+
} else {
118+
list = append(list, "trie-history", fmt.Sprintf("last %d blocks", c.TrienodeHistory))
119+
}
120+
}
111121
if c.EnableStateIndexing {
112122
list = append(list, "index-history", true)
113123
}

0 commit comments

Comments
 (0)