Skip to content

Commit a2d508a

Browse files
committed
triedb/pathdb: enable trienode history
1 parent 17e5222 commit a2d508a

File tree

16 files changed

+316
-110
lines changed

16 files changed

+316
-110
lines changed

cmd/geth/chaincmd.go

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

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ var (
9494
utils.LogNoHistoryFlag,
9595
utils.LogExportCheckpointsFlag,
9696
utils.StateHistoryFlag,
97+
utils.TrienodeHistoryFlag,
9798
utils.LightKDFFlag,
9899
utils.EthRequiredBlocksFlag,
99100
utils.LegacyWhitelistFlag, // deprecated

cmd/utils/flags.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ var (
295295
Value: ethconfig.Defaults.StateHistory,
296296
Category: flags.StateCategory,
297297
}
298+
TrienodeHistoryFlag = &cli.Int64Flag{
299+
Name: "history.trienode",
300+
Usage: "Number of recent blocks to retain trienode history for, only relevant in state.scheme=path (default/negative = disabled, 0 = entire chain)",
301+
Value: ethconfig.Defaults.TrienodeHistory,
302+
Category: flags.StateCategory,
303+
}
298304
TransactionHistoryFlag = &cli.Uint64Flag{
299305
Name: "history.transactions",
300306
Usage: "Number of recent blocks to maintain transactions index for (default = about one year, 0 = entire chain)",
@@ -1683,6 +1689,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16831689
if ctx.IsSet(StateHistoryFlag.Name) {
16841690
cfg.StateHistory = ctx.Uint64(StateHistoryFlag.Name)
16851691
}
1692+
if ctx.IsSet(TrienodeHistoryFlag.Name) {
1693+
cfg.TrienodeHistory = ctx.Int64(TrienodeHistoryFlag.Name)
1694+
}
16861695
if ctx.IsSet(StateSchemeFlag.Name) {
16871696
cfg.StateScheme = ctx.String(StateSchemeFlag.Name)
16881697
}
@@ -2272,15 +2281,16 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
22722281
Fatalf("%v", err)
22732282
}
22742283
options := &core.BlockChainConfig{
2275-
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2276-
NoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2277-
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2278-
ArchiveMode: ctx.String(GCModeFlag.Name) == "archive",
2279-
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2280-
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2281-
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2282-
StateScheme: scheme,
2283-
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2284+
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2285+
NoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2286+
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2287+
ArchiveMode: ctx.String(GCModeFlag.Name) == "archive",
2288+
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2289+
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2290+
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2291+
StateScheme: scheme,
2292+
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2293+
TrienodeHistory: ctx.Int64(TrienodeHistoryFlag.Name),
22842294
// Disable transaction indexing/unindexing.
22852295
TxLookupLimit: -1,
22862296

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,
@@ -107,6 +108,7 @@ type Config struct {
107108
LogNoHistory bool `toml:",omitempty"` // No log search index is maintained.
108109
LogExportCheckpoints string // export log index checkpoints to file
109110
StateHistory uint64 `toml:",omitempty"` // The maximum number of blocks from head whose state histories are reserved.
111+
TrienodeHistory int64 `toml:",omitempty"` // Number of blocks from the chain head for which trienode histories are retained
110112

111113
// State scheme represents the scheme used to store ethereum states and trie
112114
// 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: 4 additions & 6 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,11 +165,9 @@ 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 {
169-
if err := freezer.SyncAncient(); err != nil {
170-
b.flushErr = err
171-
return
172-
}
168+
if err := syncHistory(freezers...); err != nil {
169+
b.flushErr = err
170+
return
173171
}
174172
nodes := b.nodes.write(batch, nodesCache)
175173
accounts, slots := b.states.write(batch, progress, statesCache)

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)