Skip to content

Commit e6c8bea

Browse files
authored
Merge pull request #442 from OffchainLabs/gcproc-rand-offset
add random offset to trie-time-limit
2 parents 888c7e9 + af5cc97 commit e6c8bea

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

core/blockchain.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ type CacheConfig struct {
160160
SnapshotRestoreMaxGas uint64 // Rollback up to this much gas to restore snapshot (otherwise snapshot recalculated from nothing)
161161

162162
// Arbitrum: configure GC window
163-
TriesInMemory uint64 // Height difference before which a trie may not be garbage-collected
164-
TrieRetention time.Duration // Time limit before which a trie may not be garbage-collected
163+
TriesInMemory uint64 // Height difference before which a trie may not be garbage-collected
164+
TrieRetention time.Duration // Time limit before which a trie may not be garbage-collected
165+
TrieTimeLimitRandomOffset time.Duration // Range of random offset of each commit due to TrieTimeLimit period
165166

166167
MaxNumberOfBlocksToSkipStateSaving uint32
167168
MaxAmountOfGasToSkipStateSaving uint64
@@ -203,6 +204,7 @@ var defaultCacheConfig = &CacheConfig{
203204
// Arbitrum Config Options
204205
TriesInMemory: state.DefaultTriesInMemory,
205206
TrieRetention: 30 * time.Minute,
207+
TrieTimeLimitRandomOffset: 0,
206208
MaxNumberOfBlocksToSkipStateSaving: 0,
207209
MaxAmountOfGasToSkipStateSaving: 0,
208210

@@ -295,8 +297,10 @@ type BlockChain struct {
295297
vmConfig vm.Config
296298
logger *tracing.Hooks
297299

300+
// Arbitrum:
298301
numberOfBlocksToSkipStateSaving uint32
299302
amountOfGasInBlocksToSkipStateSaving uint64
303+
gcprocRandOffset time.Duration // random offset for gcproc time
300304
}
301305

302306
type trieGcEntry struct {
@@ -376,6 +380,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
376380
bc.prefetcher = newStatePrefetcher(chainConfig, bc.hc)
377381
bc.processor = NewStateProcessor(chainConfig, bc.hc)
378382

383+
bc.gcprocRandOffset = bc.generateGcprocRandOffset()
384+
379385
bc.genesisBlock = bc.GetBlockByNumber(0)
380386
if bc.genesisBlock == nil {
381387
return nil, ErrNoGenesis
@@ -1627,8 +1633,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
16271633
}
16281634
flushInterval := time.Duration(bc.flushInterval.Load())
16291635
// If we exceeded out time allowance, flush an entire trie to disk
1636+
// The time threshold can be offset by gcprocRandOffset if TrieTimeLimitRandomOffset > 0; the offset is re-generated after each full trie flush
16301637
// In case of archive node that skips some trie commits we don't flush tries here
1631-
if bc.gcproc > flushInterval && prevEntry != nil && !archiveNode {
1638+
if bc.gcproc+bc.gcprocRandOffset > flushInterval && prevEntry != nil && !archiveNode {
16321639
// If the header is missing (canonical chain behind), we're reorging a low
16331640
// diff sidechain. Suspend committing until this operation is completed.
16341641
header := bc.GetHeaderByNumber(prevNum)
@@ -1644,6 +1651,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
16441651
bc.triedb.Commit(header.Root, true)
16451652
bc.lastWrite = prevNum
16461653
bc.gcproc = 0
1654+
bc.gcprocRandOffset = bc.generateGcprocRandOffset()
16471655
}
16481656
}
16491657
if prevEntry != nil {

core/blockchain_arbitrum.go

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package core
1919

2020
import (
2121
"fmt"
22+
"math/rand/v2"
2223
"time"
2324

2425
"github.com/ethereum/go-ethereum/common"
@@ -29,6 +30,13 @@ import (
2930
"github.com/ethereum/go-ethereum/rpc"
3031
)
3132

33+
func (bc *BlockChain) generateGcprocRandOffset() time.Duration {
34+
if bc.cacheConfig.TrieTimeLimitRandomOffset > 0 {
35+
return time.Duration(rand.Int64N(int64(bc.cacheConfig.TrieTimeLimitRandomOffset)))
36+
}
37+
return 0
38+
}
39+
3240
func (bc *BlockChain) FlushTrieDB(capLimit common.StorageSize) error {
3341
if bc.triedb.Scheme() == rawdb.PathScheme {
3442
return nil
@@ -53,6 +61,7 @@ func (bc *BlockChain) FlushTrieDB(capLimit common.StorageSize) error {
5361
}
5462

5563
bc.gcproc = 0
64+
bc.gcprocRandOffset = bc.generateGcprocRandOffset()
5665
bc.lastWrite = blockNumber
5766
}
5867
}

0 commit comments

Comments
 (0)