Make parseBootstrap a class

Makes it easier to use the resulting function and document defaults.
This commit is contained in:
Franck Royer 2022-01-13 16:04:57 +11:00
parent ae0faa2146
commit fa4e94750f
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 60 additions and 51 deletions

View File

@ -7,10 +7,6 @@ import { getNodesFromHostedJson, getPseudoRandomSubset } from './index';
const dbg = debug('waku:discovery:bootstrap'); const dbg = debug('waku:discovery:bootstrap');
export const DefaultMaxPeers = 1;
export type BootstrapFn = () => Promise<Multiaddr[]>;
/** /**
* Setup discovery method used to bootstrap. * Setup discovery method used to bootstrap.
* *
@ -20,7 +16,7 @@ export interface BootstrapOptions {
/** /**
* The maximum of peers to connect to as part of the bootstrap process. * The maximum of peers to connect to as part of the bootstrap process.
* *
* @default [[DefaultMaxPeers]] * @default [[Bootstrap.DefaultMaxPeers]]
*/ */
maxPeers?: number; maxPeers?: number;
/** /**
@ -48,15 +44,25 @@ export interface BootstrapOptions {
} }
/** /**
* Parse the bootstrap options and returns an async function that returns node addresses upon invocation. * Parse options and expose function to return bootstrap peer addresses.
*/ */
export function parseBootstrap(opts: BootstrapOptions): BootstrapFn { export class Bootstrap {
const maxPeers = opts.maxPeers ?? DefaultMaxPeers; public static DefaultMaxPeers = 1;
public readonly getBootstrapPeers: (() => Promise<Multiaddr[]>) | undefined;
constructor(opts: BootstrapOptions) {
const maxPeers = opts.maxPeers ?? Bootstrap.DefaultMaxPeers;
if (opts.default) { if (opts.default) {
dbg('Bootstrap: Use hosted list of peers.'); dbg('Bootstrap: Use hosted list of peers.');
return getNodesFromHostedJson.bind({}, undefined, undefined, maxPeers); this.getBootstrapPeers = getNodesFromHostedJson.bind(
{},
undefined,
undefined,
maxPeers
);
} else if (opts.peers !== undefined && opts.peers.length > 0) { } else if (opts.peers !== undefined && opts.peers.length > 0) {
dbg('Bootstrap: Use provided list of peers.'); dbg('Bootstrap: Use provided list of peers.');
@ -64,16 +70,18 @@ export function parseBootstrap(opts: BootstrapOptions): BootstrapFn {
(node: string) => new Multiaddr(node) (node: string) => new Multiaddr(node)
); );
const peers = getPseudoRandomSubset(allPeers, maxPeers); const peers = getPseudoRandomSubset(allPeers, maxPeers);
return (): Promise<Multiaddr[]> => Promise.resolve(peers); this.getBootstrapPeers = (): Promise<Multiaddr[]> =>
Promise.resolve(peers);
} else if (typeof opts.getPeers === 'function') { } else if (typeof opts.getPeers === 'function') {
dbg('Bootstrap: Use provided getPeers function.'); dbg('Bootstrap: Use provided getPeers function.');
const getPeers = opts.getPeers; const getPeers = opts.getPeers;
return async (): Promise<Multiaddr[]> => { this.getBootstrapPeers = async (): Promise<Multiaddr[]> => {
const allPeers = await getPeers(); const allPeers = await getPeers();
return getPseudoRandomSubset<string | Multiaddr>(allPeers, maxPeers).map( return getPseudoRandomSubset<string | Multiaddr>(
(node) => new Multiaddr(node) allPeers,
); maxPeers
).map((node) => new Multiaddr(node));
}; };
} else if (opts.enrUrl) { } else if (opts.enrUrl) {
const enrUrl = opts.enrUrl; const enrUrl = opts.enrUrl;
@ -81,7 +89,7 @@ export function parseBootstrap(opts: BootstrapOptions): BootstrapFn {
const dns = DnsNodeDiscovery.dnsOverHttp(); const dns = DnsNodeDiscovery.dnsOverHttp();
return async (): Promise<Multiaddr[]> => { this.getBootstrapPeers = async (): Promise<Multiaddr[]> => {
const enrs = await dns.getPeers(maxPeers, [enrUrl]); const enrs = await dns.getPeers(maxPeers, [enrUrl]);
const addresses: Multiaddr[] = []; const addresses: Multiaddr[] = [];
enrs.forEach((enr) => { enrs.forEach((enr) => {
@ -98,6 +106,7 @@ export function parseBootstrap(opts: BootstrapOptions): BootstrapFn {
}; };
} else { } else {
dbg('No bootstrap method specified, no peer will be returned'); dbg('No bootstrap method specified, no peer will be returned');
return (): Promise<Multiaddr[]> => Promise.resolve([]); this.getBootstrapPeers = undefined;
}
} }
} }

View File

@ -1,7 +1,7 @@
import { shuffle } from 'libp2p-gossipsub/src/utils'; import { shuffle } from 'libp2p-gossipsub/src/utils';
export { getNodesFromHostedJson } from './hosted_json'; export { getNodesFromHostedJson } from './hosted_json';
export { parseBootstrap, BootstrapOptions, BootstrapFn } from './bootstrap'; export { Bootstrap, BootstrapOptions } from './bootstrap';
export { DnsClient, DnsNodeDiscovery } from './dns'; export { DnsClient, DnsNodeDiscovery } from './dns';
export { Endpoints, DnsOverHttps } from './dns_over_https'; export { Endpoints, DnsOverHttps } from './dns_over_https';
export { ENRTree } from './enrtree'; export { ENRTree } from './enrtree';

View File

@ -18,7 +18,7 @@ import Ping from 'libp2p/src/ping';
import { Multiaddr, multiaddr } from 'multiaddr'; import { Multiaddr, multiaddr } from 'multiaddr';
import PeerId from 'peer-id'; import PeerId from 'peer-id';
import { parseBootstrap } from './discovery'; import { Bootstrap } from './discovery';
import { BootstrapOptions } from './discovery/bootstrap'; import { BootstrapOptions } from './discovery/bootstrap';
import { getPeersForProtocol } from './select_peer'; import { getPeersForProtocol } from './select_peer';
import { LightPushCodec, WakuLightPush } from './waku_light_push'; import { LightPushCodec, WakuLightPush } from './waku_light_push';
@ -187,11 +187,11 @@ export class Waku {
}); });
if (options?.bootstrap) { if (options?.bootstrap) {
const bootstrap = parseBootstrap(options?.bootstrap); const bootstrap = new Bootstrap(options?.bootstrap);
if (bootstrap !== undefined) { if (bootstrap.getBootstrapPeers !== undefined) {
try { try {
const list = await bootstrap(); const list = await bootstrap.getBootstrapPeers();
// Note: this overrides any other peer discover // Note: this overrides any other peer discover
libp2pOpts.modules = Object.assign(libp2pOpts.modules, { libp2pOpts.modules = Object.assign(libp2pOpts.modules, {