Skip to content

fix(store): update store query validation logic to support msg hash q… #2397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions packages/core/src/lib/message_hash/message_hash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IDecodedMessage, IProtoMessage } from "@waku/interfaces";
import type { IProtoMessage } from "@waku/interfaces";
import { bytesToHex, hexToBytes } from "@waku/utils/bytes";
import { expect } from "chai";

Expand Down Expand Up @@ -93,19 +93,20 @@ describe("Message Hash: RFC Test Vectors", () => {
expect(bytesToHex(hash)).to.equal(expectedHash);
});

it("Waku message hash computation (message is IDecodedMessage)", () => {
it("Waku message hash computation (message is IProtoMessage with version)", () => {
const expectedHash =
"3f11bc950dce0e3ffdcf205ae6414c01130bb5d9f20644869bff80407fa52c8f";
const pubsubTopic = "/waku/2/default-waku/proto";
const message: IDecodedMessage = {
version: 0,
const message: IProtoMessage = {
payload: new Uint8Array(),
pubsubTopic,
contentTopic: "/waku/2/default-content/proto",
meta: hexToBytes("0x73757065722d736563726574"),
timestamp: new Date("2024-04-30T10:54:14.978Z"),
timestamp:
BigInt(new Date("2024-04-30T10:54:14.978Z").getTime()) *
BigInt(1000000),
ephemeral: undefined,
rateLimitProof: undefined
rateLimitProof: undefined,
version: 0
};
const hash = messageHash(pubsubTopic, message);
expect(bytesToHex(hash)).to.equal(expectedHash);
Expand Down Expand Up @@ -144,16 +145,17 @@ describe("messageHash and messageHashStr", () => {
expect(hashStr).to.equal(hashStrFromBytes);
});

it("messageHashStr works with IDecodedMessage", () => {
const decodedMessage: IDecodedMessage = {
version: 0,
it("messageHashStr works with IProtoMessage", () => {
const decodedMessage: IProtoMessage = {
payload: new Uint8Array([1, 2, 3, 4]),
pubsubTopic,
contentTopic: "/waku/2/default-content/proto",
meta: new Uint8Array([5, 6, 7, 8]),
timestamp: new Date("2024-04-30T10:54:14.978Z"),
timestamp:
BigInt(new Date("2024-04-30T10:54:14.978Z").getTime()) *
BigInt(1000000),
ephemeral: undefined,
rateLimitProof: undefined
rateLimitProof: undefined,
version: 0
};

const hashStr = messageHashStr(pubsubTopic, decodedMessage);
Expand Down
93 changes: 93 additions & 0 deletions packages/core/src/lib/store/rpc.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { expect } from "chai";

import { StoreQueryRequest } from "./rpc.js";

describe("StoreQueryRequest validation", () => {
it("accepts valid content-filtered query", () => {
const request = StoreQueryRequest.create({
pubsubTopic: "/waku/2/default-waku/proto",
contentTopics: ["/test/1/content/proto"],
includeData: true,
paginationForward: true
});
expect(request).to.exist;
});

it("rejects content-filtered query with only pubsubTopic", () => {
expect(() =>
StoreQueryRequest.create({
pubsubTopic: "/waku/2/default-waku/proto",
contentTopics: [],
includeData: true,
paginationForward: true
})
).to.throw(
"Both pubsubTopic and contentTopics must be set together for content-filtered queries"
);
});

it("rejects content-filtered query with only contentTopics", () => {
expect(() =>
StoreQueryRequest.create({
pubsubTopic: "",
contentTopics: ["/test/1/content/proto"],
includeData: true,
paginationForward: true
})
).to.throw(
"Both pubsubTopic and contentTopics must be set together for content-filtered queries"
);
});

it("accepts valid message hash query", () => {
const request = StoreQueryRequest.create({
pubsubTopic: "",
contentTopics: [],
messageHashes: [new Uint8Array([1, 2, 3, 4])],
includeData: true,
paginationForward: true
});
expect(request).to.exist;
});

it("rejects hash query with content filter parameters", () => {
expect(() =>
StoreQueryRequest.create({
messageHashes: [new Uint8Array([1, 2, 3, 4])],
pubsubTopic: "/waku/2/default-waku/proto",
contentTopics: ["/test/1/content/proto"],
includeData: true,
paginationForward: true
})
).to.throw(
"Message hash lookup queries cannot include content filter criteria"
);
});

it("rejects hash query with time filter", () => {
expect(() =>
StoreQueryRequest.create({
pubsubTopic: "",
contentTopics: [],
messageHashes: [new Uint8Array([1, 2, 3, 4])],
timeStart: new Date(),
includeData: true,
paginationForward: true
})
).to.throw(
"Message hash lookup queries cannot include content filter criteria"
);
});

it("accepts time-filtered query with content filter", () => {
const request = StoreQueryRequest.create({
pubsubTopic: "/waku/2/default-waku/proto",
contentTopics: ["/test/1/content/proto"],
timeStart: new Date(Date.now() - 3600000),
timeEnd: new Date(),
includeData: true,
paginationForward: true
});
expect(request).to.exist;
});
});
42 changes: 23 additions & 19 deletions packages/core/src/lib/store/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class StoreQueryRequest {
public static create(params: QueryRequestParams): StoreQueryRequest {
const request = new StoreQueryRequest({
...params,
contentTopics: params.contentTopics || [],
requestId: uuid(),
timeStart: params.timeStart
? BigInt(params.timeStart.getTime() * ONE_MILLION)
Expand All @@ -27,26 +28,29 @@ export class StoreQueryRequest {
: undefined
});

// Validate request parameters based on RFC
if (
(params.pubsubTopic && !params.contentTopics) ||
(!params.pubsubTopic && params.contentTopics)
) {
throw new Error(
"Both pubsubTopic and contentTopics must be set or unset"
);
}
const isHashQuery = params.messageHashes && params.messageHashes.length > 0;
const hasContentTopics =
params.contentTopics && params.contentTopics.length > 0;
const hasTimeFilter = params.timeStart || params.timeEnd;

if (
params.messageHashes &&
(params.pubsubTopic ||
params.contentTopics ||
params.timeStart ||
params.timeEnd)
) {
throw new Error(
"Message hash lookup queries cannot include content filter criteria"
);
if (isHashQuery) {
if (hasContentTopics || hasTimeFilter) {
throw new Error(
"Message hash lookup queries cannot include content filter criteria (contentTopics, timeStart, or timeEnd)"
);
}
} else {
if (
(params.pubsubTopic &&
(!params.contentTopics || params.contentTopics.length === 0)) ||
(!params.pubsubTopic &&
params.contentTopics &&
params.contentTopics.length > 0)
) {
throw new Error(
"Both pubsubTopic and contentTopics must be set together for content-filtered queries"
);
}
}

return request;
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/lib/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
decoders: Map<string, IDecoder<T>>,
peerId: PeerId
): AsyncGenerator<Promise<T | undefined>[]> {
// Only validate decoder content topics for content-filtered queries
const isHashQuery =
queryOpts.messageHashes && queryOpts.messageHashes.length > 0;
if (
!isHashQuery &&
queryOpts.contentTopics &&
queryOpts.contentTopics.toString() !==
Array.from(decoders.keys()).toString()
Array.from(decoders.keys()).toString()
) {
throw new Error(
"Internal error, the decoders should match the query's content topics"
Expand All @@ -56,6 +61,13 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
paginationCursor: currentCursor
});

log.info("Sending store query request:", {
hasMessageHashes: !!queryOpts.messageHashes?.length,
messageHashCount: queryOpts.messageHashes?.length,
pubsubTopic: queryOpts.pubsubTopic,
contentTopics: queryOpts.contentTopics
});

let stream;
try {
stream = await this.getStream(peerId);
Expand Down
28 changes: 25 additions & 3 deletions packages/sdk/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,32 @@ export class Store implements IStore {
decoders: IDecoder<T>[],
options?: Partial<QueryRequestParams>
): AsyncGenerator<Promise<T | undefined>[]> {
const { pubsubTopic, contentTopics, decodersAsMap } =
this.validateDecodersAndPubsubTopic(decoders);
// For message hash queries, don't validate decoders but still need decodersAsMap
const isHashQuery =
options?.messageHashes && options.messageHashes.length > 0;

let pubsubTopic: string;
let contentTopics: string[];
let decodersAsMap: Map<string, IDecoder<T>>;

if (isHashQuery) {
// For hash queries, we still need decoders to decode messages
// but we don't validate pubsubTopic consistency
// Use pubsubTopic from options if provided, otherwise from first decoder
pubsubTopic = options.pubsubTopic || decoders[0]?.pubsubTopic || "";
contentTopics = [];
decodersAsMap = new Map();
decoders.forEach((dec) => {
decodersAsMap.set(dec.contentTopic, dec);
});
} else {
const validated = this.validateDecodersAndPubsubTopic(decoders);
pubsubTopic = validated.pubsubTopic;
contentTopics = validated.contentTopics;
decodersAsMap = validated.decodersAsMap;
}

const queryOpts = {
const queryOpts: QueryRequestParams = {
pubsubTopic,
contentTopics,
includeData: true,
Expand Down
1 change: 1 addition & 0 deletions packages/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@waku/core": "*",
"@waku/enr": "*",
"@waku/interfaces": "*",
"@waku/message-hash": "^0.1.17",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's merge this PR after #2399 so that you can consume messageHash from core.

"@waku/utils": "*",
"app-root-path": "^3.1.0",
"chai-as-promised": "^7.1.1",
Expand Down
Loading
Loading