@@ -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 {
@@ -1435,7 +1441,6 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14351441 if err != nil {
14361442 return NonStatTy , err
14371443 }
1438- triedb := bc .stateCache .TrieDB ()
14391444
14401445 tradingRoot := common.Hash {}
14411446 if tradingState != nil {
@@ -1470,7 +1475,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14701475
14711476 // If we're running an archive node, always flush
14721477 if bc .cacheConfig .TrieDirtyDisabled {
1473- if err := triedb .Commit (root , false ); err != nil {
1478+ if err := bc . triedb .Commit (root , false ); err != nil {
14741479 return NonStatTy , err
14751480 }
14761481 if tradingTrieDb != nil {
@@ -1485,7 +1490,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14851490 }
14861491 } else {
14871492 // Full but not archive node, do proper garbage collection
1488- triedb .Reference (root , common.Hash {}) // metadata reference to keep trie alive
1493+ bc . triedb .Reference (root , common.Hash {}) // metadata reference to keep trie alive
14891494 bc .triegc .Push (root , - int64 (block .NumberU64 ()))
14901495 if tradingTrieDb != nil {
14911496 tradingTrieDb .Reference (tradingRoot , common.Hash {})
@@ -1512,11 +1517,11 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15121517 // size = size + lendingTrieDb.Size()
15131518 //}
15141519 var (
1515- nodes , imgs = triedb .Size ()
1520+ nodes , imgs = bc . triedb .Size ()
15161521 limit = common .StorageSize (bc .cacheConfig .TrieDirtyLimit ) * 1024 * 1024
15171522 )
15181523 if nodes > limit || imgs > 4 * 1024 * 1024 {
1519- triedb .Cap (limit - ethdb .IdealBatchSize )
1524+ bc . triedb .Cap (limit - ethdb .IdealBatchSize )
15201525 }
15211526 if bc .gcproc > bc .cacheConfig .TrieTimeLimit || chosen > lastWrite + triesInMemory {
15221527 // If the header is missing (canonical chain behind), we're reorging a low
@@ -1531,7 +1536,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15311536 log .Info ("State in memory for too long, committing" , "time" , bc .gcproc , "allowance" , bc .cacheConfig .TrieTimeLimit , "optimum" , float64 (chosen - lastWrite )/ triesInMemory )
15321537 }
15331538 // Flush an entire trie and restart the counters
1534- triedb .Commit (header .Root , true )
1539+ bc . triedb .Commit (header .Root , true )
15351540 lastWrite = chosen
15361541 bc .gcproc = 0
15371542 if tradingTrieDb != nil && lendingTrieDb != nil {
@@ -1551,7 +1556,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15511556 bc .triegc .Push (root , number )
15521557 break
15531558 }
1554- triedb .Dereference (root )
1559+ bc . triedb .Dereference (root )
15551560 }
15561561 if tradingService != nil {
15571562 for ! tradingService .GetTriegc ().Empty () {
@@ -1830,7 +1835,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
18301835 bc .UpdateBlocksHashCache (block )
18311836 }
18321837
1833- dirty , _ := bc .stateCache . TrieDB () .Size ()
1838+ dirty , _ := bc .triedb .Size ()
18341839 stats .report (chain , it .index , dirty )
18351840 if bc .chainConfig .XDPoS != nil {
18361841 engine , _ := bc .Engine ().(* XDPoS.XDPoS )
@@ -2284,7 +2289,7 @@ func (bc *BlockChain) insertBlock(block *types.Block) ([]interface{}, []*types.L
22842289 }
22852290 stats .processed ++
22862291 stats .usedGas += result .usedGas
2287- dirty , _ := bc .stateCache . TrieDB () .Size ()
2292+ dirty , _ := bc .triedb .Size ()
22882293 stats .report (types.Blocks {block }, 0 , dirty )
22892294 if bc .chainConfig .XDPoS != nil {
22902295 // epoch block
0 commit comments