Skip to content

Commit 5850253

Browse files
committed
fix(store): fix validation to allow store query by hash
1 parent c55c694 commit 5850253

File tree

6 files changed

+104
-31
lines changed

6 files changed

+104
-31
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/src/lib/store/rpc.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class StoreQueryRequest {
1414
public static create(params: QueryRequestParams): StoreQueryRequest {
1515
const request = new StoreQueryRequest({
1616
...params,
17+
contentTopics: params.contentTopics || [],
1718
requestId: uuid(),
1819
timeStart: params.timeStart
1920
? BigInt(params.timeStart.getTime() * ONE_MILLION)
@@ -28,26 +29,26 @@ export class StoreQueryRequest {
2829
});
2930

3031
// Validate request parameters based on RFC
31-
if (
32-
(params.pubsubTopic && !params.contentTopics) ||
33-
(!params.pubsubTopic && params.contentTopics)
34-
) {
35-
throw new Error(
36-
"Both pubsubTopic and contentTopics must be set or unset"
37-
);
38-
}
32+
// if (
33+
// (params.pubsubTopic && !params.contentTopics) ||
34+
// (!params.pubsubTopic && params.contentTopics)
35+
// ) {
36+
// throw new Error(
37+
// "Both pubsubTopic and contentTopics must be set or unset"
38+
// );
39+
// }
3940

40-
if (
41-
params.messageHashes &&
42-
(params.pubsubTopic ||
43-
params.contentTopics ||
44-
params.timeStart ||
45-
params.timeEnd)
46-
) {
47-
throw new Error(
48-
"Message hash lookup queries cannot include content filter criteria"
49-
);
50-
}
41+
// if (
42+
// params.messageHashes &&
43+
// (params.pubsubTopic ||
44+
// (params.contentTopics && params.contentTopics.length > 0) ||
45+
// params.timeStart ||
46+
// params.timeEnd)
47+
// ) {
48+
// throw new Error(
49+
// "Message hash lookup queries cannot include content filter criteria"
50+
// );
51+
// }
5152

5253
return request;
5354
}

packages/core/src/lib/store/store.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
4141
peerId: PeerId
4242
): AsyncGenerator<Promise<T | undefined>[]> {
4343
if (
44+
queryOpts.contentTopics &&
4445
queryOpts.contentTopics.toString() !==
45-
Array.from(decoders.keys()).toString()
46+
Array.from(decoders.keys()).toString()
4647
) {
4748
throw new Error(
4849
"Internal error, the decoders should match the query's content topics"

packages/tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@waku/core": "*",
5656
"@waku/enr": "*",
5757
"@waku/interfaces": "*",
58+
"@waku/message-hash": "^0.1.17",
5859
"@waku/utils": "*",
5960
"app-root-path": "^3.1.0",
6061
"chai-as-promised": "^7.1.1",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { DecodedMessage } from "@waku/core";
2+
import type { LightNode } from "@waku/interfaces";
3+
import { messageHash } from "@waku/message-hash";
4+
import { assert } from "chai";
5+
6+
import {
7+
afterEachCustom,
8+
beforeEachCustom,
9+
ServiceNode,
10+
tearDownNodes
11+
} from "../../src/index.js";
12+
13+
import {
14+
runStoreNodes,
15+
sendMessages,
16+
TestDecoder,
17+
TestShardInfo,
18+
totalMsgs
19+
} from "./utils.js";
20+
21+
describe("Waku Store, message hash query", function () {
22+
this.timeout(15000);
23+
let waku: LightNode;
24+
let nwaku: ServiceNode;
25+
26+
beforeEachCustom(this, async () => {
27+
[nwaku, waku] = await runStoreNodes(this.ctx, TestShardInfo);
28+
});
29+
30+
afterEachCustom(this, async () => {
31+
await tearDownNodes(nwaku, [waku]);
32+
});
33+
34+
it("can query messages by message hash", async function () {
35+
const sentMessages = await sendMessages(
36+
nwaku,
37+
totalMsgs,
38+
TestDecoder.contentTopic,
39+
TestDecoder.pubsubTopic,
40+
true
41+
);
42+
const messageHashes = sentMessages.map((msg) =>
43+
messageHash(TestDecoder.pubsubTopic, {
44+
pubsubTopic: TestDecoder.pubsubTopic,
45+
payload: Buffer.from(msg.payload, "base64"),
46+
contentTopic: TestDecoder.contentTopic,
47+
timestamp: msg.timestamp
48+
? new Date(Number(msg.timestamp / 1000000n))
49+
: undefined,
50+
meta: undefined,
51+
rateLimitProof: undefined,
52+
ephemeral: undefined
53+
})
54+
);
55+
const messages: DecodedMessage[] = [];
56+
for await (const page of waku.store.queryGenerator([TestDecoder], {
57+
messageHashes
58+
})) {
59+
for await (const msg of page) {
60+
messages.push(msg as DecodedMessage);
61+
}
62+
}
63+
assert.equal(messages.length, messageHashes.length);
64+
for (const msg of messages) {
65+
assert.equal(msg.contentTopic, TestDecoder.contentTopic);
66+
}
67+
});
68+
});

packages/tests/tests/store/utils.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { expect } from "chai";
1717
import { Context } from "mocha";
1818

1919
import { delay, NOISE_KEY_1, runNodes, ServiceNode } from "../../src/index.js";
20+
import { MessageRpcQuery } from "../../src/types.js";
2021

2122
export const log = new Logger("test:store");
2223

@@ -49,20 +50,20 @@ export async function sendMessages(
4950
instance: ServiceNode,
5051
numMessages: number,
5152
contentTopic: string,
52-
pubsubTopic: string
53-
): Promise<void> {
53+
pubsubTopic: string,
54+
timestamp: boolean = false
55+
): Promise<MessageRpcQuery[]> {
56+
const messages: MessageRpcQuery[] = new Array<MessageRpcQuery>(numMessages);
5457
for (let i = 0; i < numMessages; i++) {
55-
expect(
56-
await instance.sendMessage(
57-
ServiceNode.toMessageRpcQuery({
58-
payload: new Uint8Array([i]),
59-
contentTopic: contentTopic
60-
}),
61-
pubsubTopic
62-
)
63-
).to.eq(true);
58+
messages[i] = ServiceNode.toMessageRpcQuery({
59+
payload: new Uint8Array([i]),
60+
contentTopic: contentTopic,
61+
timestamp: timestamp ? new Date() : undefined
62+
});
63+
expect(await instance.sendMessage(messages[i], pubsubTopic)).to.eq(true);
6464
await delay(1); // to ensure each timestamp is unique.
6565
}
66+
return messages;
6667
}
6768

6869
export async function sendMessagesAutosharding(

0 commit comments

Comments
 (0)