add `libp2p` connection listeners

This commit is contained in:
Felicio Mununga 2022-11-27 23:54:43 +01:00
parent 3d5c490185
commit 5cef43991f
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
1 changed files with 39 additions and 9 deletions

View File

@ -3,7 +3,7 @@
*/
import { hexToBytes } from 'ethereum-cryptography/utils'
import { Protocols } from 'js-waku'
import { Protocols, waku_filter, waku_light_push, waku_store } 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,6 +21,12 @@ 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 {
@ -41,17 +47,18 @@ class Client {
public readonly wakuMessages: Set<string>
/**
* Tracks open connections which had their streams silently destroyed
* and closes them so new connections can be automatically created.
* and closes them so new connections can be automatically created
* by libp2p's dial mechanism.
*
* For example, in case of a websocket in browser which did not
* have its close event emitted.
*
* Note: Detection of the stream removal depends on active (by user)
* or pasive (with `Waku.relayKeepAlive`) message sending.
* Note: Detection of the stream removal depends pinging the peer.
*
* Note: This is only a workaround (@see https://github.com/libp2p/js-libp2p/issues/939).
*/
#wakuDisconnectionTimer: ReturnType<typeof setInterval>
connected: boolean
public activityCenter: ActivityCenter
public community: Community
@ -78,6 +85,33 @@ class Client {
await Promise.allSettled(connectionsToClose)
}, 10 * 1000)
/**
* Note: Assumes 1 remote node.
*/
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 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
*/
waku.libp2p.connectionManager.addEventListener('peer:disconnect', () => {
this.connected = false
})
this.connected = true
// Storage
this.storage = options.storage ?? new LocalStorage()
@ -129,11 +163,7 @@ class Client {
},
})
await waku.start()
await waitForRemotePeer(
waku,
[Protocols.Store, Protocols.Filter, Protocols.LightPush],
10 * 1000
)
await waitForRemotePeer(waku, PROTOCOLS, 10 * 1000)
// Client
client = new Client(waku, options)