From e53966964a30be3e1208cea463b87b7685703c79 Mon Sep 17 00:00:00 2001 From: Sasha Date: Sun, 1 Sep 2024 17:24:07 +0200 Subject: [PATCH 1/4] fix: improve node bootstrapping --- packages/discovery/src/dns/constants.ts | 6 +++--- packages/sdk/src/create/discovery.ts | 8 +------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/discovery/src/dns/constants.ts b/packages/discovery/src/dns/constants.ts index f95a013e5b..af4d4b0c53 100644 --- a/packages/discovery/src/dns/constants.ts +++ b/packages/discovery/src/dns/constants.ts @@ -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 = { - store: 2, - filter: 1, - lightPush: 1 + store: 1, + filter: 2, + lightPush: 2 }; diff --git a/packages/sdk/src/create/discovery.ts b/packages/sdk/src/create/discovery.ts index 5a219e47ad..d50c7c8732 100644 --- a/packages/sdk/src/create/discovery.ts +++ b/packages/sdk/src/create/discovery.ts @@ -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) ]; From 39d23895ef45c75c9ed7254a2e381a19a6f11d9a Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 3 Sep 2024 01:21:00 +0200 Subject: [PATCH 2/4] implement dns discovery on failed dials --- packages/core/src/lib/connection_manager.ts | 27 +++++++++++++++++++++ packages/discovery/src/dns/constants.ts | 4 +-- packages/discovery/src/dns/dns_discovery.ts | 11 ++++++--- packages/interfaces/src/dns_discovery.ts | 6 +++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/packages/core/src/lib/connection_manager.ts b/packages/core/src/lib/connection_manager.ts index 02ea5fea20..67d1d41c50 100644 --- a/packages/core/src/lib/connection_manager.ts +++ b/packages/core/src/lib/connection_manager.ts @@ -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, @@ -291,7 +293,11 @@ export class ConnectionManager } this.dialErrorsForPeer.delete(peerId.toString()); + this.dialAttemptsForPeer.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}` @@ -300,6 +306,27 @@ export class ConnectionManager } } + private async attemptDnsDiscovery(): Promise { + 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 && diff --git a/packages/discovery/src/dns/constants.ts b/packages/discovery/src/dns/constants.ts index af4d4b0c53..5d439b77fe 100644 --- a/packages/discovery/src/dns/constants.ts +++ b/packages/discovery/src/dns/constants.ts @@ -16,6 +16,6 @@ export const DEFAULT_BOOTSTRAP_TAG_TTL = 100_000_000; export const DEFAULT_NODE_REQUIREMENTS: Partial = { store: 1, - filter: 2, - lightPush: 2 + filter: 1, + lightPush: 1 }; diff --git a/packages/discovery/src/dns/dns_discovery.ts b/packages/discovery/src/dns/dns_discovery.ts index 99534be256..e7c33c2aab 100644 --- a/packages/discovery/src/dns/dns_discovery.ts +++ b/packages/discovery/src/dns/dns_discovery.ts @@ -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 { @@ -29,7 +31,7 @@ const log = new Logger("peer-discovery-dns"); */ export class PeerDiscoveryDns extends TypedEventEmitter - implements PeerDiscovery + implements PeerDiscovery, DiscoveryTrigger { private nextPeer: (() => AsyncGenerator) | undefined; private _started: boolean; @@ -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 { + if (!this.nextPeer) { let { enrUrls } = this._options; if (!Array.isArray(enrUrls)) enrUrls = [enrUrls]; @@ -134,7 +139,7 @@ export class PeerDiscoveryDns } public get [Symbol.toStringTag](): string { - return "@waku/bootstrap"; + return DNS_DISCOVERY_TAG; } } diff --git a/packages/interfaces/src/dns_discovery.ts b/packages/interfaces/src/dns_discovery.ts index 878aaad6d2..2921dec5f4 100644 --- a/packages/interfaces/src/dns_discovery.ts +++ b/packages/interfaces/src/dns_discovery.ts @@ -1,5 +1,7 @@ import { PeerStore } from "@libp2p/interface"; +export const DNS_DISCOVERY_TAG = "@waku/bootstrap"; + export type SearchContext = { domain: string; publicKey: string; @@ -45,3 +47,7 @@ export interface DnsDiscOptions { */ tagTTL?: number; } + +export interface DiscoveryTrigger { + findPeers: () => Promise; +} From 644dbb55bca78ed424fd548eedc63d1ec774316a Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 3 Sep 2024 01:27:25 +0200 Subject: [PATCH 3/4] update const to match RFC --- packages/discovery/src/dns/constants.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/discovery/src/dns/constants.ts b/packages/discovery/src/dns/constants.ts index 5d439b77fe..af4d4b0c53 100644 --- a/packages/discovery/src/dns/constants.ts +++ b/packages/discovery/src/dns/constants.ts @@ -16,6 +16,6 @@ export const DEFAULT_BOOTSTRAP_TAG_TTL = 100_000_000; export const DEFAULT_NODE_REQUIREMENTS: Partial = { store: 1, - filter: 1, - lightPush: 1 + filter: 2, + lightPush: 2 }; From 4571e82c4e0e73fc6f9b9f2eb1b0fe9c155c4471 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 6 Sep 2024 00:51:49 +0200 Subject: [PATCH 4/4] up --- packages/core/src/lib/connection_manager.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/src/lib/connection_manager.ts b/packages/core/src/lib/connection_manager.ts index 67d1d41c50..8fc61f4d54 100644 --- a/packages/core/src/lib/connection_manager.ts +++ b/packages/core/src/lib/connection_manager.ts @@ -293,7 +293,6 @@ export class ConnectionManager } this.dialErrorsForPeer.delete(peerId.toString()); - this.dialAttemptsForPeer.delete(peerId.toString()); await this.libp2p.peerStore.delete(peerId); // if it was last available peer - attempt DNS discovery