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

Commit

Permalink
Merge pull request #6328 from LiskHQ/6300-peers-connection-fix
Browse files Browse the repository at this point in the history
With the sentry node setup, forging node do not get block on time - Closes #6300
  • Loading branch information
shuse2 committed Apr 19, 2021
2 parents ad6553a + 585bac1 commit 9ff425d
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 5 deletions.
13 changes: 11 additions & 2 deletions elements/lisk-p2p/src/utils/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import { ConnectionKind } from '../constants';
import { ConnectionKind, PeerKind } from '../constants';
// eslint-disable-next-line import/no-cycle
import {
P2PPeerInfo,
Expand Down Expand Up @@ -85,6 +85,10 @@ export const selectPeersForSend = (
: false,
);

const fixedPeers = shuffledPeers.filter((peerInfo: P2PPeerInfo) =>
peerInfo.internalState ? peerInfo.internalState.peerKind === PeerKind.FIXED_PEER : false,
);

let shortestPeersList;
let longestPeersList;

Expand All @@ -99,8 +103,13 @@ export const selectPeersForSend = (
const selectedFirstKindPeers = shortestPeersList.slice(0, halfPeerLimit);
const remainingPeerLimit = peerLimit - selectedFirstKindPeers.length;
const selectedSecondKindPeers = longestPeersList.slice(0, remainingPeerLimit);
const selectedPeers = selectedFirstKindPeers.concat(selectedSecondKindPeers).concat(fixedPeers);
const uniquePeerIds = [...new Set(selectedPeers.map(p => p.peerId))];
const uniquePeers = uniquePeerIds.map(peerId =>
selectedPeers.find(p => p.peerId === peerId),
) as ReadonlyArray<P2PPeerInfo>;

return selectedFirstKindPeers.concat(selectedSecondKindPeers);
return uniquePeers;
};

export const selectPeersForConnection = (
Expand Down
166 changes: 163 additions & 3 deletions elements/lisk-p2p/test/unit/utils/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
selectPeersForSend,
} from '../../../src/utils/select';
import { P2PNodeInfo, P2PPeerInfo } from '../../../src/types';
import { DEFAULT_SEND_PEER_LIMIT } from '../../../src/constants';
import { ConnectionKind, PeerKind, DEFAULT_SEND_PEER_LIMIT } from '../../../src/constants';

describe('peer selector', () => {
const nodeInfo: P2PNodeInfo = {
Expand Down Expand Up @@ -137,9 +137,8 @@ describe('peer selector', () => {
});

describe('#selectPeersForSend', () => {
const peerList = initPeerInfoListWithSuffix('111.112.113', 120);

it('should return an array containing an even number of inbound and outbound peers', () => {
const peerList = initPeerInfoListWithSuffix('111.112.113', 120);
const selectedPeers = selectPeersForSend({
peers: peerList,
nodeInfo,
Expand All @@ -164,6 +163,167 @@ describe('peer selector', () => {
expect(peerKindCounts.inbound).toEqual(peerKindCounts.outbound);
expect(peerKindCounts.inbound).toBe(DEFAULT_SEND_PEER_LIMIT / 2);
});

it('should return an array containing more inbound peers and if outbound peers are less than half', () => {
const inboundPeers = initPeerInfoListWithSuffix(
'111.112.113',
DEFAULT_SEND_PEER_LIMIT / 2 - 2,
).map(
p =>
({
...p,
internalState: { ...p.internalState, connectionKind: ConnectionKind.INBOUND },
} as P2PPeerInfo),
);

const outboundPeers = initPeerInfoListWithSuffix(
'111.112.114',
DEFAULT_SEND_PEER_LIMIT / 2 + 2,
).map(
p =>
({
...p,
internalState: { ...p.internalState, connectionKind: ConnectionKind.OUTBOUND },
} as P2PPeerInfo),
);

const selectedPeers = selectPeersForSend({
peers: [...inboundPeers, ...outboundPeers],
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});
const selectedInboundPeers = selectedPeers.filter(
p => p.internalState?.connectionKind === ConnectionKind.INBOUND,
);
const selectedOutboundPeers = selectedPeers.filter(
p => p.internalState?.connectionKind === ConnectionKind.OUTBOUND,
);

// Assert
expect(selectedPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT);
expect(selectedInboundPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT / 2 - 2);
expect(selectedOutboundPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT / 2 + 2);
});

it('should return an array containing more outbound peers and if inbound peers are less than half', () => {
const inboundPeers = initPeerInfoListWithSuffix(
'111.112.113',
DEFAULT_SEND_PEER_LIMIT / 2 + 2,
).map(
p =>
({
...p,
internalState: { ...p.internalState, connectionKind: ConnectionKind.INBOUND },
} as P2PPeerInfo),
);

const outboundPeers = initPeerInfoListWithSuffix(
'111.112.114',
DEFAULT_SEND_PEER_LIMIT / 2 - 2,
).map(
p =>
({
...p,
internalState: { ...p.internalState, connectionKind: ConnectionKind.OUTBOUND },
} as P2PPeerInfo),
);

const selectedPeers = selectPeersForSend({
peers: [...inboundPeers, ...outboundPeers],
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});
const selectedInboundPeers = selectedPeers.filter(
p => p.internalState?.connectionKind === ConnectionKind.INBOUND,
);
const selectedOutboundPeers = selectedPeers.filter(
p => p.internalState?.connectionKind === ConnectionKind.OUTBOUND,
);

// Assert
expect(selectedPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT);
expect(selectedInboundPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT / 2 + 2);
expect(selectedOutboundPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT / 2 - 2);
});

it('should return an array must containing fixed peers when original list is more than limit', () => {
const peerList = initPeerInfoListWithSuffix('111.112.113', 120);
const fixedPeers = initPeerInfoListWithSuffix('111.112.114', 5).map(
p =>
({
...p,
internalState: { ...p.internalState, peerKind: PeerKind.FIXED_PEER },
} as P2PPeerInfo),
);

const selectedPeers = selectPeersForSend({
peers: [...peerList, ...fixedPeers],
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});

const selectedFixedPeers = selectedPeers.filter(
p => p.internalState?.peerKind === PeerKind.FIXED_PEER,
);

// Assert
expect(selectedPeers.length).toBeGreaterThanOrEqual(DEFAULT_SEND_PEER_LIMIT);
expect(selectedFixedPeers).toHaveLength(5);
});

it('should return an array must containing fixed peers when original list is less than limit', () => {
const peerList = initPeerInfoListWithSuffix('111.112.113', 4);
const fixedPeers = initPeerInfoListWithSuffix('111.112.114', 5).map(
p =>
({
...p,
internalState: { ...p.internalState, peerKind: PeerKind.FIXED_PEER },
} as P2PPeerInfo),
);

const selectedPeers = selectPeersForSend({
peers: [...peerList, ...fixedPeers],
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});

const selectedFixedPeers = selectedPeers.filter(
p => p.internalState?.peerKind === PeerKind.FIXED_PEER,
);

// Assert
expect(selectedPeers.length).toBeLessThanOrEqual(9);
expect(selectedFixedPeers).toHaveLength(5);
});

it('should return an unique array if fixed peers are also randomly selected as inbound or outbound', () => {
const peerList = [...initPeerInfoListWithSuffix('111.112.113', 20)];
for (let i = 0; i < 10; i += 1) {
peerList[i] = {
...peerList[i],
internalState: { ...peerList[i].internalState, peerKind: PeerKind.FIXED_PEER },
} as any;
}

const selectedPeers = selectPeersForSend({
peers: peerList,
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});

const selectedFixedPeers = selectedPeers.filter(
p => p.internalState?.peerKind === PeerKind.FIXED_PEER,
);

// Assert
expect(selectedPeers.length).toBeGreaterThanOrEqual(DEFAULT_SEND_PEER_LIMIT);
expect(selectedFixedPeers).toHaveLength(10);
});
});

describe('#selectPeersForConnection', () => {
Expand Down

0 comments on commit 9ff425d

Please sign in to comment.