From 6f478dd13a625a98dbcd1ec078e4333d0102d637 Mon Sep 17 00:00:00 2001 From: Arseniy Klempner Date: Wed, 15 May 2024 20:32:16 -0700 Subject: [PATCH] feat: use wakuv2test nodes for bootstrapping --- .../sent-received-ratio/package-lock.json | 1 + examples/sent-received-ratio/package.json | 1 + examples/sent-received-ratio/src/index.ts | 92 +++++++++++++------ 3 files changed, 65 insertions(+), 29 deletions(-) diff --git a/examples/sent-received-ratio/package-lock.json b/examples/sent-received-ratio/package-lock.json index 4fec68d..b9e95fd 100644 --- a/examples/sent-received-ratio/package-lock.json +++ b/examples/sent-received-ratio/package-lock.json @@ -8,6 +8,7 @@ "name": "sent-received-ratio", "version": "0.0.1", "dependencies": { + "@waku/discovery": "^0.0.3-e49e728.0", "@waku/sdk": "0.0.26-e49e728.0" }, "devDependencies": { diff --git a/examples/sent-received-ratio/package.json b/examples/sent-received-ratio/package.json index b9d5b53..19f901c 100644 --- a/examples/sent-received-ratio/package.json +++ b/examples/sent-received-ratio/package.json @@ -7,6 +7,7 @@ "start": "webpack-dev-server" }, "dependencies": { + "@waku/discovery": "^0.0.3-e49e728.0", "@waku/sdk": "0.0.26-e49e728.0" }, "devDependencies": { diff --git a/examples/sent-received-ratio/src/index.ts b/examples/sent-received-ratio/src/index.ts index e4c6ac7..68963aa 100644 --- a/examples/sent-received-ratio/src/index.ts +++ b/examples/sent-received-ratio/src/index.ts @@ -6,19 +6,49 @@ import { DecodedMessage, bytesToUtf8, waitForRemotePeer, - Protocols, + DefaultPubsubTopic, + LightNode, } from "@waku/sdk"; +import { + wakuDnsDiscovery, + enrTree, + wakuLocalPeerCacheDiscovery, + wakuPeerExchangeDiscovery, +} from "@waku/discovery"; +import { bootstrap } from "@libp2p/bootstrap"; + const DEFAULT_CONTENT_TOPIC = "/js-waku-examples/1/message-ratio/utf8"; -export async function sender() { - const node = await createLightNode({ - contentTopics: [DEFAULT_CONTENT_TOPIC], - defaultBootstrap: true, +const testPeers = [ + "/dns4/node-01.do-ams3.wakuv2.test.status.im/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ", + "/dns4/node-01.gc-us-central1-a.wakuv2.test.status.im/tcp/30303/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS", + "/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.status.im/tcp/30303/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm", +]; + +const wakuNode = async (): Promise => { + return await createLightNode({ + pubsubTopics: [DefaultPubsubTopic], + bootstrapPeers: testPeers, + libp2p: { + peerDiscovery: [ + wakuDnsDiscovery([enrTree["SANDBOX"]]), + wakuLocalPeerCacheDiscovery(), + wakuPeerExchangeDiscovery([DefaultPubsubTopic]), + bootstrap({ list: testPeers }), + ], + }, }); +}; + +export async function sender() { + const node = await wakuNode(); await node.start(); await waitForRemotePeer(node); - const encoder = createEncoder({ contentTopic: DEFAULT_CONTENT_TOPIC }); + const encoder = createEncoder({ + pubsubTopic: DefaultPubsubTopic, + contentTopic: DEFAULT_CONTENT_TOPIC, + }); const start = (numMessages: number, period: number = 3000) => { let intervalId: NodeJS.Timeout; @@ -26,15 +56,24 @@ export async function sender() { const stream = new ReadableStream({ start(controller) { intervalId = setInterval(async () => { - const result = await node.lightPush.send(encoder, { - payload: utf8ToBytes(String(i)), - timestamp: new Date(), - }); - controller.enqueue({ result, i }); - i++; - if (i >= numMessages) { - clearInterval(intervalId); - controller.close(); + try { + const result = await node.lightPush.send(encoder, { + payload: utf8ToBytes(String(i)), + timestamp: new Date(), + }); + if (result.successes.length > 0) { + controller.enqueue({ result, i }); + i++; + } + if (result.failures.length > 0) { + console.error("Failed to send message", result.failures); + } + if (i >= numMessages) { + clearInterval(intervalId); + controller.close(); + } + } catch (error) { + console.error("Error sending message", error); } }, period); }, @@ -56,27 +95,24 @@ export async function sender() { } export async function receiver(callback: (message: DecodedMessage) => void) { - const node = await createLightNode({ - contentTopics: [DEFAULT_CONTENT_TOPIC], - defaultBootstrap: true, - }); + const node = await wakuNode(); await node.start(); await waitForRemotePeer(node); - const decoder = createDecoder(DEFAULT_CONTENT_TOPIC); + const decoder = createDecoder(DEFAULT_CONTENT_TOPIC, DefaultPubsubTopic); const subscription = await node.filter.subscribe(decoder, callback); return { node, subscription }; } -let messages = []; +let messages = []; const startReceiver = async () => { const messagesReceived = document.getElementById("messagesReceived"); const subscriptionCallback = (message: DecodedMessage) => { - const messageValue = bytesToUtf8(message.payload) + const messageValue = bytesToUtf8(message.payload); messages.push(messageValue); const messageElement = document.createElement("div"); messageElement.textContent = `Message: ${messageValue}`; @@ -90,16 +126,12 @@ const startReceiver = async () => { (window as any).receiver = node; }; -const startSender = async (numMessages: number) => { +const startSender = async (numMessages: number, messagePeriod: number) => { const { node, start } = await sender(); (window as any).sender = node; console.log("Sender started."); document.getElementById("senderStatusText").textContent = "Running Sender"; - const messagePeriod = parseInt( - (document.getElementById("messagePeriod") as HTMLInputElement).value, - 3 - ); const { stream, stop } = start(numMessages, messagePeriod * 1000); const reader = stream.getReader(); const intervalId = setInterval(() => { @@ -118,7 +150,6 @@ const startSender = async (numMessages: number) => { }, messagePeriod * 1000 + 1); }; - document.getElementById("start").addEventListener("click", async () => { const startButton = document.getElementById("start") as HTMLButtonElement; startButton.disabled = true; @@ -126,6 +157,9 @@ document.getElementById("start").addEventListener("click", async () => { (document.getElementById("numMessages") as HTMLInputElement).value, 10 ); + const messagePeriod = parseInt( + (document.getElementById("messagePeriod") as HTMLInputElement).value + ); await startReceiver(); - await startSender(numMessages); + await startSender(numMessages, messagePeriod); });