From fb668be28cc777bbf2cb7fc960e379cc923525b7 Mon Sep 17 00:00:00 2001 From: qianbin Date: Mon, 23 Mar 2020 22:57:29 +0800 Subject: [PATCH] thor/node: improve pack loop fix a minor bug that in rare case the master node will ignore better block, when receives two or more blocks with same number almost at the same time. --- cmd/thor/node/packer_loop.go | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/cmd/thor/node/packer_loop.go b/cmd/thor/node/packer_loop.go index 26730808b..3b729b45b 100644 --- a/cmd/thor/node/packer_loop.go +++ b/cmd/thor/node/packer_loop.go @@ -66,30 +66,24 @@ func (n *Node) packerLoop(ctx context.Context) { } log.Debug("scheduled to pack block", "after", time.Duration(flow.When()-now)*time.Second) - const halfBlockInterval = thor.BlockInterval / 2 - for { - now := uint64(time.Now().Unix()) - if flow.When() > now+halfBlockInterval { - delaySec := flow.When() - (now + halfBlockInterval) - select { - case <-ctx.Done(): - return - case <-ticker.C(): - if n.repo.BestBlock().Header().TotalScore() > flow.TotalScore() { - log.Debug("re-schedule packer due to new best block") - goto RE_SCHEDULE - } - case <-time.After(time.Duration(delaySec) * time.Second): - goto PACK + if uint64(time.Now().Unix())+thor.BlockInterval/2 > flow.When() { + // time to pack block + // blockInterval/2 early to allow more time for processing txs + if err := n.pack(flow); err != nil { + log.Error("failed to pack block", "err", err) + } + break + } + select { + case <-ctx.Done(): + return + case <-time.After(time.Second): + if n.repo.BestBlock().Header().TotalScore() > flow.TotalScore() { + log.Debug("re-schedule packer due to new best block") + goto RE_SCHEDULE } - } else { - goto PACK } - } - PACK: - if err := n.pack(flow); err != nil { - log.Error("failed to pack block", "err", err) } RE_SCHEDULE: }