refactor(message): split waku message module into rpc and codec

This commit is contained in:
Lorenzo Delgado 2023-02-08 16:16:21 +01:00 committed by GitHub
parent 00f4ce4cbc
commit 1a9f633311
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 81 deletions

View File

@ -1,83 +1,7 @@
## Waku Message module.
##
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-message.md
## for spec.
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import
../../common/protobuf,
../utils/time
./waku_message/message,
./waku_message/codec
const MaxWakuMessageSize* = 1024 * 1024 # In bytes. Corresponds to PubSub default
type
PubsubTopic* = string
ContentTopic* = string
const
DefaultPubsubTopic*: PubsubTopic = PubsubTopic("/waku/2/default-waku/proto")
DefaultContentTopic*: ContentTopic = ContentTopic("/waku/2/default-content/proto")
type WakuMessage* = object
payload*: seq[byte]
contentTopic*: ContentTopic
version*: uint32
# sender generated timestamp
timestamp*: Timestamp
# the proof field indicates that the message is not a spam
# this field will be used in the rln-relay protocol
# XXX Experimental, this is part of https://rfc.vac.dev/spec/17/ spec and not yet part of WakuMessage spec
when defined(rln):
proof*: seq[byte]
# The ephemeral field indicates if the message should
# be stored. bools and uints are
# equivalent in serialization of the protobuf
ephemeral*: bool
## Encoding and decoding
proc encode*(message: WakuMessage): ProtoBuffer =
var buf = initProtoBuffer()
buf.write3(1, message.payload)
buf.write3(2, message.contentTopic)
buf.write3(3, message.version)
buf.write3(10, zint64(message.timestamp))
when defined(rln):
buf.write3(21, message.proof)
buf.write3(31, uint64(message.ephemeral))
buf.finish3()
buf
proc decode*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
var msg = WakuMessage(ephemeral: false)
let pb = initProtoBuffer(buffer)
discard ?pb.getField(1, msg.payload)
discard ?pb.getField(2, msg.contentTopic)
discard ?pb.getField(3, msg.version)
var timestamp: zint64
discard ?pb.getField(10, timestamp)
msg.timestamp = Timestamp(timestamp)
# Experimental: this is part of https://rfc.vac.dev/spec/17/ spec
when defined(rln):
var proofBytes: seq[byte]
if ?pb.getField(21, proofBytes):
msg.proof = proofBytes
var ephemeral: uint
if ?pb.getField(31, ephemeral):
msg.ephemeral = bool(ephemeral)
ok(msg)
export
message,
codec

View File

@ -0,0 +1,73 @@
## Waku Message module: encoding and decoding
# See:
# - RFC 14: https://rfc.vac.dev/spec/14/
# - Proto definition: https://github.com/vacp2p/waku/blob/main/waku/message/v1/message.proto
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import
../../../common/protobuf,
../../utils/time,
./message
proc encode*(message: WakuMessage): ProtoBuffer =
var buf = initProtoBuffer()
buf.write3(1, message.payload)
buf.write3(2, message.contentTopic)
buf.write3(3, message.version)
buf.write3(10, zint64(message.timestamp))
when defined(rln):
buf.write3(21, message.proof)
buf.write3(31, message.ephemeral)
buf.finish3()
buf
proc decode*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
var msg = WakuMessage(ephemeral: false)
let pb = initProtoBuffer(buffer)
var payload: seq[byte]
if not ?pb.getField(1, payload):
return err(ProtoError.RequiredFieldMissing)
else:
msg.payload = payload
var topic: ContentTopic
if not ?pb.getField(2, topic):
return err(ProtoError.RequiredFieldMissing)
else:
msg.contentTopic = topic
var version: uint32
if not ?pb.getField(3, version):
msg.version = 0
else:
msg.version = version
var timestamp: zint64
if not ?pb.getField(10, timestamp):
msg.timestamp = Timestamp(0)
else:
msg.timestamp = Timestamp(timestamp)
# Experimental: this is part of https://rfc.vac.dev/spec/17/ spec
when defined(rln):
var proof: seq[byte]
if not ?pb.getField(21, proof):
msg.proof = @[]
else:
msg.proof = proof
var ephemeral: uint
if not ?pb.getField(31, ephemeral):
msg.ephemeral = false
else:
msg.ephemeral = bool(ephemeral)
ok(msg)

View File

@ -0,0 +1,44 @@
## Waku Message module.
##
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-message.md
## for spec.
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import
../../utils/time
const MaxWakuMessageSize* = 1024 * 1024 # In bytes. Corresponds to PubSub default
type
PubsubTopic* = string
ContentTopic* = string
const
DefaultPubsubTopic*: PubsubTopic = PubsubTopic("/waku/2/default-waku/proto")
DefaultContentTopic*: ContentTopic = ContentTopic("/waku/2/default-content/proto")
type WakuMessage* = object
# Data payload transmitted.
payload*: seq[byte]
# String identifier that can be used for content-based filtering.
contentTopic*: ContentTopic
# Number to discriminate different types of payload encryption.
# Compatibility with Whisper/WakuV1.
version*: uint32
# Sender generated timestamp. Deprecated. Superseded by `meta` attribute.
timestamp*: Timestamp
# The ephemeral attribute indicates signifies the transient nature of the
# message (if the message should be stored).
ephemeral*: bool
# EXPERIMENTAL: Part of RFC 17: https://rfc.vac.dev/spec/17/
when defined(rln):
# The proof attribute indicates that the message is not a spam. This
# attribute will be used in the rln-relay protocol.
proof*: seq[byte]