@@ -674,7 +674,7 @@ func (bc *BlockChain) Stop() {
674674 for ! bc .triegc .Empty () {
675675 triedb .Dereference (bc .triegc .PopItem ().(common.Hash ), common.Hash {})
676676 }
677- if size := triedb .Size (); size != 0 {
677+ if size , _ := triedb .Size (); size != 0 {
678678 log .Error ("Dangling trie nodes after full cleanup" )
679679 }
680680 }
@@ -916,33 +916,29 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
916916 bc .triegc .Push (root , - float32 (block .NumberU64 ()))
917917
918918 if current := block .NumberU64 (); current > triesInMemory {
919+ // If we exceeded our memory allowance, flush matured singleton nodes to disk
920+ var (
921+ nodes , imgs = triedb .Size ()
922+ limit = common .StorageSize (bc .cacheConfig .TrieNodeLimit ) * 1024 * 1024
923+ )
924+ if nodes > limit || imgs > 4 * 1024 * 1024 {
925+ triedb .Cap (limit - ethdb .IdealBatchSize )
926+ }
919927 // Find the next state trie we need to commit
920928 header := bc .GetHeaderByNumber (current - triesInMemory )
921929 chosen := header .Number .Uint64 ()
922930
923- // Only write to disk if we exceeded our memory allowance *and* also have at
924- // least a given number of tries gapped.
925- var (
926- size = triedb .Size ()
927- limit = common .StorageSize (bc .cacheConfig .TrieNodeLimit ) * 1024 * 1024
928- )
929- if size > limit || bc .gcproc > bc .cacheConfig .TrieTimeLimit {
931+ // If we exceeded out time allowance, flush an entire trie to disk
932+ if bc .gcproc > bc .cacheConfig .TrieTimeLimit {
930933 // If we're exceeding limits but haven't reached a large enough memory gap,
931934 // warn the user that the system is becoming unstable.
932- if chosen < lastWrite + triesInMemory {
933- switch {
934- case size >= 2 * limit :
935- log .Warn ("State memory usage too high, committing" , "size" , size , "limit" , limit , "optimum" , float64 (chosen - lastWrite )/ triesInMemory )
936- case bc .gcproc >= 2 * bc .cacheConfig .TrieTimeLimit :
937- log .Info ("State in memory for too long, committing" , "time" , bc .gcproc , "allowance" , bc .cacheConfig .TrieTimeLimit , "optimum" , float64 (chosen - lastWrite )/ triesInMemory )
938- }
939- }
940- // If optimum or critical limits reached, write to disk
941- if chosen >= lastWrite + triesInMemory || size >= 2 * limit || bc .gcproc >= 2 * bc .cacheConfig .TrieTimeLimit {
942- triedb .Commit (header .Root , true )
943- lastWrite = chosen
944- bc .gcproc = 0
935+ if chosen < lastWrite + triesInMemory && bc .gcproc >= 2 * bc .cacheConfig .TrieTimeLimit {
936+ log .Info ("State in memory for too long, committing" , "time" , bc .gcproc , "allowance" , bc .cacheConfig .TrieTimeLimit , "optimum" , float64 (chosen - lastWrite )/ triesInMemory )
945937 }
938+ // Flush an entire trie and restart the counters
939+ triedb .Commit (header .Root , true )
940+ lastWrite = chosen
941+ bc .gcproc = 0
946942 }
947943 // Garbage collect anything below our required write retention
948944 for ! bc .triegc .Empty () {
@@ -1181,7 +1177,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
11811177 }
11821178 stats .processed ++
11831179 stats .usedGas += usedGas
1184- stats .report (chain , i , bc .stateCache .TrieDB ().Size ())
1180+
1181+ cache , _ := bc .stateCache .TrieDB ().Size ()
1182+ stats .report (chain , i , cache )
11851183 }
11861184 // Append a single chain head event if we've progressed the chain
11871185 if lastCanon != nil && bc .CurrentBlock ().Hash () == lastCanon .Hash () {
0 commit comments