2023-08-17 14:20:35 +00:00
|
|
|
import { CustomEvent, EventEmitter } from "@libp2p/interface/events";
|
|
|
|
import type {
|
|
|
|
PeerDiscovery,
|
|
|
|
PeerDiscoveryEvents
|
|
|
|
} from "@libp2p/interface/peer-discovery";
|
|
|
|
import { peerDiscovery as symbol } from "@libp2p/interface/peer-discovery";
|
|
|
|
import type { PeerInfo } from "@libp2p/interface/peer-info";
|
2023-10-10 14:48:02 +00:00
|
|
|
import { encodeRelayShard } from "@waku/enr";
|
2023-08-17 14:20:35 +00:00
|
|
|
import type {
|
|
|
|
DnsDiscOptions,
|
|
|
|
DnsDiscoveryComponents,
|
|
|
|
IEnr,
|
|
|
|
NodeCapabilityCount
|
|
|
|
} from "@waku/interfaces";
|
2023-10-20 11:06:47 +00:00
|
|
|
import { Logger } from "@waku/utils";
|
2023-08-17 14:20:35 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
DEFAULT_BOOTSTRAP_TAG_NAME,
|
|
|
|
DEFAULT_BOOTSTRAP_TAG_TTL,
|
|
|
|
DEFAULT_BOOTSTRAP_TAG_VALUE,
|
|
|
|
DEFAULT_NODE_REQUIREMENTS
|
|
|
|
} from "./constants.js";
|
|
|
|
import { DnsNodeDiscovery } from "./dns.js";
|
|
|
|
|
2023-10-20 11:06:47 +00:00
|
|
|
const log = new Logger("peer-discovery-dns");
|
2023-08-17 14:20:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse options and expose function to return bootstrap peer addresses.
|
|
|
|
*/
|
|
|
|
export class PeerDiscoveryDns
|
|
|
|
extends EventEmitter<PeerDiscoveryEvents>
|
|
|
|
implements PeerDiscovery
|
|
|
|
{
|
|
|
|
private nextPeer: (() => AsyncGenerator<IEnr>) | undefined;
|
|
|
|
private _started: boolean;
|
|
|
|
private _components: DnsDiscoveryComponents;
|
|
|
|
private _options: DnsDiscOptions;
|
|
|
|
|
|
|
|
constructor(components: DnsDiscoveryComponents, options: DnsDiscOptions) {
|
|
|
|
super();
|
|
|
|
this._started = false;
|
|
|
|
this._components = components;
|
|
|
|
this._options = options;
|
|
|
|
|
|
|
|
const { enrUrls } = options;
|
2023-10-20 11:06:47 +00:00
|
|
|
log.info("Use following EIP-1459 ENR Tree URLs: ", enrUrls);
|
2023-08-17 14:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start discovery process
|
|
|
|
*/
|
|
|
|
async start(): Promise<void> {
|
2023-10-20 11:06:47 +00:00
|
|
|
log.info("Starting peer discovery via dns");
|
2023-08-17 14:20:35 +00:00
|
|
|
|
|
|
|
this._started = true;
|
|
|
|
|
|
|
|
if (this.nextPeer === undefined) {
|
|
|
|
let { enrUrls } = this._options;
|
|
|
|
if (!Array.isArray(enrUrls)) enrUrls = [enrUrls];
|
|
|
|
|
|
|
|
const { wantedNodeCapabilityCount } = this._options;
|
|
|
|
const dns = await DnsNodeDiscovery.dnsOverHttp();
|
|
|
|
|
|
|
|
this.nextPeer = dns.getNextPeer.bind(
|
|
|
|
dns,
|
|
|
|
enrUrls,
|
|
|
|
wantedNodeCapabilityCount
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for await (const peerEnr of this.nextPeer()) {
|
|
|
|
if (!this._started) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:48:02 +00:00
|
|
|
const { peerInfo, shardInfo } = peerEnr;
|
2023-08-17 14:20:35 +00:00
|
|
|
|
|
|
|
if (!peerInfo) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tagsToUpdate = {
|
2023-10-10 14:48:02 +00:00
|
|
|
[DEFAULT_BOOTSTRAP_TAG_NAME]: {
|
|
|
|
value: this._options.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE,
|
|
|
|
ttl: this._options.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL
|
2023-08-17 14:20:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let isPeerChanged = false;
|
|
|
|
const isPeerExists = await this._components.peerStore.has(peerInfo.id);
|
|
|
|
|
|
|
|
if (isPeerExists) {
|
|
|
|
const peer = await this._components.peerStore.get(peerInfo.id);
|
|
|
|
const hasBootstrapTag = peer.tags.has(DEFAULT_BOOTSTRAP_TAG_NAME);
|
|
|
|
|
|
|
|
if (!hasBootstrapTag) {
|
|
|
|
isPeerChanged = true;
|
2023-10-10 14:48:02 +00:00
|
|
|
await this._components.peerStore.merge(peerInfo.id, {
|
|
|
|
tags: tagsToUpdate
|
|
|
|
});
|
2023-08-17 14:20:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isPeerChanged = true;
|
2023-10-10 14:48:02 +00:00
|
|
|
await this._components.peerStore.save(peerInfo.id, {
|
|
|
|
tags: tagsToUpdate,
|
|
|
|
...(shardInfo && {
|
|
|
|
metadata: {
|
|
|
|
shardInfo: encodeRelayShard(shardInfo)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2023-08-17 14:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isPeerChanged) {
|
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent<PeerInfo>("peer", { detail: peerInfo })
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop emitting events
|
|
|
|
*/
|
|
|
|
stop(): void {
|
|
|
|
this._started = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
get [symbol](): true {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
get [Symbol.toStringTag](): string {
|
|
|
|
return "@waku/bootstrap";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function wakuDnsDiscovery(
|
|
|
|
enrUrls: string[],
|
|
|
|
wantedNodeCapabilityCount: Partial<NodeCapabilityCount> = DEFAULT_NODE_REQUIREMENTS
|
|
|
|
): (components: DnsDiscoveryComponents) => PeerDiscoveryDns {
|
|
|
|
return (components: DnsDiscoveryComponents) =>
|
|
|
|
new PeerDiscoveryDns(components, { enrUrls, wantedNodeCapabilityCount });
|
|
|
|
}
|