stop range queries on messages with a causal history

This commit is contained in:
fryorcraken 2025-10-01 16:00:22 +10:00
parent 603c4af5a8
commit 1ce5cafc1c
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 51 additions and 23 deletions

View File

@ -823,7 +823,7 @@ describe("Reliable Channel", () => {
"msg1",
channelId,
senderId,
[],
[{ messageId: "previous-msg-id" }],
1,
undefined,
utf8ToBytes("content message")
@ -1002,7 +1002,7 @@ describe("Reliable Channel", () => {
});
});
describe("isSyncOrContentMessage predicate", () => {
describe("isChannelMessageWithCausalHistory predicate", () => {
let mockWakuNode: MockWakuNode;
let reliableChannel: ReliableChannel<IDecodedMessage>;
let encoder: IEncoder;
@ -1031,17 +1031,8 @@ describe("Reliable Channel", () => {
payload: new Uint8Array([1, 2, 3])
} as IDecodedMessage;
// SDS Message decode throws on malformed payloads, so this will return false
// because decode returns undefined on error which is caught by null check
// However, in current implementation it throws, so we check the behavior
try {
const result = reliableChannel["isSyncOrContentMessage"](msg);
expect(result).to.be.false;
} catch (error) {
// If decode throws, the predicate should ideally handle it
// Current implementation doesn't catch, so we expect the throw
expect(error).to.not.be.undefined;
}
const result = reliableChannel["isChannelMessageWithCausalHistory"](msg);
expect(result).to.be.false;
});
it("should return false for different channelId", () => {
@ -1059,11 +1050,11 @@ describe("Reliable Channel", () => {
payload: sdsMsg.encode()
} as IDecodedMessage;
const result = reliableChannel["isSyncOrContentMessage"](msg);
const result = reliableChannel["isChannelMessageWithCausalHistory"](msg);
expect(result).to.be.false;
});
it("should return true for matching sync message", () => {
it("should return false for sync message without causal history", () => {
const syncMsg = new SyncMessage(
"sync-msg-id",
"testChannel",
@ -1078,11 +1069,11 @@ describe("Reliable Channel", () => {
payload: syncMsg.encode()
} as IDecodedMessage;
const result = reliableChannel["isSyncOrContentMessage"](msg);
expect(result).to.be.true;
const result = reliableChannel["isChannelMessageWithCausalHistory"](msg);
expect(result).to.be.false;
});
it("should return true for matching content message", () => {
it("should return false for content message without causal history", () => {
const contentMsg = new ContentMessage(
"msg1",
"testChannel",
@ -1097,7 +1088,45 @@ describe("Reliable Channel", () => {
payload: contentMsg.encode()
} as IDecodedMessage;
const result = reliableChannel["isSyncOrContentMessage"](msg);
const result = reliableChannel["isChannelMessageWithCausalHistory"](msg);
expect(result).to.be.false;
});
it("should return true for message with causal history", () => {
const contentMsg = new ContentMessage(
"msg1",
"testChannel",
"sender",
[{ messageId: "previous-msg-id" }],
1,
undefined,
utf8ToBytes("content")
);
const msg = {
payload: contentMsg.encode()
} as IDecodedMessage;
const result = reliableChannel["isChannelMessageWithCausalHistory"](msg);
expect(result).to.be.true;
});
it("should return true for sync message with causal history", () => {
const syncMsg = new SyncMessage(
"sync-msg-id",
"testChannel",
"sender",
[{ messageId: "previous-msg-id" }],
1,
undefined,
undefined
);
const msg = {
payload: syncMsg.encode()
} as IDecodedMessage;
const result = reliableChannel["isChannelMessageWithCausalHistory"](msg);
expect(result).to.be.true;
});
});

View File

@ -15,7 +15,6 @@ import {
import {
type ChannelId,
isContentMessage,
isSyncMessage,
MessageChannel,
MessageChannelEvent,
type MessageChannelOptions,
@ -188,7 +187,7 @@ export class ReliableChannel<
) {
this.queryOnConnect = new QueryOnConnect(
[this.decoder],
this.isSyncOrContentMessage.bind(this),
this.isChannelMessageWithCausalHistory.bind(this),
peerManagerEvents,
node.events,
this._retrieve.bind(this)
@ -581,7 +580,7 @@ export class ReliableChannel<
this.messageChannel.sweepOutgoingBuffer();
}
private isSyncOrContentMessage(msg: T): boolean {
private isChannelMessageWithCausalHistory(msg: T): boolean {
// TODO: we do end-up decoding messages twice as this is used to stop store queries.
const sdsMessage = SdsMessage.decode(msg.payload);
@ -593,7 +592,7 @@ export class ReliableChannel<
return false;
}
return isSyncMessage(sdsMessage) || isContentMessage(sdsMessage);
return sdsMessage.causalHistory && sdsMessage.causalHistory.length > 0;
}
private setupEventListeners(): void {