Skip to content

Commit

Permalink
miner: adapt time uint for bep-520 poc test
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBSC committed Feb 21, 2025
1 parent 30b0899 commit 14ce9d0
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,10 @@ func (w *worker) delayBlocksBroadcastLoop() {

currentBlock := w.chain.CurrentBlock()
currentBlockNum := currentBlock.Number.Uint64()
parentHeader := w.chain.GetHeaderByHash(currentBlock.ParentHash)
blockInterval, _ := w.engine.BlockInterval(w.chain, parentHeader)

delayTime := (w.config.MB.BroadcastDelayBlocks - 1) * w.chainConfig.Parlia.Period
delayTime := (w.config.MB.BroadcastDelayBlocks - 1) * blockInterval
if p, ok := w.engine.(*parlia.Parlia); ok {
service := p.APIs(w.chain)[0].Service
latestBlockNumber := rpc.LatestBlockNumber
Expand All @@ -734,16 +736,16 @@ func (w *worker) delayBlocksBroadcastLoop() {
nonInTurnBackoff = uint64(currentTurnLength)
}
}
delayTime += nonInTurnBackoff
delayTime += nonInTurnBackoff * 1000
}

firstBlock := w.delayedBlocksForBroadcast[0]
if uint64(time.Now().Unix()) >= (firstBlock.Time() + delayTime) {
if uint64(time.Now().UnixMilli()) >= (firstBlock.Header().TimeInMilliseconds() + delayTime) {
time.Sleep(500 * time.Microsecond)
for _, block := range w.delayedBlocksForBroadcast {
w.mux.Post(core.NewMinedBlockEvent{Block: block})
log.Info("delayBlocksBroadcastLoop", "number", block.Number(), "hash", block.Hash(),
"time", block.Time(), "now", uint64(time.Now().Unix()), "currentBlockNum", currentBlockNum)
"time", block.Header().TimeInMilliseconds(), "now", uint64(time.Now().UnixMilli()), "currentBlockNum", currentBlockNum)
}
w.delayedBlocksForBroadcast = make([]*types.Block, 0)
}
Expand Down Expand Up @@ -1387,13 +1389,16 @@ LOOP:

delay := w.engine.Delay(w.chain, work.header, &w.config.DelayLeftOver)
if p, ok := w.engine.(*parlia.Parlia); ok {
if w.config.MB.LastBlockMiningTime > w.chainConfig.Parlia.Period*1000/2 {
currentBlock := w.chain.CurrentBlock()
parentHeader := w.chain.GetHeaderByHash(currentBlock.ParentHash)
blockInterval, _ := w.engine.BlockInterval(w.chain, parentHeader)
if w.config.MB.LastBlockMiningTime > blockInterval/2 {
service := p.APIs(w.chain)[0].Service
latestBlockNumber := rpc.LatestBlockNumber
currentTurnLength, err := service.(*parlia.API).GetTurnLength(&latestBlockNumber)
if err == nil && (work.header.Number.Uint64()+1)%uint64(currentTurnLength) == 0 {
*delay += time.Duration((w.config.MB.LastBlockMiningTime - w.chainConfig.Parlia.Period*1000/2) * uint64(time.Millisecond))
timeLeft := time.Until(time.Unix(int64(work.header.Time), 0))
*delay += time.Duration((w.config.MB.LastBlockMiningTime - blockInterval/2) * uint64(time.Millisecond))
timeLeft := time.Until(time.UnixMilli(int64(work.header.TimeInMilliseconds())))
if *delay > timeLeft {
*delay = timeLeft
}
Expand Down

0 comments on commit 14ce9d0

Please sign in to comment.