From 65de7ae6bef51f22e703cc270d8024681d2fde4a Mon Sep 17 00:00:00 2001 From: Antony Denyer Date: Mon, 21 Aug 2023 11:32:36 +0100 Subject: [PATCH] QBFT: fix chain fork when emptyblockperiodseconds is enabled (#1669) Authored-by: H.HSEL <26063868+hhsel@users.noreply.github.com> --- consensus/istanbul/qbft/core/core.go | 8 ++++++-- consensus/istanbul/qbft/core/preprepare.go | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/consensus/istanbul/qbft/core/core.go b/consensus/istanbul/qbft/core/core.go index e078d4edd4..39e6043ce0 100644 --- a/consensus/istanbul/qbft/core/core.go +++ b/consensus/istanbul/qbft/core/core.go @@ -78,8 +78,9 @@ type core struct { backlogs map[common.Address]*prque.Prque backlogsMu *sync.Mutex - current *roundState - handlerWg *sync.WaitGroup + current *roundState + currentMutex sync.Mutex + handlerWg *sync.WaitGroup roundChangeSet *roundChangeSet roundChangeTimer *time.Timer @@ -116,6 +117,9 @@ func (c *core) IsCurrentProposal(blockHash common.Hash) bool { // startNewRound starts a new round. if round equals to 0, it means to starts a new sequence func (c *core) startNewRound(round *big.Int) { + c.currentMutex.Lock() + defer c.currentMutex.Unlock() + var logger log.Logger if c.current == nil { logger = c.logger.New("old.round", -1, "old.seq", 0) diff --git a/consensus/istanbul/qbft/core/preprepare.go b/consensus/istanbul/qbft/core/preprepare.go index 31305ec5aa..098a37c417 100644 --- a/consensus/istanbul/qbft/core/preprepare.go +++ b/consensus/istanbul/qbft/core/preprepare.go @@ -34,6 +34,11 @@ import ( // - extends PRE-PREPARE message with ROUND-CHANGE and PREPARE justification // - broadcast PRE-PREPARE message to other validators func (c *core) sendPreprepareMsg(request *Request) { + // c.current and c.valSet (checked in IsProposer()) is updated asynchronously in startNewRound(), + // need to prevent race condition with mutex + c.currentMutex.Lock() + defer c.currentMutex.Unlock() + logger := c.currentLogger(true, nil) // If I'm the proposer and I have the same sequence with the proposal