Skip to content

Commit dbae8d7

Browse files
authored
Merge pull request #7590 from multiversx/backup-fixes
2 parents 4754a77 + 69139e7 commit dbae8d7

17 files changed

+136
-147
lines changed

consensus/spos/bls/v1/subroundBlock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func checkNewSubroundBlockParams(
6666

6767
// doBlockJob method does the job of the subround Block
6868
func (sr *subroundBlock) doBlockJob(ctx context.Context) bool {
69-
isSelfLeader := sr.IsSelfLeaderInCurrentRound() && sr.ShouldConsiderSelfKeyInConsensus()
69+
isSelfLeader := sr.IsSelfLeaderInCurrentRound() && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler())
7070
if !isSelfLeader && !sr.IsMultiKeyLeaderInCurrentRound() { // is NOT self leader in this round?
7171
return false
7272
}

consensus/spos/bls/v1/subroundEndRound.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func (sr *subroundEndRound) createAndBroadcastHeaderFinalInfo() {
594594
}
595595

596596
func (sr *subroundEndRound) createAndBroadcastInvalidSigners(invalidSigners []byte) {
597-
isSelfLeader := sr.IsSelfLeaderInCurrentRound() && sr.ShouldConsiderSelfKeyInConsensus()
597+
isSelfLeader := sr.IsSelfLeaderInCurrentRound() && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler())
598598
if !(isSelfLeader || sr.IsMultiKeyLeaderInCurrentRound()) {
599599
return
600600
}

consensus/spos/bls/v1/subroundSignature.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func (sr *subroundSignature) doSignatureJob(_ context.Context) bool {
8181
return false
8282
}
8383

84-
isSelfLeader := sr.IsSelfLeaderInCurrentRound() && sr.ShouldConsiderSelfKeyInConsensus()
85-
isSelfInConsensusGroup := sr.IsNodeInConsensusGroup(sr.SelfPubKey()) && sr.ShouldConsiderSelfKeyInConsensus()
84+
isSelfLeader := sr.IsSelfLeaderInCurrentRound() && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler())
85+
isSelfInConsensusGroup := sr.IsNodeInConsensusGroup(sr.SelfPubKey()) && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler())
8686

8787
if isSelfLeader || isSelfInConsensusGroup {
8888
selfIndex, err := sr.SelfConsensusGroupIndex()

consensus/spos/bls/v1/subroundStartRound.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (sr *subroundStartRound) initCurrentRound() bool {
173173
if sr.IsKeyManagedBySelf([]byte(leader)) {
174174
msg = " (my turn in multi-key)"
175175
}
176-
if leader == sr.SelfPubKey() && sr.ShouldConsiderSelfKeyInConsensus() {
176+
if leader == sr.SelfPubKey() && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler()) {
177177
msg = " (my turn)"
178178
}
179179
if len(msg) != 0 {
@@ -192,7 +192,7 @@ func (sr *subroundStartRound) initCurrentRound() bool {
192192

193193
sr.indexRoundIfNeeded(pubKeys)
194194

195-
isSingleKeyLeader := leader == sr.SelfPubKey() && sr.ShouldConsiderSelfKeyInConsensus()
195+
isSingleKeyLeader := leader == sr.SelfPubKey() && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler())
196196
isLeader := isSingleKeyLeader || sr.IsKeyManagedBySelf([]byte(leader))
197197
isSelfInConsensus := sr.IsNodeInConsensusGroup(sr.SelfPubKey()) || numMultiKeysInConsensusGroup > 0
198198
if !isSelfInConsensus {

consensus/spos/bls/v2/subroundBlock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ func (sr *subroundBlock) CanProcessReceivedHeader(headerLeader string) bool {
663663
}
664664

665665
func (sr *subroundBlock) shouldProcessBlock(headerLeader string) bool {
666-
if sr.IsNodeSelf(headerLeader) {
666+
if sr.IsNodeSelf(headerLeader) && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler()) {
667667
return false
668668
}
669669
if sr.IsJobDone(headerLeader, sr.Current()) {

consensus/spos/bls/v2/subroundEndRound.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func (sr *subroundEndRound) shouldSendProof() bool {
394394
return false
395395
}
396396

397-
return sr.IsSelfInConsensusGroup()
397+
return sr.IsSelfInConsensusGroup() && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler())
398398
}
399399

400400
func (sr *subroundEndRound) aggregateSigsAndHandleInvalidSigners(bitmap []byte, sender string) ([]byte, []byte, error) {
@@ -673,7 +673,7 @@ func (sr *subroundEndRound) createAndBroadcastInvalidSigners(
673673
invalidSignersPubKeys []string,
674674
sender string,
675675
) {
676-
if !sr.ShouldConsiderSelfKeyInConsensus() && !sr.IsMultiKeyInConsensusGroup() {
676+
if !spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler()) && !sr.IsMultiKeyInConsensusGroup() {
677677
return
678678
}
679679

consensus/spos/bls/v2/subroundSignature.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (sr *subroundSignature) doSignatureJob(ctx context.Context) bool {
100100
return true
101101
}
102102

103-
isSelfSingleKeyInConsensusGroup := sr.IsNodeInConsensusGroup(sr.SelfPubKey()) && sr.ShouldConsiderSelfKeyInConsensus()
103+
isSelfSingleKeyInConsensusGroup := sr.IsNodeInConsensusGroup(sr.SelfPubKey()) && spos.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler())
104104
if isSelfSingleKeyInConsensusGroup {
105105
if !sr.doSignatureJobForSingleKey() {
106106
return false

consensus/spos/common.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
package spos
22

33
import (
4+
"github.com/multiversx/mx-chain-core-go/core/check"
45
"github.com/multiversx/mx-chain-go/common"
6+
"github.com/multiversx/mx-chain-go/consensus"
57
"github.com/multiversx/mx-chain-go/sharding"
68
)
79

810
// GetConsensusTopicID will construct and return the topic ID based on shard coordinator
911
func GetConsensusTopicID(shardCoordinator sharding.Coordinator) string {
1012
return common.ConsensusTopic + shardCoordinator.CommunicationIdentifier(shardCoordinator.SelfId())
1113
}
14+
15+
// ShouldConsiderSelfKeyInConsensus returns true if current machine is the main one, or it is a backup machine but the main
16+
// machine failed
17+
func ShouldConsiderSelfKeyInConsensus(redundancyHandler consensus.NodeRedundancyHandler) bool {
18+
if check.IfNil(redundancyHandler) {
19+
log.Warn("redundancy handler is nil")
20+
return false
21+
}
22+
23+
isMainMachine := !redundancyHandler.IsRedundancyNode()
24+
if isMainMachine {
25+
return true
26+
}
27+
return !redundancyHandler.IsMainMachineActive()
28+
29+
}

consensus/spos/common_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package spos
2+
3+
import (
4+
"testing"
5+
6+
"github.com/multiversx/mx-chain-go/consensus/mock"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestShouldConsiderSelfKeyInConsensus(t *testing.T) {
11+
t.Parallel()
12+
13+
require.False(t, ShouldConsiderSelfKeyInConsensus(nil))
14+
require.True(t, ShouldConsiderSelfKeyInConsensus(&mock.NodeRedundancyHandlerStub{
15+
IsRedundancyNodeCalled: func() bool {
16+
return false
17+
},
18+
}))
19+
require.True(t, ShouldConsiderSelfKeyInConsensus(&mock.NodeRedundancyHandlerStub{
20+
IsMainMachineActiveCalled: func() bool {
21+
return true
22+
},
23+
}))
24+
}

consensus/spos/consensusState.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,23 @@ type ConsensusState struct {
4848
*roundStatus
4949

5050
mutState sync.RWMutex
51+
52+
redundancyHandler consensus.NodeRedundancyHandler
5153
}
5254

5355
// NewConsensusState creates a new ConsensusState object
5456
func NewConsensusState(
5557
roundConsensus *roundConsensus,
5658
roundThreshold *roundThreshold,
5759
roundStatus *roundStatus,
60+
redundancyHandler consensus.NodeRedundancyHandler,
5861
) *ConsensusState {
5962

6063
cns := ConsensusState{
61-
roundConsensus: roundConsensus,
62-
roundThreshold: roundThreshold,
63-
roundStatus: roundStatus,
64+
roundConsensus: roundConsensus,
65+
roundThreshold: roundThreshold,
66+
roundStatus: roundStatus,
67+
redundancyHandler: redundancyHandler,
6468
}
6569

6670
cns.ResetConsensusState()
@@ -266,7 +270,7 @@ func (cns *ConsensusState) CanDoSubroundJob(currentSubroundId int) bool {
266270
// CanProcessReceivedMessage method returns true if the message received can be processed and false otherwise
267271
func (cns *ConsensusState) CanProcessReceivedMessage(cnsDta *consensus.Message, currentRoundIndex int64,
268272
currentSubroundId int) bool {
269-
if cns.IsNodeSelf(string(cnsDta.PubKey)) {
273+
if cns.IsNodeSelf(string(cnsDta.PubKey)) && ShouldConsiderSelfKeyInConsensus(cns.redundancyHandler) {
270274
return false
271275
}
272276

0 commit comments

Comments
 (0)