Skip to content

Commit 351a590

Browse files
committed
core/rawdb, core/state/snapshot: runtime snapshot generation
1 parent f300c0d commit 351a590

21 files changed

Lines changed: 1539 additions & 474 deletions

‎cmd/geth/main.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ var (
106106
utils.CacheDatabaseFlag,
107107
utils.CacheTrieFlag,
108108
utils.CacheGCFlag,
109+
utils.CacheSnapshotFlag,
109110
utils.CacheNoPrefetchFlag,
110111
utils.ListenPortFlag,
111112
utils.MaxPeersFlag,

‎cmd/geth/usage.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ var AppHelpFlagGroups = []flagGroup{
137137
utils.CacheDatabaseFlag,
138138
utils.CacheTrieFlag,
139139
utils.CacheGCFlag,
140+
utils.CacheSnapshotFlag,
140141
utils.CacheNoPrefetchFlag,
141142
},
142143
},

‎cmd/utils/flags.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,19 @@ var (
383383
}
384384
CacheTrieFlag = cli.IntFlag{
385385
Name: "cache.trie",
386-
Usage: "Percentage of cache memory allowance to use for trie caching (default = 25% full mode, 50% archive mode)",
387-
Value: 25,
386+
Usage: "Percentage of cache memory allowance to use for trie caching (default = 15% full mode, 30% archive mode)",
387+
Value: 15,
388388
}
389389
CacheGCFlag = cli.IntFlag{
390390
Name: "cache.gc",
391391
Usage: "Percentage of cache memory allowance to use for trie pruning (default = 25% full mode, 0% archive mode)",
392392
Value: 25,
393393
}
394+
CacheSnapshotFlag = cli.IntFlag{
395+
Name: "cache.snapshot",
396+
Usage: "Percentage of cache memory allowance to use for snapshot caching (default = 10% full mode, 20% archive mode)",
397+
Value: 10,
398+
}
394399
CacheNoPrefetchFlag = cli.BoolFlag{
395400
Name: "cache.noprefetch",
396401
Usage: "Disable heuristic state prefetch during block import (less CPU and disk IO, more time waiting for data)",
@@ -1463,6 +1468,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
14631468
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
14641469
cfg.TrieDirtyCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
14651470
}
1471+
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheSnapshotFlag.Name) {
1472+
cfg.SnapshotCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheSnapshotFlag.Name) / 100
1473+
}
14661474
if ctx.GlobalIsSet(DocRootFlag.Name) {
14671475
cfg.DocRoot = ctx.GlobalString(DocRootFlag.Name)
14681476
}
@@ -1724,6 +1732,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
17241732
TrieDirtyLimit: eth.DefaultConfig.TrieDirtyCache,
17251733
TrieDirtyDisabled: ctx.GlobalString(GCModeFlag.Name) == "archive",
17261734
TrieTimeLimit: eth.DefaultConfig.TrieTimeout,
1735+
SnapshotLimit: eth.DefaultConfig.SnapshotCache,
17271736
}
17281737
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheTrieFlag.Name) {
17291738
cache.TrieCleanLimit = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheTrieFlag.Name) / 100

‎core/blockchain.go‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ var (
6262
storageUpdateTimer = metrics.NewRegisteredTimer("chain/storage/updates", nil)
6363
storageCommitTimer = metrics.NewRegisteredTimer("chain/storage/commits", nil)
6464

65-
snapshotAccountReadTimer = metrics.NewRegisteredTimer("chain/snapshot/accountreads", nil)
66-
snapshotStorageReadTimer = metrics.NewRegisteredTimer("chain/snapshot/storagereads", nil)
65+
snapshotAccountReadTimer = metrics.NewRegisteredTimer("chain/snapshot/account/reads", nil)
66+
snapshotStorageReadTimer = metrics.NewRegisteredTimer("chain/snapshot/storage/reads", nil)
6767
snapshotCommitTimer = metrics.NewRegisteredTimer("chain/snapshot/commits", nil)
6868

6969
blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
@@ -120,6 +120,7 @@ type CacheConfig struct {
120120
TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk
121121
TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node)
122122
TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
123+
SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory
123124
}
124125

125126
// BlockChain represents the canonical chain given a database with a genesis
@@ -194,6 +195,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
194195
TrieCleanLimit: 256,
195196
TrieDirtyLimit: 256,
196197
TrieTimeLimit: 5 * time.Minute,
198+
SnapshotLimit: 256,
197199
}
198200
}
199201
bodyCache, _ := lru.New(bodyCacheLimit)
@@ -300,10 +302,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
300302
}
301303
}
302304
// Load any existing snapshot, regenerating it if loading failed
303-
head := bc.CurrentBlock()
304-
if bc.snaps, err = snapshot.New(bc.db, "snapshot.rlp", head.Root()); err != nil {
305-
return nil, err
306-
}
305+
bc.snaps = snapshot.New(bc.db, bc.stateCache.TrieDB(), "snapshot.rlp", bc.cacheConfig.SnapshotLimit, bc.CurrentBlock().Root())
306+
307307
// Take ownership of this particular state
308308
go bc.update()
309309
return bc, nil
@@ -497,6 +497,9 @@ func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
497497
headBlockGauge.Update(int64(block.NumberU64()))
498498
bc.chainmu.Unlock()
499499

500+
// Destroy any existing state snapshot and regenerate it in the background
501+
bc.snaps.Rebuild(block.Root())
502+
500503
log.Info("Committed new head block", "number", block.Number(), "hash", hash)
501504
return nil
502505
}
@@ -851,7 +854,8 @@ func (bc *BlockChain) Stop() {
851854
bc.wg.Wait()
852855

853856
// Ensure that the entirety of the state snapshot is journalled to disk.
854-
if err := bc.snaps.Journal(bc.CurrentBlock().Root()); err != nil {
857+
snapBase, err := bc.snaps.Journal(bc.CurrentBlock().Root(), "snapshot.rlp")
858+
if err != nil {
855859
log.Error("Failed to journal state snapshot", "err", err)
856860
}
857861
// Ensure the state of a recent block is also stored to disk before exiting.
@@ -872,6 +876,12 @@ func (bc *BlockChain) Stop() {
872876
}
873877
}
874878
}
879+
if snapBase != (common.Hash{}) {
880+
log.Info("Writing snapshot state to disk", "root", snapBase)
881+
if err := triedb.Commit(snapBase, true); err != nil {
882+
log.Error("Failed to commit recent state trie", "err", err)
883+
}
884+
}
875885
for !bc.triegc.Empty() {
876886
triedb.Dereference(bc.triegc.PopItem().(common.Hash))
877887
}

‎core/rawdb/database.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ func InspectDatabase(db ethdb.Database) error {
282282
receiptSize += size
283283
case bytes.HasPrefix(key, txLookupPrefix) && len(key) == (len(txLookupPrefix)+common.HashLength):
284284
txlookupSize += size
285-
case bytes.HasPrefix(key, StateSnapshotPrefix) && len(key) == (len(StateSnapshotPrefix)+common.HashLength):
285+
case bytes.HasPrefix(key, SnapshotAccountPrefix) && len(key) == (len(SnapshotAccountPrefix)+common.HashLength):
286286
accountSnapSize += size
287-
case bytes.HasPrefix(key, StateSnapshotPrefix) && len(key) == (len(StateSnapshotPrefix)+2*common.HashLength):
287+
case bytes.HasPrefix(key, SnapshotStoragePrefix) && len(key) == (len(SnapshotStoragePrefix)+2*common.HashLength):
288288
storageSnapSize += size
289289
case bytes.HasPrefix(key, preimagePrefix) && len(key) == (len(preimagePrefix)+common.HashLength):
290290
preimageSize += size

‎core/rawdb/schema.go‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ var (
5353
blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
5454
blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
5555

56-
txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
57-
bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
58-
StateSnapshotPrefix = []byte("s") // StateSnapshotPrefix + account hash [+ storage hash] -> account/storage trie value
56+
txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
57+
bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
58+
SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
59+
SnapshotStoragePrefix = []byte("s") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
5960

6061
preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
6162
configPrefix = []byte("ethereum-config-") // config prefix for the db
@@ -149,19 +150,19 @@ func txLookupKey(hash common.Hash) []byte {
149150
return append(txLookupPrefix, hash.Bytes()...)
150151
}
151152

152-
// accountSnapshotKey = StateSnapshotPrefix + hash
153+
// accountSnapshotKey = SnapshotAccountPrefix + hash
153154
func accountSnapshotKey(hash common.Hash) []byte {
154-
return append(StateSnapshotPrefix, hash.Bytes()...)
155+
return append(SnapshotAccountPrefix, hash.Bytes()...)
155156
}
156157

157-
// storageSnapshotKey = StateSnapshotPrefix + account hash + storage hash
158+
// storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
158159
func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
159-
return append(append(StateSnapshotPrefix, accountHash.Bytes()...), storageHash.Bytes()...)
160+
return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
160161
}
161162

162-
// storageSnapshotsKey = StateSnapshotPrefix + account hash + storage hash
163+
// storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
163164
func storageSnapshotsKey(accountHash common.Hash) []byte {
164-
return append(StateSnapshotPrefix, accountHash.Bytes()...)
165+
return append(SnapshotStoragePrefix, accountHash.Bytes()...)
165166
}
166167

167168
// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash

0 commit comments

Comments
 (0)