@@ -91,7 +91,6 @@ const (
91
91
txLookupCacheLimit = 1024
92
92
maxFutureBlocks = 256
93
93
maxTimeFutureBlocks = 30
94
- TriesInMemory = 128
95
94
96
95
// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
97
96
//
@@ -132,12 +131,21 @@ type CacheConfig struct {
132
131
SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory
133
132
Preimages bool // Whether to store preimage of trie key to the disk
134
133
134
+ // Arbitrum: configure GC window
135
+ TriesInMemory uint64 // Height difference before which a trie may not be garbage-collected
136
+ TrieRetention time.Duration // Time limit before which a trie may not be garbage-collected
137
+
135
138
SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it
136
139
}
137
140
138
141
// defaultCacheConfig are the default caching values if none are specified by the
139
142
// user (also used during testing).
140
143
var defaultCacheConfig = & CacheConfig {
144
+
145
+ // Arbitrum Config Options
146
+ TriesInMemory : 128 ,
147
+ TrieRetention : 30 * time .Minute ,
148
+
141
149
TrieCleanLimit : 256 ,
142
150
TrieDirtyLimit : 256 ,
143
151
TrieTimeLimit : 5 * time .Minute ,
@@ -862,6 +870,9 @@ func (bc *BlockChain) Stop() {
862
870
}
863
871
}
864
872
873
+ // Arbitrum: only discard tries sufficiently old in both time and height
874
+ retain := bc .FindRetentionBound ()
875
+
865
876
// Ensure the state of a recent block is also stored to disk before exiting.
866
877
// We're writing three different states to catch different restart scenarios:
867
878
// - HEAD: So we don't need to reprocess any blocks in the general case
@@ -870,7 +881,7 @@ func (bc *BlockChain) Stop() {
870
881
if ! bc .cacheConfig .TrieDirtyDisabled {
871
882
triedb := bc .stateCache .TrieDB ()
872
883
873
- for _ , offset := range []uint64 {0 , 1 , TriesInMemory - 1 } {
884
+ for _ , offset := range []uint64 {0 , 1 , retain - 1 } {
874
885
if number := bc .CurrentBlock ().NumberU64 (); number > offset {
875
886
recent := bc .GetBlockByNumber (number - offset )
876
887
if recent .Root () == (common.Hash {}) {
@@ -1275,6 +1286,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1275
1286
}
1276
1287
triedb := bc .stateCache .TrieDB ()
1277
1288
1289
+ // Arbitrum: only discard tries sufficiently old in both time and height
1290
+ retain := bc .FindRetentionBound ()
1291
+
1278
1292
// If we're running an archive node, always flush
1279
1293
if bc .cacheConfig .TrieDirtyDisabled {
1280
1294
return triedb .Commit (root , false , nil )
@@ -1283,7 +1297,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1283
1297
triedb .Reference (root , common.Hash {}) // metadata reference to keep trie alive
1284
1298
bc .triegc .Push (root , - int64 (block .NumberU64 ()))
1285
1299
1286
- if current := block .NumberU64 (); current > TriesInMemory {
1300
+ if current := block .NumberU64 (); current > retain {
1287
1301
// If we exceeded our memory allowance, flush matured singleton nodes to disk
1288
1302
var (
1289
1303
nodes , imgs = triedb .Size ()
@@ -1293,7 +1307,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1293
1307
triedb .Cap (limit - ethdb .IdealBatchSize )
1294
1308
}
1295
1309
// Find the next state trie we need to commit
1296
- chosen := current - TriesInMemory
1310
+ chosen := current - retain
1297
1311
1298
1312
// If we exceeded out time allowance, flush an entire trie to disk
1299
1313
if bc .gcproc > bc .cacheConfig .TrieTimeLimit {
@@ -1305,8 +1319,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1305
1319
} else {
1306
1320
// If we're exceeding limits but haven't reached a large enough memory gap,
1307
1321
// warn the user that the system is becoming unstable.
1308
- if chosen < lastWrite + TriesInMemory && bc .gcproc >= 2 * bc .cacheConfig .TrieTimeLimit {
1309
- log .Info ("State in memory for too long, committing" , "time" , bc .gcproc , "allowance" , bc .cacheConfig .TrieTimeLimit , "optimum" , float64 (chosen - lastWrite )/ TriesInMemory )
1322
+ if chosen < lastWrite + retain && bc .gcproc >= 2 * bc .cacheConfig .TrieTimeLimit {
1323
+ log .Info ("State in memory for too long, committing" , "time" , bc .gcproc , "allowance" , bc .cacheConfig .TrieTimeLimit , "optimum" , float64 (chosen - lastWrite )/ float64 ( retain ) )
1310
1324
}
1311
1325
// Flush an entire trie and restart the counters
1312
1326
triedb .Commit (header .Root , true , nil )
0 commit comments