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