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:
Sanaz Taheri Boshrooyeh 2021-04-02 14:28:46 -07:00 committed by GitHub
parent 683577f045
commit 192c2c7ea0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -2,6 +2,7 @@
import
std/[algorithm, options],
testutils/unittests, nimcrypto/sha2,
libp2p/protobuf/minprotobuf,
../../waku/v2/protocol/waku_store/waku_store,
../test_helpers
@ -214,3 +215,47 @@ procSuite "pagination":
newPagingInfo.cursor == pagingInfo.cursor
newPagingInfo.direction == pagingInfo.direction
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)

View File

@ -19,6 +19,8 @@ type
# the proof field indicates that the message is not a spam
# this field will be used in the rln-relay protocol
proof*: seq[byte]
# sender generated timestamp
timestamp*: float64
# Encoding and decoding -------------------------------------------------------
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(3, msg.version)
discard ? pb.getField(4, msg.proof)
discard ? pb.getField(5, msg.timestamp)
ok(msg)
@ -39,3 +42,4 @@ proc encode*(message: WakuMessage): ProtoBuffer =
result.write(2, message.contentTopic)
result.write(3, message.version)
result.write(4, message.proof)
result.write(5, message.timestamp)