Skip to content

Commit

Permalink
Merge pull request #4620 from ElrondNetwork/integration-test-multi-key
Browse files Browse the repository at this point in the history
Integration test multi key
  • Loading branch information
iulianpascalau committed Dec 2, 2022
2 parents f7b1a3c + 5a8403b commit 5f9b97f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 32 deletions.
32 changes: 25 additions & 7 deletions integrationTests/consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ElrondNetwork/elrond-go-core/core/pubkeyConverter"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go-crypto"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/config"
consensusComp "github.com/ElrondNetwork/elrond-go/factory/consensus"
"github.com/ElrondNetwork/elrond-go/integrationTests"
Expand All @@ -26,6 +27,7 @@ const (
var (
p2pBootstrapDelay = time.Second * 5
testPubkeyConverter, _ = pubkeyConverter.NewHexPubkeyConverter(32)
log = logger.GetOrCreate("integrationtests/consensus")
)

func encodeAddress(address []byte) string {
Expand All @@ -47,6 +49,7 @@ func initNodesAndTest(
numInvalid uint32,
roundTime uint64,
consensusType string,
numKeysOnEachNode int,
) []*integrationTests.TestConsensusNode {

fmt.Println("Step 1. Setup nodes...")
Expand All @@ -57,6 +60,7 @@ func initNodesAndTest(
int(consensusSize),
roundTime,
consensusType,
numKeysOnEachNode,
)

for _, nodesList := range nodes {
Expand Down Expand Up @@ -198,14 +202,20 @@ func checkBlockProposedEveryRound(numCommBlock uint64, nonceForRoundMap map[uint
}
}

func runFullConsensusTest(t *testing.T, consensusType string) {
func runFullConsensusTest(t *testing.T, consensusType string, numKeysOnEachNode int) {
numNodes := uint32(4)
consensusSize := uint32(4)
consensusSize := uint32(4 * numKeysOnEachNode)
numInvalid := uint32(0)
roundTime := uint64(1000)
numCommBlock := uint64(8)

nodes := initNodesAndTest(numNodes, consensusSize, numInvalid, roundTime, consensusType)
log.Info("runFullConsensusTest",
"numNodes", numNodes,
"numKeysOnEachNode", numKeysOnEachNode,
"consensusSize", consensusSize,
)

nodes := initNodesAndTest(numNodes, consensusSize, numInvalid, roundTime, consensusType, numKeysOnEachNode)

mutex := &sync.Mutex{}
defer func() {
Expand All @@ -227,7 +237,7 @@ func runFullConsensusTest(t *testing.T, consensusType string) {
go checkBlockProposedEveryRound(numCommBlock, nonceForRoundMap, mutex, chDone, t)

extraTime := uint64(2)
endTime := time.Duration(roundTime) * time.Duration(numCommBlock+extraTime) * time.Millisecond
endTime := time.Duration(roundTime)*time.Duration(numCommBlock+extraTime)*time.Millisecond + time.Second
select {
case <-chDone:
case <-time.After(endTime):
Expand All @@ -239,20 +249,28 @@ func runFullConsensusTest(t *testing.T, consensusType string) {
}
}

func TestConsensusBLSFullTest(t *testing.T) {
func TestConsensusBLSFullTestSingleKeys(t *testing.T) {
if testing.Short() {
t.Skip("this is not a short test")
}

runFullConsensusTest(t, blsConsensusType, 1)
}

func TestConsensusBLSFullTestMultiKeys(t *testing.T) {
if testing.Short() {
t.Skip("this is not a short test")
}

runFullConsensusTest(t, blsConsensusType)
runFullConsensusTest(t, blsConsensusType, 5)
}

func runConsensusWithNotEnoughValidators(t *testing.T, consensusType string) {
numNodes := uint32(4)
consensusSize := uint32(4)
numInvalid := uint32(2)
roundTime := uint64(1000)
nodes := initNodesAndTest(numNodes, consensusSize, numInvalid, roundTime, consensusType)
nodes := initNodesAndTest(numNodes, consensusSize, numInvalid, roundTime, consensusType, 1)

mutex := &sync.Mutex{}
defer func() {
Expand Down
39 changes: 29 additions & 10 deletions integrationTests/testConsensusNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"github.com/ElrondNetwork/elrond-go/epochStart/notifier"
"github.com/ElrondNetwork/elrond-go/factory/peerSignatureHandler"
"github.com/ElrondNetwork/elrond-go/integrationTests/mock"
"github.com/ElrondNetwork/elrond-go/keysManagement"
"github.com/ElrondNetwork/elrond-go/node"
"github.com/ElrondNetwork/elrond-go/ntp"
"github.com/ElrondNetwork/elrond-go/p2p"
p2pFactory "github.com/ElrondNetwork/elrond-go/p2p/factory"
"github.com/ElrondNetwork/elrond-go/process/factory"
syncFork "github.com/ElrondNetwork/elrond-go/process/sync"
"github.com/ElrondNetwork/elrond-go/sharding"
Expand Down Expand Up @@ -83,7 +85,7 @@ func NewTestConsensusNode(
NodeKeys: nodeKeys.MainKey,
ShardCoordinator: shardCoordinator,
}
tcn.initNode(consensusSize, roundTime, consensusType, eligibleMap, waitingMap, keyGen)
tcn.initNode(consensusSize, roundTime, consensusType, eligibleMap, waitingMap, keyGen, nodeKeys.HandledKeys)

return tcn
}
Expand All @@ -95,10 +97,11 @@ func CreateNodesWithTestConsensusNode(
consensusSize int,
roundTime uint64,
consensusType string,
numKeysOnEachNode int,
) map[uint32][]*TestConsensusNode {

nodes := make(map[uint32][]*TestConsensusNode, nodesPerShard)
cp := CreateCryptoParams(nodesPerShard, numMetaNodes, maxShards)
cp := CreateCryptoParams(nodesPerShard, numMetaNodes, maxShards, numKeysOnEachNode)
keysMap := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
validatorsMap := GenValidatorsFromPubKeys(keysMap, maxShards)
eligibleMap, _ := nodesCoordinator.NodesInfoToValidators(validatorsMap)
Expand Down Expand Up @@ -131,6 +134,7 @@ func (tcn *TestConsensusNode) initNode(
eligibleMap map[uint32][]nodesCoordinator.Validator,
waitingMap map[uint32][]nodesCoordinator.Validator,
keyGen crypto.KeyGenerator,
handledKeys []*TestKeyPair,
) {

testHasher := createHasher(consensusType)
Expand Down Expand Up @@ -213,6 +217,28 @@ func (tcn *TestConsensusNode) initNode(
networkComponents.InputAntiFlood = &mock.NilAntifloodHandler{}
networkComponents.PeerHonesty = &mock.PeerHonestyHandlerStub{}

argsKeysHolder := keysManagement.ArgsManagedPeersHolder{
KeyGenerator: keyGen,
P2PIdentityGenerator: p2pFactory.NewIdentityGenerator(),
IsMainMachine: true,
MaxRoundsWithoutReceivedMessages: 10,
PrefsConfig: config.Preferences{},
}
keysHolder, _ := keysManagement.NewManagedPeersHolder(argsKeysHolder)

// adding provided handled keys
for _, key := range handledKeys {
skBytes, _ := key.Sk.ToByteArray()
_ = keysHolder.AddManagedPeer(skBytes)
}

argsKeysHandler := keysManagement.ArgsKeysHandler{
ManagedPeersHolder: keysHolder,
PrivateKey: tcn.NodeKeys.Sk,
Pid: tcn.Messenger.ID(),
}
keysHandler, _ := keysManagement.NewKeysHandler(argsKeysHandler)

cryptoComponents := GetDefaultCryptoComponents()
cryptoComponents.PrivKey = tcn.NodeKeys.Sk
cryptoComponents.PubKey = tcn.NodeKeys.Sk.GeneratePublic()
Expand All @@ -221,14 +247,7 @@ func (tcn *TestConsensusNode) initNode(
cryptoComponents.MultiSigContainer = cryptoMocks.NewMultiSignerContainerMock(testMultiSig)
cryptoComponents.BlKeyGen = keyGen
cryptoComponents.PeerSignHandler = peerSigHandler
cryptoComponents.KeysHandlerField = &testscommon.KeysHandlerStub{
GetHandledPrivateKeyCalled: func(pkBytes []byte) crypto.PrivateKey {
return tcn.NodeKeys.Sk
},
GetAssociatedPidCalled: func(pkBytes []byte) core.PeerID {
return tcn.Messenger.ID()
},
}
cryptoComponents.KeysHandlerField = keysHandler

processComponents := GetDefaultProcessComponents()
processComponents.ForkDetect = forkDetector
Expand Down
2 changes: 1 addition & 1 deletion integrationTests/testHeartbeatNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func CreateNodesWithTestHeartbeatNode(
p2pConfig p2pConfig.P2PConfig,
) map[uint32][]*TestHeartbeatNode {

cp := CreateCryptoParams(nodesPerShard, numMetaNodes, uint32(numShards))
cp := CreateCryptoParams(nodesPerShard, numMetaNodes, uint32(numShards), 1)
pubKeys := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
validatorsMap := GenValidatorsFromPubKeys(pubKeys, uint32(numShards))
validatorsForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(validatorsMap)
Expand Down
17 changes: 14 additions & 3 deletions integrationTests/testInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2245,7 +2245,7 @@ func GenValidatorsFromPubKeysAndTxPubKeys(
}

// CreateCryptoParams generates the crypto parameters (key pairs, key generator and suite) for multiple nodes
func CreateCryptoParams(nodesPerShard int, nbMetaNodes int, nbShards uint32) *CryptoParams {
func CreateCryptoParams(nodesPerShard int, nbMetaNodes int, nbShards uint32, numKeysOnEachNode int) *CryptoParams {
txSuite := ed25519.NewEd25519()
txKeyGen := signing.NewKeyGenerator(txSuite)
suite := mcl.NewSuiteBLS12()
Expand All @@ -2256,12 +2256,12 @@ func CreateCryptoParams(nodesPerShard int, nbMetaNodes int, nbShards uint32) *Cr
txKeysMap := make(map[uint32][]*TestKeyPair)
for shardId := uint32(0); shardId < nbShards; shardId++ {
for n := 0; n < nodesPerShard; n++ {
createAndAddKeys(keyGen, txKeyGen, shardId, nodesKeysMap, txKeysMap)
createAndAddKeys(keyGen, txKeyGen, shardId, nodesKeysMap, txKeysMap, numKeysOnEachNode)
}
}

for n := 0; n < nbMetaNodes; n++ {
createAndAddKeys(keyGen, txKeyGen, core.MetachainShardId, nodesKeysMap, txKeysMap)
createAndAddKeys(keyGen, txKeyGen, core.MetachainShardId, nodesKeysMap, txKeysMap, numKeysOnEachNode)
}

params := &CryptoParams{
Expand All @@ -2281,6 +2281,7 @@ func createAndAddKeys(
shardId uint32,
nodeKeysMap map[uint32][]*TestNodeKeys,
txKeysMap map[uint32][]*TestKeyPair,
numKeysOnEachNode int,
) {
kp := &TestKeyPair{}
kp.Sk, kp.Pk = keyGen.GeneratePair()
Expand All @@ -2294,6 +2295,16 @@ func createAndAddKeys(

txKeysMap[shardId] = append(txKeysMap[shardId], txKp)
nodeKeysMap[shardId] = append(nodeKeysMap[shardId], nodeKey)
if numKeysOnEachNode == 1 {
return
}

for i := 0; i < numKeysOnEachNode; i++ {
validatorKp := &TestKeyPair{}
validatorKp.Sk, validatorKp.Pk = keyGen.GeneratePair()

nodeKey.HandledKeys = append(nodeKey.HandledKeys, validatorKp)
}
}

// CloseProcessorNodes closes the used TestProcessorNodes and advertiser
Expand Down
4 changes: 2 additions & 2 deletions integrationTests/testProcessorNodeWithCoordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func CreateProcessorNodesWithNodesCoordinator(
) (map[uint32][]*TestProcessorNode, uint32) {

ncp, nbShards := createNodesCryptoParams(rewardsAddrsAssignments)
cp := CreateCryptoParams(len(ncp[0]), len(ncp[core.MetachainShardId]), nbShards)
cp := CreateCryptoParams(len(ncp[0]), len(ncp[core.MetachainShardId]), nbShards, 1)
pubKeys := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
validatorsMap := GenValidatorsFromPubKeys(pubKeys, nbShards)
validatorsMapForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(validatorsMap)

cpWaiting := CreateCryptoParams(1, 1, nbShards)
cpWaiting := CreateCryptoParams(1, 1, nbShards, 1)
pubKeysWaiting := PubKeysMapFromNodesKeysMap(cpWaiting.NodesKeys)
waitingMap := GenValidatorsFromPubKeys(pubKeysWaiting, nbShards)
waitingMapForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(waitingMap)
Expand Down
12 changes: 6 additions & 6 deletions integrationTests/testProcessorNodeWithMultisigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func CreateNodesWithNodesCoordinatorAndTxKeys(
coordinatorFactory := &IndexHashedNodesCoordinatorWithRaterFactory{
PeerAccountListAndRatingHandler: rater,
}
cp := CreateCryptoParams(nodesPerShard, nbMetaNodes, uint32(nbShards))
cp := CreateCryptoParams(nodesPerShard, nbMetaNodes, uint32(nbShards), 1)
blsPubKeys := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
txPubKeys := PubKeysMapFromTxKeysMap(cp.TxKeys)
validatorsMap := GenValidatorsFromPubKeysAndTxPubKeys(blsPubKeys, txPubKeys)
Expand Down Expand Up @@ -208,12 +208,12 @@ func CreateNodesWithNodesCoordinatorFactory(
metaConsensusGroupSize int,
nodesCoordinatorFactory NodesCoordinatorFactory,
) map[uint32][]*TestProcessorNode {
cp := CreateCryptoParams(nodesPerShard, nbMetaNodes, uint32(nbShards))
cp := CreateCryptoParams(nodesPerShard, nbMetaNodes, uint32(nbShards), 1)
pubKeys := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
validatorsMap := GenValidatorsFromPubKeys(pubKeys, uint32(nbShards))
validatorsMapForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(validatorsMap)

cpWaiting := CreateCryptoParams(1, 1, uint32(nbShards))
cpWaiting := CreateCryptoParams(1, 1, uint32(nbShards), 1)
pubKeysWaiting := PubKeysMapFromNodesKeysMap(cpWaiting.NodesKeys)
waitingMap := GenValidatorsFromPubKeys(pubKeysWaiting, uint32(nbShards))
waitingMapForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(waitingMap)
Expand Down Expand Up @@ -385,7 +385,7 @@ func CreateNodesWithNodesCoordinatorAndHeaderSigVerifier(
signer crypto.SingleSigner,
keyGen crypto.KeyGenerator,
) map[uint32][]*TestProcessorNode {
cp := CreateCryptoParams(nodesPerShard, nbMetaNodes, uint32(nbShards))
cp := CreateCryptoParams(nodesPerShard, nbMetaNodes, uint32(nbShards), 1)
pubKeys := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
validatorsMap := GenValidatorsFromPubKeys(pubKeys, uint32(nbShards))
validatorsMapForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(validatorsMap)
Expand Down Expand Up @@ -502,12 +502,12 @@ func CreateNodesWithNodesCoordinatorKeygenAndSingleSigner(
singleSigner crypto.SingleSigner,
keyGenForBlocks crypto.KeyGenerator,
) map[uint32][]*TestProcessorNode {
cp := CreateCryptoParams(nodesPerShard, nbMetaNodes, uint32(nbShards))
cp := CreateCryptoParams(nodesPerShard, nbMetaNodes, uint32(nbShards), 1)
pubKeys := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
validatorsMap := GenValidatorsFromPubKeys(pubKeys, uint32(nbShards))
validatorsMapForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(validatorsMap)

cpWaiting := CreateCryptoParams(2, 2, uint32(nbShards))
cpWaiting := CreateCryptoParams(2, 2, uint32(nbShards), 1)
pubKeysWaiting := PubKeysMapFromNodesKeysMap(cpWaiting.NodesKeys)
waitingMap := GenValidatorsFromPubKeys(pubKeysWaiting, uint32(nbShards))
waitingMapForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(waitingMap)
Expand Down
5 changes: 2 additions & 3 deletions keysManagement/peerInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ type peerInfo struct {
nodeName string
nodeIdentity string

isValidator bool
nextPeerAuthenticationTime time.Time

mutChangeableData sync.RWMutex
roundsWithoutReceivedMessages int
nextPeerAuthenticationTime time.Time
isValidator bool
}

func (pInfo *peerInfo) incrementRoundsWithoutReceivedMessages() {
Expand Down

0 comments on commit 5f9b97f

Please sign in to comment.