Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve node bootstrapping #2121

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
import {
ConnectionManagerOptions,
DiscoveryTrigger,
DNS_DISCOVERY_TAG,
EConnectionStateEvents,
EPeersByDiscoveryEvents,
IConnectionManager,
Expand Down Expand Up @@ -34,7 +36,7 @@
private options: ConnectionManagerOptions;
private libp2p: Libp2p;
private dialAttemptsForPeer: Map<string, number> = new Map();
private dialErrorsForPeer: Map<string, any> = new Map();

Check warning on line 39 in packages/core/src/lib/connection_manager.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 39 in packages/core/src/lib/connection_manager.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type

private currentActiveParallelDialCount = 0;
private pendingPeerDialQueue: Array<PeerId> = [];
Expand Down Expand Up @@ -251,7 +253,7 @@
// Handle generic error
log.error(
`Error dialing peer ${peerId.toString()} - ${
(error as any).message

Check warning on line 256 in packages/core/src/lib/connection_manager.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 256 in packages/core/src/lib/connection_manager.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
}`
);
}
Expand Down Expand Up @@ -292,6 +294,9 @@

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

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,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

matches RFC and decreases changes of hitting bad DNS node (tho it shouldn't)

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 = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is not needed as there is another on in the dns_discovery/const

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
Loading