Skip to content

Commit a715190

Browse files
committed
#628 add checks if all blocksmith time has expired
1 parent c7e2fa3 commit a715190

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

core/service/blockMainService.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,33 @@ func (bs *BlockService) ScanBlockPool() error {
650650
}
651651
}
652652
}
653+
// check if we have passed the last blocksmith expiry time
654+
numberOfBlocksmiths := len(bs.BlocksmithStrategy.GetSortedBlocksmithsMap(previousBlock))
655+
lastBlocksmithExpiry := bs.BlocksmithStrategy.GetSmithTime(int64(numberOfBlocksmiths-1), previousBlock) +
656+
constant.SmithingBlockCreationTime + constant.SmithingNetworkTolerance
657+
if len(blocks) > 0 &&
658+
time.Now().Unix() > lastBlocksmithExpiry {
659+
// choose block to persist since all blocksmith time has expired
660+
var (
661+
bestIndex int64
662+
bestCumDiff = new(big.Int).SetInt64(0)
663+
)
664+
for index, block := range blocks {
665+
currBlockCumDiff, _ := new(big.Int).SetString(block.GetCumulativeDifficulty(), 10)
666+
if currBlockCumDiff.Cmp(bestCumDiff) > 0 {
667+
bestCumDiff = currBlockCumDiff
668+
bestIndex = index
669+
}
670+
}
671+
bs.ChainWriteLock(constant.BlockchainStatusReceivingBlock)
672+
err := bs.PushBlock(previousBlock, blocks[bestIndex], true, true)
673+
bs.ChainWriteUnlock(constant.BlockchainStatusReceivingBlock)
674+
if err != nil {
675+
return blocker.NewBlocker(
676+
blocker.BlockErr, "ScanBlockPool:PushBlockFail",
677+
)
678+
}
679+
}
653680
return nil
654681
}
655682

0 commit comments

Comments
 (0)