@@ -159,10 +159,10 @@ var defaultCacheConfig = &CacheConfig{
159159// included in the canonical one where as GetBlockByNumber always represents the
160160// canonical chain.
161161type BlockChain struct {
162- // trieFlushFreq is accessed atomically and needs to be 64-bit aligned.
163- trieFlushFreq uint64 // # blocks after which to flush the current in-memory trie to disk (0 = only consider time limit, 1 = archive mode)
164- chainConfig * params.ChainConfig // Chain & network configuration
165- cacheConfig * CacheConfig // Cache configuration for pruning
162+ // trieFlushInterval is accessed atomically and needs to be 64-bit aligned.
163+ trieFlushInterval uint64 // # blocks after which to flush the current in-memory trie to disk (0 = only consider time limit, 1 = archive mode)
164+ chainConfig * params.ChainConfig // Chain & network configuration
165+ cacheConfig * CacheConfig // Cache configuration for pruning
166166
167167 db ethdb.Database // Low level persistent database to store final content in
168168 snaps * snapshot.Tree // Snapshot tree for fast trie leaf access
@@ -252,7 +252,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
252252 vmConfig : vmConfig ,
253253 }
254254 if cacheConfig .TrieDirtyDisabled {
255- bc .trieFlushFreq = 1
255+ bc .trieFlushInterval = 1
256256 }
257257 bc .forker = NewForkChoice (bc , shouldPreserve )
258258 bc .validator = NewBlockValidator (chainConfig , bc , engine )
@@ -838,7 +838,7 @@ func (bc *BlockChain) Stop() {
838838 // - HEAD: So we don't need to reprocess any blocks in the general case
839839 // - HEAD-1: So we don't do large reorgs if our HEAD becomes an uncle
840840 // - HEAD-127: So we have a hard limit on the number of blocks reexecuted
841- if freq := atomic .LoadUint64 (& bc .trieFlushFreq ); freq != 1 {
841+ if interval := atomic .LoadUint64 (& bc .trieFlushInterval ); interval != 1 {
842842 triedb := bc .stateCache .TrieDB ()
843843
844844 for _ , offset := range []uint64 {0 , 1 , TriesInMemory - 1 } {
@@ -1240,19 +1240,19 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12401240 return err
12411241 }
12421242 var (
1243- triedb = bc .stateCache .TrieDB ()
1244- freq = atomic .LoadUint64 (& bc .trieFlushFreq )
1243+ triedb = bc .stateCache .TrieDB ()
1244+ interval = atomic .LoadUint64 (& bc .trieFlushInterval )
12451245 )
12461246 // If we're running an archive node, always flush
1247- if freq == 1 {
1247+ if interval == 1 {
12481248 return triedb .Commit (root , false , nil )
12491249 }
12501250
12511251 // Full but not archive node, do proper garbage collection
12521252 triedb .Reference (root , common.Hash {}) // metadata reference to keep trie alive
12531253 bc .triegc .Push (root , - int64 (block .NumberU64 ()))
12541254
1255- // Note: flush frequency is not considered for the first TriesInMemory blocks.
1255+ // Note: flush interval is not considered for the first TriesInMemory blocks.
12561256 current := block .NumberU64 ()
12571257 if current <= TriesInMemory {
12581258 return nil
@@ -1271,12 +1271,13 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12711271 sinceFlush = chosen - bc .lastWrite
12721272 doFlush = bc .gcproc > bc .cacheConfig .TrieTimeLimit
12731273 )
1274- if freq > 1 {
1275- doFlush = doFlush || sinceFlush >= freq
1274+ if interval > 1 {
1275+ doFlush = doFlush || sinceFlush >= interval
12761276 }
12771277 // If we exceeded out time allowance or passed number of blocks threshold,
12781278 // then flush an entire trie to disk
12791279 if doFlush {
1280+ log .Warn ("Flushing trie" , "current" , current , "chosen" , chosen , "lastWrite" , bc .lastWrite , "sinceFlush" , sinceFlush , "interval" , interval )
12801281 // If the header is missing (canonical chain behind), we're reorging a low
12811282 // diff sidechain. Suspend committing until this operation is completed.
12821283 header := bc .GetHeaderByNumber (chosen )
@@ -2373,6 +2374,8 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
23732374 return 0 , err
23742375}
23752376
2376- func (bc * BlockChain ) SetGCMode (flushFreq int ) {
2377- atomic .StoreUint64 (& bc .trieFlushFreq , uint64 (flushFreq ))
2377+ // SetTrieFlushInterval configures how often in-memory tries are persisted to disk.
2378+ // It is thread-safe and can be called repeatedly without side-effects.
2379+ func (bc * BlockChain ) SetTrieFlushInterval (interval uint64 ) {
2380+ atomic .StoreUint64 (& bc .trieFlushInterval , interval )
23782381}
0 commit comments