Skip to content

Commit 262b452

Browse files
committed
rename to interval, other improvs
1 parent 6d92bca commit 262b452

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

core/blockchain.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ var defaultCacheConfig = &CacheConfig{
159159
// included in the canonical one where as GetBlockByNumber always represents the
160160
// canonical chain.
161161
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
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
}

eth/api.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,12 @@ func (api *PrivateDebugAPI) GetAccessibleState(from, to rpc.BlockNumber) (uint64
612612
return 0, fmt.Errorf("No state found")
613613
}
614614

615-
type gcPolicy struct {
616-
FlushFrequency int
617-
}
618-
619-
func (api *PrivateDebugAPI) SetGCPolicy(policy gcPolicy) error {
620-
api.eth.blockchain.SetGCMode(policy.FlushFrequency)
615+
// SetTrieFlushInterval configures how often (i.e. after how many blocks) in-memory tries
616+
// are persisted to disk. The acceptable values are:
617+
// - interval = 0: uses the default time-based interval
618+
// - interval = 1: persist the trie for every block (i.e. archive mode)
619+
// - interval > 1: persist the trie after every `interval` block
620+
func (api *PrivateDebugAPI) SetTrieFlushInterval(interval uint64) error {
621+
api.eth.blockchain.SetTrieFlushInterval(interval)
621622
return nil
622623
}

internal/web3ext/web3ext.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ web3._extend({
486486
params: 0
487487
}),
488488
new web3._extend.Method({
489-
name: 'setGCPolicy',
490-
call: 'debug_setGCPolicy',
489+
name: 'setTrieFlushInterval',
490+
call: 'debug_setTrieFlushInterval',
491491
params: 1
492492
}),
493493
],

0 commit comments

Comments
 (0)