Skip to content

Commit

Permalink
chore: use protocol-level class for getting peer
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Dec 16, 2024
1 parent 9c98cc4 commit 35a8de3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
17 changes: 1 addition & 16 deletions packages/core/src/lib/base_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,15 @@ export class BaseProtocol implements IBaseProtocolCore {
public async getPeers(
{
numPeers,
maxBootstrapPeers,
peerIdStr
maxBootstrapPeers
}: {
numPeers: number;
maxBootstrapPeers: number;
peerIdStr?: PeerIdStr;
} = {
maxBootstrapPeers: 0,
numPeers: 0
}
): Promise<Peer[]> {
if (peerIdStr) {
const peer = (await this.connectedPeers()).find(
(p) => p.id.toString() === peerIdStr
);
if (peer) {
return [peer];
}
this.log.warn(
`Passed node to use for ${this.multicodec} not found: ${peerIdStr}. Attempting to use random peers.`
);
return this.getPeers({ numPeers, maxBootstrapPeers });
}

// Retrieve all connected peers that support the protocol & shard (if configured)
const allAvailableConnectedPeers = await this.connectedPeers();

Expand Down
30 changes: 23 additions & 7 deletions packages/sdk/src/protocols/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Peer } from "@libp2p/interface-peer-id";
import { ConnectionManager, StoreCore } from "@waku/core";
import {
IDecodedMessage,
Expand Down Expand Up @@ -62,13 +63,14 @@ export class Store extends BaseProtocolSDK implements IStore {
...options
};

const peer = (
await this.protocol.getPeers({
numPeers: this.numPeersToUse,
maxBootstrapPeers: 1,
peerIdStr: this.peerIdStrToUse
})
)[0];
const peer =
(await this.getPeerToUse()) ??
(
await this.protocol.getPeers({
numPeers: this.numPeersToUse,
maxBootstrapPeers: 1
})
)[0];

if (!peer) {
log.error("No peers available to query");
Expand Down Expand Up @@ -234,6 +236,20 @@ export class Store extends BaseProtocolSDK implements IStore {
decodersAsMap
};
}

private async getPeerToUse(): Promise<Peer | null> {
const peer = this.connectedPeers.find(
(p) => p.id.toString() === this.peerIdStrToUse
);
if (peer) {
return peer;
}

log.warn(
`Passed node to use for Store not found: ${this.peerIdStrToUse}. Attempting to use random peers.`
);
return null;
}
}

/**
Expand Down

0 comments on commit 35a8de3

Please sign in to comment.