Skip to content

Commit 68a0244

Browse files
authored
Merge pull request #6806 from multiversx/metablock-consensus-fixes
avoid executing twice the block
2 parents a88d56f + 5e9d8b2 commit 68a0244

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

consensus/spos/bls/v2/subroundBlock.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/hex"
7+
"sync"
78
"time"
89

910
"github.com/multiversx/mx-chain-core-go/core"
@@ -25,6 +26,7 @@ type subroundBlock struct {
2526

2627
processingThresholdPercentage int
2728
worker spos.WorkerHandler
29+
mutBlockProcessing sync.Mutex
2830
}
2931

3032
// NewSubroundBlock creates a subroundBlock object
@@ -555,10 +557,13 @@ func (sr *subroundBlock) receivedBlockHeader(headerHandler data.HeaderHandler) {
555557

556558
// CanProcessReceivedHeader method returns true if the received header can be processed and false otherwise
557559
func (sr *subroundBlock) CanProcessReceivedHeader(headerLeader string) bool {
560+
return sr.shouldProcessBlock(headerLeader)
561+
}
562+
563+
func (sr *subroundBlock) shouldProcessBlock(headerLeader string) bool {
558564
if sr.IsNodeSelf(headerLeader) {
559565
return false
560566
}
561-
562567
if sr.IsJobDone(headerLeader, sr.Current()) {
563568
return false
564569
}
@@ -582,6 +587,9 @@ func (sr *subroundBlock) processReceivedBlock(
582587
return false
583588
}
584589

590+
sr.mutBlockProcessing.Lock()
591+
defer sr.mutBlockProcessing.Unlock()
592+
585593
defer func() {
586594
sr.SetProcessingBlock(false)
587595
}()
@@ -599,6 +607,11 @@ func (sr *subroundBlock) processReceivedBlock(
599607
return false
600608
}
601609

610+
// check again under critical section to avoid double execution
611+
if !sr.shouldProcessBlock(string(senderPK)) {
612+
return false
613+
}
614+
602615
return sr.processBlock(ctx, round, senderPK)
603616
}
604617

0 commit comments

Comments
 (0)