Skip to content

Commit

Permalink
feat(core-p2p): Fetch list of peers from at least a few others (#2152)
Browse files Browse the repository at this point in the history
Previously we would fetch the list of peers from a single other peer and
be satisfied with it as long as our list of peers satisfies
hasMinimumPeers().

With this change we will query at least 4 peers and combine their
lists. This will help in getting a better view of the network and all of
its peers in times of splits and bad network connectivity.
  • Loading branch information
vasild authored and faustbrian committed Feb 25, 2019
1 parent a3c2e40 commit 499c52e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/core-p2p/src/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,21 @@ export class Monitor implements P2P.IMonitor {
* Populate list of available peers from random peers.
*/
public async discoverPeers() {
const queryAtLeastNPeers = 4;
let queriedPeers = 0;

const shuffledPeers = shuffle(this.getPeers());

for (const peer of shuffledPeers) {
try {
const hisPeers = await peer.getPeers();
queriedPeers++;
await Promise.all(hisPeers.map(p => this.acceptNewPeer(p, { lessVerbose: true })));
} catch (error) {
// Just try with the next peer from shuffledPeers.
}

if (this.hasMinimumPeers()) {
if (this.hasMinimumPeers() && queriedPeers >= queryAtLeastNPeers) {
return;
}
}
Expand Down

0 comments on commit 499c52e

Please sign in to comment.