mirror of https://github.com/waku-org/js-waku.git
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:
parent
2b02f829c2
commit
a739ada33a
|
@ -50,12 +50,16 @@ export interface Options {
|
||||||
maxRetries?: number;
|
maxRetries?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface CustomDiscoveryEvent extends PeerDiscoveryEvents {
|
||||||
|
"waku:peer-exchange:started": CustomEvent<boolean>;
|
||||||
|
}
|
||||||
|
|
||||||
export const DEFAULT_PEER_EXCHANGE_TAG_NAME = Tags.PEER_EXCHANGE;
|
export const DEFAULT_PEER_EXCHANGE_TAG_NAME = Tags.PEER_EXCHANGE;
|
||||||
const DEFAULT_PEER_EXCHANGE_TAG_VALUE = 50;
|
const DEFAULT_PEER_EXCHANGE_TAG_VALUE = 50;
|
||||||
const DEFAULT_PEER_EXCHANGE_TAG_TTL = 100_000_000;
|
const DEFAULT_PEER_EXCHANGE_TAG_TTL = 100_000_000;
|
||||||
|
|
||||||
export class PeerExchangeDiscovery
|
export class PeerExchangeDiscovery
|
||||||
extends TypedEventEmitter<PeerDiscoveryEvents>
|
extends TypedEventEmitter<CustomDiscoveryEvent>
|
||||||
implements PeerDiscovery
|
implements PeerDiscovery
|
||||||
{
|
{
|
||||||
private readonly components: Libp2pComponents;
|
private readonly components: Libp2pComponents;
|
||||||
|
@ -102,6 +106,10 @@ export class PeerExchangeDiscovery
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.dispatchEvent(
|
||||||
|
new CustomEvent("waku:peer-exchange:started", { detail: true })
|
||||||
|
);
|
||||||
|
|
||||||
log.info("Starting peer exchange node discovery, discovering peers");
|
log.info("Starting peer exchange node discovery, discovering peers");
|
||||||
|
|
||||||
// might be better to use "peer:identify" or "peer:update"
|
// might be better to use "peer:identify" or "peer:update"
|
||||||
|
|
|
@ -24,36 +24,41 @@ describe("Peer Exchange", function () {
|
||||||
beforeEachCustom(this, async () => {
|
beforeEachCustom(this, async () => {
|
||||||
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1");
|
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1");
|
||||||
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2");
|
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2");
|
||||||
|
|
||||||
|
await nwaku1.start({
|
||||||
|
relay: true,
|
||||||
|
discv5Discovery: true,
|
||||||
|
peerExchange: true
|
||||||
|
});
|
||||||
|
const enr = (await nwaku1.info()).enrUri;
|
||||||
|
|
||||||
|
await nwaku2.start({
|
||||||
|
relay: true,
|
||||||
|
discv5Discovery: true,
|
||||||
|
peerExchange: true,
|
||||||
|
discv5BootstrapNode: enr
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tests({
|
tests({
|
||||||
async setup() {
|
async setup() {
|
||||||
await nwaku1.start({
|
|
||||||
relay: true,
|
|
||||||
discv5Discovery: true,
|
|
||||||
peerExchange: true
|
|
||||||
});
|
|
||||||
|
|
||||||
const enr = (await nwaku1.info()).enrUri;
|
|
||||||
|
|
||||||
await nwaku2.start({
|
|
||||||
relay: true,
|
|
||||||
discv5Discovery: true,
|
|
||||||
peerExchange: true,
|
|
||||||
discv5BootstrapNode: enr
|
|
||||||
});
|
|
||||||
|
|
||||||
waku = await createLightNode();
|
waku = await createLightNode();
|
||||||
await waku.start();
|
await waku.start();
|
||||||
|
|
||||||
const nwaku2Ma = await nwaku2.getMultiaddrWithId();
|
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
|
const peerExchange = new PeerExchangeDiscovery(
|
||||||
setTimeout(() => {
|
waku.libp2p.components,
|
||||||
void waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
|
pubsubTopic
|
||||||
}, 1000);
|
);
|
||||||
|
|
||||||
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 () => {
|
teardown: async () => {
|
||||||
this.timeout(15000);
|
this.timeout(15000);
|
||||||
|
|
Loading…
Reference in New Issue