mirror of https://github.com/waku-org/js-waku.git
Connects to a limited number of bootstrap nodes, defaults to 1
This commit is contained in:
parent
3bf7e2250c
commit
8a4e7f5f3c
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Changed
|
||||
- Upgrade libp2p libraries: @chainsafe/libp2p-noise@4.1.1, libp2p@0.32.4, libp2p-gossipsub@0.11.1.
|
||||
- Connects to a limited number of bootstrap nodes, defaults to 1.
|
||||
|
||||
## [0.12.0] - 2021-09-2
|
||||
|
||||
### Added
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
import { expect } from 'chai';
|
||||
|
||||
import { getPseudoRandomSubset } from './discovery';
|
||||
|
||||
describe('Discovery', () => {
|
||||
it('returns all values when wanted number matches available values', function () {
|
||||
const values = ['a', 'b', 'c'];
|
||||
|
||||
const res = getPseudoRandomSubset(values, 3);
|
||||
|
||||
expect(res.length).to.eq(3);
|
||||
expect(res.includes('a')).to.be.true;
|
||||
expect(res.includes('b')).to.be.true;
|
||||
expect(res.includes('c')).to.be.true;
|
||||
});
|
||||
|
||||
it('returns all values when wanted number is greater than available values', function () {
|
||||
const values = ['a', 'b', 'c'];
|
||||
|
||||
const res = getPseudoRandomSubset(values, 5);
|
||||
|
||||
expect(res.length).to.eq(3);
|
||||
expect(res.includes('a')).to.be.true;
|
||||
expect(res.includes('b')).to.be.true;
|
||||
expect(res.includes('c')).to.be.true;
|
||||
});
|
||||
|
||||
it('returns a subset of values when wanted number is lesser than available values', function () {
|
||||
const values = ['a', 'b', 'c'];
|
||||
|
||||
const res = getPseudoRandomSubset(values, 2);
|
||||
|
||||
expect(res.length).to.eq(2);
|
||||
});
|
||||
});
|
|
@ -1,8 +1,11 @@
|
|||
import axios from 'axios';
|
||||
import debug from 'debug';
|
||||
import { shuffle } from 'libp2p-gossipsub/src/utils';
|
||||
|
||||
const dbg = debug('waku:discovery');
|
||||
|
||||
const DefaultWantedNumber = 1;
|
||||
|
||||
/**
|
||||
* GET list of nodes from remote HTTP host.
|
||||
*
|
||||
|
@ -14,6 +17,7 @@ const dbg = debug('waku:discovery');
|
|||
* request returns `{ foo: { bar: [address1, address2] } }` then `path` should be
|
||||
* `[ "foo", "bar" ]`.
|
||||
* @param url Remote host containing bootstrap peers in JSON format.
|
||||
* @param wantedNumber The number of connections desired. Defaults to [DefaultWantedNumber].
|
||||
*
|
||||
* @returns An array of multiaddresses.
|
||||
* @throws If the remote host is unreachable or the response cannot be parsed
|
||||
|
@ -21,8 +25,13 @@ const dbg = debug('waku:discovery');
|
|||
*/
|
||||
export async function getBootstrapNodes(
|
||||
path: string[] = ['fleets', 'wakuv2.prod', 'waku-websocket'],
|
||||
url = 'https://fleets.status.im/'
|
||||
url = 'https://fleets.status.im/',
|
||||
wantedNumber: number = DefaultWantedNumber
|
||||
): Promise<string[]> {
|
||||
if (wantedNumber <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const res = await axios.get(url, {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
@ -43,7 +52,7 @@ export async function getBootstrapNodes(
|
|||
}
|
||||
|
||||
if (Array.isArray(nodes)) {
|
||||
return nodes;
|
||||
return getPseudoRandomSubset(nodes, wantedNumber);
|
||||
}
|
||||
|
||||
if (typeof nodes === 'string') {
|
||||
|
@ -51,10 +60,22 @@ export async function getBootstrapNodes(
|
|||
}
|
||||
|
||||
if (typeof nodes === 'object') {
|
||||
return Object.values(nodes);
|
||||
nodes = Object.values(nodes);
|
||||
getPseudoRandomSubset(nodes, wantedNumber);
|
||||
}
|
||||
|
||||
throw `Failed to retrieve bootstrap nodes: response format is not supported: ${JSON.stringify(
|
||||
nodes
|
||||
)}`;
|
||||
}
|
||||
|
||||
export function getPseudoRandomSubset(
|
||||
values: string[],
|
||||
wantedNumber: number
|
||||
): string[] {
|
||||
if (values.length <= wantedNumber) {
|
||||
return values;
|
||||
}
|
||||
|
||||
return shuffle(values).slice(0, wantedNumber);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue