chore(peer-exchange): use an event listener to gauge if the service is mounted (#2071)

* chore: use service mounted event listener

* chore: update event name

* chore: change event name to isStarted

* chore: address comments
This commit is contained in:
Danish Arora 2024-07-22 18:01:15 +05:30 committed by GitHub
parent 2b02f829c2
commit a739ada33a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 21 deletions

View File

@ -50,12 +50,16 @@ export interface Options {
maxRetries?: number;
}
interface CustomDiscoveryEvent extends PeerDiscoveryEvents {
"waku:peer-exchange:started": CustomEvent<boolean>;
}
export const DEFAULT_PEER_EXCHANGE_TAG_NAME = Tags.PEER_EXCHANGE;
const DEFAULT_PEER_EXCHANGE_TAG_VALUE = 50;
const DEFAULT_PEER_EXCHANGE_TAG_TTL = 100_000_000;
export class PeerExchangeDiscovery
extends TypedEventEmitter<PeerDiscoveryEvents>
extends TypedEventEmitter<CustomDiscoveryEvent>
implements PeerDiscovery
{
private readonly components: Libp2pComponents;
@ -102,6 +106,10 @@ export class PeerExchangeDiscovery
return;
}
this.dispatchEvent(
new CustomEvent("waku:peer-exchange:started", { detail: true })
);
log.info("Starting peer exchange node discovery, discovering peers");
// might be better to use "peer:identify" or "peer:update"

View File

@ -24,16 +24,12 @@ describe("Peer Exchange", function () {
beforeEachCustom(this, async () => {
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1");
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2");
});
tests({
async setup() {
await nwaku1.start({
relay: true,
discv5Discovery: true,
peerExchange: true
});
const enr = (await nwaku1.info()).enrUri;
await nwaku2.start({
@ -42,18 +38,27 @@ describe("Peer Exchange", function () {
peerExchange: true,
discv5BootstrapNode: enr
});
});
tests({
async setup() {
waku = await createLightNode();
await waku.start();
const nwaku2Ma = await nwaku2.getMultiaddrWithId();
// 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);
const peerExchange = new PeerExchangeDiscovery(
waku.libp2p.components,
pubsubTopic
);
return new PeerExchangeDiscovery(waku.libp2p.components, pubsubTopic);
peerExchange.addEventListener("waku:peer-exchange:started", (event) => {
if (event.detail === true) {
void waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
}
});
return peerExchange;
},
teardown: async () => {
this.timeout(15000);