mirror of https://github.com/status-im/js-waku.git
feat: confirm metadata and protocols needed in waitForRemotePeer
This commit is contained in:
parent
06498f8c37
commit
eaee6351d9
|
@ -39,7 +39,7 @@ export async function waitForRemotePeer(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connections.length > 0 && !protocols.includes(Protocols.Relay)) {
|
if (connections.length > 0 && !protocols.includes(Protocols.Relay)) {
|
||||||
const success = await waitForMetadata(waku.libp2p);
|
const success = await waitForMetadata(waku);
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
return;
|
return;
|
||||||
|
@ -135,33 +135,52 @@ async function waitForConnectedPeer(
|
||||||
/**
|
/**
|
||||||
* Waits for the metadata from the remote peer.
|
* Waits for the metadata from the remote peer.
|
||||||
*/
|
*/
|
||||||
async function waitForMetadata(libp2p: Libp2p): Promise<boolean> {
|
async function waitForMetadata(waku: Waku): Promise<boolean> {
|
||||||
const connections = libp2p.getConnections();
|
const connectedPeers = waku.libp2p.getPeers();
|
||||||
const metadataService = libp2p.services.metadata;
|
const metadataService = waku.libp2p.services.metadata;
|
||||||
|
const enabledCodes = getEnabledCodecs(waku);
|
||||||
|
|
||||||
if (!connections.length || !metadataService) {
|
if (!connectedPeers.length || !metadataService) {
|
||||||
log.info(
|
log.info(
|
||||||
`Skipping waitForMetadata due to missing connections:${connections.length} or metadataService:${!!metadataService}`
|
`Skipping waitForMetadata due to missing connections:${connectedPeers.length} or metadataService:${!!metadataService}`
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
for (const peerId of connectedPeers) {
|
||||||
// confirm at least with one connected peer
|
const confirmedAllCodecs = Array.from(enabledCodes.values()).every(
|
||||||
await Promise.any(
|
(v) => v
|
||||||
connections
|
|
||||||
.map((c) => c.remotePeer)
|
|
||||||
.map((peer) => metadataService.confirmOrAttemptHandshake(peer))
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (confirmedAllCodecs) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const peer = await waku.libp2p.peerStore.get(peerId);
|
||||||
|
const hasSomeCodes = peer.protocols.some((c) => enabledCodes.has(c));
|
||||||
|
|
||||||
|
if (hasSomeCodes) {
|
||||||
|
const response =
|
||||||
|
await metadataService.confirmOrAttemptHandshake(peerId);
|
||||||
|
|
||||||
|
if (!response.error) {
|
||||||
|
peer.protocols.forEach((c) => {
|
||||||
|
if (enabledCodes.has(c)) {
|
||||||
|
enabledCodes.set(c, true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED") {
|
if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED") {
|
||||||
log.error("Connection closed. Some peers can be on different shard.");
|
log.error("Connection closed. Some peers can be on different shard.");
|
||||||
}
|
}
|
||||||
|
|
||||||
log.error(`Error waiting for metadata: ${e}`);
|
log.error(`Error while iterating through peers: ${e}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -216,3 +235,21 @@ function getEnabledProtocols(waku: Waku): Protocols[] {
|
||||||
|
|
||||||
return protocols;
|
return protocols;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getEnabledCodecs(waku: Waku): 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (waku.lightPush) {
|
||||||
|
codecs.set(LightPushCodec, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return codecs;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue