Skip to content

Commit

Permalink
chore: increase meta size to 64 bytes + tests (#1629)
Browse files Browse the repository at this point in the history
  • Loading branch information
alrevuelta authored Mar 30, 2023
1 parent 2ffd2f8 commit 1f79375
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
55 changes: 55 additions & 0 deletions tests/v2/test_waku_message_digest.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{.used.}

import
std/sequtils,
stew/byteutils,
testutils/unittests
import
../../waku/v2/protocol/waku_message,
../../waku/v2/protocol/waku_message/codec,
../../waku/v2/protocol/waku_message/digest,
./testlib/waku2

Expand Down Expand Up @@ -32,6 +35,10 @@ suite "Waku Message - Deterministic hashing":

## Then
check:
byteutils.toHex(pubsubTopic.toBytes()) == "2f77616b752f322f64656661756c742d77616b752f70726f746f"
byteutils.toHex(message.contentTopic.toBytes()) == "2f77616b752f322f64656661756c742d636f6e74656e742f70726f746f"
byteutils.toHex(message.payload) == "010203045445535405060708"
byteutils.toHex(message.meta) == ""
messageHash.toHex() == "87619d05e563521d9126749b45bd4cc2430df0607e77e23572d874ed9c1aaa62"

test "digest computation - meta field (12 bytes)":
Expand All @@ -57,8 +64,41 @@ suite "Waku Message - Deterministic hashing":

## Then
check:
byteutils.toHex(pubsubTopic.toBytes()) == "2f77616b752f322f64656661756c742d77616b752f70726f746f"
byteutils.toHex(message.contentTopic.toBytes()) == "2f77616b752f322f64656661756c742d636f6e74656e742f70726f746f"
byteutils.toHex(message.payload) == "010203045445535405060708"
byteutils.toHex(message.meta) == "73757065722d736563726574"
messageHash.toHex() == "4fdde1099c9f77f6dae8147b6b3179aba1fc8e14a7bf35203fc253ee479f135f"

test "digest computation - meta field (64 bytes)":
## Test vector:
##
## pubsub_topic = 0x2f77616b752f322f64656661756c742d77616b752f70726f746f
## waku_message.payload = 0x010203045445535405060708
## waku_message.content_topic = 0x2f77616b752f322f64656661756c742d636f6e74656e742f70726f746f
## waku_message.meta = 0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f
##
## message_hash = 0xc32ed3b51f0c432be1c7f50880110e1a1a60f6067cd8193ca946909efe1b26ad

## Given
let pubsubTopic = DefaultPubsubTopic # /waku/2/default-waku/proto
let message = fakeWakuMessage(
contentTopic = DefaultContentTopic, # /waku/2/default-content/proto
payload = "\x01\x02\x03\x04TEST\x05\x06\x07\x08".toBytes(),
meta = toSeq(0.byte..63.byte)
)

## When
let messageHash = digest(pubsubTopic, message)

## Then
check:
byteutils.toHex(pubsubTopic.toBytes()) == "2f77616b752f322f64656661756c742d77616b752f70726f746f"
byteutils.toHex(message.contentTopic.toBytes()) == "2f77616b752f322f64656661756c742d636f6e74656e742f70726f746f"
byteutils.toHex(message.payload) == "010203045445535405060708"
byteutils.toHex(message.meta) == "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
messageHash.toHex() == "c32ed3b51f0c432be1c7f50880110e1a1a60f6067cd8193ca946909efe1b26ad"

test "digest computation - zero length payload":
## Test vector:
##
Expand All @@ -83,3 +123,18 @@ suite "Waku Message - Deterministic hashing":
## Then
check:
messageHash.toHex() == "e1a9596237dbe2cc8aaf4b838c46a7052df6bc0d42ba214b998a8bfdbe8487d6"

test "waku message - check meta size is enforced":
# create message with meta size > 64 bytes (invalid)
let message = fakeWakuMessage(
contentTopic = DefaultContentTopic,
payload = "\x01\x02\x03\x04TEST\x05\x06\x07\x08".toBytes(),
meta = toSeq(0.byte..66.byte) # 67 bytes
)

let encodedInvalidMsg = message.encode
let decoded = WakuMessage.decode(encodedInvalidMsg.buffer)

check:
decoded.isErr == true
$decoded.error == "(kind: InvalidLengthField, field: \"meta\")"
2 changes: 1 addition & 1 deletion waku/v2/protocol/waku_message/message.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import
./topics

const
MaxMetaAttrLength* = 32 # 32 bytes
MaxMetaAttrLength* = 64 # 64 bytes

MaxWakuMessageSize* = 1024 * 1024 # 1 Mibytes. Corresponds to PubSub default

Expand Down

0 comments on commit 1f79375

Please sign in to comment.