72
72
storageUpdateTimer = metrics .NewRegisteredResettingTimer ("chain/storage/updates" , nil )
73
73
storageCommitTimer = metrics .NewRegisteredResettingTimer ("chain/storage/commits" , nil )
74
74
75
- snapshotAccountReadTimer = metrics .NewRegisteredResettingTimer ("chain/snapshot/account/reads" , nil )
76
- snapshotStorageReadTimer = metrics .NewRegisteredResettingTimer ("chain/snapshot/storage/reads" , nil )
77
- snapshotCommitTimer = metrics .NewRegisteredResettingTimer ("chain/snapshot/commits" , nil )
78
-
79
- triedbCommitTimer = metrics .NewRegisteredResettingTimer ("chain/triedb/commits" , nil )
75
+ snapshotCommitTimer = metrics .NewRegisteredResettingTimer ("chain/snapshot/commits" , nil )
76
+ triedbCommitTimer = metrics .NewRegisteredResettingTimer ("chain/triedb/commits" , nil )
80
77
81
78
blockInsertTimer = metrics .NewRegisteredResettingTimer ("chain/inserts" , nil )
82
79
blockValidationTimer = metrics .NewRegisteredResettingTimer ("chain/validation" , nil )
@@ -217,7 +214,7 @@ type BlockChain struct {
217
214
lastWrite uint64 // Last block when the state was flushed
218
215
flushInterval atomic.Int64 // Time interval (processing time) after which to flush a state
219
216
triedb * triedb.Database // The database handler for maintaining trie nodes.
220
- stateCache state. Database // State database to reuse between imports (contains state cache)
217
+ stateDb * state. CachingDB // State database to reuse between imports (contains state cache)
221
218
txIndexer * txIndexer // Transaction indexer, might be nil if not enabled
222
219
223
220
hc * HeaderChain
@@ -310,7 +307,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
310
307
}
311
308
bc .flushInterval .Store (int64 (cacheConfig .TrieTimeLimit ))
312
309
bc .forker = NewForkChoice (bc , shouldPreserve )
313
- bc .stateCache = state .NewDatabaseWithNodeDB (bc .db , bc .triedb )
310
+ bc .stateDb = state .NewDatabase (bc .db , bc .triedb , nil )
314
311
bc .validator = NewBlockValidator (chainConfig , bc )
315
312
bc .prefetcher = newStatePrefetcher (chainConfig , bc .hc )
316
313
bc .processor = NewStateProcessor (chainConfig , bc .hc )
@@ -448,7 +445,11 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
448
445
AsyncBuild : ! bc .cacheConfig .SnapshotWait ,
449
446
}
450
447
bc .snaps , _ = snapshot .New (snapconfig , bc .db , bc .triedb , head .Root )
448
+
449
+ // Re-initialize the state database with snapshot
450
+ bc .stateDb = state .NewDatabase (bc .db , bc .triedb , bc .snaps )
451
451
}
452
+
452
453
// Rewind the chain in case of an incompatible config upgrade.
453
454
if compat , ok := genesisErr .(* params.ConfigCompatError ); ok {
454
455
log .Warn ("Rewinding chain to upgrade configuration" , "err" , compat )
@@ -1800,7 +1801,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
1800
1801
if parent == nil {
1801
1802
parent = bc .GetHeader (block .ParentHash (), block .NumberU64 ()- 1 )
1802
1803
}
1803
- statedb , err := state .New (parent .Root , bc .stateCache , bc . snaps )
1804
+ statedb , err := state .New (parent .Root , bc .stateDb )
1804
1805
if err != nil {
1805
1806
return it .index , err
1806
1807
}
@@ -1826,7 +1827,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
1826
1827
var followupInterrupt atomic.Bool
1827
1828
if ! bc .cacheConfig .TrieCleanNoPrefetch {
1828
1829
if followup , err := it .peek (); followup != nil && err == nil {
1829
- throwaway , _ := state .New (parent .Root , bc .stateCache , bc . snaps )
1830
+ throwaway , _ := state .New (parent .Root , bc .stateDb )
1830
1831
1831
1832
go func (start time.Time , followup * types.Block , throwaway * state.StateDB ) {
1832
1833
// Disable tracing for prefetcher executions.
@@ -1946,21 +1947,6 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
1946
1947
}
1947
1948
proctime := time .Since (start ) // processing + validation
1948
1949
1949
- // Update the metrics touched during block processing and validation
1950
- accountReadTimer .Update (statedb .AccountReads ) // Account reads are complete(in processing)
1951
- storageReadTimer .Update (statedb .StorageReads ) // Storage reads are complete(in processing)
1952
- snapshotAccountReadTimer .Update (statedb .SnapshotAccountReads ) // Account reads are complete(in processing)
1953
- snapshotStorageReadTimer .Update (statedb .SnapshotStorageReads ) // Storage reads are complete(in processing)
1954
- accountUpdateTimer .Update (statedb .AccountUpdates ) // Account updates are complete(in validation)
1955
- storageUpdateTimer .Update (statedb .StorageUpdates ) // Storage updates are complete(in validation)
1956
- accountHashTimer .Update (statedb .AccountHashes ) // Account hashes are complete(in validation)
1957
- triehash := statedb .AccountHashes // The time spent on tries hashing
1958
- trieUpdate := statedb .AccountUpdates + statedb .StorageUpdates // The time spent on tries update
1959
- trieRead := statedb .SnapshotAccountReads + statedb .AccountReads // The time spent on account read
1960
- trieRead += statedb .SnapshotStorageReads + statedb .StorageReads // The time spent on storage read
1961
- blockExecutionTimer .Update (ptime - trieRead ) // The time spent on EVM processing
1962
- blockValidationTimer .Update (vtime - (triehash + trieUpdate )) // The time spent on block validation
1963
-
1964
1950
// Write the block to the chain and get the status.
1965
1951
var (
1966
1952
wstart = time .Now ()
@@ -1975,6 +1961,18 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
1975
1961
if err != nil {
1976
1962
return nil , err
1977
1963
}
1964
+ // Update the metrics touched during block processing and validation
1965
+ accountReadTimer .Update (statedb .AccountReads ) // Account reads are complete(in processing)
1966
+ storageReadTimer .Update (statedb .StorageReads ) // Storage reads are complete(in processing)
1967
+ accountUpdateTimer .Update (statedb .AccountUpdates ) // Account updates are complete(in validation)
1968
+ storageUpdateTimer .Update (statedb .StorageUpdates ) // Storage updates are complete(in validation)
1969
+ accountHashTimer .Update (statedb .AccountHashes ) // Account hashes are complete(in validation)
1970
+ triehash := statedb .AccountHashes // The time spent on account trie hashing
1971
+ trieUpdate := statedb .AccountUpdates + statedb .StorageUpdates // The time spent on account trie update + storage tries update and hashing
1972
+ trieRead := statedb .AccountReads + statedb .StorageReads // The time spent on account read and storage read
1973
+ blockExecutionTimer .Update (ptime - trieRead ) // The time spent on EVM processing
1974
+ blockValidationTimer .Update (vtime - (triehash + trieUpdate )) // The time spent on block validation
1975
+
1978
1976
// Update the metrics touched during block commit
1979
1977
accountCommitTimer .Update (statedb .AccountCommits ) // Account commits are complete, we can mark them
1980
1978
storageCommitTimer .Update (statedb .StorageCommits ) // Storage commits are complete, we can mark them
0 commit comments