diff --git a/consensus/spos/bls/v2/subroundBlock.go b/consensus/spos/bls/v2/subroundBlock.go index 6838343f57f..3677ef7a635 100644 --- a/consensus/spos/bls/v2/subroundBlock.go +++ b/consensus/spos/bls/v2/subroundBlock.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/hex" + "sync" "time" "github.com/multiversx/mx-chain-core-go/core" @@ -25,6 +26,7 @@ type subroundBlock struct { processingThresholdPercentage int worker spos.WorkerHandler + mutBlockProcessing sync.Mutex } // NewSubroundBlock creates a subroundBlock object @@ -555,10 +557,13 @@ func (sr *subroundBlock) receivedBlockHeader(headerHandler data.HeaderHandler) { // CanProcessReceivedHeader method returns true if the received header can be processed and false otherwise func (sr *subroundBlock) CanProcessReceivedHeader(headerLeader string) bool { + return sr.shouldProcessBlock(headerLeader) +} + +func (sr *subroundBlock) shouldProcessBlock(headerLeader string) bool { if sr.IsNodeSelf(headerLeader) { return false } - if sr.IsJobDone(headerLeader, sr.Current()) { return false } @@ -582,6 +587,9 @@ func (sr *subroundBlock) processReceivedBlock( return false } + sr.mutBlockProcessing.Lock() + defer sr.mutBlockProcessing.Unlock() + defer func() { sr.SetProcessingBlock(false) }() @@ -599,6 +607,11 @@ func (sr *subroundBlock) processReceivedBlock( return false } + // check again under critical section to avoid double execution + if !sr.shouldProcessBlock(string(senderPK)) { + return false + } + return sr.processBlock(ctx, round, senderPK) }