@@ -159,10 +159,10 @@ var defaultCacheConfig = &CacheConfig{
159
159
// included in the canonical one where as GetBlockByNumber always represents the
160
160
// canonical chain.
161
161
type 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
166
166
167
167
db ethdb.Database // Low level persistent database to store final content in
168
168
snaps * snapshot.Tree // Snapshot tree for fast trie leaf access
@@ -252,7 +252,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
252
252
vmConfig : vmConfig ,
253
253
}
254
254
if cacheConfig .TrieDirtyDisabled {
255
- bc .trieFlushFreq = 1
255
+ bc .trieFlushInterval = 1
256
256
}
257
257
bc .forker = NewForkChoice (bc , shouldPreserve )
258
258
bc .validator = NewBlockValidator (chainConfig , bc , engine )
@@ -838,7 +838,7 @@ func (bc *BlockChain) Stop() {
838
838
// - HEAD: So we don't need to reprocess any blocks in the general case
839
839
// - HEAD-1: So we don't do large reorgs if our HEAD becomes an uncle
840
840
// - 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 {
842
842
triedb := bc .stateCache .TrieDB ()
843
843
844
844
for _ , offset := range []uint64 {0 , 1 , TriesInMemory - 1 } {
@@ -1240,19 +1240,19 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1240
1240
return err
1241
1241
}
1242
1242
var (
1243
- triedb = bc .stateCache .TrieDB ()
1244
- freq = atomic .LoadUint64 (& bc .trieFlushFreq )
1243
+ triedb = bc .stateCache .TrieDB ()
1244
+ interval = atomic .LoadUint64 (& bc .trieFlushInterval )
1245
1245
)
1246
1246
// If we're running an archive node, always flush
1247
- if freq == 1 {
1247
+ if interval == 1 {
1248
1248
return triedb .Commit (root , false , nil )
1249
1249
}
1250
1250
1251
1251
// Full but not archive node, do proper garbage collection
1252
1252
triedb .Reference (root , common.Hash {}) // metadata reference to keep trie alive
1253
1253
bc .triegc .Push (root , - int64 (block .NumberU64 ()))
1254
1254
1255
- // Note: flush frequency is not considered for the first TriesInMemory blocks.
1255
+ // Note: flush interval is not considered for the first TriesInMemory blocks.
1256
1256
current := block .NumberU64 ()
1257
1257
if current <= TriesInMemory {
1258
1258
return nil
@@ -1271,12 +1271,13 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1271
1271
sinceFlush = chosen - bc .lastWrite
1272
1272
doFlush = bc .gcproc > bc .cacheConfig .TrieTimeLimit
1273
1273
)
1274
- if freq > 1 {
1275
- doFlush = doFlush || sinceFlush >= freq
1274
+ if interval > 1 {
1275
+ doFlush = doFlush || sinceFlush >= interval
1276
1276
}
1277
1277
// If we exceeded out time allowance or passed number of blocks threshold,
1278
1278
// then flush an entire trie to disk
1279
1279
if doFlush {
1280
+ log .Warn ("Flushing trie" , "current" , current , "chosen" , chosen , "lastWrite" , bc .lastWrite , "sinceFlush" , sinceFlush , "interval" , interval )
1280
1281
// If the header is missing (canonical chain behind), we're reorging a low
1281
1282
// diff sidechain. Suspend committing until this operation is completed.
1282
1283
header := bc .GetHeaderByNumber (chosen )
@@ -2373,6 +2374,8 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
2373
2374
return 0 , err
2374
2375
}
2375
2376
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 )
2378
2381
}
0 commit comments