Skip to content

Commit

Permalink
chore: use ConnectionManager for dial ops
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Dec 16, 2024
1 parent 35a8de3 commit 83acb84
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
1 change: 0 additions & 1 deletion packages/core/src/lib/base_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { Peer, Stream } from "@libp2p/interface";
import type {
IBaseProtocolCore,
Libp2pComponents,
PeerIdStr,
PubsubTopic
} from "@waku/interfaces";
import { Logger } from "@waku/utils";
Expand Down
31 changes: 29 additions & 2 deletions packages/core/src/lib/connection_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Peer, PeerId, PeerInfo, PeerStore } from "@libp2p/interface";
import {} from "@libp2p/peer-id";
import { TypedEventEmitter } from "@libp2p/interface";
import { multiaddr } from "@multiformats/multiaddr";
import {
ConnectionManagerOptions,
DiscoveryTrigger,
Expand All @@ -11,6 +13,7 @@ import {
IPeersByDiscoveryEvents,
IRelay,
KeepAliveOptions,
MultiaddrStr,
PeersByDiscoveryResult,
PubsubTopic,
ShardInfo
Expand Down Expand Up @@ -219,15 +222,39 @@ export class ConnectionManager
this.startNetworkStatusListener();
}

private async dialPeer(peerId: PeerId): Promise<void> {
public async dialPeer(
peer: PeerId | MultiaddrStr,
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;
}

this.currentActiveParallelDialCount += 1;
let dialAttempt = 0;
while (dialAttempt < this.options.maxDialAttemptsForPeer) {
try {
log.info(
`Dialing peer ${peerId.toString()} on attempt ${dialAttempt + 1}`
);
await this.libp2p.dial(peerId);
protocols
? await this.libp2p.dialProtocol(peerId, protocols)
: await this.libp2p.dial(peerId);

const tags = await this.getTagNamesForPeer(peerId);
// add tag to connection describing discovery mechanism
Expand Down
2 changes: 2 additions & 0 deletions packages/interfaces/src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export type CreateLibp2pOptions = Libp2pOptions & {
*/
filterMultiaddrs?: boolean;
};

export type MultiaddrStr = string;
2 changes: 1 addition & 1 deletion packages/sdk/src/protocols/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Peer } from "@libp2p/interface-peer-id";
import type { Peer } from "@libp2p/interface";
import { ConnectionManager, StoreCore } from "@waku/core";
import {
IDecodedMessage,
Expand Down
20 changes: 5 additions & 15 deletions packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ export class WakuNode implements IWaku {
if (protocolsEnabled.store) {
let peerIdStr: PeerIdStr | undefined;
if (options.store?.peer) {
this.dialMultiaddr(options.store.peer, StoreCodec).catch((e) => {
log.error("Failed to dial store peer", e);
});
this.connectionManager
.dialPeer(options.store.peer, [StoreCodec])
.catch((e) => {
log.error("Failed to dial store peer", e);
});
}

const store = wakuStore(this.connectionManager, peerIdStr);
Expand Down Expand Up @@ -232,18 +234,6 @@ export class WakuNode implements IWaku {
return this.connectionManager.isConnected();
}

private async dialMultiaddr(
multiaddrStr: string,
protocol: string
): Promise<PeerIdStr> {
const ma = multiaddr(multiaddrStr);
if (!ma.getPeerId()) {
throw new Error("Failed to dial multiaddr: missing peer ID");
}
await this.libp2p.dialProtocol(ma, [protocol]);
return ma.getPeerId()!;
}

private mapToPeerIdOrMultiaddr(
peerId: PeerId | MultiaddrInput
): PeerId | Multiaddr {
Expand Down

0 comments on commit 83acb84

Please sign in to comment.