Skip to content

Commit 60648fb

Browse files
committed
random offset gcproc for each full trie flush
1 parent eef1d24 commit 60648fb

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

core/blockchain.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"io"
2424
"math"
2525
"math/big"
26-
"math/rand/v2"
2726
"runtime"
2827
"slices"
2928
"strings"
@@ -163,7 +162,7 @@ type CacheConfig struct {
163162
// Arbitrum: configure GC window
164163
TriesInMemory uint64 // Height difference before which a trie may not be garbage-collected
165164
TrieRetention time.Duration // Time limit before which a trie may not be garbage-collected
166-
TrieTimeLimitRandomOffset time.Duration // Range of random offset of the commit due to TrieTimeLimit period (randomizes initial gcproc value)
165+
TrieTimeLimitRandomOffset time.Duration // Range of random offset of each commit due to TrieTimeLimit period
167166

168167
MaxNumberOfBlocksToSkipStateSaving uint32
169168
MaxAmountOfGasToSkipStateSaving uint64
@@ -298,8 +297,10 @@ type BlockChain struct {
298297
vmConfig vm.Config
299298
logger *tracing.Hooks
300299

300+
// Arbitrum:
301301
numberOfBlocksToSkipStateSaving uint32
302302
amountOfGasInBlocksToSkipStateSaving uint64
303+
gcprocRandOffset time.Duration // random offset for gcproc time
303304
}
304305

305306
type trieGcEntry struct {
@@ -379,9 +380,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
379380
bc.prefetcher = newStatePrefetcher(chainConfig, bc.hc)
380381
bc.processor = NewStateProcessor(chainConfig, bc.hc)
381382

382-
if cacheConfig.TrieTimeLimitRandomOffset > 0 {
383-
bc.gcproc = time.Duration(rand.Int64N(int64(cacheConfig.TrieTimeLimitRandomOffset)))
384-
}
383+
bc.gcprocRandOffset = bc.generateGcprocRandOffset()
385384

386385
bc.genesisBlock = bc.GetBlockByNumber(0)
387386
if bc.genesisBlock == nil {
@@ -1634,8 +1633,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
16341633
}
16351634
flushInterval := time.Duration(bc.flushInterval.Load())
16361635
// 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
16371637
// In case of archive node that skips some trie commits we don't flush tries here
1638-
if bc.gcproc > flushInterval && prevEntry != nil && !archiveNode {
1638+
if bc.gcproc+bc.gcprocRandOffset > flushInterval && prevEntry != nil && !archiveNode {
16391639
// If the header is missing (canonical chain behind), we're reorging a low
16401640
// diff sidechain. Suspend committing until this operation is completed.
16411641
header := bc.GetHeaderByNumber(prevNum)
@@ -1651,6 +1651,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
16511651
bc.triedb.Commit(header.Root, true)
16521652
bc.lastWrite = prevNum
16531653
bc.gcproc = 0
1654+
bc.gcprocRandOffset = bc.generateGcprocRandOffset()
16541655
}
16551656
}
16561657
if prevEntry != nil {

core/blockchain_arbitrum.go

Lines changed: 9 additions & 0 deletions
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)