Skip to content

Commit 69778d0

Browse files
authored
Merge pull request #294 from status-im/limit-connections
Connects to a limited number of bootstrap nodes, defaults to 1
2 parents 1a9ab2e + 6504106 commit 69778d0

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111
- Upgrade libp2p libraries: @chainsafe/libp2p-noise@4.1.1, [email protected], [email protected].
12+
- Connects to a limited number of bootstrap nodes, defaults to 1.
1213

1314
## [0.12.0] - 2021-09-2
1415

src/lib/discovery.browser.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect } from 'chai';
2+
3+
import { getPseudoRandomSubset } from './discovery';
4+
5+
describe('Discovery', () => {
6+
it('returns all values when wanted number matches available values', function () {
7+
const values = ['a', 'b', 'c'];
8+
9+
const res = getPseudoRandomSubset(values, 3);
10+
11+
expect(res.length).to.eq(3);
12+
expect(res.includes('a')).to.be.true;
13+
expect(res.includes('b')).to.be.true;
14+
expect(res.includes('c')).to.be.true;
15+
});
16+
17+
it('returns all values when wanted number is greater than available values', function () {
18+
const values = ['a', 'b', 'c'];
19+
20+
const res = getPseudoRandomSubset(values, 5);
21+
22+
expect(res.length).to.eq(3);
23+
expect(res.includes('a')).to.be.true;
24+
expect(res.includes('b')).to.be.true;
25+
expect(res.includes('c')).to.be.true;
26+
});
27+
28+
it('returns a subset of values when wanted number is lesser than available values', function () {
29+
const values = ['a', 'b', 'c'];
30+
31+
const res = getPseudoRandomSubset(values, 2);
32+
33+
expect(res.length).to.eq(2);
34+
});
35+
});

src/lib/discovery.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import axios from 'axios';
22
import debug from 'debug';
3+
import { shuffle } from 'libp2p-gossipsub/src/utils';
34

45
const dbg = debug('waku:discovery');
56

7+
const DefaultWantedNumber = 1;
8+
69
/**
710
* GET list of nodes from remote HTTP host.
811
*
@@ -14,15 +17,21 @@ const dbg = debug('waku:discovery');
1417
* request returns `{ foo: { bar: [address1, address2] } }` then `path` should be
1518
* `[ "foo", "bar" ]`.
1619
* @param url Remote host containing bootstrap peers in JSON format.
20+
* @param wantedNumber The number of connections desired. Defaults to [DefaultWantedNumber].
1721
*
1822
* @returns An array of multiaddresses.
1923
* @throws If the remote host is unreachable or the response cannot be parsed
2024
* according to the passed _path_.
2125
*/
2226
export async function getBootstrapNodes(
2327
path: string[] = ['fleets', 'wakuv2.prod', 'waku-websocket'],
24-
url = 'https://fleets.status.im/'
28+
url = 'https://fleets.status.im/',
29+
wantedNumber: number = DefaultWantedNumber
2530
): Promise<string[]> {
31+
if (wantedNumber <= 0) {
32+
return [];
33+
}
34+
2635
const res = await axios.get(url, {
2736
headers: { 'Content-Type': 'application/json' },
2837
});
@@ -43,18 +52,30 @@ export async function getBootstrapNodes(
4352
}
4453

4554
if (Array.isArray(nodes)) {
46-
return nodes;
55+
return getPseudoRandomSubset(nodes, wantedNumber);
4756
}
4857

4958
if (typeof nodes === 'string') {
5059
return [nodes];
5160
}
5261

5362
if (typeof nodes === 'object') {
54-
return Object.values(nodes);
63+
nodes = Object.values(nodes);
64+
getPseudoRandomSubset(nodes, wantedNumber);
5565
}
5666

5767
throw `Failed to retrieve bootstrap nodes: response format is not supported: ${JSON.stringify(
5868
nodes
5969
)}`;
6070
}
71+
72+
export function getPseudoRandomSubset(
73+
values: string[],
74+
wantedNumber: number
75+
): string[] {
76+
if (values.length <= wantedNumber) {
77+
return values;
78+
}
79+
80+
return shuffle(values).slice(0, wantedNumber);
81+
}

0 commit comments

Comments
 (0)