2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
import options
|
2021-04-18 08:08:33 +00:00
|
|
|
import stew/assign2
|
2019-12-06 02:16:18 +00:00
|
|
|
import chronicles
|
|
|
|
import messages,
|
2020-07-01 06:25:09 +00:00
|
|
|
../../../peerid,
|
2020-05-06 16:31:47 +00:00
|
|
|
../../../utility,
|
|
|
|
../../../protobuf/minprotobuf
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-09-24 16:43:20 +00:00
|
|
|
|
2020-09-15 06:03:53 +00:00
|
|
|
logScope:
|
2022-11-04 10:19:27 +00:00
|
|
|
topics = "libp2p pubsubprotobuf"
|
2020-09-15 06:03:53 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
import metrics
|
|
|
|
|
|
|
|
declareCounter(libp2p_pubsub_rpc_bytes_read, "pubsub rpc bytes read", labels = ["kind"])
|
|
|
|
declareCounter(libp2p_pubsub_rpc_bytes_write, "pubsub rpc bytes write", labels = ["kind"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, graft: ControlGraft) =
|
|
|
|
var ipb = initProtoBuffer()
|
2024-03-25 11:06:34 +00:00
|
|
|
ipb.write(1, graft.topicID)
|
2020-07-13 12:43:07 +00:00
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_write.inc(ipb.getLen().int64, labelValues = ["graft"])
|
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, infoMsg: PeerInfoMsg) =
|
|
|
|
var ipb = initProtoBuffer()
|
2021-12-16 10:05:20 +00:00
|
|
|
ipb.write(1, infoMsg.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
ipb.write(2, infoMsg.signedPeerRecord)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, prune: ControlPrune) =
|
|
|
|
var ipb = initProtoBuffer()
|
2024-03-25 11:06:34 +00:00
|
|
|
ipb.write(1, prune.topicID)
|
2020-09-21 09:16:29 +00:00
|
|
|
for peer in prune.peers:
|
|
|
|
ipb.write(2, peer)
|
|
|
|
ipb.write(3, prune.backoff)
|
2020-07-13 12:43:07 +00:00
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_write.inc(ipb.getLen().int64, labelValues = ["prune"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, ihave: ControlIHave) =
|
|
|
|
var ipb = initProtoBuffer()
|
2024-03-25 11:06:34 +00:00
|
|
|
ipb.write(1, ihave.topicID)
|
|
|
|
for mid in ihave.messageIDs:
|
2020-07-13 12:43:07 +00:00
|
|
|
ipb.write(2, mid)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_write.inc(ipb.getLen().int64, labelValues = ["ihave"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, iwant: ControlIWant) =
|
|
|
|
var ipb = initProtoBuffer()
|
2024-03-25 11:06:34 +00:00
|
|
|
for mid in iwant.messageIDs:
|
2020-07-13 12:43:07 +00:00
|
|
|
ipb.write(1, mid)
|
|
|
|
if len(ipb.buffer) > 0:
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_write.inc(ipb.getLen().int64, labelValues = ["iwant"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, control: ControlMessage) =
|
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
for ihave in control.ihave:
|
|
|
|
ipb.write(1, ihave)
|
|
|
|
for iwant in control.iwant:
|
|
|
|
ipb.write(2, iwant)
|
|
|
|
for graft in control.graft:
|
|
|
|
ipb.write(3, graft)
|
|
|
|
for prune in control.prune:
|
|
|
|
ipb.write(4, prune)
|
2023-07-28 08:58:05 +00:00
|
|
|
for idontwant in control.idontwant:
|
|
|
|
ipb.write(5, idontwant)
|
2020-07-13 12:43:07 +00:00
|
|
|
if len(ipb.buffer) > 0:
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
|
|
|
proc write*(pb: var ProtoBuffer, field: int, subs: SubOpts) =
|
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
ipb.write(1, uint64(subs.subscribe))
|
|
|
|
ipb.write(2, subs.topic)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_write.inc(ipb.getLen().int64, labelValues = ["subs"])
|
|
|
|
|
2020-09-25 16:39:34 +00:00
|
|
|
proc encodeMessage*(msg: Message, anonymize: bool): seq[byte] =
|
2020-07-13 12:43:07 +00:00
|
|
|
var pb = initProtoBuffer()
|
2020-09-25 16:39:34 +00:00
|
|
|
if len(msg.fromPeer) > 0 and not anonymize:
|
2020-09-15 10:33:18 +00:00
|
|
|
pb.write(1, msg.fromPeer)
|
2020-07-13 12:43:07 +00:00
|
|
|
pb.write(2, msg.data)
|
2020-09-25 16:39:34 +00:00
|
|
|
if len(msg.seqno) > 0 and not anonymize:
|
2020-09-15 10:33:18 +00:00
|
|
|
pb.write(3, msg.seqno)
|
2024-03-25 11:06:34 +00:00
|
|
|
pb.write(4, msg.topic)
|
2020-09-25 16:39:34 +00:00
|
|
|
if len(msg.signature) > 0 and not anonymize:
|
2020-07-13 12:43:07 +00:00
|
|
|
pb.write(5, msg.signature)
|
2020-09-25 16:39:34 +00:00
|
|
|
if len(msg.key) > 0 and not anonymize:
|
2020-07-13 12:43:07 +00:00
|
|
|
pb.write(6, msg.key)
|
2019-12-06 02:16:18 +00:00
|
|
|
pb.finish()
|
2021-01-08 05:21:24 +00:00
|
|
|
|
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_write.inc(pb.getLen().int64, labelValues = ["message"])
|
2021-04-18 08:08:33 +00:00
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
pb.buffer
|
|
|
|
|
2020-09-25 16:39:34 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, msg: Message, anonymize: bool) =
|
|
|
|
pb.write(field, encodeMessage(msg, anonymize))
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeGraft*(pb: ProtoBuffer): ProtoResult[ControlGraft] {.
|
|
|
|
inline.} =
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["graft"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeGraft: decoding message"
|
|
|
|
var control = ControlGraft()
|
2024-03-25 11:06:34 +00:00
|
|
|
if ? pb.getField(1, control.topicID):
|
|
|
|
trace "decodeGraft: read topicID", topicID = control.topicID
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
2024-03-25 11:06:34 +00:00
|
|
|
trace "decodeGraft: topicID is missing"
|
2020-07-15 08:25:39 +00:00
|
|
|
ok(control)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-10-09 00:12:38 +00:00
|
|
|
proc decodePeerInfoMsg*(pb: ProtoBuffer): ProtoResult[PeerInfoMsg] {.
|
|
|
|
inline.} =
|
|
|
|
trace "decodePeerInfoMsg: decoding message"
|
|
|
|
var pi = PeerInfoMsg()
|
2021-12-16 10:05:20 +00:00
|
|
|
if ? pb.getField(1, pi.peerId):
|
|
|
|
trace "decodePeerInfoMsg: read peerId", peerId = pi.peerId
|
2020-10-09 00:12:38 +00:00
|
|
|
else:
|
2021-12-16 10:05:20 +00:00
|
|
|
trace "decodePeerInfoMsg: peerId is missing"
|
2020-10-09 00:12:38 +00:00
|
|
|
if ? pb.getField(2, pi.signedPeerRecord):
|
|
|
|
trace "decodePeerInfoMsg: read signedPeerRecord", signedPeerRecord = pi.signedPeerRecord
|
|
|
|
else:
|
|
|
|
trace "decodePeerInfoMsg: signedPeerRecord is missing"
|
|
|
|
ok(pi)
|
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodePrune*(pb: ProtoBuffer): ProtoResult[ControlPrune] {.
|
|
|
|
inline.} =
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["prune"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodePrune: decoding message"
|
|
|
|
var control = ControlPrune()
|
2024-03-25 11:06:34 +00:00
|
|
|
if ? pb.getField(1, control.topicID):
|
|
|
|
trace "decodePrune: read topicID", topic = control.topicID
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
2024-03-25 11:06:34 +00:00
|
|
|
trace "decodePrune: topicID is missing"
|
2020-10-09 00:12:38 +00:00
|
|
|
var bpeers: seq[seq[byte]]
|
|
|
|
if ? pb.getRepeatedField(2, bpeers):
|
|
|
|
for bpeer in bpeers:
|
|
|
|
control.peers &= ? decodePeerInfoMsg(initProtoBuffer(bpeer))
|
|
|
|
if ? pb.getField(3, control.backoff):
|
|
|
|
trace "decodePrune: read backoff", backoff = control.backoff
|
2020-07-15 08:25:39 +00:00
|
|
|
ok(control)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeIHave*(pb: ProtoBuffer): ProtoResult[ControlIHave] {.
|
|
|
|
inline.} =
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["ihave"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeIHave: decoding message"
|
|
|
|
var control = ControlIHave()
|
2024-03-25 11:06:34 +00:00
|
|
|
if ? pb.getField(1, control.topicID):
|
|
|
|
trace "decodeIHave: read topicID", topic = control.topicID
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
2024-03-25 11:06:34 +00:00
|
|
|
trace "decodeIHave: topicID is missing"
|
|
|
|
if ? pb.getRepeatedField(2, control.messageIDs):
|
|
|
|
trace "decodeIHave: read messageIDs", message_ids = control.messageIDs
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
|
|
|
trace "decodeIHave: no messageIDs"
|
2020-07-15 08:25:39 +00:00
|
|
|
ok(control)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeIWant*(pb: ProtoBuffer): ProtoResult[ControlIWant] {.inline.} =
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["iwant"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeIWant: decoding message"
|
|
|
|
var control = ControlIWant()
|
2024-03-25 11:06:34 +00:00
|
|
|
if ? pb.getRepeatedField(1, control.messageIDs):
|
|
|
|
trace "decodeIWant: read messageIDs", message_ids = control.messageIDs
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
|
|
|
trace "decodeIWant: no messageIDs"
|
2020-07-15 08:25:39 +00:00
|
|
|
ok(control)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeControl*(pb: ProtoBuffer): ProtoResult[Option[ControlMessage]] {.
|
|
|
|
inline.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeControl: decoding message"
|
|
|
|
var buffer: seq[byte]
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(3, buffer):
|
2020-07-13 12:43:07 +00:00
|
|
|
var control: ControlMessage
|
|
|
|
var cpb = initProtoBuffer(buffer)
|
|
|
|
var ihavepbs: seq[seq[byte]]
|
|
|
|
var iwantpbs: seq[seq[byte]]
|
|
|
|
var graftpbs: seq[seq[byte]]
|
|
|
|
var prunepbs: seq[seq[byte]]
|
2023-07-28 08:58:05 +00:00
|
|
|
var idontwant: seq[seq[byte]]
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? cpb.getRepeatedField(1, ihavepbs):
|
|
|
|
for item in ihavepbs:
|
|
|
|
control.ihave.add(? decodeIHave(initProtoBuffer(item)))
|
|
|
|
if ? cpb.getRepeatedField(2, iwantpbs):
|
|
|
|
for item in iwantpbs:
|
|
|
|
control.iwant.add(? decodeIWant(initProtoBuffer(item)))
|
|
|
|
if ? cpb.getRepeatedField(3, graftpbs):
|
|
|
|
for item in graftpbs:
|
|
|
|
control.graft.add(? decodeGraft(initProtoBuffer(item)))
|
|
|
|
if ? cpb.getRepeatedField(4, prunepbs):
|
|
|
|
for item in prunepbs:
|
|
|
|
control.prune.add(? decodePrune(initProtoBuffer(item)))
|
2023-07-28 08:58:05 +00:00
|
|
|
if ? cpb.getRepeatedField(5, idontwant):
|
|
|
|
for item in idontwant:
|
|
|
|
control.idontwant.add(? decodeIWant(initProtoBuffer(item)))
|
2020-07-15 08:25:39 +00:00
|
|
|
trace "decodeControl: message statistics", graft_count = len(control.graft),
|
|
|
|
prune_count = len(control.prune),
|
|
|
|
ihave_count = len(control.ihave),
|
|
|
|
iwant_count = len(control.iwant)
|
|
|
|
ok(some(control))
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
2020-07-15 08:25:39 +00:00
|
|
|
ok(none[ControlMessage]())
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeSubscription*(pb: ProtoBuffer): ProtoResult[SubOpts] {.inline.} =
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["subs"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeSubscription: decoding message"
|
|
|
|
var subflag: uint64
|
|
|
|
var sub = SubOpts()
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(1, subflag):
|
2020-07-13 12:43:07 +00:00
|
|
|
sub.subscribe = bool(subflag)
|
|
|
|
trace "decodeSubscription: read subscribe", subscribe = subflag
|
|
|
|
else:
|
|
|
|
trace "decodeSubscription: subscribe is missing"
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(2, sub.topic):
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeSubscription: read topic", topic = sub.topic
|
|
|
|
else:
|
|
|
|
trace "decodeSubscription: topic is missing"
|
2020-07-15 08:25:39 +00:00
|
|
|
ok(sub)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeSubscriptions*(pb: ProtoBuffer): ProtoResult[seq[SubOpts]] {.
|
|
|
|
inline.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeSubscriptions: decoding message"
|
|
|
|
var subpbs: seq[seq[byte]]
|
|
|
|
var subs: seq[SubOpts]
|
2020-07-15 08:25:39 +00:00
|
|
|
let res = ? pb.getRepeatedField(1, subpbs)
|
|
|
|
if res:
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeSubscriptions: read subscriptions", count = len(subpbs)
|
|
|
|
for item in subpbs:
|
2020-07-15 08:25:39 +00:00
|
|
|
subs.add(? decodeSubscription(initProtoBuffer(item)))
|
|
|
|
if len(subs) == 0:
|
|
|
|
trace "decodeSubscription: no subscriptions found"
|
|
|
|
ok(subs)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeMessage*(pb: ProtoBuffer): ProtoResult[Message] {.inline.} =
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_protobuf_metrics):
|
|
|
|
libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["message"])
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeMessage: decoding message"
|
|
|
|
var msg: Message
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(1, msg.fromPeer):
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "decodeMessage: read fromPeer", fromPeer = msg.fromPeer
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
|
|
|
trace "decodeMessage: fromPeer is missing"
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(2, msg.data):
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeMessage: read data", data = msg.data.shortLog()
|
|
|
|
else:
|
|
|
|
trace "decodeMessage: data is missing"
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(3, msg.seqno):
|
2020-08-02 10:22:49 +00:00
|
|
|
trace "decodeMessage: read seqno", seqno = msg.seqno
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
|
|
|
trace "decodeMessage: seqno is missing"
|
2024-03-25 11:06:34 +00:00
|
|
|
if ?pb.getField(4, msg.topic):
|
|
|
|
trace "decodeMessage: read topic", topic = msg.topic
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
2024-03-25 11:06:34 +00:00
|
|
|
trace "decodeMessage: topic is required"
|
|
|
|
return err(ProtoError.RequiredFieldMissing)
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(5, msg.signature):
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeMessage: read signature", signature = msg.signature.shortLog()
|
|
|
|
else:
|
|
|
|
trace "decodeMessage: signature is missing"
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(6, msg.key):
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeMessage: read public key", key = msg.key.shortLog()
|
|
|
|
else:
|
|
|
|
trace "decodeMessage: public key is missing"
|
2020-07-15 08:25:39 +00:00
|
|
|
ok(msg)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeMessages*(pb: ProtoBuffer): ProtoResult[seq[Message]] {.inline.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeMessages: decoding message"
|
|
|
|
var msgpbs: seq[seq[byte]]
|
|
|
|
var msgs: seq[Message]
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getRepeatedField(2, msgpbs):
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeMessages: read messages", count = len(msgpbs)
|
|
|
|
for item in msgpbs:
|
2022-11-24 16:43:45 +00:00
|
|
|
# size is constrained at the network level
|
|
|
|
msgs.add(? decodeMessage(initProtoBuffer(item, maxSize = uint.high)))
|
2020-07-15 08:25:39 +00:00
|
|
|
else:
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeMessages: no messages found"
|
2020-07-15 08:25:39 +00:00
|
|
|
ok(msgs)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-09-25 16:39:34 +00:00
|
|
|
proc encodeRpcMsg*(msg: RPCMsg, anonymize: bool): seq[byte] =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "encodeRpcMsg: encoding message", msg = msg.shortLog()
|
2022-11-24 16:43:45 +00:00
|
|
|
var pb = initProtoBuffer(maxSize = uint.high)
|
2020-07-13 12:43:07 +00:00
|
|
|
for item in msg.subscriptions:
|
|
|
|
pb.write(1, item)
|
|
|
|
for item in msg.messages:
|
2020-09-25 16:39:34 +00:00
|
|
|
pb.write(2, item, anonymize)
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.control.withValue(control):
|
|
|
|
pb.write(3, control)
|
2023-06-21 08:40:10 +00:00
|
|
|
# nim-libp2p extension, using fields which are unlikely to be used
|
|
|
|
# by other extensions
|
|
|
|
if msg.ping.len > 0:
|
|
|
|
pb.write(60, msg.ping)
|
|
|
|
if msg.pong.len > 0:
|
|
|
|
pb.write(61, msg.pong)
|
2020-07-13 12:43:07 +00:00
|
|
|
if len(pb.buffer) > 0:
|
|
|
|
pb.finish()
|
2020-07-15 08:25:39 +00:00
|
|
|
pb.buffer
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeRpcMsg*(msg: seq[byte]): ProtoResult[RPCMsg] {.inline.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeRpcMsg: decoding message", msg = msg.shortLog()
|
2022-11-24 16:43:45 +00:00
|
|
|
var pb = initProtoBuffer(msg, maxSize = uint.high)
|
2023-06-28 14:44:58 +00:00
|
|
|
var rpcMsg = RPCMsg()
|
|
|
|
assign(rpcMsg.messages, ? pb.decodeMessages())
|
|
|
|
assign(rpcMsg.subscriptions, ? pb.decodeSubscriptions())
|
|
|
|
assign(rpcMsg.control, ? pb.decodeControl())
|
|
|
|
discard ? pb.getField(60, rpcMsg.ping)
|
|
|
|
discard ? pb.getField(61, rpcMsg.pong)
|
|
|
|
ok(rpcMsg)
|