Skip to content

Commit 47cabdb

Browse files
committed
fix after review
1 parent 0931dbc commit 47cabdb

File tree

6 files changed

+80
-7
lines changed

6 files changed

+80
-7
lines changed

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

consensus/spos/consensusState_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
p2pMessage "github.com/multiversx/mx-chain-communication-go/p2p/message"
1111
"github.com/multiversx/mx-chain-core-go/core"
1212
"github.com/multiversx/mx-chain-core-go/data/block"
13+
"github.com/multiversx/mx-chain-go/consensus/mock"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
1516

@@ -58,6 +59,7 @@ func internalInitConsensusStateWithKeysHandler(keysHandler consensus.KeysHandler
5859
rcns,
5960
rthr,
6061
rstatus,
62+
&mock.NodeRedundancyHandlerStub{},
6163
)
6264

6365
return cns
@@ -423,11 +425,52 @@ func TestConsensusState_CanProcessReceivedMessageShouldReturnFalseWhenMessageIsR
423425
func TestConsensusState_CanProcessReceivedMessageShouldReturnFalseWhenMessageIsReceivedForOtherRound(t *testing.T) {
424426
t.Parallel()
425427

426-
cns := internalInitConsensusState()
428+
eligibleList := []string{"1", "2", "3"}
429+
430+
eligibleNodesPubKeys := make(map[string]struct{})
431+
for _, key := range eligibleList {
432+
eligibleNodesPubKeys[key] = struct{}{}
433+
}
434+
435+
rcns, _ := spos.NewRoundConsensus(
436+
eligibleNodesPubKeys,
437+
3,
438+
"2",
439+
&testscommon.KeysHandlerStub{},
440+
)
441+
442+
rcns.SetConsensusGroup(eligibleList)
443+
rcns.SetLeader(eligibleList[0])
444+
rcns.ResetRoundState()
445+
446+
rthr := spos.NewRoundThreshold()
447+
448+
rthr.SetThreshold(bls.SrBlock, 1)
449+
rthr.SetThreshold(bls.SrSignature, 3)
450+
rthr.SetFallbackThreshold(bls.SrBlock, 1)
451+
rthr.SetFallbackThreshold(bls.SrSignature, 2)
452+
453+
rstatus := spos.NewRoundStatus()
454+
rstatus.ResetRoundStatus()
455+
456+
// force backup case for coverage
457+
cns := spos.NewConsensusState(
458+
rcns,
459+
rthr,
460+
rstatus,
461+
&mock.NodeRedundancyHandlerStub{
462+
IsRedundancyNodeCalled: func() bool {
463+
return true
464+
},
465+
IsMainMachineActiveCalled: func() bool {
466+
return false
467+
},
468+
},
469+
)
427470

428471
cnsDta := &consensus.Message{
429472
RoundIndex: 0,
430-
PubKey: []byte("1"),
473+
PubKey: []byte(cns.SelfPubKey()),
431474
}
432475

433476
assert.False(t, cns.CanProcessReceivedMessage(cnsDta, 1, bls.SrBlock))

consensus/spos/subround_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/multiversx/mx-chain-core-go/core"
11+
"github.com/multiversx/mx-chain-go/consensus/mock"
1112
"github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock"
1213

1314
"github.com/stretchr/testify/assert"
@@ -88,6 +89,7 @@ func initConsensusState() *spos.ConsensusState {
8889
rcns,
8990
rthr,
9091
rstatus,
92+
&mock.NodeRedundancyHandlerStub{},
9193
)
9294

9395
cns.Data = []byte("X")

factory/consensus/consensusComponents.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ func (ccf *consensusComponentsFactory) createConsensusState(epoch uint32, consen
420420
consensusState := spos.NewConsensusState(
421421
roundConsensus,
422422
roundThreshold,
423-
roundStatus)
423+
roundStatus,
424+
ccf.processComponents.NodeRedundancyHandler(),
425+
)
424426

425427
return consensusState, nil
426428
}
@@ -775,6 +777,9 @@ func checkArgs(args ConsensusComponentsFactoryArgs) error {
775777
if check.IfNil(args.ProcessComponents.HardforkTrigger()) {
776778
return errors.ErrNilHardforkTrigger
777779
}
780+
if check.IfNil(args.ProcessComponents.NodeRedundancyHandler()) {
781+
return errors.ErrNilNodeRedundancyHandler
782+
}
778783
if check.IfNil(args.StateComponents) {
779784
return errors.ErrNilStateComponentsHolder
780785
}

factory/consensus/consensusComponents_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,22 @@ func TestNewConsensusComponentsFactory(t *testing.T) {
351351
require.Nil(t, ccf)
352352
require.Equal(t, errorsMx.ErrNilRoundHandler, err)
353353
})
354+
t.Run("nil NodeRedundancyHandler should error", func(t *testing.T) {
355+
t.Parallel()
356+
357+
args := createMockConsensusComponentsFactoryArgs()
358+
args.ProcessComponents = &testsMocks.ProcessComponentsStub{
359+
NodesCoord: &shardingMocks.NodesCoordinatorMock{},
360+
ShardCoord: &testscommon.ShardsCoordinatorMock{},
361+
RoundHandlerField: &testscommon.RoundHandlerMock{},
362+
HardforkTriggerField: &testscommon.HardforkTriggerStub{},
363+
NodeRedundancyHandlerInternal: nil,
364+
}
365+
ccf, err := consensusComp.NewConsensusComponentsFactory(args)
366+
367+
require.Nil(t, ccf)
368+
require.Equal(t, errorsMx.ErrNilNodeRedundancyHandler, err)
369+
})
354370
t.Run("nil HardforkTrigger should error", func(t *testing.T) {
355371
t.Parallel()
356372

testscommon/consensus/initializers/initializers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package initializers
22

33
import (
44
crypto "github.com/multiversx/mx-chain-crypto-go"
5+
"github.com/multiversx/mx-chain-go/consensus/mock"
56
"golang.org/x/exp/slices"
67

78
"github.com/multiversx/mx-chain-go/consensus"
@@ -90,6 +91,7 @@ func InitConsensusStateWithArgsVerifySignature(keysHandler consensus.KeysHandler
9091
rcns,
9192
rthr,
9293
rstatus,
94+
&mock.NodeRedundancyHandlerStub{},
9395
)
9496
cns.Data = []byte("X")
9597
cns.SetRoundIndex(0)
@@ -148,6 +150,7 @@ func createConsensusStateWithNodes(eligibleNodesPubKeys map[string]struct{}, con
148150
rcns,
149151
rthr,
150152
rstatus,
153+
&mock.NodeRedundancyHandlerStub{},
151154
)
152155

153156
cns.Data = []byte("X")

0 commit comments

Comments
 (0)