@@ -91,7 +91,6 @@ const (
91
91
maxBeyondBlocks = 2048
92
92
93
93
diffLayerFreezerRecheckInterval = 3 * time .Second
94
- diffLayerFreezerBlockLimit = 864000 // The number of diff layers that should be kept in disk.
95
94
diffLayerPruneRecheckInterval = 1 * time .Second // The interval to prune unverified diff layers
96
95
maxDiffQueueDist = 2048 // Maximum allowed distance from the chain head to queue diffLayers
97
96
maxDiffLimit = 2048 // Maximum number of unique diff layers a peer may have responded
@@ -213,9 +212,11 @@ type BlockChain struct {
213
212
futureBlocks * lru.Cache // future blocks are blocks added for later processing
214
213
215
214
// trusted diff layers
216
- diffLayerCache * lru.Cache // Cache for the diffLayers
217
- diffLayerRLPCache * lru.Cache // Cache for the rlp encoded diffLayers
218
- diffQueue * prque.Prque // A Priority queue to store recent diff layer
215
+ diffLayerCache * lru.Cache // Cache for the diffLayers
216
+ diffLayerRLPCache * lru.Cache // Cache for the rlp encoded diffLayers
217
+ diffQueue * prque.Prque // A Priority queue to store recent diff layer
218
+ diffQueueBuffer chan * types.DiffLayer
219
+ diffLayerFreezerBlockLimit uint64
219
220
220
221
// untrusted diff layers
221
222
diffMux sync.RWMutex
@@ -285,6 +286,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
285
286
engine : engine ,
286
287
vmConfig : vmConfig ,
287
288
diffQueue : prque .New (nil ),
289
+ diffQueueBuffer : make (chan * types.DiffLayer ),
288
290
blockHashToDiffLayers : make (map [common.Hash ]map [common.Hash ]* types.DiffLayer ),
289
291
diffHashToBlockHash : make (map [common.Hash ]common.Hash ),
290
292
diffHashToPeers : make (map [common.Hash ]map [string ]struct {}),
@@ -444,7 +446,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
444
446
}
445
447
// Need persist and prune diff layer
446
448
if bc .db .DiffStore () != nil {
447
- go bc .trustedDiffLayerFreezeLoop ()
449
+ go bc .trustedDiffLayerLoop ()
448
450
}
449
451
go bc .untrustedDiffLayerPruneLoop ()
450
452
@@ -464,7 +466,7 @@ func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer) {
464
466
bc .diffLayerCache .Add (diffLayer .BlockHash , diffLayer )
465
467
if bc .db .DiffStore () != nil {
466
468
// push to priority queue before persisting
467
- bc .diffQueue . Push ( diffLayer , - ( int64 ( diffLayer . Number )))
469
+ bc .diffQueueBuffer <- diffLayer
468
470
}
469
471
}
470
472
@@ -967,7 +969,7 @@ func (bc *BlockChain) GetDiffLayerRLP(blockHash common.Hash) rlp.RawValue {
967
969
}
968
970
rawData := rawdb .ReadDiffLayerRLP (diffStore , blockHash )
969
971
if len (rawData ) != 0 {
970
- bc .diffLayerRLPCache .Add (blockHash , rlp . RawValue ( rawData ) )
972
+ bc .diffLayerRLPCache .Add (blockHash , rawData )
971
973
}
972
974
return rawData
973
975
}
@@ -2009,8 +2011,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
2009
2011
storageUpdateTimer .Update (statedb .StorageUpdates ) // Storage updates are complete, we can mark them
2010
2012
snapshotAccountReadTimer .Update (statedb .SnapshotAccountReads ) // Account reads are complete, we can mark them
2011
2013
snapshotStorageReadTimer .Update (statedb .SnapshotStorageReads ) // Storage reads are complete, we can mark them
2012
- trieproc := statedb .SnapshotAccountReads + statedb .AccountReads + statedb .AccountUpdates
2013
- trieproc += statedb .SnapshotStorageReads + statedb .StorageReads + statedb .StorageUpdates
2014
2014
2015
2015
blockExecutionTimer .Update (time .Since (substart ))
2016
2016
@@ -2401,12 +2401,14 @@ func (bc *BlockChain) update() {
2401
2401
}
2402
2402
}
2403
2403
2404
- func (bc * BlockChain ) trustedDiffLayerFreezeLoop () {
2404
+ func (bc * BlockChain ) trustedDiffLayerLoop () {
2405
2405
recheck := time .Tick (diffLayerFreezerRecheckInterval )
2406
2406
bc .wg .Add (1 )
2407
2407
defer bc .wg .Done ()
2408
2408
for {
2409
2409
select {
2410
+ case diff := <- bc .diffQueueBuffer :
2411
+ bc .diffQueue .Push (diff , - (int64 (diff .Number )))
2410
2412
case <- bc .quit :
2411
2413
// Persist all diffLayers when shutdown, it will introduce redundant storage, but it is acceptable.
2412
2414
// If the client been ungracefully shutdown, it will missing all cached diff layers, it is acceptable as well.
@@ -2451,7 +2453,7 @@ func (bc *BlockChain) trustedDiffLayerFreezeLoop() {
2451
2453
batch = bc .db .DiffStore ().NewBatch ()
2452
2454
}
2453
2455
rawdb .WriteDiffLayer (batch , diffLayer .BlockHash , diffLayer )
2454
- staleHash := bc .GetCanonicalHash (uint64 (- prio ) - diffLayerFreezerBlockLimit )
2456
+ staleHash := bc .GetCanonicalHash (uint64 (- prio ) - bc . diffLayerFreezerBlockLimit )
2455
2457
rawdb .DeleteDiffLayer (batch , staleHash )
2456
2458
}
2457
2459
} else {
@@ -2511,16 +2513,12 @@ func (bc *BlockChain) removeDiffLayers(diffHash common.Hash) {
2511
2513
// Untrusted peers
2512
2514
pids := bc .diffHashToPeers [diffHash ]
2513
2515
invalidDiffHashes := make (map [common.Hash ]struct {})
2514
- if pids != nil {
2515
- for pid := range pids {
2516
- invaliDiffHashesPeer := bc .diffPeersToDiffHashes [pid ]
2517
- if invaliDiffHashesPeer != nil {
2518
- for invaliDiffHash := range invaliDiffHashesPeer {
2519
- invalidDiffHashes [invaliDiffHash ] = struct {}{}
2520
- }
2521
- }
2522
- delete (bc .diffPeersToDiffHashes , pid )
2516
+ for pid := range pids {
2517
+ invaliDiffHashesPeer := bc .diffPeersToDiffHashes [pid ]
2518
+ for invaliDiffHash := range invaliDiffHashesPeer {
2519
+ invalidDiffHashes [invaliDiffHash ] = struct {}{}
2523
2520
}
2521
+ delete (bc .diffPeersToDiffHashes , pid )
2524
2522
}
2525
2523
for invalidDiffHash := range invalidDiffHashes {
2526
2524
delete (bc .diffHashToPeers , invalidDiffHash )
@@ -2602,7 +2600,7 @@ func (bc *BlockChain) pruneDiffLayer() {
2602
2600
break
2603
2601
}
2604
2602
}
2605
- staleDiffHashes := make (map [common.Hash ]struct {}, 0 )
2603
+ staleDiffHashes := make (map [common.Hash ]struct {})
2606
2604
for blockHash := range staleBlockHashes {
2607
2605
if diffHashes , exist := bc .blockHashToDiffLayers [blockHash ]; exist {
2608
2606
for diffHash := range diffHashes {
@@ -2932,3 +2930,10 @@ func EnableLightProcessor(bc *BlockChain) *BlockChain {
2932
2930
bc .processor = NewLightStateProcessor (bc .Config (), bc , bc .engine )
2933
2931
return bc
2934
2932
}
2933
+
2934
+ func EnablePersistDiff (limit uint64 ) BlockChainOption {
2935
+ return func (chain * BlockChain ) * BlockChain {
2936
+ chain .diffLayerFreezerBlockLimit = limit
2937
+ return chain
2938
+ }
2939
+ }
0 commit comments