Skip to content

Commit b7dc3d7

Browse files
fryorcrakenweboko
andauthored
fix: measure total message size (#1643)
Network message limitations are imposed on the whole message, not just the payload. Co-authored-by: Sasha <118575614+weboko@users.noreply.github.com>
1 parent b3864f8 commit b7dc3d7

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

packages/core/src/lib/light_push/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
SendResult
1212
} from "@waku/interfaces";
1313
import { PushResponse } from "@waku/proto";
14-
import { ensurePubsubTopicIsConfigured, isSizeUnderCap } from "@waku/utils";
14+
import {
15+
ensurePubsubTopicIsConfigured,
16+
isMessageSizeUnderCap
17+
} from "@waku/utils";
1518
import { Logger } from "@waku/utils";
1619
import all from "it-all";
1720
import * as lp from "it-length-prefixed";
@@ -61,7 +64,7 @@ class LightPush extends BaseProtocol implements ILightPush {
6164
return { query: null, error: SendError.EMPTY_PAYLOAD };
6265
}
6366

64-
if (!isSizeUnderCap(message.payload)) {
67+
if (!(await isMessageSizeUnderCap(encoder, message))) {
6568
log.error("Failed to send waku light push: message is bigger than 1MB");
6669
return { query: null, error: SendError.SIZE_TOO_BIG };
6770
}

packages/relay/src/index.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
SendError,
2626
SendResult
2727
} from "@waku/interfaces";
28-
import { isSizeUnderCap, toAsyncIterator } from "@waku/utils";
28+
import { isWireSizeUnderCap, toAsyncIterator } from "@waku/utils";
2929
import { pushOrInitMapSet } from "@waku/utils";
3030
import { Logger } from "@waku/utils";
3131

@@ -112,20 +112,20 @@ class Relay implements IRelay {
112112
};
113113
}
114114

115-
if (!isSizeUnderCap(message.payload)) {
116-
log.error("Failed to send waku relay: message is bigger that 1MB");
115+
const msg = await encoder.toWire(message);
116+
if (!msg) {
117+
log.error("Failed to encode message, aborting publish");
117118
return {
118119
recipients,
119-
errors: [SendError.SIZE_TOO_BIG]
120+
errors: [SendError.ENCODE_FAILED]
120121
};
121122
}
122123

123-
const msg = await encoder.toWire(message);
124-
if (!msg) {
125-
log.error("Failed to encode message, aborting publish");
124+
if (!isWireSizeUnderCap(msg)) {
125+
log.error("Failed to send waku relay: message is bigger that 1MB");
126126
return {
127127
recipients,
128-
errors: [SendError.ENCODE_FAILED]
128+
errors: [SendError.SIZE_TOO_BIG]
129129
};
130130
}
131131

packages/tests/tests/light-push/index.node.spec.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,8 @@ describe("Waku Light Push", function () {
204204
});
205205

206206
it("Push message equal or less that 1MB", async function () {
207-
const oneMbPayload = generateRandomUint8Array(1024 ** 2);
208-
let pushResponse = await waku.lightPush.send(TestEncoder, {
209-
payload: oneMbPayload
210-
});
211-
expect(pushResponse.recipients.length).to.greaterThan(0);
212-
213207
const bigPayload = generateRandomUint8Array(65536);
214-
pushResponse = await waku.lightPush.send(TestEncoder, {
208+
const pushResponse = await waku.lightPush.send(TestEncoder, {
215209
payload: bigPayload
216210
});
217211
expect(pushResponse.recipients.length).to.greaterThan(0);

packages/tests/tests/relay/publish.node.spec.ts

-11
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,6 @@ describe("Waku Relay, Publish", function () {
141141
expect(await messageCollector.waitForMessages(1)).to.eq(false);
142142
});
143143

144-
it("Publish message with size of 1 MB", async function () {
145-
const pushResponse = await waku1.relay.send(TestEncoder, {
146-
payload: generateRandomUint8Array(1024 ** 2)
147-
});
148-
expect(pushResponse.recipients.length).to.eq(1);
149-
expect(pushResponse.recipients[0].toString()).to.eq(
150-
waku2.libp2p.peerId.toString()
151-
);
152-
expect(await messageCollector.waitForMessages(1)).to.eq(true);
153-
});
154-
155144
[1024 ** 2 + 65536, 2 * 1024 ** 2].forEach((testItem) => {
156145
it("Fails to publish message with size larger than 1 MB", async function () {
157146
const pushResponse = await waku1.relay.send(TestEncoder, {
+20-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
import type { IEncoder, IMessage } from "@waku/interfaces";
2+
13
const MB = 1024 ** 2;
2-
const SIZE_CAP = 1; // 1 MB
4+
const SIZE_CAP_IN_MB = 1;
35

4-
export const isSizeUnderCap = (payload: Uint8Array): boolean => {
5-
if (payload.length / MB > SIZE_CAP) {
6-
return false;
7-
}
6+
/**
7+
* Return whether the size of the message is under the upper limit for the network.
8+
* This performs a protobuf encoding! If you have access to the fully encoded message,
9+
* use {@link isSizeUnderCapBuf} instead.
10+
* @param message
11+
* @param encoder
12+
*/
13+
export async function isMessageSizeUnderCap(
14+
encoder: IEncoder,
15+
message: IMessage
16+
): Promise<boolean> {
17+
const buf = await encoder.toWire(message);
18+
if (!buf) return false;
19+
return isWireSizeUnderCap(buf);
20+
}
821

9-
return true;
10-
};
22+
export const isWireSizeUnderCap = (buf: Uint8Array): boolean =>
23+
buf.length / MB <= SIZE_CAP_IN_MB;

0 commit comments

Comments
 (0)