remove codec eval from `connectionManager` listener

This commit is contained in:
Felicio Mununga 2022-11-28 00:11:19 +01:00
parent 5cef43991f
commit cd0f10d94e
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
1 changed files with 15 additions and 26 deletions

View File

@ -3,7 +3,7 @@
*/
import { hexToBytes } from 'ethereum-cryptography/utils'
import { Protocols, waku_filter, waku_light_push, waku_store } from 'js-waku'
import { Protocols } from 'js-waku'
import { createLightNode } from 'js-waku/lib/create_waku'
import { PeerDiscoveryStaticPeers } from 'js-waku/lib/peer_discovery_static_list'
import { waitForRemotePeer } from 'js-waku/lib/wait_for_remote_peer'
@ -21,12 +21,6 @@ import type { Storage } from './storage'
import type { WakuLight } from 'js-waku/lib/interfaces'
import type { MessageV1 as WakuMessage } from 'js-waku/lib/waku_message/version_1'
const PROTOCOLS = [Protocols.Store, Protocols.Filter, Protocols.LightPush]
const CODECS = [
waku_store.StoreCodec,
waku_filter.FilterCodec,
waku_light_push.LightPushCodec,
]
const THROWAWAY_ACCOUNT_STORAGE_KEY = 'throwaway_account'
export interface ClientOptions {
@ -70,6 +64,10 @@ class Client {
constructor(waku: WakuLight, options: ClientOptions) {
// Waku
/**
* Waku should be connected and protocols awaited at this point, thus connected.
*/
this.connected = true
this.waku = waku
this.wakuMessages = new Set()
this.#wakuDisconnectionTimer = setInterval(async () => {
@ -86,24 +84,12 @@ class Client {
await Promise.allSettled(connectionsToClose)
}, 10 * 1000)
/**
* Note: Assumes 1 remote node.
* Note: Assumes 1 remote node and that the diconnection does not require calling
* `waitForRemotePeer` again to ensure protocols/codecs.
*/
this.waku.libp2p.connectionManager.addEventListener(
'peer:connect',
async event => {
const connection = event.detail
const protocols = await this.waku.libp2p.peerStore.protoBook.get(
connection.remotePeer
)
const isMissingProtocol = CODECS.some(
codec => !protocols.includes(codec)
)
if (!isMissingProtocol) {
this.connected = true
}
}
)
this.waku.libp2p.connectionManager.addEventListener('peer:connect', () => {
this.connected = true // reconnect
})
/**
* >This event will **only** be triggered when the last connection is closed.
* @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
@ -111,7 +97,6 @@ class Client {
waku.libp2p.connectionManager.addEventListener('peer:disconnect', () => {
this.connected = false
})
this.connected = true
// Storage
this.storage = options.storage ?? new LocalStorage()
@ -163,7 +148,11 @@ class Client {
},
})
await waku.start()
await waitForRemotePeer(waku, PROTOCOLS, 10 * 1000)
await waitForRemotePeer(
waku,
[Protocols.Store, Protocols.Filter, Protocols.LightPush],
10 * 1000
)
// Client
client = new Client(waku, options)