Send nanoseconds timestamp in nim-waku RPC

This commit is contained in:
Franck Royer 2022-03-09 12:59:47 +11:00 committed by Franck
parent c07396c30b
commit d95d773f45
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 46 additions and 27 deletions

View File

@ -379,7 +379,7 @@ describe("Waku Relay [node only]", () => {
} }
); );
await nimWaku.sendMessage(message); await nimWaku.sendMessage(NimWaku.toWakuRelayMessage(message));
const receivedMsg = await receivedMsgPromise; const receivedMsg = await receivedMsgPromise;

View File

@ -40,7 +40,9 @@ describe("Waku Store", () => {
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
expect( expect(
await nimWaku.sendMessage( await nimWaku.sendMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic) NimWaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
)
) )
).to.be.true; ).to.be.true;
} }
@ -70,7 +72,9 @@ describe("Waku Store", () => {
for (let i = 0; i < totalMsgs; i++) { for (let i = 0; i < totalMsgs; i++) {
expect( expect(
await nimWaku.sendMessage( await nimWaku.sendMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic) NimWaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
)
) )
).to.be.true; ).to.be.true;
} }
@ -107,7 +111,9 @@ describe("Waku Store", () => {
for (let i = 0; i < availMsgs; i++) { for (let i = 0; i < availMsgs; i++) {
expect( expect(
await nimWaku.sendMessage( await nimWaku.sendMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic) NimWaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
)
) )
).to.be.true; ).to.be.true;
} }
@ -141,7 +147,9 @@ describe("Waku Store", () => {
for (let i = 0; i < 15; i++) { for (let i = 0; i < 15; i++) {
expect( expect(
await nimWaku.sendMessage( await nimWaku.sendMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic) NimWaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
)
) )
).to.be.true; ).to.be.true;
} }
@ -176,7 +184,9 @@ describe("Waku Store", () => {
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
expect( expect(
await nimWaku.sendMessage( await nimWaku.sendMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic), NimWaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
),
customPubSubTopic customPubSubTopic
) )
).to.be.true; ).to.be.true;
@ -453,9 +463,11 @@ describe("Waku Store", () => {
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
expect( expect(
await nimWaku.sendMessage( await nimWaku.sendMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic, { NimWaku.toWakuRelayMessage(
timestamp: messageTimestamps[i], await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic, {
}) timestamp: messageTimestamps[i],
})
)
) )
).to.be.true; ).to.be.true;
if (!firstMessageTime) firstMessageTime = Date.now() / 1000; if (!firstMessageTime) firstMessageTime = Date.now() / 1000;

View File

@ -62,9 +62,9 @@ export interface KeyPair {
} }
export interface WakuRelayMessage { export interface WakuRelayMessage {
payload: string; payload: string; // Hex encoded data string without `0x` prefix.
contentTopic?: string; contentTopic?: string;
timestamp?: number; // Integer in seconds timestamp?: number; // Unix epoch time in nanoseconds as a 64-bits integer value.
} }
export class NimWaku { export class NimWaku {
@ -75,6 +75,27 @@ export class NimWaku {
private readonly logPath: string; private readonly logPath: string;
private rpcPort?: number; private rpcPort?: number;
/**
* Convert a [[WakuMessage]] to a [[WakuRelayMessage]]. The latter is used
* by the nwaku JSON-RPC API.
*/
static toWakuRelayMessage(message: WakuMessage): WakuRelayMessage {
if (!message.payload) {
throw "Attempting to convert empty message";
}
let timestamp;
if (message.proto.timestamp) {
timestamp = message.proto.timestamp.toNumber();
}
return {
payload: bytesToHex(message.payload),
contentTopic: message.contentTopic,
timestamp,
};
}
constructor(logName: string) { constructor(logName: string) {
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`; this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
} }
@ -184,28 +205,14 @@ export class NimWaku {
} }
async sendMessage( async sendMessage(
message: WakuMessage, message: WakuRelayMessage,
pubSubTopic?: string pubSubTopic?: string
): Promise<boolean> { ): Promise<boolean> {
this.checkProcess(); this.checkProcess();
if (!message.payload) {
throw "Attempting to send empty message";
}
let timestamp;
if (message.timestamp) {
timestamp = message.timestamp.valueOf() * 1000;
}
const rpcMessage = {
payload: bytesToHex(message.payload),
contentTopic: message.contentTopic,
timestamp,
};
return this.rpcCall<boolean>("post_waku_v2_relay_v1_message", [ return this.rpcCall<boolean>("post_waku_v2_relay_v1_message", [
pubSubTopic ? pubSubTopic : DefaultPubSubTopic, pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
rpcMessage, message,
]); ]);
} }