From fcd500e0455aa73ff8ec108146bae42d07d6bbe5 Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Wed, 11 Jan 2023 11:11:49 +0530 Subject: [PATCH] fix: invoke peer exchange query immediately (#1115) when the query is set in an interval, it first gets invoked when the interval time is reached. refactor: invoke it immmediately, and then set an interval --- .../src/waku_peer_exchange_discovery.ts | 113 +++++++++--------- 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/packages/peer-exchange/src/waku_peer_exchange_discovery.ts b/packages/peer-exchange/src/waku_peer_exchange_discovery.ts index 449390b8b6..88d9b0862a 100644 --- a/packages/peer-exchange/src/waku_peer_exchange_discovery.ts +++ b/packages/peer-exchange/src/waku_peer_exchange_discovery.ts @@ -55,61 +55,9 @@ export class PeerExchangeDiscovery if (!protocols.includes(PeerExchangeCodec) || this.intervals.get(peerId)) return; + await this.query(peerId); const interval = setInterval(async () => { - await this.peerExchange.query( - { - numPeers: DEFAULT_PEER_EXCHANGE_REQUEST_NODES, - peerId, - }, - async (response) => { - const { peerInfos } = response; - - for (const _peerInfo of peerInfos) { - const { ENR } = _peerInfo; - if (!ENR) { - log("no ENR"); - continue; - } - - const { peerId, multiaddrs } = ENR; - - if (!peerId) { - log("no peerId"); - continue; - } - if (!multiaddrs || multiaddrs.length === 0) { - log("no multiaddrs"); - continue; - } - - // check if peer is already in peerStore - const existingPeer = await this.components.peerStore.get(peerId); - if (existingPeer) { - log("peer already in peerStore"); - continue; - } - - await this.components.peerStore.tagPeer( - peerId, - DEFAULT_BOOTSTRAP_TAG_NAME, - { - value: this.options.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE, - ttl: this.options.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL, - } - ); - - this.dispatchEvent( - new CustomEvent("peer", { - detail: { - id: peerId, - multiaddrs, - protocols: [], - }, - }) - ); - } - } - ); + await this.query(peerId); }, PEER_EXCHANGE_QUERY_INTERVAL); this.intervals.set(peerId, interval); @@ -160,6 +108,63 @@ export class PeerExchangeDiscovery get [Symbol.toStringTag](): string { return "@waku/peer-exchange"; } + + private query(peerId: PeerId): Promise { + return this.peerExchange.query( + { + numPeers: DEFAULT_PEER_EXCHANGE_REQUEST_NODES, + peerId, + }, + async (response) => { + const { peerInfos } = response; + + for (const _peerInfo of peerInfos) { + const { ENR } = _peerInfo; + if (!ENR) { + log("no ENR"); + continue; + } + + const { peerId, multiaddrs } = ENR; + + if (!peerId) { + log("no peerId"); + continue; + } + if (!multiaddrs || multiaddrs.length === 0) { + log("no multiaddrs"); + continue; + } + + // check if peer is already in peerStore + const existingPeer = await this.components.peerStore.get(peerId); + if (existingPeer) { + log("peer already in peerStore"); + continue; + } + + await this.components.peerStore.tagPeer( + peerId, + DEFAULT_BOOTSTRAP_TAG_NAME, + { + value: this.options.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE, + ttl: this.options.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL, + } + ); + + this.dispatchEvent( + new CustomEvent("peer", { + detail: { + id: peerId, + multiaddrs, + protocols: [], + }, + }) + ); + } + } + ); + } } export function wakuPeerExchangeDiscovery(): (