Skip to content

Commit

Permalink
chore: Waku.dial() proxies through ConnectionManager.dialPeer
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Jan 15, 2025
1 parent b9221d9 commit 7ad9acc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
54 changes: 32 additions & 22 deletions packages/core/src/lib/connection_manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Peer, PeerId, PeerInfo, PeerStore } from "@libp2p/interface";
import { TypedEventEmitter } from "@libp2p/interface";
import { multiaddr } from "@multiformats/multiaddr";
import { isPeerId, TypedEventEmitter } from "@libp2p/interface";
import { multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
import {
ConnectionManagerOptions,
DiscoveryTrigger,
Expand All @@ -12,7 +12,6 @@ import {
IPeersByDiscoveryEvents,
IRelay,
KeepAliveOptions,
MultiaddrStr,
PeersByDiscoveryResult,
PubsubTopic,
ShardInfo
Expand Down Expand Up @@ -222,27 +221,10 @@ export class ConnectionManager
}

public async dialPeer(
peer: PeerId | MultiaddrStr,
peer: PeerId | MultiaddrInput,
protocols?: string[]
): Promise<void> {
let peerId: PeerId;
if (typeof peer === "string") {
const ma = multiaddr(peer);
const peerIdStr = ma.getPeerId();
if (!peerIdStr) {
throw new Error("Failed to dial multiaddr: missing peer ID");
}
const conn = this.libp2p
.getConnections()
.find((conn) => conn.remotePeer.toString() === peerIdStr);
if (conn) {
peerId = conn.remotePeer;
} else {
throw new Error("Failed to dial multiaddr: no connection found");
}
} else {
peerId = peer;
}
const peerId = this.getPeerIdFromPeerOrMultiaddr(peer);

this.currentActiveParallelDialCount += 1;
let dialAttempt = 0;
Expand Down Expand Up @@ -331,6 +313,34 @@ export class ConnectionManager
}
}

/**
* Internal utility to extract a PeerId from either a PeerId object or multiaddr input.
* This is used internally by the connection manager to handle different peer input formats.
* @internal
* @param peer - The PeerId or MultiaddrInput to extract the PeerId from
* @returns The extracted PeerId
*/
private getPeerIdFromPeerOrMultiaddr(peer: PeerId | MultiaddrInput): PeerId {
if (isPeerId(peer)) {
return peer;
} else {
// peer is of MultiaddrInput type
const ma = multiaddr(peer);
const peerIdStr = ma.getPeerId();
if (!peerIdStr) {
throw new Error("Failed to dial multiaddr: missing peer ID");
}
const conn = this.libp2p
.getConnections()
.find((conn) => conn.remotePeer.toString() === peerIdStr);
if (conn) {
return conn.remotePeer;
} else {
throw new Error("Failed to dial multiaddr: no connection found");
}
}
}

private async attemptDnsDiscovery(): Promise<void> {
if (this.libp2p.getConnections().length > 0) return;
if ((await this.libp2p.peerStore.all()).length > 0) return;
Expand Down
2 changes: 0 additions & 2 deletions packages/interfaces/src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ export type CreateLibp2pOptions = Libp2pOptions & {
*/
filterMultiaddrs?: boolean;
};

export type MultiaddrStr = string;
4 changes: 2 additions & 2 deletions packages/interfaces/src/waku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PeerId, Stream } from "@libp2p/interface";
import type { PeerId } from "@libp2p/interface";
import type { MultiaddrInput } from "@multiformats/multiaddr";

import { IConnectionManager } from "./connection_manager.js";
Expand Down Expand Up @@ -55,7 +55,7 @@ export interface IWaku {
* waku.isConnected() === true;
* ```
*/
dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise<Stream>;
dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise<void>;

/**
* Starts all services and components related to functionality of Waku node.
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/create/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export async function defaultLibp2p(
options?: Partial<CreateLibp2pOptions>,
userAgent?: string
): Promise<Libp2p> {
if (!options?.hideWebSocketInfo && process?.env?.NODE_ENV !== "test") {
const isTestEnv = process?.env?.NODE_ENV === "test" ?? false;
if (!options?.hideWebSocketInfo && !isTestEnv) {
/* eslint-disable no-console */
console.info(
"%cIgnore WebSocket connection failures",
Expand Down
8 changes: 3 additions & 5 deletions packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Stream } from "@libp2p/interface";
import { isPeerId, PeerId } from "@libp2p/interface";
import { multiaddr, Multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
import { ConnectionManager, getHealthManager, StoreCodec } from "@waku/core";
Expand Down Expand Up @@ -153,9 +152,8 @@ export class WakuNode implements IWaku {
public async dial(
peer: PeerId | MultiaddrInput,
protocols?: Protocols[]
): Promise<Stream> {
): Promise<void> {
const _protocols = protocols ?? [];
const peerId = this.mapToPeerIdOrMultiaddr(peer);

if (typeof protocols === "undefined") {
this.relay && _protocols.push(Protocols.Relay);
Expand Down Expand Up @@ -204,9 +202,9 @@ export class WakuNode implements IWaku {
}
}

const peerId = this.mapToPeerIdOrMultiaddr(peer);
log.info(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);

return this.libp2p.dialProtocol(peerId, codecs);
return await this.connectionManager.dialPeer(peer, codecs);
}

public async start(): Promise<void> {
Expand Down

0 comments on commit 7ad9acc

Please sign in to comment.