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 }
0 commit comments