Skip to content

Commit 89f4a6d

Browse files
committed
feat(browser-tests): use nwaku-style format for light push log
1 parent bf55591 commit 89f4a6d

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

packages/browser-tests/src/routes/waku.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import {
55
validators,
66
errorHandlers,
77
} from "../utils/endpoint-handler.js";
8-
9-
interface LightPushResult {
10-
successes: string[];
11-
failures: Array<{ error: string; peerId?: string }>;
12-
}
8+
import type { SerializableSDKProtocolResult } from "../../web/index.js";
139

1410
const log = new Logger("routes:waku");
1511
const router = Router();
@@ -67,9 +63,17 @@ router.post(
6763
},
6864
handleError: errorHandlers.lightpushError,
6965
transformResult: (result: unknown) => {
70-
const lightPushResult = result as LightPushResult;
66+
const lightPushResult = result as SerializableSDKProtocolResult;
7167
if (lightPushResult && lightPushResult.successes && lightPushResult.successes.length > 0) {
7268
log.info("[Server] Message successfully sent via v3 lightpush!");
69+
70+
const sentTime = Date.now() * 1000000;
71+
const msgHash = lightPushResult.messageHash;
72+
73+
const myPeerId = lightPushResult.myPeerId || 'unknown';
74+
lightPushResult.successes.forEach((peerId: string) => {
75+
log.info(`publishWithConn my_peer_id=${myPeerId} peer_id=${peerId} msg_hash=${msgHash} sentTime=${sentTime}`);
76+
});
7377
return {
7478
success: true,
7579
result: lightPushResult,

packages/browser-tests/web/index.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import {
1515
CreateLibp2pOptions,
1616
IEncoder,
1717
ILightPush,
18+
IMessage,
1819
} from "@waku/interfaces";
1920
import { bootstrap } from "@libp2p/bootstrap";
2021
import { EnrDecoder, TransportProtocol } from "@waku/enr";
2122
import type { Multiaddr } from "@multiformats/multiaddr";
2223
import type { ITestBrowser } from "../types/global.js";
2324
import { Logger, StaticShardingRoutingInfo } from "@waku/utils";
2425
import type { PeerId } from "@libp2p/interface";
26+
import { messageHashStr } from "@waku/core";
2527

2628
const log = new Logger("waku-headless");
2729

@@ -32,6 +34,8 @@ export interface SerializableSDKProtocolResult {
3234
peerId?: string;
3335
}>;
3436
myPeerId?: string;
37+
messageHash?: string;
38+
timestamp?: number;
3539
}
3640

3741
function makeSerializable(result: { successes: PeerId[], failures: Array<{ error: any, peerId?: PeerId }> }): SerializableSDKProtocolResult {
@@ -155,12 +159,9 @@ export class WakuHeadless {
155159
private async send(
156160
lightPush: ILightPush,
157161
encoder: IEncoder,
158-
payload: Uint8Array,
162+
message: IMessage,
159163
) {
160-
return lightPush.send(encoder, {
161-
payload,
162-
timestamp: new Date(),
163-
});
164+
return lightPush.send(encoder, message);
164165
}
165166

166167
async pushMessageV3(
@@ -192,6 +193,11 @@ export class WakuHeadless {
192193
processedPayload = new TextEncoder().encode(payload);
193194
}
194195

196+
const message: IMessage = {
197+
payload: processedPayload,
198+
timestamp: new Date(),
199+
};
200+
195201
try {
196202
const lightPush = this.waku.lightPush;
197203
if (!lightPush) {
@@ -216,6 +222,11 @@ export class WakuHeadless {
216222
log.info("Pubsub topic:", pubsubTopic);
217223
log.info("Encoder pubsub topic:", encoder.pubsubTopic);
218224

225+
const protoObj = await encoder.toProtoObj(message);
226+
if (!protoObj) {
227+
throw new Error("Failed to convert message to proto object");
228+
}
229+
219230
if (pubsubTopic && pubsubTopic !== encoder.pubsubTopic) {
220231
log.warn(
221232
`Explicit pubsubTopic ${pubsubTopic} provided, but auto-sharding determined ${encoder.pubsubTopic}. Using auto-sharding.`,
@@ -229,7 +240,7 @@ export class WakuHeadless {
229240
this.lightpushNode,
230241
);
231242
if (preferredPeerId) {
232-
result = await this.send(lightPush, encoder, processedPayload);
243+
result = await this.send(lightPush, encoder, message);
233244
log.info("✅ Message sent via preferred lightpush node");
234245
} else {
235246
throw new Error(
@@ -241,13 +252,22 @@ export class WakuHeadless {
241252
"Couldn't send message via preferred lightpush node:",
242253
error,
243254
);
244-
result = await this.send(lightPush, encoder, processedPayload);
255+
result = await this.send(lightPush, encoder, message);
245256
}
246257
} else {
247-
result = await this.send(lightPush, encoder, processedPayload);
258+
result = await this.send(lightPush, encoder, message);
248259
}
249260

250-
const serializableResult = makeSerializable(result);
261+
let serializableResult = makeSerializable(result);
262+
263+
serializableResult.myPeerId = this.waku.libp2p.peerId.toString();
264+
265+
const messageHash = '0x' + messageHashStr(
266+
encoder.pubsubTopic,
267+
protoObj,
268+
);
269+
270+
serializableResult.messageHash = messageHash;
251271

252272
return serializableResult;
253273
} catch (error) {

0 commit comments

Comments
 (0)