From 71fe0472548d009e10af6036d81352304ef99078 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 10 Nov 2022 22:44:53 +0530 Subject: [PATCH 01/10] add: support for user agent to identify libp2p --- packages/core/src/lib/waku.ts | 6 ++++++ packages/create/src/index.ts | 27 ++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/core/src/lib/waku.ts b/packages/core/src/lib/waku.ts index f792eb6c56..a8accc7202 100644 --- a/packages/core/src/lib/waku.ts +++ b/packages/core/src/lib/waku.ts @@ -19,6 +19,7 @@ import { StoreCodec, WakuStore } from "./waku_store"; export const DefaultPingKeepAliveValueSecs = 0; export const DefaultRelayKeepAliveValueSecs = 5 * 60; +export const DefaultUserAgent = "waku-js"; const log = debug("waku:waku"); @@ -37,6 +38,11 @@ export interface WakuOptions { * @default {@link DefaultRelayKeepAliveValueSecs} */ relayKeepAlive?: number; + /** + * Set the user agent string to be used in identification of the node. + * @default {@link DefaultUserAgent} + */ + userAgent?: string; } export class WakuNode implements Waku { diff --git a/packages/create/src/index.ts b/packages/create/src/index.ts index 89a6015f4e..52d3c00915 100644 --- a/packages/create/src/index.ts +++ b/packages/create/src/index.ts @@ -14,6 +14,7 @@ import { } from "@waku/core"; import { PeerDiscoveryStaticPeers } from "@waku/core/lib/peer_discovery_static_list"; import { getPredefinedBootstrapNodes } from "@waku/core/lib/predefined_bootstrap_nodes"; +import { DefaultUserAgent } from "@waku/core/lib/waku"; import type { WakuFull, WakuLight, WakuPrivacy } from "@waku/interfaces"; import type { Libp2p } from "libp2p"; import { createLibp2p, Libp2pOptions } from "libp2p"; @@ -71,7 +72,11 @@ export async function createLightNode( Object.assign(libp2pOptions, { peerDiscovery }); } - const libp2p = await defaultLibp2p(undefined, libp2pOptions); + const libp2p = await defaultLibp2p( + undefined, + libp2pOptions, + options?.userAgent + ); const wakuStore = new WakuStore(libp2p, options); const wakuLightPush = new WakuLightPush(libp2p, options); @@ -100,7 +105,11 @@ export async function createPrivacyNode( Object.assign(libp2pOptions, { peerDiscovery }); } - const libp2p = await defaultLibp2p(new WakuRelay(options), libp2pOptions); + const libp2p = await defaultLibp2p( + new WakuRelay(options), + libp2pOptions, + options?.userAgent + ); return new WakuNode(options ?? {}, libp2p) as WakuPrivacy; } @@ -128,7 +137,11 @@ export async function createFullNode( Object.assign(libp2pOptions, { peerDiscovery }); } - const libp2p = await defaultLibp2p(new WakuRelay(options), libp2pOptions); + const libp2p = await defaultLibp2p( + new WakuRelay(options), + libp2pOptions, + options?.userAgent + ); const wakuStore = new WakuStore(libp2p, options); const wakuLightPush = new WakuLightPush(libp2p, options); @@ -149,14 +162,18 @@ export function defaultPeerDiscovery(): PeerDiscovery { export async function defaultLibp2p( wakuRelay?: WakuRelay, - options?: Partial + options?: Partial, + userAgent?: string ): Promise { const libp2pOpts = Object.assign( { transports: [new WebSockets({ filter: filterAll })], streamMuxers: [new Mplex()], connectionEncryption: [new Noise()], - }, + identify: { + host: userAgent || DefaultUserAgent, + }, + } as Libp2pOptions, wakuRelay ? { pubsub: wakuRelay } : {}, options ?? {} ); From 00e57d61ec0dfd7bf1c8b601bbac507d8829f126 Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Fri, 11 Nov 2022 19:51:27 +0530 Subject: [PATCH 02/10] Update packages/core/src/lib/waku.ts Co-authored-by: fryorcraken.eth <110212804+fryorcraken@users.noreply.github.com> --- packages/core/src/lib/waku.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/lib/waku.ts b/packages/core/src/lib/waku.ts index a8accc7202..d0fa84c0e5 100644 --- a/packages/core/src/lib/waku.ts +++ b/packages/core/src/lib/waku.ts @@ -19,7 +19,7 @@ import { StoreCodec, WakuStore } from "./waku_store"; export const DefaultPingKeepAliveValueSecs = 0; export const DefaultRelayKeepAliveValueSecs = 5 * 60; -export const DefaultUserAgent = "waku-js"; +export const DefaultUserAgent = "js-waku"; const log = debug("waku:waku"); From 55890f1b9e75954a67e53edeccebf828319897a6 Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Fri, 11 Nov 2022 19:51:34 +0530 Subject: [PATCH 03/10] Update packages/create/src/index.ts Co-authored-by: fryorcraken.eth <110212804+fryorcraken@users.noreply.github.com> --- packages/create/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create/src/index.ts b/packages/create/src/index.ts index 52d3c00915..7356d3f14b 100644 --- a/packages/create/src/index.ts +++ b/packages/create/src/index.ts @@ -171,7 +171,7 @@ export async function defaultLibp2p( streamMuxers: [new Mplex()], connectionEncryption: [new Noise()], identify: { - host: userAgent || DefaultUserAgent, + host: userAgent ?? DefaultUserAgent, }, } as Libp2pOptions, wakuRelay ? { pubsub: wakuRelay } : {}, From 7d8c410b5b2a98ee45b965a762f1b6b6409f9f55 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Mon, 14 Nov 2022 15:13:16 +0530 Subject: [PATCH 04/10] address comments --- packages/core/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e90b55e3f4..635fc89d4b 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,4 +1,5 @@ export { DefaultPubSubTopic } from "./lib/constants"; +export { DefaultUserAgent } from "./lib/waku"; export * as proto_message from "./proto/message"; export * as proto_topic_only_message from "./proto/topic_only_message"; From 593a037e1f1f039978f5f43a4aa5dab7f7014fc0 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Mon, 14 Nov 2022 15:15:53 +0530 Subject: [PATCH 05/10] address comments --- packages/create/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create/src/index.ts b/packages/create/src/index.ts index 7356d3f14b..496a8708d5 100644 --- a/packages/create/src/index.ts +++ b/packages/create/src/index.ts @@ -12,9 +12,9 @@ import { WakuRelay, WakuStore, } from "@waku/core"; +import { DefaultUserAgent } from "@waku/core"; import { PeerDiscoveryStaticPeers } from "@waku/core/lib/peer_discovery_static_list"; import { getPredefinedBootstrapNodes } from "@waku/core/lib/predefined_bootstrap_nodes"; -import { DefaultUserAgent } from "@waku/core/lib/waku"; import type { WakuFull, WakuLight, WakuPrivacy } from "@waku/interfaces"; import type { Libp2p } from "libp2p"; import { createLibp2p, Libp2pOptions } from "libp2p"; From f3575afdfd380fa43594ae1aae76e26197499fe4 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Wed, 16 Nov 2022 00:57:42 +0530 Subject: [PATCH 06/10] add: test case --- packages/tests/tests/waku.node.spec.ts | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 587f6ab808..e2a8eebf51 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -186,3 +186,38 @@ describe("Decryption Keys", () => { expect(receivedMsg.timestamp?.valueOf()).to.eq(messageTimestamp.valueOf()); }); }); + +describe("User Agent", () => { + let waku: Waku; + let nwaku: Nwaku; + + afterEach(async function () { + !!nwaku && nwaku.stop(); + !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); + }); + + it("Sets default value correctly", async function () { + this.timeout(20_000); + nwaku = new Nwaku(makeLogFileName(this)); + await nwaku.start({ + filter: true, + store: true, + lightpush: true, + persistMessages: true, + }); + const multiAddrWithId = await nwaku.getMultiaddrWithId(); + + waku = await createLightNode({ + staticNoiseKey: NOISE_KEY_1, + userAgent: "test-user-agent", + }); + await waku.start(); + await waku.dial(multiAddrWithId); + await waitForRemotePeer(waku); + + const peerInfo = await waku.libp2p.peerStore.metadataBook.get( + waku.libp2p.peerId + ); + console.log(bytesToUtf8(peerInfo.get("AgentVersion")!)); + }); +}); From 1cfba385c9d2af54dc604b9955a31f187f24f170 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 17 Nov 2022 01:34:49 +0530 Subject: [PATCH 08/10] rn persist --- packages/tests/tests/waku.node.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index c5fda5c9f6..7e8da4fef8 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -202,7 +202,6 @@ describe("User Agent", () => { filter: true, store: true, lightpush: true, - persistMessages: true, }); const multiAddrWithId = await nwaku.getMultiaddrWithId(); From c7920f09f3784b80f3d53ff2b3d854c3e7787b3b Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Thu, 17 Nov 2022 11:29:34 +1100 Subject: [PATCH 09/10] test: check both custom and default agents --- packages/tests/tests/waku.node.spec.ts | 55 ++++++++++++++++---------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 7e8da4fef8..a6590fd6a9 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -1,5 +1,6 @@ import type { PeerId } from "@libp2p/interface-peer-id"; import { bytesToUtf8, utf8ToBytes } from "@waku/byte-utils"; +import { DefaultUserAgent } from "@waku/core"; import { PeerDiscoveryStaticPeers } from "@waku/core/lib/peer_discovery_static_list"; import { waitForRemotePeer } from "@waku/core/lib/wait_for_remote_peer"; import { createLightNode, createPrivacyNode } from "@waku/create"; @@ -187,35 +188,47 @@ describe("Decryption Keys", () => { }); describe("User Agent", () => { - let waku: Waku; - let nwaku: Nwaku; + let waku1: Waku; + let waku2: Waku; afterEach(async function () { - !!nwaku && nwaku.stop(); - !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); + !!waku1 && waku1.stop().catch((e) => console.log("Waku failed to stop", e)); + !!waku2 && waku2.stop().catch((e) => console.log("Waku failed to stop", e)); }); it("Sets default value correctly", async function () { this.timeout(20_000); - nwaku = new Nwaku(makeLogFileName(this)); - await nwaku.start({ - filter: true, - store: true, - lightpush: true, - }); - const multiAddrWithId = await nwaku.getMultiaddrWithId(); - waku = await createLightNode({ - staticNoiseKey: NOISE_KEY_1, - userAgent: "test-user-agent", - }); - await waku.start(); - await waku.dial(multiAddrWithId); - await waitForRemotePeer(waku); + const waku1UserAgent = "test-user-agent"; - const peerInfo = await waku.libp2p.peerStore.metadataBook.get( - waku.libp2p.peerId + [waku1, waku2] = await Promise.all([ + createPrivacyNode({ + staticNoiseKey: NOISE_KEY_1, + userAgent: waku1UserAgent, + }).then((waku) => waku.start().then(() => waku)), + createPrivacyNode({ + staticNoiseKey: NOISE_KEY_2, + libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } }, + }).then((waku) => waku.start().then(() => waku)), + ]); + + waku1.addPeerToAddressBook( + waku2.libp2p.peerId, + waku2.libp2p.getMultiaddrs() + ); + await waku1.dial(waku2.libp2p.peerId); + await waitForRemotePeer(waku1); + + const [waku1PeerInfo, waku2PeerInfo] = await Promise.all([ + waku2.libp2p.peerStore.metadataBook.get(waku1.libp2p.peerId), + waku1.libp2p.peerStore.metadataBook.get(waku2.libp2p.peerId), + ]); + + expect(bytesToUtf8(waku1PeerInfo.get("AgentVersion")!)).to.eq( + waku1UserAgent + ); + expect(bytesToUtf8(waku2PeerInfo.get("AgentVersion")!)).to.eq( + DefaultUserAgent ); - console.log(bytesToUtf8(peerInfo.get("AgentVersion")!)); }); }); From e3e1197a9186192c72d09a8bc146eca73b92c745 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Thu, 17 Nov 2022 11:30:07 +1100 Subject: [PATCH 10/10] fix: correct usage of the libp2p API --- packages/create/src/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/create/src/index.ts b/packages/create/src/index.ts index 496a8708d5..a72ff81cc1 100644 --- a/packages/create/src/index.ts +++ b/packages/create/src/index.ts @@ -171,7 +171,9 @@ export async function defaultLibp2p( streamMuxers: [new Mplex()], connectionEncryption: [new Noise()], identify: { - host: userAgent ?? DefaultUserAgent, + host: { + agentVersion: userAgent ?? DefaultUserAgent, + }, }, } as Libp2pOptions, wakuRelay ? { pubsub: wakuRelay } : {},