Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored nodes keys in tests #4747

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestInterceptedShardBlockHeaderVerifiedWithCorrectConsensusGroup(t *testing
header, err = fillHeaderFields(nodesMap[0][0], header, singleSigner)
assert.Nil(t, err)

pk := nodesMap[0][0].NodeKeys.Pk
pk := nodesMap[0][0].NodeKeys.MainKey.Pk
nodesMap[0][0].BroadcastBlock(body, header, pk)

time.Sleep(broadcastDelay)
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestInterceptedMetaBlockVerifiedWithCorrectConsensusGroup(t *testing.T) {
0,
)

pk := nodesMap[core.MetachainShardId][0].NodeKeys.Pk
pk := nodesMap[core.MetachainShardId][0].NodeKeys.MainKey.Pk
nodesMap[core.MetachainShardId][0].BroadcastBlock(body, header, pk)

time.Sleep(broadcastDelay)
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestInterceptedShardBlockHeaderWithLeaderSignatureAndRandSeedChecks(t *test
header, err = fillHeaderFields(nodeToSendFrom, header, singleSigner)
assert.Nil(t, err)

pk := nodeToSendFrom.NodeKeys.Pk
pk := nodeToSendFrom.NodeKeys.MainKey.Pk
nodeToSendFrom.BroadcastBlock(body, header, pk)

time.Sleep(broadcastDelay)
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestInterceptedShardHeaderBlockWithWrongPreviousRandSeedShouldNotBeAccepted
nonce := uint64(2)
body, header, _, _ := integrationTests.ProposeBlockWithConsensusSignature(0, nodesMap, round, nonce, wrongRandomness, 0)

pk := nodesMap[0][0].NodeKeys.Pk
pk := nodesMap[0][0].NodeKeys.MainKey.Pk
nodesMap[0][0].BroadcastBlock(body, header, pk)

time.Sleep(broadcastDelay)
Expand All @@ -292,7 +292,7 @@ func TestInterceptedShardHeaderBlockWithWrongPreviousRandSeedShouldNotBeAccepted
}

func fillHeaderFields(proposer *integrationTests.TestProcessorNode, hdr data.HeaderHandler, signer crypto.SingleSigner) (data.HeaderHandler, error) {
leaderSk := proposer.NodeKeys.Sk
leaderSk := proposer.NodeKeys.MainKey.Sk

randSeed, _ := signer.Sign(leaderSk, hdr.GetPrevRandSeed())
err := hdr.SetRandSeed(randSeed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func testNodeStartsInEpoch(t *testing.T, shardID uint32, expectedHighestRound ui

roundHandler := &mock.RoundHandlerMock{IndexField: int64(round)}
cryptoComponents := integrationTests.GetDefaultCryptoComponents()
cryptoComponents.PubKey = nodeToJoinLate.NodeKeys.Pk
cryptoComponents.PubKey = nodeToJoinLate.NodeKeys.MainKey.Pk
cryptoComponents.BlockSig = &mock.SignerMock{}
cryptoComponents.TxSig = &mock.SignerMock{}
cryptoComponents.BlKeyGen = &mock.KeyGenMock{}
Expand Down
4 changes: 2 additions & 2 deletions integrationTests/node/heartbeatV2/heartbeatV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestHeartbeatV2_PeerAuthenticationMessageExpiration(t *testing.T) {

requestHashes := make([][]byte, 0)
for i := 1; i < len(nodes); i++ {
pkBytes, err := nodes[i].NodeKeys.Pk.ToByteArray()
pkBytes, err := nodes[i].NodeKeys.MainKey.Pk.ToByteArray()
assert.Nil(t, err)

requestHashes = append(requestHashes, pkBytes)
Expand Down Expand Up @@ -151,7 +151,7 @@ func checkMessages(t *testing.T, nodes []*integrationTests.TestHeartbeatNode, ma

// Check this node received messages from all peers
for _, node := range nodes {
pkBytes, err := node.NodeKeys.Pk.ToByteArray()
pkBytes, err := node.NodeKeys.MainKey.Pk.ToByteArray()
assert.Nil(t, err)

assert.True(t, paCache.Has(pkBytes))
Expand Down
8 changes: 4 additions & 4 deletions integrationTests/nodesCoordinatorFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type IndexHashedNodesCoordinatorFactory struct {
// CreateNodesCoordinator -
func (tpn *IndexHashedNodesCoordinatorFactory) CreateNodesCoordinator(arg ArgIndexHashedNodesCoordinatorFactory) nodesCoordinator.NodesCoordinator {

keys := arg.cp.Keys[arg.shardId][arg.keyIndex]
pubKeyBytes, _ := keys.Pk.ToByteArray()
keys := arg.cp.NodesKeys[arg.shardId][arg.keyIndex]
pubKeyBytes, _ := keys.MainKey.Pk.ToByteArray()

nodeShufflerArgs := &nodesCoordinator.NodesShufflerArgs{
NodesShard: uint32(arg.nodesPerShard),
Expand Down Expand Up @@ -93,8 +93,8 @@ type IndexHashedNodesCoordinatorWithRaterFactory struct {
func (ihncrf *IndexHashedNodesCoordinatorWithRaterFactory) CreateNodesCoordinator(
arg ArgIndexHashedNodesCoordinatorFactory,
) nodesCoordinator.NodesCoordinator {
keys := arg.cp.Keys[arg.shardId][arg.keyIndex]
pubKeyBytes, _ := keys.Pk.ToByteArray()
keys := arg.cp.NodesKeys[arg.shardId][arg.keyIndex]
pubKeyBytes, _ := keys.MainKey.Pk.ToByteArray()

shufflerArgs := &nodesCoordinator.NodesShufflerArgs{
NodesShard: uint32(arg.nodesPerShard),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestConsensus_BlockWithoutTwoThirdsPlusOneSignaturesOrWrongBitmapShouldNotB
assert.NotNil(t, body)
assert.NotNil(t, hdr)

pk := nodesMap[0][0].NodeKeys.Pk
pk := nodesMap[0][0].NodeKeys.MainKey.Pk
nodesMap[0][0].BroadcastBlock(body, hdr, pk)
time.Sleep(testBlock.StepDelay)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func proposeAndCommitBlock(node *integrationTests.TestProcessorNode, round uint6
return err
}

pk := node.NodeKeys.Pk
pk := node.NodeKeys.MainKey.Pk
node.BroadcastBlock(body, hdr, pk)
time.Sleep(testBlock.StepDelay)
return nil
Expand Down
2 changes: 1 addition & 1 deletion integrationTests/sync/basicSync/basicSync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func proposeBlockWithPubKeyBitmap(n *integrationTests.TestProcessorNode, round u
log.Error("header.SetPubKeysBitmap", "error", err.Error())
}

pk := n.NodeKeys.Pk
pk := n.NodeKeys.MainKey.Pk
n.BroadcastBlock(body, header, pk)
n.CommitBlock(body, header)
}
Expand Down
13 changes: 7 additions & 6 deletions integrationTests/testConsensusNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ type TestConsensusNode struct {
BlockProcessor *mock.BlockProcessorMock
ResolverFinder dataRetriever.ResolversFinder
AccountsDB *state.AccountsDB
NodeKeys TestKeyPair
NodeKeys *TestKeyPair
}

// NewTestConsensusNode returns a new TestConsensusNode
func NewTestConsensusNode(
consensusSize int,
roundTime uint64,
consensusType string,
nodeKeys TestKeyPair,
nodeKeys *TestNodeKeys,
eligibleMap map[uint32][]nodesCoordinator.Validator,
waitingMap map[uint32][]nodesCoordinator.Validator,
keyGen crypto.KeyGenerator,
Expand All @@ -80,7 +80,7 @@ func NewTestConsensusNode(
shardCoordinator, _ := sharding.NewMultiShardCoordinator(maxShards, nodeShardId)

tcn := &TestConsensusNode{
NodeKeys: nodeKeys,
NodeKeys: nodeKeys.MainKey,
ShardCoordinator: shardCoordinator,
}
tcn.initNode(consensusSize, roundTime, consensusType, eligibleMap, waitingMap, keyGen)
Expand All @@ -99,18 +99,19 @@ func CreateNodesWithTestConsensusNode(

nodes := make(map[uint32][]*TestConsensusNode, nodesPerShard)
cp := CreateCryptoParams(nodesPerShard, numMetaNodes, maxShards)
keysMap := PubKeysMapFromKeysMap(cp.Keys)
keysMap := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
validatorsMap := GenValidatorsFromPubKeys(keysMap, maxShards)
eligibleMap, _ := nodesCoordinator.NodesInfoToValidators(validatorsMap)
waitingMap := make(map[uint32][]nodesCoordinator.Validator)
connectableNodes := make([]Connectable, 0)

for _, keysPair := range cp.Keys[0] {
shardID := uint32(0)
for _, keysPair := range cp.NodesKeys[shardID] {
tcn := NewTestConsensusNode(
consensusSize,
roundTime,
consensusType,
*keysPair,
keysPair,
eligibleMap,
waitingMap,
cp.KeyGen)
Expand Down
31 changes: 18 additions & 13 deletions integrationTests/testHeartbeatNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type TestHeartbeatNode struct {
NodesCoordinator nodesCoordinator.NodesCoordinator
PeerShardMapper process.NetworkShardingCollector
Messenger p2p.Messenger
NodeKeys TestKeyPair
NodeKeys *TestNodeKeys
DataPool dataRetriever.PoolsHolder
Sender update.Closer
PeerAuthInterceptor *interceptors.MultiDataInterceptor
Expand Down Expand Up @@ -188,9 +188,11 @@ func NewTestHeartbeatNode(
pkBytes, _ := pk.ToByteArray()
thn.PeerShardMapper.UpdatePeerIDInfo(localId, pkBytes, shardCoordinator.SelfId())

thn.NodeKeys = TestKeyPair{
Sk: sk,
Pk: pk,
thn.NodeKeys = &TestNodeKeys{
MainKey: &TestKeyPair{
Sk: sk,
Pk: pk,
},
}

// start a go routine in order to allow peers to connect first
Expand All @@ -206,7 +208,7 @@ func NewTestHeartbeatNodeWithCoordinator(
nodeShardId uint32,
p2pConfig p2pConfig.P2PConfig,
coordinator nodesCoordinator.NodesCoordinator,
keys TestKeyPair,
keys *TestNodeKeys,
) *TestHeartbeatNode {
keygen := signing.NewKeyGenerator(mcl.NewSuiteBLS12())
singleSigner := singlesig.NewBlsSigner()
Expand Down Expand Up @@ -277,7 +279,7 @@ func CreateNodesWithTestHeartbeatNode(
) map[uint32][]*TestHeartbeatNode {

cp := CreateCryptoParams(nodesPerShard, numMetaNodes, uint32(numShards))
pubKeys := PubKeysMapFromKeysMap(cp.Keys)
pubKeys := PubKeysMapFromNodesKeysMap(cp.NodesKeys)
validatorsMap := GenValidatorsFromPubKeys(pubKeys, uint32(numShards))
validatorsForNodesCoordinator, _ := nodesCoordinator.NodesInfoToValidators(validatorsMap)
nodesMap := make(map[uint32][]*TestHeartbeatNode)
Expand Down Expand Up @@ -311,13 +313,13 @@ func CreateNodesWithTestHeartbeatNode(

nodesList := make([]*TestHeartbeatNode, len(validatorList))
for i := range validatorList {
kp := cp.Keys[shardId][i]
kp := cp.NodesKeys[shardId][i]
nodesList[i] = NewTestHeartbeatNodeWithCoordinator(
uint32(numShards),
shardId,
p2pConfig,
nodesCoordinatorInstance,
*kp,
kp,
)
}
nodesMap[shardId] = nodesList
Expand Down Expand Up @@ -355,12 +357,15 @@ func CreateNodesWithTestHeartbeatNode(
nodesCoordinatorInstance, err := nodesCoordinator.NewIndexHashedNodesCoordinator(argumentsNodesCoordinator)
log.LogIfError(err)

nodeKeysInstance := &TestNodeKeys{
MainKey: createCryptoPair(),
}
n := NewTestHeartbeatNodeWithCoordinator(
uint32(numShards),
shardId,
p2pConfig,
nodesCoordinatorInstance,
createCryptoPair(),
nodeKeysInstance,
)

nodesMap[shardId] = append(nodesMap[shardId], n)
Expand Down Expand Up @@ -415,7 +420,7 @@ func (thn *TestHeartbeatNode) initSender() {
PeerSubType: core.RegularPeer,
CurrentBlockProvider: &testscommon.ChainHandlerStub{},
PeerSignatureHandler: thn.PeerSigHandler,
PrivateKey: thn.NodeKeys.Sk,
PrivateKey: thn.NodeKeys.MainKey.Sk,
RedundancyHandler: &mock.RedundancyHandlerStub{},
NodesCoordinator: thn.NodesCoordinator,
HardforkTrigger: &testscommon.HardforkTriggerStub{},
Expand Down Expand Up @@ -696,7 +701,7 @@ func MakeDisplayTableForHeartbeatNodes(nodes map[uint32][]*TestHeartbeatNode) st

for shardId, nodesList := range nodes {
for _, n := range nodesList {
buffPk, _ := n.NodeKeys.Pk.ToByteArray()
buffPk, _ := n.NodeKeys.MainKey.Pk.ToByteArray()

peerInfo := n.Messenger.GetConnectedPeersInfo()

Expand Down Expand Up @@ -807,11 +812,11 @@ func (thn *TestHeartbeatNode) IsInterfaceNil() bool {
return thn == nil
}

func createCryptoPair() TestKeyPair {
func createCryptoPair() *TestKeyPair {
suite := mcl.NewSuiteBLS12()
keyGen := signing.NewKeyGenerator(suite)

kp := TestKeyPair{}
kp := &TestKeyPair{}
kp.Sk, kp.Pk = keyGen.GeneratePair()

return kp
Expand Down
Loading