@@ -183,8 +183,13 @@ func (c *CacheConfig) triedbConfig(isVerkle bool) *triedb.Config {
183183 }
184184 if c .StateScheme == rawdb .PathScheme {
185185 config .PathDB = & pathdb.Config {
186- StateHistory : c .StateHistory ,
187- CleanCacheSize : c .TrieCleanLimit * 1024 * 1024 ,
186+ StateHistory : c .StateHistory ,
187+ TrieCleanSize : c .TrieCleanLimit * 1024 * 1024 ,
188+ StateCleanSize : c .SnapshotLimit * 1024 * 1024 ,
189+
190+ // TODO(rjl493456442): The write buffer represents the memory limit used
191+ // for flushing both trie data and state data to disk. The config name
192+ // should be updated to eliminate the confusion.
188193 WriteBufferSize : c .TrieDirtyLimit * 1024 * 1024 ,
189194 }
190195 }
@@ -380,11 +385,14 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
380385 // Do nothing here until the state syncer picks it up.
381386 log .Info ("Genesis state is missing, wait state sync" )
382387 } else {
383- // Head state is missing, before the state recovery, find out the
384- // disk layer point of snapshot(if it's enabled). Make sure the
385- // rewound point is lower than disk layer.
388+ // Head state is missing, before the state recovery, find out the disk
389+ // layer point of snapshot(if it's enabled). Make sure the rewound point
390+ // is lower than disk layer.
391+ //
392+ // Note it's unnecessary in path mode which always keep trie data and
393+ // state data consistent.
386394 var diskRoot common.Hash
387- if bc .cacheConfig .SnapshotLimit > 0 {
395+ if bc .cacheConfig .SnapshotLimit > 0 && bc . cacheConfig . StateScheme == rawdb . HashScheme {
388396 diskRoot = rawdb .ReadSnapshotRoot (bc .db )
389397 }
390398 if diskRoot != (common.Hash {}) {
@@ -457,15 +465,39 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
457465 bc .logger .OnGenesisBlock (bc .genesisBlock , alloc )
458466 }
459467 }
468+ bc .setupSnapshot ()
469+
470+ // Rewind the chain in case of an incompatible config upgrade.
471+ if compatErr != nil {
472+ log .Warn ("Rewinding chain to upgrade configuration" , "err" , compatErr )
473+ if compatErr .RewindToTime > 0 {
474+ bc .SetHeadWithTimestamp (compatErr .RewindToTime )
475+ } else {
476+ bc .SetHead (compatErr .RewindToBlock )
477+ }
478+ rawdb .WriteChainConfig (db , genesisHash , chainConfig )
479+ }
480+
481+ // Start tx indexer if it's enabled.
482+ if txLookupLimit != nil {
483+ bc .txIndexer = newTxIndexer (* txLookupLimit , bc )
484+ }
485+ return bc , nil
486+ }
460487
488+ func (bc * BlockChain ) setupSnapshot () {
489+ // Short circuit if the chain is established with path scheme, as the
490+ // state snapshot has been integrated into path database natively.
491+ if bc .cacheConfig .StateScheme == rawdb .PathScheme {
492+ return
493+ }
461494 // Load any existing snapshot, regenerating it if loading failed
462495 if bc .cacheConfig .SnapshotLimit > 0 {
463496 // If the chain was rewound past the snapshot persistent layer (causing
464497 // a recovery block number to be persisted to disk), check if we're still
465498 // in recovery mode and in that case, don't invalidate the snapshot on a
466499 // head mismatch.
467500 var recover bool
468-
469501 head := bc .CurrentBlock ()
470502 if layer := rawdb .ReadSnapshotRecoveryNumber (bc .db ); layer != nil && * layer >= head .Number .Uint64 () {
471503 log .Warn ("Enabling snapshot recovery" , "chainhead" , head .Number , "diskbase" , * layer )
@@ -482,22 +514,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
482514 // Re-initialize the state database with snapshot
483515 bc .statedb = state .NewDatabase (bc .triedb , bc .snaps )
484516 }
485-
486- // Rewind the chain in case of an incompatible config upgrade.
487- if compatErr != nil {
488- log .Warn ("Rewinding chain to upgrade configuration" , "err" , compatErr )
489- if compatErr .RewindToTime > 0 {
490- bc .SetHeadWithTimestamp (compatErr .RewindToTime )
491- } else {
492- bc .SetHead (compatErr .RewindToBlock )
493- }
494- rawdb .WriteChainConfig (db , genesisHash , chainConfig )
495- }
496- // Start tx indexer if it's enabled.
497- if txLookupLimit != nil {
498- bc .txIndexer = newTxIndexer (* txLookupLimit , bc )
499- }
500- return bc , nil
501517}
502518
503519// empty returns an indicator whether the blockchain is empty.
0 commit comments