Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Fix tests related to custom nodeInfo and peerInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Jul 3, 2020
1 parent 6bb8d72 commit f9dc9b3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 35 deletions.
55 changes: 35 additions & 20 deletions elements/lisk-p2p/test/functional/actions/apply_node_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import { platform } from 'os';
import { P2P, events } from '../../../src/index';
import { InvalidNodeInfoError } from '../../../src/errors';
import { wait } from '../../utils/helpers';
import { createNetwork, destroyNetwork } from '../../utils/network_setup';
import { customNodeInfoSchema, customPeerInfoSchema } from '../../utils/schema';

const { EVENT_MESSAGE_RECEIVED, REMOTE_EVENT_POST_NODE_INFO } = events;

Expand All @@ -25,7 +25,21 @@ describe('P2P.applyNodeInfo', () => {
let collectedMessages: Array<any> = [];

beforeAll(async () => {
p2pNodeList = await createNetwork();
const customRPCSchemas = {
nodeInfo: customNodeInfoSchema,
peerInfo: customPeerInfoSchema,
};

const customConfig = () => ({
customRPCSchemas,
nodeInfo: {
options: {
height: 1,
},
},
});

p2pNodeList = await createNetwork({ customConfig });

collectedMessages = [];
for (const p2p of p2pNodeList) {
Expand All @@ -36,7 +50,7 @@ describe('P2P.applyNodeInfo', () => {
request.peerId === '127.0.0.1:5000'
) {
collectedMessages.push({
nodePort: p2p.nodeInfo.port,
nodePort: p2p.config.port,
request,
});
}
Expand All @@ -46,14 +60,14 @@ describe('P2P.applyNodeInfo', () => {
const firstP2PNode = p2pNodeList[0];

firstP2PNode.applyNodeInfo({
os: platform(),
networkId:
'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba',
networkVersion: '1.1',
port: firstP2PNode.nodeInfo.port,
height: 10,
nonce: 'nonce',
advertiseAddress: true,
options: {
height: 10,
},
});

await wait(200);
Expand All @@ -68,16 +82,14 @@ describe('P2P.applyNodeInfo', () => {

expect(() =>
firstP2PNode.applyNodeInfo({
os: platform(),
networkId:
'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba',
version: firstP2PNode.nodeInfo.version,
networkVersion: '1.1',
port: firstP2PNode.nodeInfo.port,
options: firstP2PNode.nodeInfo.options,
junk: '1.'.repeat(13000),
nonce: 'nonce',
advertiseAddress: true,
options: {
junk: '1.'.repeat(13000),
},
}),
).toThrow(InvalidNodeInfoError);
});
Expand All @@ -104,23 +116,29 @@ describe('P2P.applyNodeInfo', () => {
.filter(
(receivedMessages: any) =>
receivedMessages?.[0] &&
receivedMessages[0].nodePort !== firstP2PNode.nodeInfo.port,
receivedMessages[0].nodePort !== firstP2PNode.config.port,
)
.forEach((receivedMessages: any) => {
expect(receivedMessages).toHaveLength(1);

expect(receivedMessages[0].request).toMatchObject({
data: { height: 10 },
data: {
networkId:
'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba',
networkVersion: '1.1',
nonce: firstP2PNode.nodeInfo.nonce,
advertiseAddress: true,
},
});
});

// For each peer of firstP2PNode, check that the firstP2PNode's P2PPeerInfo was updated with the new height.
for (const p2pNode of p2pNodeList.slice(1)) {
const firstP2PNodePeerInfo = p2pNode
.getConnectedPeers()
.find(peerInfo => peerInfo.port === firstP2PNode.nodeInfo.port);
.find(peerInfo => peerInfo.port === firstP2PNode.config.port);
expect(firstP2PNodePeerInfo).toMatchObject({
height: 10,
options: {},
ipAddress: '127.0.0.1',
networkId:
'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba',
Expand All @@ -137,19 +155,18 @@ describe('P2P.applyNodeInfo', () => {
for (const p2pNode of p2pNodeList.slice(1)) {
const firstNodeInConnectedPeer = p2pNode
.getConnectedPeers()
.find(peerInfo => peerInfo.port === firstP2PNode.nodeInfo.port);
.find(peerInfo => peerInfo.port === firstP2PNode.config.port);

const allPeersList = p2pNode['_peerBook'].allPeers;

const firstNodeInAllPeersList = allPeersList.find(
peerInfo => peerInfo.port === firstP2PNode.nodeInfo.port,
peerInfo => peerInfo.port === firstP2PNode.config.port,
);

// Check if the peerinfo is updated in new peer list
if (firstNodeInAllPeersList) {
expect(firstNodeInAllPeersList).toMatchObject({
sharedState: {
height: 10,
networkId:
'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba',
nonce: expect.any(String),
Expand All @@ -163,11 +180,9 @@ describe('P2P.applyNodeInfo', () => {
// Check if the peerinfo is updated in connected peer list
if (firstNodeInConnectedPeer) {
expect(firstNodeInConnectedPeer).toMatchObject({
height: 10,
networkId:
'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba',
nonce: expect.any(String),
advertiseAddress: true,
ipAddress: '127.0.0.1',
port: 5000,
peerId: '127.0.0.1:5000',
Expand Down
47 changes: 45 additions & 2 deletions elements/lisk-p2p/test/functional/custom_peer_selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,42 @@ import {

const { ConnectionKind } = constants;

const customNodeInfoSchema = {
$id: '/custom',
type: 'object',
properties: {
modules: {
type: 'array',
fieldNumber: 1,
items: {
dataType: 'string',
},
},
height: {
dataType: 'uint32',
fieldNumber: 2,
},
},
};

const customPeerInfoSchema = {
$id: '/custom',
type: 'object',
properties: {
modules: {
type: 'array',
fieldNumber: 1,
items: {
dataType: 'string',
},
},
height: {
dataType: 'uint32',
fieldNumber: 2,
},
},
};

describe('Custom peer selection', () => {
let p2pNodeList: ReadonlyArray<P2P> = [];

Expand Down Expand Up @@ -105,15 +141,22 @@ describe('Custom peer selection', () => {

beforeEach(async () => {
const customNodeInfo = (index: number) => ({
modules: index % 2 === 0 ? ['fileTransfer'] : ['socialSite'],
height: 1000 + (index % 2),
options: {
modules: index % 2 === 0 ? ['fileTransfer'] : ['socialSite'],
height: 1000 + (index % 2),
},
});

const customRPCSchemas = {
nodeInfo: customNodeInfoSchema,
peerInfo: customPeerInfoSchema,
};
const customConfig = (index: number) => ({
peerSelectionForSend: peerSelectionForSendRequest as P2PPeerSelectionForSendFunction,
peerSelectionForRequest: peerSelectionForSendRequest as P2PPeerSelectionForRequestFunction,
peerSelectionForConnection,
nodeInfo: customNodeInfo(index),
customRPCSchemas,
});

p2pNodeList = await createNetwork({
Expand Down
24 changes: 16 additions & 8 deletions elements/lisk-p2p/test/integration/custom_node_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ describe('Custom nodeInfo', () => {
beforeEach(async () => {
const customConfig = () => ({
nodeInfo: {
maxHeightPreviouslyForged: 11,
maxHeightPrevoted: 2,
options: {
maxHeightPreviouslyForged: 11,
maxHeightPrevoted: 2,
},
},
customRPCSchemas,
});
Expand All @@ -46,25 +48,31 @@ describe('Custom nodeInfo', () => {
for (const peer of triedPeers) {
expect(peer).toMatchObject({
sharedState: {
maxHeightPrevoted: 2,
maxHeightPreviouslyForged: 11,
options: {
maxHeightPrevoted: 2,
maxHeightPreviouslyForged: 11,
},
},
});
}
for (const peer of newPeers) {
if (peer.modules) {
expect(peer).toMatchObject({
sharedState: {
maxHeightPrevoted: 2,
maxHeightPreviouslyForged: 11,
options: {
maxHeightPrevoted: 2,
maxHeightPreviouslyForged: 11,
},
},
});
}
}
for (const peer of p2p.getConnectedPeers()) {
expect(peer).toMatchObject({
maxHeightPrevoted: 2,
maxHeightPreviouslyForged: 11,
options: {
maxHeightPrevoted: 2,
maxHeightPreviouslyForged: 11,
},
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ import { createNetwork, destroyNetwork } from '../utils/network_setup';
const { EVENT_BAN_PEER } = events;

const customNodeInfoSchema = {
$id: '/malformed',
type: 'object',
properties: {
invalid: {
dataType: 'string',
fieldNumber: 8,
fieldNumber: 1,
},
},
};

const customPeerInfoSchema = {
$id: '/malformed',
type: 'object',
properties: {
invalid: {
dataType: 'string',
fieldNumber: 8,
fieldNumber: 1,
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ describe('penalty sending malformed Peer List', () => {
ipAddress: generatedIP,
port: 1000,
sharedState: {
height: 0,
networkVersion: '1.1',
version: '1.1',
networkId: '',
nonce: '',
},
});
}
Expand Down Expand Up @@ -91,8 +91,9 @@ describe('penalty sending malformed Peer List', () => {
ipAddress: '1.1.1.1',
port: 1000,
sharedState: {
version: '1.1',
networkVersion: '1.'.repeat(13000),
networkId: '',
nonce: '',
},
});

Expand Down

0 comments on commit f9dc9b3

Please sign in to comment.