From 3ffb1df7fbd9543c46e2a3a673caf884312bda70 Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Tue, 9 Jan 2024 11:52:31 +0200 Subject: [PATCH 1/5] send invalid signers from leader --- consensus/spos/bls/subroundEndRound.go | 12 +++-- consensus/spos/bls/subroundEndRound_test.go | 49 ++++++++++++++++++++- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/consensus/spos/bls/subroundEndRound.go b/consensus/spos/bls/subroundEndRound.go index 723fc0bcbf3..a1f96cc8ffc 100644 --- a/consensus/spos/bls/subroundEndRound.go +++ b/consensus/spos/bls/subroundEndRound.go @@ -189,7 +189,7 @@ func (sr *subroundEndRound) receivedInvalidSignersInfo(_ context.Context, cnsDta return false } - if sr.IsSelfLeaderInCurrentRound() { + if sr.IsSelfLeaderInCurrentRound() || sr.IsMultiKeyLeaderInCurrentRound() { return false } @@ -589,12 +589,18 @@ func (sr *subroundEndRound) createAndBroadcastHeaderFinalInfo() { } func (sr *subroundEndRound) createAndBroadcastInvalidSigners(invalidSigners []byte) { + leader, errGetLeader := sr.GetLeader() + if errGetLeader != nil { + log.Debug("createAndBroadcastInvalidSigners.GetLeader", "error", errGetLeader) + return + } + cnsMsg := consensus.NewConsensusMessage( sr.GetData(), nil, nil, nil, - []byte(sr.SelfPubKey()), + []byte(leader), nil, int(MtInvalidSigners), sr.RoundHandler().Index(), @@ -602,7 +608,7 @@ func (sr *subroundEndRound) createAndBroadcastInvalidSigners(invalidSigners []by nil, nil, nil, - sr.CurrentPid(), + sr.GetAssociatedPid([]byte(leader)), invalidSigners, ) diff --git a/consensus/spos/bls/subroundEndRound_test.go b/consensus/spos/bls/subroundEndRound_test.go index 456277e23fc..3a6c9fa80f6 100644 --- a/consensus/spos/bls/subroundEndRound_test.go +++ b/consensus/spos/bls/subroundEndRound_test.go @@ -1322,7 +1322,7 @@ func TestSubroundEndRound_ReceivedInvalidSignersInfo(t *testing.T) { assert.False(t, res) }) - t.Run("received message for self leader", func(t *testing.T) { + t.Run("received message from self leader should return false", func(t *testing.T) { t.Parallel() container := mock.InitConsensusCore() @@ -1339,6 +1339,53 @@ func TestSubroundEndRound_ReceivedInvalidSignersInfo(t *testing.T) { assert.False(t, res) }) + t.Run("received message from self multikey leader should return false", func(t *testing.T) { + t.Parallel() + + container := mock.InitConsensusCore() + keysHandler := &testscommon.KeysHandlerStub{ + IsKeyManagedByCurrentNodeCalled: func(pkBytes []byte) bool { + return string(pkBytes) == "A" + }, + } + ch := make(chan bool, 1) + consensusState := initConsensusStateWithKeysHandler(keysHandler) + sr, _ := spos.NewSubround( + bls.SrSignature, + bls.SrEndRound, + -1, + int64(85*roundTimeDuration/100), + int64(95*roundTimeDuration/100), + "(END_ROUND)", + consensusState, + ch, + executeStoredMessages, + container, + chainID, + currentPid, + &statusHandler.AppStatusHandlerStub{}, + ) + + srEndRound, _ := bls.NewSubroundEndRound( + sr, + extend, + bls.ProcessingThresholdPercent, + displayStatistics, + &statusHandler.AppStatusHandlerStub{}, + &mock.SentSignatureTrackerStub{}, + ) + + srEndRound.SetSelfPubKey("A") + + cnsData := consensus.Message{ + BlockHeaderHash: []byte("X"), + PubKey: []byte("A"), + } + + res := srEndRound.ReceivedInvalidSignersInfo(&cnsData) + assert.False(t, res) + }) + t.Run("received hash does not match the hash from current consensus state", func(t *testing.T) { t.Parallel() From aaa62e11cc1c52e6abdc2d0390cc598e6ef95b8c Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Tue, 9 Jan 2024 12:06:58 +0200 Subject: [PATCH 2/5] redundancy node should not send invalid signers --- consensus/spos/bls/subroundEndRound.go | 5 ++ consensus/spos/bls/subroundEndRound_test.go | 63 +++++++++++++++------ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/consensus/spos/bls/subroundEndRound.go b/consensus/spos/bls/subroundEndRound.go index a1f96cc8ffc..c9d1a8a62db 100644 --- a/consensus/spos/bls/subroundEndRound.go +++ b/consensus/spos/bls/subroundEndRound.go @@ -589,6 +589,11 @@ func (sr *subroundEndRound) createAndBroadcastHeaderFinalInfo() { } func (sr *subroundEndRound) createAndBroadcastInvalidSigners(invalidSigners []byte) { + isSelfLeader := sr.IsSelfLeaderInCurrentRound() && sr.ShouldConsiderSelfKeyInConsensus() + if !isSelfLeader && !sr.IsMultiKeyLeaderInCurrentRound() { + return + } + leader, errGetLeader := sr.GetLeader() if errGetLeader != nil { log.Debug("createAndBroadcastInvalidSigners.GetLeader", "error", errGetLeader) diff --git a/consensus/spos/bls/subroundEndRound_test.go b/consensus/spos/bls/subroundEndRound_test.go index 3a6c9fa80f6..d6966a5e870 100644 --- a/consensus/spos/bls/subroundEndRound_test.go +++ b/consensus/spos/bls/subroundEndRound_test.go @@ -1603,29 +1603,56 @@ func TestVerifyInvalidSigners(t *testing.T) { func TestSubroundEndRound_CreateAndBroadcastInvalidSigners(t *testing.T) { t.Parallel() - wg := &sync.WaitGroup{} - wg.Add(1) + t.Run("redundancy node should not send", func(t *testing.T) { + t.Parallel() - expectedInvalidSigners := []byte("invalid signers") + expectedInvalidSigners := []byte("invalid signers") - wasCalled := false - container := mock.InitConsensusCore() - messenger := &mock.BroadcastMessengerMock{ - BroadcastConsensusMessageCalled: func(message *consensus.Message) error { - wg.Done() - assert.Equal(t, expectedInvalidSigners, message.InvalidSigners) - wasCalled = true - return nil - }, - } - container.SetBroadcastMessenger(messenger) - sr := *initSubroundEndRoundWithContainer(container, &statusHandler.AppStatusHandlerStub{}) + container := mock.InitConsensusCore() + nodeRedundancy := &mock.NodeRedundancyHandlerStub{ + IsRedundancyNodeCalled: func() bool { + return true + }, + } + container.SetNodeRedundancyHandler(nodeRedundancy) + messenger := &mock.BroadcastMessengerMock{ + BroadcastConsensusMessageCalled: func(message *consensus.Message) error { + assert.Fail(t, "should have not been called") + return nil + }, + } + container.SetBroadcastMessenger(messenger) + sr := *initSubroundEndRoundWithContainer(container, &statusHandler.AppStatusHandlerStub{}) - sr.CreateAndBroadcastInvalidSigners(expectedInvalidSigners) + sr.CreateAndBroadcastInvalidSigners(expectedInvalidSigners) + }) + t.Run("should work", func(t *testing.T) { + t.Parallel() + + wg := &sync.WaitGroup{} + wg.Add(1) + + expectedInvalidSigners := []byte("invalid signers") + + wasCalled := false + container := mock.InitConsensusCore() + messenger := &mock.BroadcastMessengerMock{ + BroadcastConsensusMessageCalled: func(message *consensus.Message) error { + wg.Done() + assert.Equal(t, expectedInvalidSigners, message.InvalidSigners) + wasCalled = true + return nil + }, + } + container.SetBroadcastMessenger(messenger) + sr := *initSubroundEndRoundWithContainer(container, &statusHandler.AppStatusHandlerStub{}) + + sr.CreateAndBroadcastInvalidSigners(expectedInvalidSigners) - wg.Wait() + wg.Wait() - require.True(t, wasCalled) + require.True(t, wasCalled) + }) } func TestGetFullMessagesForInvalidSigners(t *testing.T) { From 53ad9b31e08ebd5bb86bb962e7ad047fd3f85553 Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Tue, 9 Jan 2024 13:09:02 +0200 Subject: [PATCH 3/5] fixed tests --- consensus/spos/bls/subroundEndRound_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/consensus/spos/bls/subroundEndRound_test.go b/consensus/spos/bls/subroundEndRound_test.go index d6966a5e870..0c5ac3f2284 100644 --- a/consensus/spos/bls/subroundEndRound_test.go +++ b/consensus/spos/bls/subroundEndRound_test.go @@ -1646,6 +1646,7 @@ func TestSubroundEndRound_CreateAndBroadcastInvalidSigners(t *testing.T) { } container.SetBroadcastMessenger(messenger) sr := *initSubroundEndRoundWithContainer(container, &statusHandler.AppStatusHandlerStub{}) + sr.SetSelfPubKey("A") sr.CreateAndBroadcastInvalidSigners(expectedInvalidSigners) From 2b34ce7fe019b211ecb98e9cd45a3d76cbc7aa61 Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Tue, 9 Jan 2024 16:16:57 +0200 Subject: [PATCH 4/5] fix after review --- consensus/spos/bls/subroundEndRound_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/consensus/spos/bls/subroundEndRound_test.go b/consensus/spos/bls/subroundEndRound_test.go index 0c5ac3f2284..70992e7aec5 100644 --- a/consensus/spos/bls/subroundEndRound_test.go +++ b/consensus/spos/bls/subroundEndRound_test.go @@ -1603,7 +1603,7 @@ func TestVerifyInvalidSigners(t *testing.T) { func TestSubroundEndRound_CreateAndBroadcastInvalidSigners(t *testing.T) { t.Parallel() - t.Run("redundancy node should not send", func(t *testing.T) { + t.Run("redundancy node should not send while main is active", func(t *testing.T) { t.Parallel() expectedInvalidSigners := []byte("invalid signers") @@ -1613,6 +1613,9 @@ func TestSubroundEndRound_CreateAndBroadcastInvalidSigners(t *testing.T) { IsRedundancyNodeCalled: func() bool { return true }, + IsMainMachineActiveCalled: func() bool { + return true + }, } container.SetNodeRedundancyHandler(nodeRedundancy) messenger := &mock.BroadcastMessengerMock{ From 3121214b4dd961fa5aa684c6f72caa8797f03bbc Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Mon, 15 Jan 2024 12:37:45 +0200 Subject: [PATCH 5/5] fixes after review --- consensus/spos/bls/subroundEndRound.go | 2 +- consensus/spos/bls/subroundEndRound_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/spos/bls/subroundEndRound.go b/consensus/spos/bls/subroundEndRound.go index c9d1a8a62db..26c845511b5 100644 --- a/consensus/spos/bls/subroundEndRound.go +++ b/consensus/spos/bls/subroundEndRound.go @@ -590,7 +590,7 @@ func (sr *subroundEndRound) createAndBroadcastHeaderFinalInfo() { func (sr *subroundEndRound) createAndBroadcastInvalidSigners(invalidSigners []byte) { isSelfLeader := sr.IsSelfLeaderInCurrentRound() && sr.ShouldConsiderSelfKeyInConsensus() - if !isSelfLeader && !sr.IsMultiKeyLeaderInCurrentRound() { + if !(isSelfLeader || sr.IsMultiKeyLeaderInCurrentRound()) { return } diff --git a/consensus/spos/bls/subroundEndRound_test.go b/consensus/spos/bls/subroundEndRound_test.go index 70992e7aec5..b6556b8ad70 100644 --- a/consensus/spos/bls/subroundEndRound_test.go +++ b/consensus/spos/bls/subroundEndRound_test.go @@ -1641,9 +1641,9 @@ func TestSubroundEndRound_CreateAndBroadcastInvalidSigners(t *testing.T) { container := mock.InitConsensusCore() messenger := &mock.BroadcastMessengerMock{ BroadcastConsensusMessageCalled: func(message *consensus.Message) error { - wg.Done() assert.Equal(t, expectedInvalidSigners, message.InvalidSigners) wasCalled = true + wg.Done() return nil }, }