Skip to content

Commit bea73e5

Browse files
committed
Make trie flush interval settable via rpc method
1 parent 9f78416 commit bea73e5

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

core/blockchain.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,14 @@ type BlockChain struct {
169169
chainConfig *params.ChainConfig // Chain & network configuration
170170
cacheConfig *CacheConfig // Cache configuration for pruning
171171

172-
db ethdb.Database // Low level persistent database to store final content in
173-
snaps *snapshot.Tree // Snapshot tree for fast trie leaf access
174-
triegc *prque.Prque // Priority queue mapping block numbers to tries to gc
175-
gcproc time.Duration // Accumulates canonical block processing for trie dumping
176-
lastWrite uint64 // Last block when the state was flushed
177-
triedb *trie.Database // The database handler for maintaining trie nodes.
178-
stateCache state.Database // State database to reuse between imports (contains state cache)
172+
db ethdb.Database // Low level persistent database to store final content in
173+
snaps *snapshot.Tree // Snapshot tree for fast trie leaf access
174+
triegc *prque.Prque // Priority queue mapping block numbers to tries to gc
175+
gcproc time.Duration // Accumulates canonical block processing for trie dumping
176+
lastWrite uint64 // Last block when the state was flushed
177+
flushInterval int64 // Time interval (processing time) after which to flush a state
178+
triedb *trie.Database // The database handler for maintaining trie nodes.
179+
stateCache state.Database // State database to reuse between imports (contains state cache)
179180

180181
// txLookupLimit is the maximum number of blocks from head whose tx indices
181182
// are reserved:
@@ -259,6 +260,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
259260
cacheConfig: cacheConfig,
260261
db: db,
261262
triedb: triedb,
263+
flushInterval: int64(cacheConfig.TrieTimeLimit),
262264
triegc: prque.New(nil),
263265
quit: make(chan struct{}),
264266
chainmu: syncx.NewClosableMutex(),
@@ -1330,9 +1332,10 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
13301332
}
13311333
// Find the next state trie we need to commit
13321334
chosen := current - TriesInMemory
1333-
1334-
// If we exceeded out time allowance, flush an entire trie to disk
1335-
if bc.gcproc > bc.cacheConfig.TrieTimeLimit {
1335+
flushInterval := time.Duration(atomic.LoadInt64(&bc.flushInterval))
1336+
log.Info("Deciding to flush", "interval", flushInterval, "chosen", chosen, "time", bc.gcproc, "lastWrite", bc.lastWrite)
1337+
// If we exceeded time allowance, flush an entire trie to disk
1338+
if bc.gcproc > flushInterval {
13361339
// If the header is missing (canonical chain behind), we're reorging a low
13371340
// diff sidechain. Suspend committing until this operation is completed.
13381341
header := bc.GetHeaderByNumber(chosen)
@@ -1341,8 +1344,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
13411344
} else {
13421345
// If we're exceeding limits but haven't reached a large enough memory gap,
13431346
// warn the user that the system is becoming unstable.
1344-
if chosen < bc.lastWrite+TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit {
1345-
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-bc.lastWrite)/TriesInMemory)
1347+
if chosen < bc.lastWrite+TriesInMemory && bc.gcproc >= 2*flushInterval {
1348+
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", flushInterval, "optimum", float64(chosen-bc.lastWrite)/TriesInMemory)
13461349
}
13471350
// Flush an entire trie and restart the counters
13481351
bc.triedb.Commit(header.Root, true, nil)
@@ -2437,3 +2440,9 @@ func (bc *BlockChain) SetBlockValidatorAndProcessorForTesting(v Validator, p Pro
24372440
bc.validator = v
24382441
bc.processor = p
24392442
}
2443+
2444+
// SetTrieFlushInterval configures how often in-memory tries are persisted to disk.
2445+
// It is thread-safe and can be called repeatedly without side effects.
2446+
func (bc *BlockChain) SetTrieFlushInterval(interval time.Duration) {
2447+
atomic.StoreInt64(&bc.flushInterval, int64(interval))
2448+
}

eth/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,14 @@ func (api *DebugAPI) GetAccessibleState(from, to rpc.BlockNumber) (uint64, error
590590
}
591591
return 0, errors.New("no state found")
592592
}
593+
594+
// SetTrieFlushInterval configures how often in-memory tries are persisted
595+
// to disk. The value is
596+
func (api *DebugAPI) SetTrieFlushInterval(interval string) error {
597+
t, err := time.ParseDuration(interval)
598+
if err != nil {
599+
return err
600+
}
601+
api.eth.blockchain.SetTrieFlushInterval(t)
602+
return nil
603+
}

internal/web3ext/web3ext.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ web3._extend({
490490
call: 'debug_dbAncients',
491491
params: 0
492492
}),
493+
new web3._extend.Method({
494+
name: 'setTrieFlushInterval',
495+
call: 'debug_setTrieFlushInterval',
496+
params: 1
497+
}),
493498
],
494499
properties: []
495500
});

0 commit comments

Comments
 (0)