Skip to content

Commit

Permalink
Merge pull request #4577 from LiskHQ/4351-adding_condition_to_select_…
Browse files Browse the repository at this point in the history
…peers_for_connection

Add P2P condition to select peers for connection - Closed #4351
  • Loading branch information
ManuGowda committed Dec 2, 2019
2 parents a30343f + a562264 commit 080f8aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions elements/lisk-p2p/src/utils/select.ts
Expand Up @@ -127,9 +127,12 @@ export const selectPeersForConnection = (
}

// LIP004 https://github.com/LiskHQ/lips/blob/master/proposals/lip-0004.md#peer-discovery-and-selection
const x =
input.triedPeers.length / (input.triedPeers.length + input.newPeers.length);
const minimumProbability = 0.5;
const x =
input.triedPeers.length < 100
? minimumProbability
: input.triedPeers.length /
(input.triedPeers.length + input.newPeers.length);
const r = Math.max(x, minimumProbability);

const shuffledTriedPeers = shuffle(input.triedPeers);
Expand Down
29 changes: 29 additions & 0 deletions elements/lisk-p2p/test/unit/utils/select.ts
Expand Up @@ -351,6 +351,35 @@ describe('peer selector', () => {
});
});

describe('when there are less than 100 peers', () => {
it('should return peers uniformly from both lists', () => {
const triedPeers = initPeerInfoListWithSuffix('111.112.113', 25);
const newPeers = initPeerInfoListWithSuffix('111.112.114', 75);

const selectedPeers = selectPeersForConnection({
triedPeers,
newPeers,
peerLimit: 50,
});
expect(selectedPeers)
.to.be.an('array')
.of.length(50);

expect([...triedPeers, ...newPeers]).to.include.members(selectedPeers);

let triedCount = 0;
let newCount = 0;
for (const peer of selectedPeers) {
if (triedPeers.find(triedPeer => peer.peerId === triedPeer.peerId)) {
triedCount++;
}
newCount++;
}
expect(triedCount).to.gte(23);
expect(newCount).to.gte(23);
});
});

describe('when there are multiple peer from same IP with different height', () => {
it('should return only unique IPs', () => {
let uniqIpAddresses: Array<string> = [];
Expand Down

0 comments on commit 080f8aa

Please sign in to comment.