@@ -68,9 +68,10 @@ const (
6868// CacheConfig contains the configuration values for the trie caching/pruning
6969// that's resident in a blockchain.
7070type CacheConfig struct {
71- Disabled bool // Whether to disable trie write caching (archive node)
72- TrieNodeLimit int // Memory limit (MB) at which to flush the current in-memory trie to disk
73- TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
71+ Disabled bool // Whether to disable trie write caching (archive node)
72+ TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory
73+ TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk
74+ TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
7475}
7576
7677// BlockChain represents the canonical chain given a database with a genesis
@@ -140,8 +141,9 @@ type BlockChain struct {
140141func NewBlockChain (db ethdb.Database , cacheConfig * CacheConfig , chainConfig * params.ChainConfig , engine consensus.Engine , vmConfig vm.Config , shouldPreserve func (block * types.Block ) bool ) (* BlockChain , error ) {
141142 if cacheConfig == nil {
142143 cacheConfig = & CacheConfig {
143- TrieNodeLimit : 256 ,
144- TrieTimeLimit : 5 * time .Minute ,
144+ TrieCleanLimit : 256 ,
145+ TrieDirtyLimit : 256 ,
146+ TrieTimeLimit : 5 * time .Minute ,
145147 }
146148 }
147149 bodyCache , _ := lru .New (bodyCacheLimit )
@@ -156,7 +158,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
156158 cacheConfig : cacheConfig ,
157159 db : db ,
158160 triegc : prque .New (nil ),
159- stateCache : state .NewDatabase (db ),
161+ stateCache : state .NewDatabaseWithCache (db , cacheConfig . TrieCleanLimit ),
160162 quit : make (chan struct {}),
161163 shouldPreserve : shouldPreserve ,
162164 bodyCache : bodyCache ,
@@ -393,6 +395,11 @@ func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
393395 return state .New (root , bc .stateCache )
394396}
395397
398+ // StateCache returns the caching database underpinning the blockchain instance.
399+ func (bc * BlockChain ) StateCache () state.Database {
400+ return bc .stateCache
401+ }
402+
396403// Reset purges the entire blockchain, restoring it to its genesis state.
397404func (bc * BlockChain ) Reset () error {
398405 return bc .ResetWithGenesisBlock (bc .genesisBlock )
@@ -938,7 +945,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
938945 // If we exceeded our memory allowance, flush matured singleton nodes to disk
939946 var (
940947 nodes , imgs = triedb .Size ()
941- limit = common .StorageSize (bc .cacheConfig .TrieNodeLimit ) * 1024 * 1024
948+ limit = common .StorageSize (bc .cacheConfig .TrieDirtyLimit ) * 1024 * 1024
942949 )
943950 if nodes > limit || imgs > 4 * 1024 * 1024 {
944951 triedb .Cap (limit - ethdb .IdealBatchSize )
0 commit comments