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,
|
|
|
|
../../../protobuf/minprotobuf,
|
|
|
|
../../../crypto/crypto,
|
2020-03-23 06:03:36 +00:00
|
|
|
../../../peer,
|
|
|
|
../../../utility
|
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:
|
|
|
|
trace "unable to read topic field from graft msg, breaking"
|
|
|
|
break
|
|
|
|
|
|
|
|
trace "read topic field from graft msg", topicID = topic
|
|
|
|
result.add(ControlGraft(topicID: topic))
|
|
|
|
|
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))
|
|
|
|
|
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
|
|
|
|
trace "read topic field", topicID = topic
|
|
|
|
|
|
|
|
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
|
|
|
|
if pb.enterSubMessage() > 0:
|
|
|
|
if pb.getString(1, control.topicID) < 0:
|
|
|
|
trace "topic field missing from ihave msg"
|
|
|
|
break
|
|
|
|
|
|
|
|
trace "read topic field", topicID = control.topicID
|
|
|
|
|
|
|
|
while true:
|
|
|
|
var mid: string
|
|
|
|
if pb.getString(2, mid) < 0:
|
|
|
|
break
|
|
|
|
trace "read messageID field", mid = mid
|
|
|
|
control.messageIDs.add(mid)
|
|
|
|
|
|
|
|
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.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "decoding ihave msg"
|
|
|
|
|
|
|
|
while pb.enterSubMessage() > 0:
|
|
|
|
var mid: string
|
|
|
|
var iWant: ControlIWant
|
|
|
|
while pb.getString(1, mid) > 0:
|
|
|
|
trace "read messageID field", mid = mid
|
|
|
|
iWant.messageIDs.add(mid)
|
|
|
|
result.add(iWant)
|
|
|
|
|
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:
|
|
|
|
control.ihave = pb.decodeIHave()
|
|
|
|
of 2:
|
|
|
|
control.iwant = pb.decodeIWant()
|
|
|
|
of 3:
|
|
|
|
control.graft = pb.decodeGraft()
|
|
|
|
of 4:
|
|
|
|
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:
|
|
|
|
var subs = initProtoBuffer()
|
|
|
|
for s in msg.subscriptions:
|
|
|
|
encodeSubs(s, subs)
|
|
|
|
|
|
|
|
# write subscriptions to protobuf
|
|
|
|
subs.finish()
|
|
|
|
result.write(initProtoField(1, subs))
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
result.subscriptions = newSeq[SubOpts]()
|
|
|
|
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:
|
|
|
|
result.subscriptions = pb.decodeSubs()
|
|
|
|
of 2:
|
|
|
|
result.messages = pb.decodeMessages()
|
|
|
|
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")
|