diff --git a/examples/dogfooding/src/index.ts b/examples/dogfooding/src/index.ts index 1fad16c..4a2b773 100644 --- a/examples/dogfooding/src/index.ts +++ b/examples/dogfooding/src/index.ts @@ -10,24 +10,12 @@ import { generateKeyPairFromSeed } from "@libp2p/crypto/keys"; import { fromString } from "uint8arrays"; import { Type, Field } from "protobufjs"; -import { - TelemetryClient, - TelemetryPushFilter, - TelemetryType, -} from "./telemetry_client"; import { generateRandomNumber, sha256, - buildExtraData, - DEFAULT_EXTRA_DATA_STR, } from "./util"; const DEFAULT_CONTENT_TOPIC = "/js-waku-examples/1/message-ratio/utf8"; -const DEFAULT_PUBSUB_TOPIC = utils.contentTopicToPubsubTopic( - DEFAULT_CONTENT_TOPIC -); -const TELEMETRY_URL = - process.env.TELEMETRY_URL || "http://localhost:8080/waku-metrics"; const ProtoSequencedMessage = new Type("SequencedMessage") .add(new Field("hash", 1, "string")) @@ -36,8 +24,6 @@ const ProtoSequencedMessage = new Type("SequencedMessage") .add(new Field("sender", 4, "string")); const sequenceCompletedEvent = new CustomEvent("sequenceCompleted"); -const messageSentEvent = new CustomEvent("messageSent"); -const messageReceivedEvent = new CustomEvent("messageReceived"); async function wakuNode(): Promise { let seed = localStorage.getItem("seed"); @@ -61,16 +47,18 @@ async function wakuNode(): Promise { }, }); - await node.dial("/dns4/waku-test.bloxy.one/tcp/8095/wss/p2p/16Uiu2HAmSZbDB7CusdRhgkD81VssRjQV5ZH13FbzCGcdnbbh6VwZ"); - await node.dial("/dns4/vps-aaa00d52.vps.ovh.ca/tcp/8000/wss/p2p/16Uiu2HAm9PftGgHZwWE3wzdMde4m3kT2eYJFXLZfGoSED3gysofk"); - await node.dial("/dns4/waku.fryorcraken.xyz/tcp/8000/wss/p2p/16Uiu2HAmMRvhDHrtiHft1FTUYnn6cVA8AWVrTyLUayJJ3MWpUZDB"); + (window as any).waku = node; + + await Promise.allSettled([ + node.dial("/dns4/waku-test.bloxy.one/tcp/8095/wss/p2p/16Uiu2HAmSZbDB7CusdRhgkD81VssRjQV5ZH13FbzCGcdnbbh6VwZ"), + node.dial("/dns4/vps-aaa00d52.vps.ovh.ca/tcp/8000/wss/p2p/16Uiu2HAm9PftGgHZwWE3wzdMde4m3kT2eYJFXLZfGoSED3gysofk") + ]); return node; } -export async function app(telemetryClient: TelemetryClient) { +export async function app() { const node = await wakuNode(); - (window as any).waku = node; console.log("DEBUG: your peer ID is:", node.libp2p.peerId.toString()); @@ -86,31 +74,6 @@ export async function app(telemetryClient: TelemetryClient) { } }); - node.libp2p.addEventListener("peer:discovery", async (event) => { - const discoveredPeerId = event.detail.id.toString(); - - const timestamp = Math.floor(new Date().getTime() / 1000); - const extraData = await buildExtraData(node, discoveredPeerId); - const hash = await sha256(`${peerId}-${discoveredPeerId}-${timestamp}`); - - telemetryClient.push([ - { - type: TelemetryType.LIGHT_PUSH_FILTER, - protocol: "discovery", - timestamp, - createdAt: timestamp, - seenTimestamp: timestamp, - peerId, - contentTopic: DEFAULT_CONTENT_TOPIC, - pubsubTopic: DEFAULT_PUBSUB_TOPIC, - ephemeral: false, - messageHash: hash, - errorMessage: "", - extraData, - }, - ]); - }); - const startLightPushSequence = async ( numMessages: number, period: number = 3000 @@ -125,7 +88,6 @@ export async function app(telemetryClient: TelemetryClient) { `${sequenceHash}-${sequenceIndex}-${sequenceTotal}` ); - const timestamp = Math.floor(new Date().getTime() / 1000); const message = ProtoSequencedMessage.create({ hash: messageHash, total: sequenceTotal, @@ -157,6 +119,13 @@ export async function app(telemetryClient: TelemetryClient) { } else { document.dispatchEvent(sequenceCompletedEvent); } + + if ( result.successes.length > 0) { + const messageElement = document.createElement("div"); + messageElement.textContent = messageHash; + + document.getElementById("messagesSent")?.appendChild(messageElement); + } } catch (error) { console.error("DEBUG: Error sending message", error); } @@ -177,14 +146,14 @@ export async function app(telemetryClient: TelemetryClient) { return; } - const timestamp = Math.floor(new Date().getTime() / 1000); - const messageElement = document.createElement("div"); - messageElement.textContent = `Message: ${decodedMessage.hash}`; - document.dispatchEvent(messageReceivedEvent); + messageElement.textContent = decodedMessage.hash; + + document.getElementById("messagesReceived")?.appendChild(messageElement); }; - await node.filter.subscribe(decoder, subscriptionCallback); + await node.nextFilter.subscribe(decoder, subscriptionCallback); + await node.nextFilter.subscribe(decoder, subscriptionCallback); }; return { @@ -195,10 +164,7 @@ export async function app(telemetryClient: TelemetryClient) { } (async () => { - const telemetryClient = new TelemetryClient(TELEMETRY_URL, 5000); - const { node, startLightPushSequence, startFilterSubscription } = await app( - telemetryClient - ); + const { startLightPushSequence, startFilterSubscription } = await app(); startFilterSubscription(); @@ -207,4 +173,4 @@ export async function app(telemetryClient: TelemetryClient) { ); startLightPushSequence(10, 3000); -})(); \ No newline at end of file +})(); diff --git a/examples/dogfooding/src/telemetry_client.ts b/examples/dogfooding/src/telemetry_client.ts deleted file mode 100644 index 8025292..0000000 --- a/examples/dogfooding/src/telemetry_client.ts +++ /dev/null @@ -1,85 +0,0 @@ -export enum TelemetryType { - LIGHT_PUSH_FILTER = "LightPushFilter", -} - -interface TelemetryMessage { - type: string; - - timestamp: number; - contentTopic: string; - pubsubTopic: string; - peerId: string; - errorMessage: string; - extraData: string; -} - -export interface TelemetryPushFilter extends TelemetryMessage { - type: "LightPushFilter", - protocol: string; - ephemeral: boolean; - seenTimestamp: number; - createdAt: number; - messageHash: string; -} - -export class TelemetryClient { - constructor( - private readonly url: string, - private intervalPeriod: number = 5000 - ) {} - - private queue: TelemetryMessage[] = []; - private intervalId: NodeJS.Timeout | null = null; - private requestId = 0; - - public push(messages: T[]) { - this.queue.push(...messages); - } - - public async start() { - if (!this.intervalId) { - this.intervalId = setInterval(async () => { - if (this.queue.length > 0) { - const success = await this.send(this.queue); - if (success) { - console.log("Sent ", this.queue.length, " telemetry logs"); - this.queue = []; - } - } - }, this.intervalPeriod); - } - } - - public stop() { - if (this.intervalId) { - clearInterval(this.intervalId); - this.intervalId = null; - } - } - private async send(messages: T[]) { - if (!window.location.hostname.includes("lab.waku.org")) { - return; - } - - const telemetryRequests = messages.map((message) => ({ - id: ++this.requestId, - telemetryType: message.type.toString(), - telemetryData: message - })); - - try { - const res = await fetch(this.url, { - method: "POST", - body: JSON.stringify(telemetryRequests), - }); - if (res.status !== 201) { - console.log("DEBUG: Error sending messages to telemetry service: ", res.status, res.statusText, res.json); - return false - } - return true; - } catch (e) { - console.log("DEBUG: Error sending messages to telemetry service", e); - return false; - } - } -} diff --git a/examples/dogfooding/src/util.ts b/examples/dogfooding/src/util.ts index 8eed6a8..911e9cb 100644 --- a/examples/dogfooding/src/util.ts +++ b/examples/dogfooding/src/util.ts @@ -1,6 +1,3 @@ -import { Peer } from "@libp2p/interface"; -import type { LightNode } from "@waku/sdk"; - export const generateRandomNumber = (): number => { return Math.floor(Math.random() * 1000000); }; @@ -13,27 +10,3 @@ export const sha256 = async (number: number | string ): Promise => { .map((b) => b.toString(16).padStart(2, "0")) .join(""); }; - -const DEFAULT_EXTRA_DATA = { sdk: "0.0.29-rc" }; -export const DEFAULT_EXTRA_DATA_STR = JSON.stringify(DEFAULT_EXTRA_DATA); - -export const buildExtraData = async (node: LightNode, peerId: string): Promise => { - let extraData = { ...DEFAULT_EXTRA_DATA }; - const peer: Peer = (await node.libp2p.peerStore.all()).find(p => p.id.toString() === peerId); - - if (!peer || peerId === node.libp2p.peerId.toString()) { - return JSON.stringify(extraData); - } - - const websocket = peer - .addresses - .map(addr => addr.multiaddr.toString()) - .some(addr => addr.includes("ws") || addr.includes("wss")); - - return JSON.stringify({ - ...extraData, - peerId, - websocket, - enabledProtocols: peer.protocols, - }); -}; \ No newline at end of file diff --git a/examples/dogfooding/webpack.config.js b/examples/dogfooding/webpack.config.js index cc7d368..56eef45 100644 --- a/examples/dogfooding/webpack.config.js +++ b/examples/dogfooding/webpack.config.js @@ -1,6 +1,5 @@ const CopyWebpackPlugin = require("copy-webpack-plugin"); const path = require("path"); -const webpack = require("webpack"); module.exports = { entry: "./src/index.ts", // Changed from index.js to index.ts @@ -32,8 +31,5 @@ module.exports = { new CopyWebpackPlugin({ patterns: ["index.html", "favicon.ico", "favicon.png", "manifest.json"], }), - new webpack.DefinePlugin({ - 'process.env.TELEMETRY_URL': JSON.stringify(process.env.TELEMETRY_URL || "https://telemetry.status.im/waku-metrics") - }), ], };