Skip to content

Commit

Permalink
feat: time.Tick replaced with time.Ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Oct 21, 2021
1 parent 7910bf8 commit 8ddd7ae
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2486,9 +2486,12 @@ func (bc *BlockChain) update() {
}

func (bc *BlockChain) trustedDiffLayerLoop() {
recheck := time.Tick(diffLayerFreezerRecheckInterval)
recheck := time.NewTicker(diffLayerFreezerRecheckInterval)
bc.wg.Add(1)
defer bc.wg.Done()
defer func() {
bc.wg.Done()
recheck.Stop()
}()
for {
select {
case diff := <-bc.diffQueueBuffer:
Expand Down Expand Up @@ -2521,29 +2524,28 @@ func (bc *BlockChain) trustedDiffLayerLoop() {
batch.Reset()
}
return
case <-recheck:
case <-recheck.C:
currentHeight := bc.CurrentBlock().NumberU64()
var batch ethdb.Batch
for !bc.diffQueue.Empty() {
diff, prio := bc.diffQueue.Pop()
diffLayer := diff.(*types.DiffLayer)

// if the block old enough
if int64(currentHeight)+prio >= int64(bc.triesInMemory) {
canonicalHash := bc.GetCanonicalHash(uint64(-prio))
// on the canonical chain
if canonicalHash == diffLayer.BlockHash {
if batch == nil {
batch = bc.db.DiffStore().NewBatch()
}
rawdb.WriteDiffLayer(batch, diffLayer.BlockHash, diffLayer)
staleHash := bc.GetCanonicalHash(uint64(-prio) - bc.diffLayerFreezerBlockLimit)
rawdb.DeleteDiffLayer(batch, staleHash)
}
} else {
// if the block not old enough
if int64(currentHeight)+prio < int64(bc.triesInMemory) {
bc.diffQueue.Push(diffLayer, prio)
break
}
canonicalHash := bc.GetCanonicalHash(uint64(-prio))
// on the canonical chain
if canonicalHash == diffLayer.BlockHash {
if batch == nil {
batch = bc.db.DiffStore().NewBatch()
}
rawdb.WriteDiffLayer(batch, diffLayer.BlockHash, diffLayer)
staleHash := bc.GetCanonicalHash(uint64(-prio) - bc.diffLayerFreezerBlockLimit)
rawdb.DeleteDiffLayer(batch, staleHash)
}
if batch != nil && batch.ValueSize() > ethdb.IdealBatchSize {
if err := batch.Write(); err != nil {
panic(fmt.Sprintf("Failed to write diff layer, error %v", err))
Expand Down

0 comments on commit 8ddd7ae

Please sign in to comment.