Skip to content

Commit

Permalink
Merge branch 'master' into feat/px-default
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 authored Aug 21, 2023
2 parents befbd37 + 0d9ea1b commit aa87190
Show file tree
Hide file tree
Showing 16 changed files with 782 additions and 430 deletions.
783 changes: 557 additions & 226 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@types/chai": "^4.3.4",
"@types/chai": "^4.3.5",
"@types/debug": "^4.1.7",
"@types/mocha": "^10.0.1",
"@types/uuid": "^9.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/dns-discovery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@
},
"devDependencies": {
"@libp2p/peer-id": "^2.0.4",
"@libp2p/peer-id-factory": "^2.0.4",
"@libp2p/peer-id-factory": "^3.0.3",
"@multiformats/multiaddr": "^12.0.0",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@types/chai": "^4.3.4",
"@types/chai": "^4.3.5",
"@waku/build-utils": "*",
"@waku/interfaces": "0.0.17",
"chai": "^4.3.7",
Expand Down
16 changes: 16 additions & 0 deletions packages/dns-discovery/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { NodeCapabilityCount } from "@waku/interfaces";

export const enrTree = {
TEST: "enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@test.waku.nodes.status.im",
PROD: "enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@prod.waku.nodes.status.im"
};

export const DEFAULT_BOOTSTRAP_TAG_NAME = "bootstrap";
export const DEFAULT_BOOTSTRAP_TAG_VALUE = 50;
export const DEFAULT_BOOTSTRAP_TAG_TTL = 100_000_000;

export const DEFAULT_NODE_REQUIREMENTS: Partial<NodeCapabilityCount> = {
store: 2,
filter: 1,
lightPush: 1
};
3 changes: 2 additions & 1 deletion packages/dns-discovery/src/dns.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DnsClient } from "@waku/interfaces";
import { expect } from "chai";

import { DnsClient, DnsNodeDiscovery } from "./dns.js";
import { DnsNodeDiscovery } from "./dns.js";
import testData from "./testdata.json" assert { type: "json" };

import { enrTree } from "./index.js";
Expand Down
24 changes: 6 additions & 18 deletions packages/dns-discovery/src/dns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ENR, EnrDecoder } from "@waku/enr";
import type { IEnr } from "@waku/interfaces";
import type {
DnsClient,
IEnr,
NodeCapabilityCount,
SearchContext
} from "@waku/interfaces";
import debug from "debug";

import { DnsOverHttps } from "./dns_over_https.js";
Expand All @@ -11,23 +16,6 @@ import {

const log = debug("waku:discovery:dns");

export type SearchContext = {
domain: string;
publicKey: string;
visits: { [key: string]: boolean };
};

export interface DnsClient {
resolveTXT: (domain: string) => Promise<string[]>;
}

export interface NodeCapabilityCount {
relay: number;
store: number;
filter: number;
lightPush: number;
}

export class DnsNodeDiscovery {
private readonly dns: DnsClient;
private readonly _DNSTreeCache: { [key: string]: string };
Expand Down
136 changes: 136 additions & 0 deletions packages/dns-discovery/src/dns_discovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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";
import type {
DnsDiscOptions,
DnsDiscoveryComponents,
IEnr,
NodeCapabilityCount
} from "@waku/interfaces";
import debug from "debug";

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 = debug("waku:peer-discovery-dns");

/**
* 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;
log("Use following EIP-1459 ENR Tree URLs: ", enrUrls);
}

/**
* Start discovery process
*/
async start(): Promise<void> {
log("Starting peer discovery via dns");

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;
}

const peerInfo = peerEnr.peerInfo;

if (!peerInfo) {
continue;
}

const tagsToUpdate = {
tags: {
[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, tagsToUpdate);
}
} else {
isPeerChanged = true;
await this._components.peerStore.save(peerInfo.id, tagsToUpdate);
}

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 });
}
3 changes: 1 addition & 2 deletions packages/dns-discovery/src/dns_over_https.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { DnsClient } from "@waku/interfaces";
import { bytesToUtf8 } from "@waku/utils/bytes";
import debug from "debug";
import { Endpoint, query, wellknown } from "dns-query";

import { DnsClient } from "./dns.js";

const log = debug("waku:dns-over-https");

export class DnsOverHttps implements DnsClient {
Expand Down
4 changes: 1 addition & 3 deletions packages/dns-discovery/src/fetch_nodes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { IEnr, Waku2 } from "@waku/interfaces";
import type { IEnr, NodeCapabilityCount, Waku2 } from "@waku/interfaces";
import debug from "debug";

import { NodeCapabilityCount } from "./dns.js";

const log = debug("waku:discovery:fetch_nodes");

/**
Expand Down
Loading

0 comments on commit aa87190

Please sign in to comment.