Skip to content

Commit

Permalink
rely on passed protocols and fallback to mounted
Browse files Browse the repository at this point in the history
  • Loading branch information
weboko committed Oct 4, 2024
1 parent 3928603 commit 21056fd
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/sdk/src/wait_for_remote_peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ export async function waitForRemotePeer(
protocols?: Protocols[],
timeoutMs?: number
): Promise<void> {
protocols = protocols ?? getEnabledProtocols(waku);
// if no protocols or empty array passed - try to derive from mounted
protocols = protocols?.length ? protocols : getEnabledProtocols(waku);
const connections = waku.libp2p.getConnections();

if (!waku.isStarted()) {
throw Error("Waku node is not started");
}

if (connections.length > 0 && !protocols.includes(Protocols.Relay)) {
const success = await waitForMetadata(waku);
const success = await waitForMetadata(waku, protocols);

if (success) {
return;
Expand Down Expand Up @@ -135,10 +136,13 @@ async function waitForConnectedPeer(
/**
* Waits for the metadata from the remote peer.
*/
async function waitForMetadata(waku: Waku): Promise<boolean> {
async function waitForMetadata(
waku: Waku,
protocols: Protocols[]
): Promise<boolean> {
const connectedPeers = waku.libp2p.getPeers();
const metadataService = waku.libp2p.services.metadata;
const enabledCodes = getEnabledCodecs(waku);
const enabledCodes = mapProtocolsToCodecs(protocols);

if (!connectedPeers.length || !metadataService) {
log.info(
Expand Down Expand Up @@ -236,19 +240,19 @@ function getEnabledProtocols(waku: Waku): Protocols[] {
return protocols;
}

function getEnabledCodecs(waku: Waku): Map<string, boolean> {
function mapProtocolsToCodecs(protocols: Protocols[]): Map<string, boolean> {
const codecs: Map<string, boolean> = new Map();

if (waku.filter) {
codecs.set(FilterCodecs.SUBSCRIBE, false);
}

if (waku.store) {
codecs.set(StoreCodec, false);
}
const protocolToCodec: Record<string, string> = {
[Protocols.Filter]: FilterCodecs.SUBSCRIBE,
[Protocols.LightPush]: LightPushCodec,
[Protocols.Store]: StoreCodec
};

if (waku.lightPush) {
codecs.set(LightPushCodec, false);
for (const protocol of protocols) {
if (protocolToCodec[protocol]) {
codecs.set(protocolToCodec[protocol], false);
}
}

return codecs;
Expand Down

0 comments on commit 21056fd

Please sign in to comment.