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;
|
const receivedMsg = await receivedMsgPromise;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue