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-05-06 16:31:47 +00:00
|
|
|
../../../utility,
|
|
|
|
../../../protobuf/minprotobuf
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodeGraft*(graft: ControlGraft, pb: var ProtoBuffer) {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
pb.write(initProtoField(1, graft.topicID))
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodeGraft*(pb: var ProtoBuffer): seq[ControlGraft] {.gcsafe.} =
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "decoding graft msg", buffer = pb.buffer.shortLog
|
2019-12-06 02:16:18 +00:00
|
|
|
while true:
|
|
|
|
var topic: string
|
|
|
|
if pb.getString(1, topic) < 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
trace "read topic field from graft msg", topicID = topic
|
|
|
|
result.add(ControlGraft(topicID: topic))
|
|
|
|
|
2020-06-23 08:01:21 +00:00
|
|
|
proc encodePeerInfo*(info: PeerInfoMsg, pb: var ProtoBuffer) {.gcsafe.} =
|
|
|
|
pb.write(initProtoField(1, info.peerID))
|
|
|
|
pb.write(initProtoField(2, info.signedPeerRecord))
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodePrune*(prune: ControlPrune, pb: var ProtoBuffer) {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
pb.write(initProtoField(1, prune.topicID))
|
2020-06-23 08:01:21 +00:00
|
|
|
for p in prune.peers:
|
|
|
|
encodePeerInfo(p, pb)
|
|
|
|
pb.write(initProtoField(3, prune.backoff))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodePrune*(pb: var ProtoBuffer): seq[ControlPrune] {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "decoding prune msg"
|
|
|
|
while true:
|
|
|
|
var topic: string
|
|
|
|
if pb.getString(1, topic) < 0:
|
|
|
|
break
|
2020-05-06 16:31:47 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
trace "read topic field from prune msg", topicID = topic
|
2019-12-06 02:16:18 +00:00
|
|
|
result.add(ControlPrune(topicID: topic))
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodeIHave*(ihave: ControlIHave, pb: var ProtoBuffer) {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
pb.write(initProtoField(1, ihave.topicID))
|
|
|
|
for mid in ihave.messageIDs:
|
|
|
|
pb.write(initProtoField(2, mid))
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodeIHave*(pb: var ProtoBuffer): seq[ControlIHave] {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "decoding ihave msg"
|
|
|
|
|
|
|
|
while true:
|
|
|
|
var control: ControlIHave
|
2020-05-21 20:24:20 +00:00
|
|
|
if pb.getString(1, control.topicID) < 0:
|
|
|
|
trace "topic field missing from ihave msg"
|
|
|
|
break
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
trace "read topic field", topicID = control.topicID
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
while true:
|
|
|
|
var mid: string
|
|
|
|
if pb.getString(2, mid) < 0:
|
|
|
|
break
|
|
|
|
trace "read messageID field", mid = mid
|
|
|
|
control.messageIDs.add(mid)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
result.add(control)
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodeIWant*(iwant: ControlIWant, pb: var ProtoBuffer) {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
for mid in iwant.messageIDs:
|
|
|
|
pb.write(initProtoField(1, mid))
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodeIWant*(pb: var ProtoBuffer): seq[ControlIWant] {.gcsafe.} =
|
2020-05-21 20:24:20 +00:00
|
|
|
trace "decoding iwant msg"
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
var control: ControlIWant
|
|
|
|
while true:
|
2019-12-06 02:16:18 +00:00
|
|
|
var mid: string
|
2020-05-21 20:24:20 +00:00
|
|
|
if pb.getString(1, mid) < 0:
|
|
|
|
break
|
|
|
|
control.messageIDs.add(mid)
|
|
|
|
trace "read messageID field", mid = mid
|
|
|
|
result.add(control)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodeControl*(control: ControlMessage, pb: var ProtoBuffer) {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
if control.ihave.len > 0:
|
|
|
|
var ihave = initProtoBuffer()
|
|
|
|
for h in control.ihave:
|
|
|
|
h.encodeIHave(ihave)
|
|
|
|
|
|
|
|
# write messages to protobuf
|
|
|
|
ihave.finish()
|
|
|
|
pb.write(initProtoField(1, ihave))
|
|
|
|
|
|
|
|
if control.iwant.len > 0:
|
|
|
|
var iwant = initProtoBuffer()
|
|
|
|
for w in control.iwant:
|
|
|
|
w.encodeIWant(iwant)
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
# write messages to protobuf
|
|
|
|
iwant.finish()
|
|
|
|
pb.write(initProtoField(2, iwant))
|
|
|
|
|
|
|
|
if control.graft.len > 0:
|
|
|
|
var graft = initProtoBuffer()
|
|
|
|
for g in control.graft:
|
|
|
|
g.encodeGraft(graft)
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
# write messages to protobuf
|
|
|
|
graft.finish()
|
|
|
|
pb.write(initProtoField(3, graft))
|
|
|
|
|
|
|
|
if control.prune.len > 0:
|
|
|
|
var prune = initProtoBuffer()
|
|
|
|
for p in control.prune:
|
|
|
|
p.encodePrune(prune)
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
# write messages to protobuf
|
|
|
|
prune.finish()
|
|
|
|
pb.write(initProtoField(4, prune))
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodeControl*(pb: var ProtoBuffer): Option[ControlMessage] {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "decoding control submessage"
|
|
|
|
var control: ControlMessage
|
|
|
|
while true:
|
|
|
|
var field = pb.enterSubMessage()
|
|
|
|
trace "processing submessage", field = field
|
|
|
|
case field:
|
|
|
|
of 0:
|
|
|
|
trace "no submessage found in Control msg"
|
|
|
|
break
|
|
|
|
of 1:
|
2020-05-21 20:24:20 +00:00
|
|
|
control.ihave &= pb.decodeIHave()
|
2019-12-06 02:16:18 +00:00
|
|
|
of 2:
|
2020-05-21 20:24:20 +00:00
|
|
|
control.iwant &= pb.decodeIWant()
|
2019-12-06 02:16:18 +00:00
|
|
|
of 3:
|
2020-05-21 20:24:20 +00:00
|
|
|
control.graft &= pb.decodeGraft()
|
2019-12-06 02:16:18 +00:00
|
|
|
of 4:
|
2020-05-21 20:24:20 +00:00
|
|
|
control.prune &= pb.decodePrune()
|
2019-12-10 20:50:35 +00:00
|
|
|
else:
|
2019-12-06 02:16:18 +00:00
|
|
|
raise newException(CatchableError, "message type not recognized")
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
if result.isNone:
|
|
|
|
result = some(control)
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodeSubs*(subs: SubOpts, pb: var ProtoBuffer) {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
pb.write(initProtoField(1, subs.subscribe))
|
|
|
|
pb.write(initProtoField(2, subs.topic))
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodeSubs*(pb: var ProtoBuffer): seq[SubOpts] {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
while true:
|
|
|
|
var subOpt: SubOpts
|
2020-03-06 19:19:43 +00:00
|
|
|
var subscr: uint
|
2019-12-06 02:16:18 +00:00
|
|
|
discard pb.getVarintValue(1, subscr)
|
|
|
|
subOpt.subscribe = cast[bool](subscr)
|
|
|
|
trace "read subscribe field", subscribe = subOpt.subscribe
|
|
|
|
|
|
|
|
if pb.getString(2, subOpt.topic) < 0:
|
|
|
|
break
|
|
|
|
trace "read subscribe field", topicName = subOpt.topic
|
|
|
|
|
|
|
|
result.add(subOpt)
|
|
|
|
|
|
|
|
trace "got subscriptions", subscriptions = result
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodeMessage*(msg: Message, pb: var ProtoBuffer) {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
pb.write(initProtoField(1, msg.fromPeer))
|
|
|
|
pb.write(initProtoField(2, msg.data))
|
|
|
|
pb.write(initProtoField(3, msg.seqno))
|
|
|
|
|
|
|
|
for t in msg.topicIDs:
|
|
|
|
pb.write(initProtoField(4, t))
|
|
|
|
|
|
|
|
if msg.signature.len > 0:
|
|
|
|
pb.write(initProtoField(5, msg.signature))
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
if msg.key.len > 0:
|
|
|
|
pb.write(initProtoField(6, msg.key))
|
|
|
|
|
|
|
|
pb.finish()
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodeMessages*(pb: var ProtoBuffer): seq[Message] {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
# TODO: which of this fields are really optional?
|
|
|
|
while true:
|
|
|
|
var msg: Message
|
|
|
|
if pb.getBytes(1, msg.fromPeer) < 0:
|
|
|
|
break
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "read message field", fromPeer = msg.fromPeer.shortLog
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
if pb.getBytes(2, msg.data) < 0:
|
|
|
|
break
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "read message field", data = msg.data.shortLog
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
if pb.getBytes(3, msg.seqno) < 0:
|
|
|
|
break
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "read message field", seqno = msg.seqno.shortLog
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var topic: string
|
|
|
|
while true:
|
|
|
|
if pb.getString(4, topic) < 0:
|
|
|
|
break
|
|
|
|
msg.topicIDs.add(topic)
|
|
|
|
trace "read message field", topicName = topic
|
|
|
|
topic = ""
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
discard pb.getBytes(5, msg.signature)
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "read message field", signature = msg.signature.shortLog
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
discard pb.getBytes(6, msg.key)
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "read message field", key = msg.key.shortLog
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
result.add(msg)
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodeRpcMsg*(msg: RPCMsg): ProtoBuffer {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
result = initProtoBuffer()
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "encoding msg: ", msg = msg.shortLog
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
if msg.subscriptions.len > 0:
|
|
|
|
for s in msg.subscriptions:
|
2020-05-27 18:33:49 +00:00
|
|
|
var subs = initProtoBuffer()
|
2019-12-06 02:16:18 +00:00
|
|
|
encodeSubs(s, subs)
|
2020-05-27 18:33:49 +00:00
|
|
|
# write subscriptions to protobuf
|
|
|
|
subs.finish()
|
|
|
|
result.write(initProtoField(1, subs))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
if msg.messages.len > 0:
|
|
|
|
var messages = initProtoBuffer()
|
|
|
|
for m in msg.messages:
|
|
|
|
encodeMessage(m, messages)
|
|
|
|
|
|
|
|
# write messages to protobuf
|
|
|
|
messages.finish()
|
|
|
|
result.write(initProtoField(2, messages))
|
|
|
|
|
|
|
|
if msg.control.isSome:
|
|
|
|
var control = initProtoBuffer()
|
|
|
|
msg.control.get.encodeControl(control)
|
|
|
|
|
|
|
|
# write messages to protobuf
|
|
|
|
control.finish()
|
|
|
|
result.write(initProtoField(3, control))
|
|
|
|
|
|
|
|
if result.buffer.len > 0:
|
|
|
|
result.finish()
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodeRpcMsg*(msg: seq[byte]): RPCMsg {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
var pb = initProtoBuffer(msg)
|
|
|
|
|
|
|
|
while true:
|
|
|
|
# decode SubOpts array
|
|
|
|
var field = pb.enterSubMessage()
|
|
|
|
trace "processing submessage", field = field
|
|
|
|
case field:
|
|
|
|
of 0:
|
|
|
|
trace "no submessage found in RPC msg"
|
|
|
|
break
|
|
|
|
of 1:
|
2020-05-27 18:33:49 +00:00
|
|
|
result.subscriptions &= pb.decodeSubs()
|
2019-12-06 02:16:18 +00:00
|
|
|
of 2:
|
2020-05-27 18:33:49 +00:00
|
|
|
result.messages &= pb.decodeMessages()
|
2019-12-06 02:16:18 +00:00
|
|
|
of 3:
|
|
|
|
result.control = pb.decodeControl()
|
2019-12-10 20:50:35 +00:00
|
|
|
else:
|
2019-12-06 02:16:18 +00:00
|
|
|
raise newException(CatchableError, "message type not recognized")
|