@@ -167,10 +167,12 @@ type BlockChain struct {
167167 chainConfig * params.ChainConfig // Chain & network configuration
168168 cacheConfig * CacheConfig // Cache configuration for pruning
169169
170- db ethdb.Database // Low level persistent database to store final content in
171- XDCxDb ethdb.XDCxDatabase
172- triegc * prque.Prque [int64 , common.Hash ] // Priority queue mapping block numbers to tries to gc
173- gcproc time.Duration // Accumulates canonical block processing for trie dumping
170+ db ethdb.Database // Low level persistent database to store final content in
171+ XDCxDb ethdb.XDCxDatabase // XDCx database
172+ triegc * prque.Prque [int64 , common.Hash ] // Priority queue mapping block numbers to tries to gc
173+ gcproc time.Duration // Accumulates canonical block processing for trie dumping
174+ triedb * trie.Database // The database handler for maintaining trie nodes.
175+ stateCache state.Database // State database to reuse between imports (contains state cache)
174176
175177 hc * HeaderChain
176178 rmLogsFeed event.Feed
@@ -188,8 +190,6 @@ type BlockChain struct {
188190 currentBlock atomic.Value // Current head of the block chain
189191 currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!)
190192
191- stateCache state.Database // State database to reuse between imports (contains state cache)
192-
193193 bodyCache * lru.Cache [common.Hash , * types.Body ] // Cache for the most recent block bodies
194194 bodyRLPCache * lru.Cache [common.Hash , rlp.RawValue ] // Cache for the most recent block bodies in RLP encoded format
195195 receiptsCache * lru.Cache [common.Hash , types.Receipts ] // Cache for the most recent block receipts
@@ -239,10 +239,16 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
239239 }
240240 }
241241
242+ // Open trie database with provided config
243+ triedb := trie .NewDatabaseWithConfig (db , & trie.Config {
244+ Cache : cacheConfig .TrieCleanLimit ,
245+ Preimages : cacheConfig .Preimages ,
246+ })
242247 bc := & BlockChain {
243248 chainConfig : chainConfig ,
244249 cacheConfig : cacheConfig ,
245250 db : db ,
251+ triedb : triedb ,
246252 triegc : prque.New [int64 , common.Hash ](nil ),
247253 stateCache : state .NewDatabaseWithConfig (db , & trie.Config {
248254 Cache : cacheConfig .TrieCleanLimit ,
@@ -268,6 +274,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
268274 rejectedLendingItem : lru.NewCache [common.Hash , interface {}](tradingstate .OrderCacheLimit ),
269275 finalizedTrade : lru.NewCache [common.Hash , interface {}](tradingstate .OrderCacheLimit ),
270276 }
277+ bc .stateCache = state .NewDatabaseWithNodeDB (bc .db , bc .triedb )
271278 bc .validator = NewBlockValidator (chainConfig , bc , engine )
272279 bc .prefetcher = newStatePrefetcher (chainConfig , bc , engine )
273280 bc .processor = NewStateProcessor (chainConfig , bc , engine )
@@ -373,8 +380,7 @@ func (bc *BlockChain) loadLastState() error {
373380 }
374381 // Make sure the state associated with the block is available
375382 repair := false
376- _ , err := state .New (currentBlock .Root (), bc .stateCache )
377- if err != nil {
383+ if ! bc .HasState (currentBlock .Root ()) {
378384 repair = true
379385 } else {
380386 engine , ok := bc .Engine ().(* XDPoS.XDPoS )
@@ -487,7 +493,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64) error {
487493 if newHeadBlock == nil {
488494 newHeadBlock = bc .genesisBlock
489495 } else {
490- if _ , err := state . New (newHeadBlock .Root (), bc . stateCache ); err != nil {
496+ if ! bc . HasState (newHeadBlock .Root ()) {
491497 // Rewound state missing, rolled back to before pivot, reset to genesis
492498 newHeadBlock = bc .genesisBlock
493499 }
@@ -712,7 +718,7 @@ func (bc *BlockChain) repair(head **types.Block) error {
712718 for {
713719 // Abort if we've rewound to a head block that does have associated state
714720 if (common .RollbackNumber == 0 ) || ((* head ).Number ().Uint64 () < common .RollbackNumber ) {
715- if _ , err := state . New ((* head ).Root (), bc . stateCache ); err == nil {
721+ if bc . HasState ((* head ).Root ()) {
716722 log .Info ("Rewound blockchain to past state" , "number" , (* head ).Number (), "hash" , (* head ).Hash ())
717723 engine , ok := bc .Engine ().(* XDPoS.XDPoS )
718724 if ok {
@@ -1082,10 +1088,10 @@ func (bc *BlockChain) saveData() {
10821088 if ! bc .cacheConfig .TrieDirtyDisabled {
10831089 var tradingTriedb * trie.Database
10841090 var lendingTriedb * trie.Database
1085- engine , _ := bc .Engine ().(* XDPoS.XDPoS )
1086- triedb := bc .stateCache .TrieDB ()
10871091 var tradingService utils.TradingService
10881092 var lendingService utils.LendingService
1093+ triedb := bc .triedb
1094+ engine , _ := bc .Engine ().(* XDPoS.XDPoS )
10891095 if bc .Config ().IsTIPXDCX (bc .CurrentBlock ().Number ()) && bc .chainConfig .XDPoS != nil && bc .CurrentBlock ().NumberU64 () > bc .chainConfig .XDPoS .Epoch && engine != nil {
10901096 tradingService = engine .GetXDCXService ()
10911097 if tradingService != nil && tradingService .GetStateCache () != nil {
@@ -1439,7 +1445,6 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14391445 if err != nil {
14401446 return NonStatTy , err
14411447 }
1442- triedb := bc .stateCache .TrieDB ()
14431448
14441449 tradingRoot := common.Hash {}
14451450 if tradingState != nil {
@@ -1474,7 +1479,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14741479
14751480 // If we're running an archive node, always flush
14761481 if bc .cacheConfig .TrieDirtyDisabled {
1477- if err := triedb .Commit (root , false ); err != nil {
1482+ if err := bc . triedb .Commit (root , false ); err != nil {
14781483 return NonStatTy , err
14791484 }
14801485 if tradingTrieDb != nil {
@@ -1489,7 +1494,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14891494 }
14901495 } else {
14911496 // Full but not archive node, do proper garbage collection
1492- triedb .Reference (root , common.Hash {}) // metadata reference to keep trie alive
1497+ bc . triedb .Reference (root , common.Hash {}) // metadata reference to keep trie alive
14931498 bc .triegc .Push (root , - int64 (block .NumberU64 ()))
14941499 if tradingTrieDb != nil {
14951500 tradingTrieDb .Reference (tradingRoot , common.Hash {})
@@ -1516,11 +1521,11 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15161521 // size = size + lendingTrieDb.Size()
15171522 //}
15181523 var (
1519- nodes , imgs = triedb .Size ()
1524+ nodes , imgs = bc . triedb .Size ()
15201525 limit = common .StorageSize (bc .cacheConfig .TrieDirtyLimit ) * 1024 * 1024
15211526 )
15221527 if nodes > limit || imgs > 4 * 1024 * 1024 {
1523- triedb .Cap (limit - ethdb .IdealBatchSize )
1528+ bc . triedb .Cap (limit - ethdb .IdealBatchSize )
15241529 }
15251530 if bc .gcproc > bc .cacheConfig .TrieTimeLimit || chosen > lastWrite + triesInMemory {
15261531 // If the header is missing (canonical chain behind), we're reorging a low
@@ -1535,7 +1540,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15351540 log .Info ("State in memory for too long, committing" , "time" , bc .gcproc , "allowance" , bc .cacheConfig .TrieTimeLimit , "optimum" , float64 (chosen - lastWrite )/ triesInMemory )
15361541 }
15371542 // Flush an entire trie and restart the counters
1538- triedb .Commit (header .Root , true )
1543+ bc . triedb .Commit (header .Root , true )
15391544 lastWrite = chosen
15401545 bc .gcproc = 0
15411546 if tradingTrieDb != nil && lendingTrieDb != nil {
@@ -1555,7 +1560,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15551560 bc .triegc .Push (root , number )
15561561 break
15571562 }
1558- triedb .Dereference (root )
1563+ bc . triedb .Dereference (root )
15591564 }
15601565 if tradingService != nil {
15611566 for ! tradingService .GetTriegc ().Empty () {
@@ -1831,7 +1836,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
18311836 bc .UpdateBlocksHashCache (block )
18321837 }
18331838
1834- dirty , _ := bc .stateCache . TrieDB () .Size ()
1839+ dirty , _ := bc .triedb .Size ()
18351840 stats .report (chain , it .index , dirty )
18361841 if bc .chainConfig .XDPoS != nil {
18371842 engine , _ := bc .Engine ().(* XDPoS.XDPoS )
@@ -2287,7 +2292,7 @@ func (bc *BlockChain) insertBlock(block *types.Block) ([]interface{}, []*types.L
22872292 }
22882293 stats .processed ++
22892294 stats .usedGas += result .usedGas
2290- dirty , _ := bc .stateCache . TrieDB () .Size ()
2295+ dirty , _ := bc .triedb .Size ()
22912296 stats .report (types.Blocks {block }, 0 , dirty )
22922297 if bc .chainConfig .XDPoS != nil {
22932298 // epoch block
0 commit comments