feat: track discovery and improve websocket tracking (#91)

This commit is contained in:
Sasha 2024-09-26 21:24:41 +02:00 committed by GitHub
parent 671347d42f
commit 8962b975ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1274 additions and 517 deletions

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,7 @@
"protobufjs": "^7.3.0"
},
"devDependencies": {
"@libp2p/interface": "^1.7.0",
"@types/node": "^20.12.11",
"copy-webpack-plugin": "^11.0.0",
"eslint": "^8",

View File

@ -51,6 +51,32 @@ export async function app(telemetryClient: TelemetryClient) {
contentTopic: DEFAULT_CONTENT_TOPIC,
});
node.libp2p.addEventListener("peer:discovery", async (event) => {
const peerId = node.libp2p.peerId.toString();
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<TelemetryPushFilter>([
{
type: TelemetryType.LIGHT_PUSH_FILTER,
protocol: "discovery",
timestamp,
createdAt: timestamp,
seenTimestamp: timestamp,
peerId: peerId,
contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: DEFAULT_PUBSUB_TOPIC,
ephemeral: false,
messageHash: hash,
errorMessage: "",
extraData,
},
]);
});
const startLightPushSequence = async (
numMessages: number,
period: number = 3000
@ -176,7 +202,7 @@ export async function app(telemetryClient: TelemetryClient) {
return;
}
const extraData = await buildExtraData(node, peerId);
const extraData = await buildExtraData(node, decodedMessage.sender);
const timestamp = Math.floor(new Date().getTime() / 1000);
telemetryClient.push<TelemetryPushFilter>([
{
@ -196,7 +222,7 @@ export async function app(telemetryClient: TelemetryClient) {
]);
const messageElement = document.createElement("div");
messageElement.textContent = `Message: ${decodedMessage.hash} ${decodedMessage.index} of ${decodedMessage.total}`;
messageElement.textContent = `Message: ${decodedMessage.seqHash} ${decodedMessage.index} of ${decodedMessage.total}`;
messagesReceived.appendChild(messageElement);
messagesReceived.appendChild(document.createElement("br"));

View File

@ -1,5 +1,6 @@
import { peerIdFromString } from "@libp2p/peer-id";
import type { Waku } from "@waku/interfaces";
import { Peer } from "@libp2p/interface";
import type { LightNode } from "@waku/sdk";
export const generateRandomNumber = (): number => {
return Math.floor(Math.random() * 1000000);
@ -17,17 +18,23 @@ export const sha256 = async (number: number | string ): Promise<string> => {
const DEFAULT_EXTRA_DATA = { sdk: "0.0.28" };
export const DEFAULT_EXTRA_DATA_STR = JSON.stringify(DEFAULT_EXTRA_DATA);
export const buildExtraData = async (node: Waku, peerId: string): Promise<string> => {
const peer = await node.libp2p.peerStore.get(peerIdFromString(peerId));
const hasWebsockes = peer
export const buildExtraData = async (node: LightNode, peerId: string): Promise<string> => {
let extraData = { ...DEFAULT_EXTRA_DATA };
const peer: Peer = await node.libp2p.peerStore.get(peerIdFromString(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({
...DEFAULT_EXTRA_DATA,
...extraData,
peerId,
hasWebsockes,
websocket,
enabledProtocols: peer.protocols,
});
};