@waku/discovery
Version:
Contains various discovery mechanisms: DNS Discovery (EIP-1459, Peer Exchange, Local Peer Cache Discovery.
99 lines • 3.62 kB
JavaScript
import { TypedEventEmitter } from "@libp2p/interface";
import { peerDiscoverySymbol as symbol } from "@libp2p/interface";
import { DNS_DISCOVERY_TAG } from "@waku/interfaces";
import { encodeRelayShard, Logger } from "@waku/utils";
import { DEFAULT_BOOTSTRAP_TAG_NAME, DEFAULT_BOOTSTRAP_TAG_TTL, DEFAULT_BOOTSTRAP_TAG_VALUE, DEFAULT_NODE_REQUIREMENTS } from "./constants.js";
import { DnsNodeDiscovery } from "./dns.js";
const log = new Logger("peer-discovery-dns");
/**
* Parse options and expose function to return bootstrap peer addresses.
*/
export class PeerDiscoveryDns extends TypedEventEmitter {
nextPeer;
_started;
_components;
_options;
constructor(components, options) {
super();
this._started = false;
this._components = components;
this._options = options;
const { enrUrls } = options;
log.info("Use following EIP-1459 ENR Tree URLs: ", enrUrls);
}
/**
* Start discovery process
*/
async start() {
log.info("Starting peer discovery via dns");
this._started = true;
await this.findPeers();
}
async findPeers() {
if (!this.nextPeer) {
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;
}
const { peerInfo, shardInfo } = peerEnr;
if (!peerInfo) {
continue;
}
const tagsToUpdate = {
[DEFAULT_BOOTSTRAP_TAG_NAME]: {
value: this._options.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE,
ttl: this._options.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL
}
};
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;
await this._components.peerStore.merge(peerInfo.id, {
tags: tagsToUpdate
});
}
}
else {
isPeerChanged = true;
await this._components.peerStore.save(peerInfo.id, {
tags: tagsToUpdate,
...(shardInfo && {
metadata: {
shardInfo: encodeRelayShard(shardInfo)
}
})
});
}
if (isPeerChanged) {
this.dispatchEvent(new CustomEvent("peer", { detail: peerInfo }));
}
}
}
/**
* Stop emitting events
*/
stop() {
this._started = false;
}
get [symbol]() {
return true;
}
get [Symbol.toStringTag]() {
return DNS_DISCOVERY_TAG;
}
}
export function wakuDnsDiscovery(enrUrls, wantedNodeCapabilityCount = DEFAULT_NODE_REQUIREMENTS) {
return (components) => new PeerDiscoveryDns(components, { enrUrls, wantedNodeCapabilityCount });
}
//# sourceMappingURL=dns_discovery.js.map