mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-08 17:03:09 +00:00
Add timestamp field to the waku message (#450)
* adds timestamp field * unit test for timestamp * adds comments * updates test title
This commit is contained in:
parent
d37a305f21
commit
0ffd17006e
@ -2,6 +2,7 @@
|
|||||||
import
|
import
|
||||||
std/[algorithm, options],
|
std/[algorithm, options],
|
||||||
testutils/unittests, nimcrypto/sha2,
|
testutils/unittests, nimcrypto/sha2,
|
||||||
|
libp2p/protobuf/minprotobuf,
|
||||||
../../waku/v2/protocol/waku_store/waku_store,
|
../../waku/v2/protocol/waku_store/waku_store,
|
||||||
../test_helpers
|
../test_helpers
|
||||||
|
|
||||||
@ -214,3 +215,47 @@ procSuite "pagination":
|
|||||||
newPagingInfo.cursor == pagingInfo.cursor
|
newPagingInfo.cursor == pagingInfo.cursor
|
||||||
newPagingInfo.direction == pagingInfo.direction
|
newPagingInfo.direction == pagingInfo.direction
|
||||||
newPagingInfo.pageSize == 0
|
newPagingInfo.pageSize == 0
|
||||||
|
|
||||||
|
suite "time-window history query":
|
||||||
|
test "Encode/Decode waku message with timestamp":
|
||||||
|
# test encoding and decoding of the timestamp field of a WakuMessage
|
||||||
|
# Encoding
|
||||||
|
let
|
||||||
|
version = 0'u32
|
||||||
|
payload = @[byte 0, 1, 2]
|
||||||
|
proof = @[byte 0, 1, 2, 3]
|
||||||
|
timestamp = float64(10)
|
||||||
|
msg = WakuMessage(payload: payload, version: version, proof: proof, timestamp: timestamp)
|
||||||
|
pb = msg.encode()
|
||||||
|
|
||||||
|
# Decoding
|
||||||
|
let
|
||||||
|
msgDecoded = WakuMessage.init(pb.buffer)
|
||||||
|
check:
|
||||||
|
msgDecoded.isOk()
|
||||||
|
|
||||||
|
let
|
||||||
|
timestampDecoded = msgDecoded.value.timestamp
|
||||||
|
check:
|
||||||
|
timestampDecoded == timestamp
|
||||||
|
test "Encode/Decode waku message without timestamp":
|
||||||
|
# test the encoding and decoding of a WakuMessage with an empty timestamp field
|
||||||
|
|
||||||
|
# Encoding
|
||||||
|
let
|
||||||
|
version = 0'u32
|
||||||
|
payload = @[byte 0, 1, 2]
|
||||||
|
proof = @[byte 0, 1, 2, 3]
|
||||||
|
msg = WakuMessage(payload: payload, version: version, proof: proof)
|
||||||
|
pb = msg.encode()
|
||||||
|
|
||||||
|
# Decoding
|
||||||
|
let
|
||||||
|
msgDecoded = WakuMessage.init(pb.buffer)
|
||||||
|
doAssert:
|
||||||
|
msgDecoded.isOk()
|
||||||
|
|
||||||
|
let
|
||||||
|
timestampDecoded = msgDecoded.value.timestamp
|
||||||
|
check:
|
||||||
|
timestampDecoded == float64(0)
|
||||||
|
|||||||
@ -19,6 +19,8 @@ type
|
|||||||
# the proof field indicates that the message is not a spam
|
# the proof field indicates that the message is not a spam
|
||||||
# this field will be used in the rln-relay protocol
|
# this field will be used in the rln-relay protocol
|
||||||
proof*: seq[byte]
|
proof*: seq[byte]
|
||||||
|
# sender generated timestamp
|
||||||
|
timestamp*: float64
|
||||||
|
|
||||||
# Encoding and decoding -------------------------------------------------------
|
# Encoding and decoding -------------------------------------------------------
|
||||||
proc init*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
|
proc init*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
|
||||||
@ -29,6 +31,7 @@ proc init*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
|
|||||||
discard ? pb.getField(2, msg.contentTopic)
|
discard ? pb.getField(2, msg.contentTopic)
|
||||||
discard ? pb.getField(3, msg.version)
|
discard ? pb.getField(3, msg.version)
|
||||||
discard ? pb.getField(4, msg.proof)
|
discard ? pb.getField(4, msg.proof)
|
||||||
|
discard ? pb.getField(5, msg.timestamp)
|
||||||
|
|
||||||
ok(msg)
|
ok(msg)
|
||||||
|
|
||||||
@ -39,3 +42,4 @@ proc encode*(message: WakuMessage): ProtoBuffer =
|
|||||||
result.write(2, message.contentTopic)
|
result.write(2, message.contentTopic)
|
||||||
result.write(3, message.version)
|
result.write(3, message.version)
|
||||||
result.write(4, message.proof)
|
result.write(4, message.proof)
|
||||||
|
result.write(5, message.timestamp)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user