mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-05 23:33:08 +00:00
feat(browser-tests): use nwaku-style format for light push log
This commit is contained in:
parent
bf5559150d
commit
89f4a6dbc2
@ -5,11 +5,7 @@ import {
|
|||||||
validators,
|
validators,
|
||||||
errorHandlers,
|
errorHandlers,
|
||||||
} from "../utils/endpoint-handler.js";
|
} from "../utils/endpoint-handler.js";
|
||||||
|
import type { SerializableSDKProtocolResult } from "../../web/index.js";
|
||||||
interface LightPushResult {
|
|
||||||
successes: string[];
|
|
||||||
failures: Array<{ error: string; peerId?: string }>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const log = new Logger("routes:waku");
|
const log = new Logger("routes:waku");
|
||||||
const router = Router();
|
const router = Router();
|
||||||
@ -67,9 +63,17 @@ router.post(
|
|||||||
},
|
},
|
||||||
handleError: errorHandlers.lightpushError,
|
handleError: errorHandlers.lightpushError,
|
||||||
transformResult: (result: unknown) => {
|
transformResult: (result: unknown) => {
|
||||||
const lightPushResult = result as LightPushResult;
|
const lightPushResult = result as SerializableSDKProtocolResult;
|
||||||
if (lightPushResult && lightPushResult.successes && lightPushResult.successes.length > 0) {
|
if (lightPushResult && lightPushResult.successes && lightPushResult.successes.length > 0) {
|
||||||
log.info("[Server] Message successfully sent via v3 lightpush!");
|
log.info("[Server] Message successfully sent via v3 lightpush!");
|
||||||
|
|
||||||
|
const sentTime = Date.now() * 1000000;
|
||||||
|
const msgHash = lightPushResult.messageHash;
|
||||||
|
|
||||||
|
const myPeerId = lightPushResult.myPeerId || 'unknown';
|
||||||
|
lightPushResult.successes.forEach((peerId: string) => {
|
||||||
|
log.info(`publishWithConn my_peer_id=${myPeerId} peer_id=${peerId} msg_hash=${msgHash} sentTime=${sentTime}`);
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
result: lightPushResult,
|
result: lightPushResult,
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import {
|
|||||||
CreateLibp2pOptions,
|
CreateLibp2pOptions,
|
||||||
IEncoder,
|
IEncoder,
|
||||||
ILightPush,
|
ILightPush,
|
||||||
|
IMessage,
|
||||||
} from "@waku/interfaces";
|
} from "@waku/interfaces";
|
||||||
import { bootstrap } from "@libp2p/bootstrap";
|
import { bootstrap } from "@libp2p/bootstrap";
|
||||||
import { EnrDecoder, TransportProtocol } from "@waku/enr";
|
import { EnrDecoder, TransportProtocol } from "@waku/enr";
|
||||||
@ -22,6 +23,7 @@ import type { Multiaddr } from "@multiformats/multiaddr";
|
|||||||
import type { ITestBrowser } from "../types/global.js";
|
import type { ITestBrowser } from "../types/global.js";
|
||||||
import { Logger, StaticShardingRoutingInfo } from "@waku/utils";
|
import { Logger, StaticShardingRoutingInfo } from "@waku/utils";
|
||||||
import type { PeerId } from "@libp2p/interface";
|
import type { PeerId } from "@libp2p/interface";
|
||||||
|
import { messageHashStr } from "@waku/core";
|
||||||
|
|
||||||
const log = new Logger("waku-headless");
|
const log = new Logger("waku-headless");
|
||||||
|
|
||||||
@ -32,6 +34,8 @@ export interface SerializableSDKProtocolResult {
|
|||||||
peerId?: string;
|
peerId?: string;
|
||||||
}>;
|
}>;
|
||||||
myPeerId?: string;
|
myPeerId?: string;
|
||||||
|
messageHash?: string;
|
||||||
|
timestamp?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeSerializable(result: { successes: PeerId[], failures: Array<{ error: any, peerId?: PeerId }> }): SerializableSDKProtocolResult {
|
function makeSerializable(result: { successes: PeerId[], failures: Array<{ error: any, peerId?: PeerId }> }): SerializableSDKProtocolResult {
|
||||||
@ -155,12 +159,9 @@ export class WakuHeadless {
|
|||||||
private async send(
|
private async send(
|
||||||
lightPush: ILightPush,
|
lightPush: ILightPush,
|
||||||
encoder: IEncoder,
|
encoder: IEncoder,
|
||||||
payload: Uint8Array,
|
message: IMessage,
|
||||||
) {
|
) {
|
||||||
return lightPush.send(encoder, {
|
return lightPush.send(encoder, message);
|
||||||
payload,
|
|
||||||
timestamp: new Date(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async pushMessageV3(
|
async pushMessageV3(
|
||||||
@ -192,6 +193,11 @@ export class WakuHeadless {
|
|||||||
processedPayload = new TextEncoder().encode(payload);
|
processedPayload = new TextEncoder().encode(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const message: IMessage = {
|
||||||
|
payload: processedPayload,
|
||||||
|
timestamp: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const lightPush = this.waku.lightPush;
|
const lightPush = this.waku.lightPush;
|
||||||
if (!lightPush) {
|
if (!lightPush) {
|
||||||
@ -216,6 +222,11 @@ export class WakuHeadless {
|
|||||||
log.info("Pubsub topic:", pubsubTopic);
|
log.info("Pubsub topic:", pubsubTopic);
|
||||||
log.info("Encoder pubsub topic:", encoder.pubsubTopic);
|
log.info("Encoder pubsub topic:", encoder.pubsubTopic);
|
||||||
|
|
||||||
|
const protoObj = await encoder.toProtoObj(message);
|
||||||
|
if (!protoObj) {
|
||||||
|
throw new Error("Failed to convert message to proto object");
|
||||||
|
}
|
||||||
|
|
||||||
if (pubsubTopic && pubsubTopic !== encoder.pubsubTopic) {
|
if (pubsubTopic && pubsubTopic !== encoder.pubsubTopic) {
|
||||||
log.warn(
|
log.warn(
|
||||||
`Explicit pubsubTopic ${pubsubTopic} provided, but auto-sharding determined ${encoder.pubsubTopic}. Using auto-sharding.`,
|
`Explicit pubsubTopic ${pubsubTopic} provided, but auto-sharding determined ${encoder.pubsubTopic}. Using auto-sharding.`,
|
||||||
@ -229,7 +240,7 @@ export class WakuHeadless {
|
|||||||
this.lightpushNode,
|
this.lightpushNode,
|
||||||
);
|
);
|
||||||
if (preferredPeerId) {
|
if (preferredPeerId) {
|
||||||
result = await this.send(lightPush, encoder, processedPayload);
|
result = await this.send(lightPush, encoder, message);
|
||||||
log.info("✅ Message sent via preferred lightpush node");
|
log.info("✅ Message sent via preferred lightpush node");
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -241,13 +252,22 @@ export class WakuHeadless {
|
|||||||
"Couldn't send message via preferred lightpush node:",
|
"Couldn't send message via preferred lightpush node:",
|
||||||
error,
|
error,
|
||||||
);
|
);
|
||||||
result = await this.send(lightPush, encoder, processedPayload);
|
result = await this.send(lightPush, encoder, message);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result = await this.send(lightPush, encoder, processedPayload);
|
result = await this.send(lightPush, encoder, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
const serializableResult = makeSerializable(result);
|
let serializableResult = makeSerializable(result);
|
||||||
|
|
||||||
|
serializableResult.myPeerId = this.waku.libp2p.peerId.toString();
|
||||||
|
|
||||||
|
const messageHash = '0x' + messageHashStr(
|
||||||
|
encoder.pubsubTopic,
|
||||||
|
protoObj,
|
||||||
|
);
|
||||||
|
|
||||||
|
serializableResult.messageHash = messageHash;
|
||||||
|
|
||||||
return serializableResult;
|
return serializableResult;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user