mirror of https://github.com/waku-org/js-waku.git
Send nanoseconds timestamp in nim-waku RPC
This commit is contained in:
parent
c07396c30b
commit
d95d773f45
|
@ -379,7 +379,7 @@ describe("Waku Relay [node only]", () => {
|
|||
}
|
||||
);
|
||||
|
||||
await nimWaku.sendMessage(message);
|
||||
await nimWaku.sendMessage(NimWaku.toWakuRelayMessage(message));
|
||||
|
||||
const receivedMsg = await receivedMsgPromise;
|
||||
|
||||
|
|
|
@ -40,7 +40,9 @@ describe("Waku Store", () => {
|
|||
for (let i = 0; i < 2; i++) {
|
||||
expect(
|
||||
await nimWaku.sendMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
NimWaku.toWakuRelayMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
)
|
||||
)
|
||||
).to.be.true;
|
||||
}
|
||||
|
@ -70,7 +72,9 @@ describe("Waku Store", () => {
|
|||
for (let i = 0; i < totalMsgs; i++) {
|
||||
expect(
|
||||
await nimWaku.sendMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
NimWaku.toWakuRelayMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
)
|
||||
)
|
||||
).to.be.true;
|
||||
}
|
||||
|
@ -107,7 +111,9 @@ describe("Waku Store", () => {
|
|||
for (let i = 0; i < availMsgs; i++) {
|
||||
expect(
|
||||
await nimWaku.sendMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
NimWaku.toWakuRelayMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
)
|
||||
)
|
||||
).to.be.true;
|
||||
}
|
||||
|
@ -141,7 +147,9 @@ describe("Waku Store", () => {
|
|||
for (let i = 0; i < 15; i++) {
|
||||
expect(
|
||||
await nimWaku.sendMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
NimWaku.toWakuRelayMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
)
|
||||
)
|
||||
).to.be.true;
|
||||
}
|
||||
|
@ -176,7 +184,9 @@ describe("Waku Store", () => {
|
|||
for (let i = 0; i < 2; i++) {
|
||||
expect(
|
||||
await nimWaku.sendMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic),
|
||||
NimWaku.toWakuRelayMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
|
||||
),
|
||||
customPubSubTopic
|
||||
)
|
||||
).to.be.true;
|
||||
|
@ -453,9 +463,11 @@ describe("Waku Store", () => {
|
|||
for (let i = 0; i < 2; i++) {
|
||||
expect(
|
||||
await nimWaku.sendMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic, {
|
||||
timestamp: messageTimestamps[i],
|
||||
})
|
||||
NimWaku.toWakuRelayMessage(
|
||||
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic, {
|
||||
timestamp: messageTimestamps[i],
|
||||
})
|
||||
)
|
||||
)
|
||||
).to.be.true;
|
||||
if (!firstMessageTime) firstMessageTime = Date.now() / 1000;
|
||||
|
|
|
@ -62,9 +62,9 @@ export interface KeyPair {
|
|||
}
|
||||
|
||||
export interface WakuRelayMessage {
|
||||
payload: string;
|
||||
payload: string; // Hex encoded data string without `0x` prefix.
|
||||
contentTopic?: string;
|
||||
timestamp?: number; // Integer in seconds
|
||||
timestamp?: number; // Unix epoch time in nanoseconds as a 64-bits integer value.
|
||||
}
|
||||
|
||||
export class NimWaku {
|
||||
|
@ -75,6 +75,27 @@ export class NimWaku {
|
|||
private readonly logPath: string;
|
||||
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) {
|
||||
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
|
||||
}
|
||||
|
@ -184,28 +205,14 @@ export class NimWaku {
|
|||
}
|
||||
|
||||
async sendMessage(
|
||||
message: WakuMessage,
|
||||
message: WakuRelayMessage,
|
||||
pubSubTopic?: string
|
||||
): Promise<boolean> {
|
||||
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", [
|
||||
pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
|
||||
rpcMessage,
|
||||
message,
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue