2019-12-06 02:16:18 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
import options
|
|
|
|
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-15 06:03:53 +00:00
|
|
|
logScope:
|
|
|
|
topics = "pubsubprotobuf"
|
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, graft: ControlGraft) =
|
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
ipb.write(1, graft.topicID)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, infoMsg: PeerInfoMsg) =
|
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
ipb.write(1, infoMsg.peerID)
|
|
|
|
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()
|
|
|
|
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)
|
|
|
|
|
|
|
|
proc write*(pb: var ProtoBuffer, field: int, ihave: ControlIHave) =
|
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
ipb.write(1, ihave.topicID)
|
2019-12-06 02:16:18 +00:00
|
|
|
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
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, iwant: ControlIWant) =
|
|
|
|
var ipb = initProtoBuffer()
|
2019-12-06 02:16:18 +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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
|
|
|
|
proc encodeMessage*(msg: Message): seq[byte] =
|
|
|
|
var pb = initProtoBuffer()
|
2020-09-15 10:33:18 +00:00
|
|
|
if len(msg.fromPeer) > 0:
|
|
|
|
pb.write(1, msg.fromPeer)
|
2020-07-13 12:43:07 +00:00
|
|
|
pb.write(2, msg.data)
|
2020-09-15 10:33:18 +00:00
|
|
|
if len(msg.seqno) > 0:
|
|
|
|
pb.write(3, msg.seqno)
|
2020-07-13 12:43:07 +00:00
|
|
|
for topic in msg.topicIDs:
|
|
|
|
pb.write(4, topic)
|
|
|
|
if len(msg.signature) > 0:
|
|
|
|
pb.write(5, msg.signature)
|
|
|
|
if len(msg.key) > 0:
|
|
|
|
pb.write(6, msg.key)
|
2019-12-06 02:16:18 +00:00
|
|
|
pb.finish()
|
2020-07-13 12:43:07 +00:00
|
|
|
pb.buffer
|
|
|
|
|
|
|
|
proc write*(pb: var ProtoBuffer, field: int, msg: Message) =
|
|
|
|
pb.write(field, encodeMessage(msg))
|
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeGraft*(pb: ProtoBuffer): ProtoResult[ControlGraft] {.
|
|
|
|
inline.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeGraft: decoding message"
|
|
|
|
var control = ControlGraft()
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(1, control.topicId):
|
|
|
|
trace "decodeGraft: read topicId", topic_id = control.topicId
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
|
|
|
trace "decodeGraft: topicId is missing"
|
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 decodePrune*(pb: ProtoBuffer): ProtoResult[ControlPrune] {.
|
|
|
|
inline.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodePrune: decoding message"
|
|
|
|
var control = ControlPrune()
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(1, control.topicId):
|
|
|
|
trace "decodePrune: read topicId", topic_id = control.topicId
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
|
|
|
trace "decodePrune: topicId is missing"
|
2020-09-21 09:16:29 +00:00
|
|
|
# TODO gossip 1.1 fields
|
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.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeIHave: decoding message"
|
|
|
|
var control = ControlIHave()
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getField(1, control.topicId):
|
|
|
|
trace "decodeIHave: read topicId", topic_id = control.topicId
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
|
|
|
trace "decodeIHave: topicId is missing"
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getRepeatedField(2, control.messageIDs):
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeIHave: read messageIDs", message_ids = control.messageIDs
|
|
|
|
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.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeIWant: decoding message"
|
|
|
|
var control = ControlIWant()
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getRepeatedField(1, control.messageIDs):
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeIWant: read messageIDs", message_ids = control.messageIDs
|
|
|
|
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]]
|
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)))
|
|
|
|
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.} =
|
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.} =
|
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"
|
2020-07-15 08:25:39 +00:00
|
|
|
if ? pb.getRepeatedField(4, msg.topicIDs):
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "decodeMessage: read topics", topic_ids = msg.topicIDs
|
|
|
|
else:
|
|
|
|
trace "decodeMessage: topics are missing"
|
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:
|
2020-07-15 08:25:39 +00:00
|
|
|
msgs.add(? decodeMessage(initProtoBuffer(item)))
|
|
|
|
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-07-15 08:25:39 +00:00
|
|
|
proc encodeRpcMsg*(msg: RPCMsg): seq[byte] =
|
2020-07-13 12:43:07 +00:00
|
|
|
trace "encodeRpcMsg: encoding message", msg = msg.shortLog()
|
|
|
|
var pb = initProtoBuffer()
|
|
|
|
for item in msg.subscriptions:
|
|
|
|
pb.write(1, item)
|
|
|
|
for item in msg.messages:
|
|
|
|
pb.write(2, item)
|
|
|
|
if msg.control.isSome():
|
|
|
|
pb.write(3, msg.control.get())
|
|
|
|
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()
|
2019-12-06 02:16:18 +00:00
|
|
|
var pb = initProtoBuffer(msg)
|
2020-07-13 12:43:07 +00:00
|
|
|
var rpcMsg: RPCMsg
|
2020-07-15 08:25:39 +00:00
|
|
|
rpcMsg.messages = ? pb.decodeMessages()
|
|
|
|
rpcMsg.subscriptions = ? pb.decodeSubscriptions()
|
|
|
|
rpcMsg.control = ? pb.decodeControl()
|
|
|
|
ok(rpcMsg)
|