Skip to content

Commit

Permalink
fix: improve node bootstrapping (#2121)
Browse files Browse the repository at this point in the history
* fix: improve node bootstrapping

* implement dns discovery on failed dials

* update const to match RFC

* up
  • Loading branch information
weboko committed Sep 5, 2024
1 parent f4b6bb0 commit 0263cb8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
26 changes: 26 additions & 0 deletions packages/core/src/lib/connection_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Peer, PeerId, PeerInfo, PeerStore } from "@libp2p/interface";
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
import {
ConnectionManagerOptions,
DiscoveryTrigger,
DNS_DISCOVERY_TAG,
EConnectionStateEvents,
EPeersByDiscoveryEvents,
IConnectionManager,
Expand Down Expand Up @@ -292,6 +294,9 @@ export class ConnectionManager

this.dialErrorsForPeer.delete(peerId.toString());
await this.libp2p.peerStore.delete(peerId);

// if it was last available peer - attempt DNS discovery
await this.attemptDnsDiscovery();
} catch (error) {
throw new Error(
`Error deleting undialable peer ${peerId.toString()} from peer store - ${error}`
Expand All @@ -300,6 +305,27 @@ export class ConnectionManager
}
}

private async attemptDnsDiscovery(): Promise<void> {
if (this.libp2p.getConnections().length > 0) return;
if ((await this.libp2p.peerStore.all()).length > 0) return;

log.info("Attempting to trigger DNS discovery.");

const dnsDiscovery = Object.values(this.libp2p.components.components).find(
(v: unknown) => {
if (v && v.toString) {
return v.toString().includes(DNS_DISCOVERY_TAG);
}

return false;
}
) as DiscoveryTrigger;

if (!dnsDiscovery) return;

await dnsDiscovery.findPeers();
}

private processDialQueue(): void {
if (
this.pendingPeerDialQueue.length > 0 &&
Expand Down
6 changes: 3 additions & 3 deletions packages/discovery/src/dns/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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
store: 1,
filter: 2,
lightPush: 2
};
11 changes: 8 additions & 3 deletions packages/discovery/src/dns/dns_discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
import { peerDiscoverySymbol as symbol } from "@libp2p/interface";
import type { PeerInfo } from "@libp2p/interface";
import type {
DiscoveryTrigger,
DnsDiscOptions,
DnsDiscoveryComponents,
IEnr,
NodeCapabilityCount
} from "@waku/interfaces";
import { DNS_DISCOVERY_TAG } from "@waku/interfaces";
import { encodeRelayShard, Logger } from "@waku/utils";

import {
Expand All @@ -29,7 +31,7 @@ const log = new Logger("peer-discovery-dns");
*/
export class PeerDiscoveryDns
extends TypedEventEmitter<PeerDiscoveryEvents>
implements PeerDiscovery
implements PeerDiscovery, DiscoveryTrigger
{
private nextPeer: (() => AsyncGenerator<IEnr>) | undefined;
private _started: boolean;
Expand All @@ -56,8 +58,11 @@ export class PeerDiscoveryDns
log.info("Starting peer discovery via dns");

this._started = true;
await this.findPeers();
}

if (this.nextPeer === undefined) {
public async findPeers(): Promise<void> {
if (!this.nextPeer) {
let { enrUrls } = this._options;
if (!Array.isArray(enrUrls)) enrUrls = [enrUrls];

Expand Down Expand Up @@ -134,7 +139,7 @@ export class PeerDiscoveryDns
}

public get [Symbol.toStringTag](): string {
return "@waku/bootstrap";
return DNS_DISCOVERY_TAG;
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/interfaces/src/dns_discovery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PeerStore } from "@libp2p/interface";

export const DNS_DISCOVERY_TAG = "@waku/bootstrap";

export type SearchContext = {
domain: string;
publicKey: string;
Expand Down Expand Up @@ -45,3 +47,7 @@ export interface DnsDiscOptions {
*/
tagTTL?: number;
}

export interface DiscoveryTrigger {
findPeers: () => Promise<void>;
}
8 changes: 1 addition & 7 deletions packages/sdk/src/create/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ import {
} from "@waku/discovery";
import { type Libp2pComponents, PubsubTopic } from "@waku/interfaces";

const DEFAULT_NODE_REQUIREMENTS = {
lightPush: 1,
filter: 1,
store: 1
};

export function defaultPeerDiscoveries(
pubsubTopics: PubsubTopic[]
): ((components: Libp2pComponents) => PeerDiscovery)[] {
const dnsEnrTrees = [enrTree["SANDBOX"], enrTree["TEST"]];

const discoveries = [
wakuDnsDiscovery(dnsEnrTrees, DEFAULT_NODE_REQUIREMENTS),
wakuDnsDiscovery(dnsEnrTrees),
wakuLocalPeerCacheDiscovery(),
wakuPeerExchangeDiscovery(pubsubTopics)
];
Expand Down

0 comments on commit 0263cb8

Please sign in to comment.