Skip to content

Commit 9cb5d6b

Browse files
committed
triedb/pathdb: enable trienode history
1 parent de24450 commit 9cb5d6b

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
@@ -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
@@ -93,6 +93,7 @@ var (
9393
utils.LogNoHistoryFlag,
9494
utils.LogExportCheckpointsFlag,
9595
utils.StateHistoryFlag,
96+
utils.TrienodeHistoryFlag,
9697
utils.LightKDFFlag,
9798
utils.EthRequiredBlocksFlag,
9899
utils.LegacyWhitelistFlag, // deprecated

cmd/utils/flags.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ var (
291291
Value: ethconfig.Defaults.StateHistory,
292292
Category: flags.StateCategory,
293293
}
294+
TrienodeHistoryFlag = &cli.Int64Flag{
295+
Name: "history.trienode",
296+
Usage: "Number of recent blocks to retain trienode history for, only relevant in state.scheme=path (default/negative = disabled, 0 = entire chain)",
297+
Value: ethconfig.Defaults.TrienodeHistory,
298+
Category: flags.StateCategory,
299+
}
294300
TransactionHistoryFlag = &cli.Uint64Flag{
295301
Name: "history.transactions",
296302
Usage: "Number of recent blocks to maintain transactions index for (default = about one year, 0 = entire chain)",
@@ -1672,6 +1678,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16721678
if ctx.IsSet(StateHistoryFlag.Name) {
16731679
cfg.StateHistory = ctx.Uint64(StateHistoryFlag.Name)
16741680
}
1681+
if ctx.IsSet(TrienodeHistoryFlag.Name) {
1682+
cfg.TrienodeHistory = ctx.Int64(TrienodeHistoryFlag.Name)
1683+
}
16751684
if ctx.IsSet(StateSchemeFlag.Name) {
16761685
cfg.StateScheme = ctx.String(StateSchemeFlag.Name)
16771686
}
@@ -2243,15 +2252,16 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
22432252
Fatalf("%v", err)
22442253
}
22452254
options := &core.BlockChainConfig{
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+
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2256+
NoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2257+
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2258+
ArchiveMode: ctx.String(GCModeFlag.Name) == "archive",
2259+
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2260+
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2261+
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2262+
StateScheme: scheme,
2263+
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2264+
TrienodeHistory: ctx.Int64(TrienodeHistoryFlag.Name),
22552265
// Disable transaction indexing/unindexing.
22562266
TxLookupLimit: -1,
22572267

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)