diff --git a/consensus/spos/bls/subroundStartRound.go b/consensus/spos/bls/subroundStartRound.go index 8e330f791bb..c622779fdac 100644 --- a/consensus/spos/bls/subroundStartRound.go +++ b/consensus/spos/bls/subroundStartRound.go @@ -189,15 +189,14 @@ func (sr *subroundStartRound) initCurrentRound() bool { sr.indexRoundIfNeeded(pubKeys) - _, err = sr.SelfConsensusGroupIndex() - if err != nil { - if numMultiKeysInConsensusGroup == 0 { - log.Debug("not in consensus group") - } + isSingleKeyLeader := leader == sr.SelfPubKey() && sr.ShouldConsiderSelfKeyInConsensus() + isLeader := isSingleKeyLeader || sr.IsKeyManagedByCurrentNode([]byte(leader)) + isSelfInConsensus := sr.IsNodeInConsensusGroup(sr.SelfPubKey()) || numMultiKeysInConsensusGroup > 0 + if !isSelfInConsensus { + log.Debug("not in consensus group") sr.AppStatusHandler().SetStringValue(common.MetricConsensusState, "not in consensus group") } else { - isLeader := leader == sr.SelfPubKey() && sr.ShouldConsiderSelfKeyInConsensus() - if !isLeader && !sr.IsKeyManagedByCurrentNode([]byte(leader)) { + if !isLeader { sr.AppStatusHandler().Increment(common.MetricCountConsensus) sr.AppStatusHandler().SetStringValue(common.MetricConsensusState, "participant") } diff --git a/consensus/spos/bls/subroundStartRound_test.go b/consensus/spos/bls/subroundStartRound_test.go index 583861032d1..51c96117dbc 100644 --- a/consensus/spos/bls/subroundStartRound_test.go +++ b/consensus/spos/bls/subroundStartRound_test.go @@ -566,10 +566,66 @@ func TestSubroundStartRound_InitCurrentRoundShouldMetrics(t *testing.T) { srStartRound.Check() assert.True(t, wasCalled) }) - t.Run("participant node", func(t *testing.T) { + t.Run("main key participant", func(t *testing.T) { t.Parallel() wasCalled := false + wasIncrementCalled := false + container := mock.InitConsensusCore() + keysHandler := &testscommon.KeysHandlerStub{ + IsKeyManagedByCurrentNodeCalled: func(pkBytes []byte) bool { + return string(pkBytes) == "B" + }, + } + appStatusHandler := &statusHandler.AppStatusHandlerStub{ + SetStringValueHandler: func(key string, value string) { + if key == common.MetricConsensusState { + wasCalled = true + assert.Equal(t, "participant", value) + } + }, + IncrementHandler: func(key string) { + if key == common.MetricCountConsensus { + wasIncrementCalled = true + } + }, + } + ch := make(chan bool, 1) + consensusState := initConsensusStateWithKeysHandler(keysHandler) + consensusState.SetSelfPubKey("B") + sr, _ := spos.NewSubround( + -1, + bls.SrStartRound, + bls.SrBlock, + int64(85*roundTimeDuration/100), + int64(95*roundTimeDuration/100), + "(START_ROUND)", + consensusState, + ch, + executeStoredMessages, + container, + chainID, + currentPid, + appStatusHandler, + ) + + srStartRound, _ := bls.NewSubroundStartRound( + sr, + extend, + bls.ProcessingThresholdPercent, + displayStatistics, + executeStoredMessages, + &mock.SentSignatureTrackerStub{}, + ) + srStartRound.Check() + assert.True(t, wasCalled) + assert.True(t, wasIncrementCalled) + }) + t.Run("multi key participant", func(t *testing.T) { + t.Parallel() + + wasCalled := false + wasIncrementCalled := false container := mock.InitConsensusCore() keysHandler := &testscommon.KeysHandlerStub{} appStatusHandler := &statusHandler.AppStatusHandlerStub{ @@ -579,9 +635,17 @@ func TestSubroundStartRound_InitCurrentRoundShouldMetrics(t *testing.T) { assert.Equal(t, value, "participant") } }, + IncrementHandler: func(key string) { + if key == common.MetricCountConsensus { + wasIncrementCalled = true + } + }, } ch := make(chan bool, 1) consensusState := initConsensusStateWithKeysHandler(keysHandler) + keysHandler.IsKeyManagedByCurrentNodeCalled = func(pkBytes []byte) bool { + return string(pkBytes) == consensusState.SelfPubKey() + } sr, _ := spos.NewSubround( -1, bls.SrStartRound, @@ -608,6 +672,7 @@ func TestSubroundStartRound_InitCurrentRoundShouldMetrics(t *testing.T) { ) srStartRound.Check() assert.True(t, wasCalled) + assert.True(t, wasIncrementCalled) }) t.Run("main key leader", func(t *testing.T) { t.Parallel() @@ -709,6 +774,7 @@ func TestSubroundStartRound_InitCurrentRoundShouldMetrics(t *testing.T) { ch := make(chan bool, 1) consensusState := initConsensusStateWithKeysHandler(keysHandler) leader, _ := consensusState.GetLeader() + consensusState.SetSelfPubKey(leader) keysHandler.IsKeyManagedByCurrentNodeCalled = func(pkBytes []byte) bool { return string(pkBytes) == leader }