@@ -34,6 +34,7 @@ import (
3434 "github.com/ethereum/go-ethereum/consensus"
3535 "github.com/ethereum/go-ethereum/core/rawdb"
3636 "github.com/ethereum/go-ethereum/core/state"
37+ "github.com/ethereum/go-ethereum/core/state/snapshot"
3738 "github.com/ethereum/go-ethereum/core/types"
3839 "github.com/ethereum/go-ethereum/core/vm"
3940 "github.com/ethereum/go-ethereum/ethdb"
6162 storageUpdateTimer = metrics.NewRegisteredTimer("chain/storage/updates", nil)
6263 storageCommitTimer = metrics.NewRegisteredTimer("chain/storage/commits", nil)
6364
65+ snapshotAccountReadTimer = metrics.NewRegisteredTimer("chain/snapshot/accountreads", nil)
66+ snapshotStorageReadTimer = metrics.NewRegisteredTimer("chain/snapshot/storagereads", nil)
67+ snapshotCommitTimer = metrics.NewRegisteredTimer("chain/snapshot/commits", nil)
68+
6469 blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
6570 blockValidationTimer = metrics.NewRegisteredTimer("chain/validation", nil)
6671 blockExecutionTimer = metrics.NewRegisteredTimer("chain/execution", nil)
@@ -135,9 +140,10 @@ type BlockChain struct {
135140 chainConfig *params.ChainConfig // Chain & network configuration
136141 cacheConfig *CacheConfig // Cache configuration for pruning
137142
138- db ethdb.Database // Low level persistent database to store final content in
139- triegc *prque.Prque // Priority queue mapping block numbers to tries to gc
140- gcproc time.Duration // Accumulates canonical block processing for trie dumping
143+ db ethdb.Database // Low level persistent database to store final content in
144+ snaps *snapshot.SnapshotTree // Snapshot tree for fast trie leaf access
145+ triegc *prque.Prque // Priority queue mapping block numbers to tries to gc
146+ gcproc time.Duration // Accumulates canonical block processing for trie dumping
141147
142148 hc *HeaderChain
143149 rmLogsFeed event.Feed
@@ -293,6 +299,11 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
293299 }
294300 }
295301 }
302+ // 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.NumberU64(), head.Root()); err != nil {
305+ return nil, err
306+ }
296307 // Take ownership of this particular state
297308 go bc.update()
298309 return bc, nil
@@ -339,7 +350,7 @@ func (bc *BlockChain) loadLastState() error {
339350 return bc.Reset()
340351 }
341352 // Make sure the state associated with the block is available
342- if _, err := state.New(currentBlock.Root(), bc.stateCache); err != nil {
353+ if _, err := state.New(currentBlock.Root(), bc.stateCache, bc.snaps ); err != nil {
343354 // Dangling block without a state associated, init from scratch
344355 log.Warn("Head state missing, repairing chain", "number", currentBlock.Number(), "hash", currentBlock.Hash())
345356 if err := bc.repair(¤tBlock); err != nil {
@@ -401,7 +412,7 @@ func (bc *BlockChain) SetHead(head uint64) error {
401412 if newHeadBlock == nil {
402413 newHeadBlock = bc.genesisBlock
403414 } else {
404- if _, err := state.New(newHeadBlock.Root(), bc.stateCache); err != nil {
415+ if _, err := state.New(newHeadBlock.Root(), bc.stateCache, bc.snaps ); err != nil {
405416 // Rewound state missing, rolled back to before pivot, reset to genesis
406417 newHeadBlock = bc.genesisBlock
407418 }
@@ -524,7 +535,7 @@ func (bc *BlockChain) State() (*state.StateDB, error) {
524535
525536// StateAt returns a new mutable state based on a particular point in time.
526537func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
527- return state.New(root, bc.stateCache)
538+ return state.New(root, bc.stateCache, bc.snaps )
528539}
529540
530541// StateCache returns the caching database underpinning the blockchain instance.
@@ -576,7 +587,7 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
576587func (bc *BlockChain) repair(head **types.Block) error {
577588 for {
578589 // Abort if we've rewound to a head block that does have associated state
579- if _, err := state.New((*head).Root(), bc.stateCache); err == nil {
590+ if _, err := state.New((*head).Root(), bc.stateCache, bc.snaps ); err == nil {
580591 log.Info("Rewound blockchain to past state", "number", (*head).Number(), "hash", (*head).Hash())
581592 return nil
582593 }
@@ -839,6 +850,10 @@ func (bc *BlockChain) Stop() {
839850
840851 bc.wg.Wait()
841852
853+ // Ensure that the entirety of the state snapshot is journalled to disk.
854+ if err := bc.snaps.Journal(bc.CurrentBlock().Root()); err != nil {
855+ log.Error("Failed to journal state snapshot", "err", err)
856+ }
842857 // Ensure the state of a recent block is also stored to disk before exiting.
843858 // We're writing three different states to catch different restart scenarios:
844859 // - HEAD: So we don't need to reprocess any blocks in the general case
@@ -1647,7 +1662,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
16471662 if parent == nil {
16481663 parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
16491664 }
1650- statedb, err := state.New(parent.Root, bc.stateCache)
1665+ statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps )
16511666 if err != nil {
16521667 return it.index, err
16531668 }
@@ -1656,9 +1671,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
16561671 var followupInterrupt uint32
16571672 if !bc.cacheConfig.TrieCleanNoPrefetch {
16581673 if followup, err := it.peek(); followup != nil && err == nil {
1659- throwaway, _ := state.New(parent.Root, bc.stateCache)
1674+ throwaway, _ := state.New(parent.Root, bc.stateCache, bc.snaps )
16601675 go func(start time.Time, followup *types.Block, throwaway *state.StateDB, interrupt *uint32) {
1661- bc.prefetcher.Prefetch(followup, throwaway, bc.vmConfig, interrupt )
1676+ bc.prefetcher.Prefetch(followup, throwaway, bc.vmConfig, &followupInterrupt )
16621677
16631678 blockPrefetchExecuteTimer.Update(time.Since(start))
16641679 if atomic.LoadUint32(interrupt) == 1 {
@@ -1676,14 +1691,16 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
16761691 return it.index, err
16771692 }
16781693 // Update the metrics touched during block processing
1679- accountReadTimer.Update(statedb.AccountReads) // Account reads are complete, we can mark them
1680- storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete, we can mark them
1681- accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete, we can mark them
1682- storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete, we can mark them
1694+ accountReadTimer.Update(statedb.AccountReads) // Account reads are complete, we can mark them
1695+ storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete, we can mark them
1696+ accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete, we can mark them
1697+ storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete, we can mark them
1698+ snapshotAccountReadTimer.Update(statedb.SnapshotAccountReads) // Account reads are complete, we can mark them
1699+ snapshotStorageReadTimer.Update(statedb.SnapshotStorageReads) // Storage reads are complete, we can mark them
16831700
16841701 triehash := statedb.AccountHashes + statedb.StorageHashes // Save to not double count in validation
1685- trieproc := statedb.AccountReads + statedb.AccountUpdates
1686- trieproc += statedb.StorageReads + statedb.StorageUpdates
1702+ trieproc := statedb.SnapshotAccountReads + statedb. AccountReads + statedb.AccountUpdates
1703+ trieproc += statedb.SnapshotStorageReads + statedb. StorageReads + statedb.StorageUpdates
16871704
16881705 blockExecutionTimer.Update(time.Since(substart) - trieproc - triehash)
16891706
@@ -1712,10 +1729,11 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
17121729 atomic.StoreUint32(&followupInterrupt, 1)
17131730
17141731 // Update the metrics touched during block commit
1715- accountCommitTimer.Update(statedb.AccountCommits) // Account commits are complete, we can mark them
1716- storageCommitTimer.Update(statedb.StorageCommits) // Storage commits are complete, we can mark them
1732+ accountCommitTimer.Update(statedb.AccountCommits) // Account commits are complete, we can mark them
1733+ storageCommitTimer.Update(statedb.StorageCommits) // Storage commits are complete, we can mark them
1734+ snapshotCommitTimer.Update(statedb.SnapshotCommits) // Snapshot commits are complete, we can mark them
17171735
1718- blockWriteTimer.Update(time.Since(substart) - statedb.AccountCommits - statedb.StorageCommits)
1736+ blockWriteTimer.Update(time.Since(substart) - statedb.AccountCommits - statedb.StorageCommits - statedb.SnapshotCommits )
17191737 blockInsertTimer.UpdateSince(start)
17201738
17211739 switch status {
0 commit comments