Skip to content

Commit a14a5bc

Browse files
authored
Merge pull request #6836 from multiversx/fix-getting-multi-key-proof-sender
Subround endround equivalent proofs unit tests
2 parents 17be243 + c2a102d commit a14a5bc

File tree

3 files changed

+147
-4
lines changed

3 files changed

+147
-4
lines changed

consensus/spos/bls/v2/export_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,13 @@ func (sr *subroundSignature) DoSignatureJobForManagedKeys(ctx context.Context) b
350350
func (sr *subroundEndRound) ReceivedSignature(cnsDta *consensus.Message) bool {
351351
return sr.receivedSignature(context.Background(), cnsDta)
352352
}
353+
354+
// WaitForProof -
355+
func (sr *subroundEndRound) WaitForProof() bool {
356+
return sr.waitForProof()
357+
}
358+
359+
// GetEquivalentProofSender -
360+
func (sr *subroundEndRound) GetEquivalentProofSender() string {
361+
return sr.getEquivalentProofSender()
362+
}

consensus/spos/bls/v2/subroundEndRound.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func (sr *subroundEndRound) verifyNodesOnAggSigFail(ctx context.Context) ([]stri
457457

458458
wg.Add(1)
459459

460-
go func(i int, pk string, wg *sync.WaitGroup, sigShare []byte) {
460+
go func(i int, pk string, sigShare []byte) {
461461
defer func() {
462462
sr.signatureThrottler.EndProcessing()
463463
wg.Done()
@@ -468,7 +468,7 @@ func (sr *subroundEndRound) verifyNodesOnAggSigFail(ctx context.Context) ([]stri
468468
invalidPubKeys = append(invalidPubKeys, pk)
469469
mutex.Unlock()
470470
}
471-
}(i, pk, wg, sigShare)
471+
}(i, pk, sigShare)
472472
}
473473
wg.Wait()
474474

consensus/spos/bls/v2/subroundEndRound_test.go

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/multiversx/mx-chain-core-go/data"
1515
"github.com/multiversx/mx-chain-core-go/data/block"
1616
crypto "github.com/multiversx/mx-chain-crypto-go"
17+
"github.com/multiversx/mx-chain-crypto-go/signing"
18+
"github.com/multiversx/mx-chain-crypto-go/signing/mcl"
1719
"github.com/stretchr/testify/assert"
1820
"github.com/stretchr/testify/require"
1921

@@ -27,12 +29,14 @@ import (
2729
dataRetrieverMocks "github.com/multiversx/mx-chain-go/dataRetriever/mock"
2830
"github.com/multiversx/mx-chain-go/p2p"
2931
"github.com/multiversx/mx-chain-go/p2p/factory"
32+
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
3033
"github.com/multiversx/mx-chain-go/testscommon"
3134
consensusMocks "github.com/multiversx/mx-chain-go/testscommon/consensus"
3235
"github.com/multiversx/mx-chain-go/testscommon/consensus/initializers"
3336
"github.com/multiversx/mx-chain-go/testscommon/dataRetriever"
3437
"github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock"
3538
"github.com/multiversx/mx-chain-go/testscommon/p2pmocks"
39+
"github.com/multiversx/mx-chain-go/testscommon/shardingMocks"
3640
"github.com/multiversx/mx-chain-go/testscommon/statusHandler"
3741
)
3842

@@ -1808,7 +1812,7 @@ func TestSubroundEndRound_getMinConsensusGroupIndexOfManagedKeys(t *testing.T) {
18081812
})
18091813
}
18101814

1811-
func TestSubroundSignature_ReceivedSignature(t *testing.T) {
1815+
func TestSubroundEndRound_ReceivedSignature(t *testing.T) {
18121816
t.Parallel()
18131817

18141818
sr := initSubroundEndRound(&statusHandler.AppStatusHandlerStub{})
@@ -1867,7 +1871,7 @@ func TestSubroundSignature_ReceivedSignature(t *testing.T) {
18671871
assert.True(t, r)
18681872
}
18691873

1870-
func TestSubroundSignature_ReceivedSignatureStoreShareFailed(t *testing.T) {
1874+
func TestSubroundEndRound_ReceivedSignatureStoreShareFailed(t *testing.T) {
18711875
t.Parallel()
18721876

18731877
errStore := errors.New("signature share store failed")
@@ -1941,3 +1945,132 @@ func TestSubroundSignature_ReceivedSignatureStoreShareFailed(t *testing.T) {
19411945
assert.False(t, r)
19421946
assert.True(t, storeSigShareCalled)
19431947
}
1948+
1949+
func TestSubroundEndRound_WaitForProof(t *testing.T) {
1950+
t.Parallel()
1951+
1952+
t.Run("should return true if there is proof", func(t *testing.T) {
1953+
t.Parallel()
1954+
1955+
container := consensusMocks.InitConsensusCore()
1956+
container.SetEquivalentProofsPool(&dataRetriever.ProofsPoolMock{
1957+
HasProofCalled: func(shardID uint32, headerHash []byte) bool {
1958+
return true
1959+
},
1960+
})
1961+
1962+
sr := initSubroundEndRoundWithContainer(container, &statusHandler.AppStatusHandlerStub{})
1963+
1964+
ok := sr.WaitForProof()
1965+
require.True(t, ok)
1966+
})
1967+
1968+
t.Run("should return true after waiting and finding proof", func(t *testing.T) {
1969+
t.Parallel()
1970+
1971+
container := consensusMocks.InitConsensusCore()
1972+
1973+
numCalls := 0
1974+
container.SetEquivalentProofsPool(&dataRetriever.ProofsPoolMock{
1975+
HasProofCalled: func(shardID uint32, headerHash []byte) bool {
1976+
if numCalls < 2 {
1977+
numCalls++
1978+
return false
1979+
}
1980+
1981+
return true
1982+
},
1983+
})
1984+
1985+
sr := initSubroundEndRoundWithContainer(container, &statusHandler.AppStatusHandlerStub{})
1986+
1987+
ok := sr.WaitForProof()
1988+
require.True(t, ok)
1989+
1990+
require.Equal(t, 2, numCalls)
1991+
})
1992+
1993+
t.Run("should return false on timeout", func(t *testing.T) {
1994+
t.Parallel()
1995+
1996+
container := consensusMocks.InitConsensusCore()
1997+
1998+
container.SetEquivalentProofsPool(&dataRetriever.ProofsPoolMock{
1999+
HasProofCalled: func(shardID uint32, headerHash []byte) bool {
2000+
return false
2001+
},
2002+
})
2003+
2004+
sr := initSubroundEndRoundWithContainer(container, &statusHandler.AppStatusHandlerStub{})
2005+
2006+
ok := sr.WaitForProof()
2007+
require.False(t, ok)
2008+
})
2009+
}
2010+
2011+
func TestSubroundEndRound_GetEquivalentProofSender(t *testing.T) {
2012+
t.Parallel()
2013+
2014+
t.Run("for single key, return self pubkey", func(t *testing.T) {
2015+
t.Parallel()
2016+
2017+
container := consensusMocks.InitConsensusCore()
2018+
sr := initSubroundEndRoundWithContainer(container, &statusHandler.AppStatusHandlerStub{})
2019+
2020+
selfKey := sr.SelfPubKey()
2021+
2022+
sender := sr.GetEquivalentProofSender()
2023+
require.Equal(t, selfKey, sender)
2024+
})
2025+
2026+
t.Run("for multi key, return random key", func(t *testing.T) {
2027+
t.Parallel()
2028+
2029+
container := consensusMocks.InitConsensusCore()
2030+
2031+
suite := mcl.NewSuiteBLS12()
2032+
kg := signing.NewKeyGenerator(suite)
2033+
2034+
mapKeys := generateKeyPairs(kg)
2035+
2036+
pubKeys := make([]string, 0)
2037+
for pubKey := range mapKeys {
2038+
pubKeys = append(pubKeys, pubKey)
2039+
}
2040+
2041+
nodesCoordinator := &shardingMocks.NodesCoordinatorMock{
2042+
ComputeValidatorsGroupCalled: func(randomness []byte, round uint64, shardId uint32, epoch uint32) (nodesCoordinator.Validator, []nodesCoordinator.Validator, error) {
2043+
defaultSelectionChances := uint32(1)
2044+
leader := shardingMocks.NewValidatorMock([]byte(pubKeys[0]), 1, defaultSelectionChances)
2045+
return leader, []nodesCoordinator.Validator{
2046+
leader,
2047+
shardingMocks.NewValidatorMock([]byte(pubKeys[1]), 1, defaultSelectionChances),
2048+
shardingMocks.NewValidatorMock([]byte(pubKeys[2]), 1, defaultSelectionChances),
2049+
shardingMocks.NewValidatorMock([]byte(pubKeys[3]), 1, defaultSelectionChances),
2050+
shardingMocks.NewValidatorMock([]byte(pubKeys[4]), 1, defaultSelectionChances),
2051+
shardingMocks.NewValidatorMock([]byte(pubKeys[5]), 1, defaultSelectionChances),
2052+
shardingMocks.NewValidatorMock([]byte(pubKeys[6]), 1, defaultSelectionChances),
2053+
shardingMocks.NewValidatorMock([]byte(pubKeys[7]), 1, defaultSelectionChances),
2054+
shardingMocks.NewValidatorMock([]byte(pubKeys[8]), 1, defaultSelectionChances),
2055+
}, nil
2056+
},
2057+
}
2058+
container.SetNodesCoordinator(nodesCoordinator)
2059+
2060+
keysHandlerMock := &testscommon.KeysHandlerStub{
2061+
IsKeyManagedByCurrentNodeCalled: func(pkBytes []byte) bool {
2062+
_, ok := mapKeys[string(pkBytes)]
2063+
return ok
2064+
},
2065+
}
2066+
2067+
consensusState := initializers.InitConsensusStateWithArgs(keysHandlerMock, mapKeys)
2068+
sr := initSubroundEndRoundWithContainerAndConsensusState(container, &statusHandler.AppStatusHandlerStub{}, consensusState, &dataRetrieverMocks.ThrottlerStub{})
2069+
sr.SetSelfPubKey("not in consensus")
2070+
2071+
selfKey := sr.SelfPubKey()
2072+
2073+
sender := sr.GetEquivalentProofSender()
2074+
assert.NotEqual(t, selfKey, sender)
2075+
})
2076+
}

0 commit comments

Comments
 (0)