chore(peer-exchange): refactor/fix compliance test (#1478)

* use `peer:identify` instead of `peer:update`

* dial the peer after initialising peer-exchange
This commit is contained in:
Danish Arora 2023-08-16 11:14:33 +05:30 committed by GitHub
parent d97579bf53
commit 1548a4f8e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import type { PeerUpdate } from "@libp2p/interface-libp2p";
import type { IdentifyResult } from "@libp2p/interface-libp2p";
import type {
PeerDiscovery,
PeerDiscoveryEvents,
@ -61,11 +61,10 @@ export class PeerExchangeDiscovery
private queryAttempts: Map<string, number> = new Map();
private readonly handleDiscoveredPeer = (
event: CustomEvent<PeerUpdate>,
event: CustomEvent<IdentifyResult>,
): void => {
const {
peer: { protocols, id: peerId },
} = event.detail;
const { protocols, peerId } = event.detail;
if (
!protocols.includes(PeerExchangeCodec) ||
this.queryingPeers.has(peerId.toString())
@ -98,7 +97,7 @@ export class PeerExchangeDiscovery
// might be better to use "peer:identify" or "peer:update"
this.components.events.addEventListener(
"peer:update",
"peer:identify",
this.handleDiscoveredPeer,
);
}
@ -112,7 +111,7 @@ export class PeerExchangeDiscovery
this.isStarted = false;
this.queryingPeers.clear();
this.components.events.removeEventListener(
"peer:update",
"peer:identify",
this.handleDiscoveredPeer,
);
}

View File

@ -117,7 +117,11 @@ describe("Peer Exchange", () => {
await waku.start();
const nwaku2Ma = await nwaku2.getMultiaddrWithId();
await waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
// we do this because we want peer-exchange discovery to get initialised before we dial the peer which contains info about the other peer
setTimeout(() => {
void waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
}, 1000);
return new PeerExchangeDiscovery(waku.libp2p.components);
},