Skip to content

Commit ccdb71e

Browse files
committed
triedb/pathdb: enable trienode history
1 parent ca6e68c commit ccdb71e

File tree

15 files changed

+300
-106
lines changed

15 files changed

+300
-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)",
@@ -1647,6 +1653,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16471653
if ctx.IsSet(StateHistoryFlag.Name) {
16481654
cfg.StateHistory = ctx.Uint64(StateHistoryFlag.Name)
16491655
}
1656+
if ctx.IsSet(TrienodeHistoryFlag.Name) {
1657+
cfg.TrienodeHistory = ctx.Int64(TrienodeHistoryFlag.Name)
1658+
}
16501659
if ctx.IsSet(StateSchemeFlag.Name) {
16511660
cfg.StateScheme = ctx.String(StateSchemeFlag.Name)
16521661
}
@@ -2200,15 +2209,16 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
22002209
Fatalf("%v", err)
22012210
}
22022211
options := &core.BlockChainConfig{
2203-
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2204-
NoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2205-
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2206-
ArchiveMode: ctx.String(GCModeFlag.Name) == "archive",
2207-
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2208-
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2209-
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2210-
StateScheme: scheme,
2211-
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2212+
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2213+
NoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2214+
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2215+
ArchiveMode: ctx.String(GCModeFlag.Name) == "archive",
2216+
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2217+
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2218+
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2219+
StateScheme: scheme,
2220+
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2221+
TrienodeHistory: ctx.Int64(TrienodeHistoryFlag.Name),
22122222
// Disable transaction indexing/unindexing.
22132223
TxLookupLimit: -1,
22142224

core/blockchain.go

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

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

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

108110
// State scheme represents the scheme used to store ethereum states and trie
109111
// 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)