2019-09-10 02:15:52 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-09-10 02:15:52 +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
|
|
|
import options, hashes, strutils, tables, hashes
|
2019-09-10 02:15:52 +00:00
|
|
|
import chronos, chronicles
|
2019-12-06 02:16:18 +00:00
|
|
|
import rpc/[messages, message, protobuf],
|
2019-10-03 21:34:12 +00:00
|
|
|
timedcache,
|
2019-09-12 02:10:38 +00:00
|
|
|
../../peer,
|
2019-09-24 16:16:39 +00:00
|
|
|
../../peerinfo,
|
|
|
|
../../connection,
|
2019-12-04 04:44:54 +00:00
|
|
|
../../stream/lpstream,
|
2019-09-12 05:46:08 +00:00
|
|
|
../../crypto/crypto,
|
2019-09-24 16:16:39 +00:00
|
|
|
../../protobuf/minprotobuf
|
2019-09-10 02:15:52 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topic = "PubSubPeer"
|
|
|
|
|
|
|
|
type
|
|
|
|
PubSubPeer* = ref object of RootObj
|
2019-12-06 02:16:18 +00:00
|
|
|
proto: string # the protocol that this peer joined from
|
|
|
|
sendConn: Connection
|
2019-09-12 02:10:38 +00:00
|
|
|
peerInfo*: PeerInfo
|
2019-09-10 02:15:52 +00:00
|
|
|
handler*: RPCHandler
|
|
|
|
topics*: seq[string]
|
2019-12-06 02:16:18 +00:00
|
|
|
sentRpcCache: TimedCache[string] # a cache of already sent messages
|
|
|
|
recvdRpcCache: TimedCache[string] # a cache of already sent messages
|
|
|
|
refs*: int # refcount of the connections this peer is handling
|
|
|
|
onConnect: AsyncEvent
|
2019-09-10 02:15:52 +00:00
|
|
|
|
|
|
|
RPCHandler* = proc(peer: PubSubPeer, msg: seq[RPCMsg]): Future[void] {.gcsafe.}
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
proc id*(p: PubSubPeer): string = p.peerInfo.id
|
|
|
|
|
|
|
|
proc isConnected*(p: PubSubPeer): bool =
|
|
|
|
(not isNil(p.sendConn))
|
|
|
|
|
|
|
|
proc `conn=`*(p: PubSubPeer, conn: Connection) =
|
|
|
|
trace "attaching send connection for peer", peer = p.id
|
|
|
|
p.sendConn = conn
|
|
|
|
p.onConnect.fire()
|
|
|
|
|
|
|
|
proc handle*(p: PubSubPeer, conn: Connection) {.async, gcsafe.} =
|
|
|
|
trace "handling pubsub rpc", peer = p.id, closed = conn.closed
|
2019-09-10 02:15:52 +00:00
|
|
|
try:
|
2019-12-06 02:16:18 +00:00
|
|
|
while not conn.closed:
|
|
|
|
trace "waiting for data", peer = p.id, closed = conn.closed
|
|
|
|
let data = await conn.readLp()
|
|
|
|
let hexData = data.toHex()
|
|
|
|
trace "read data from peer", peer = p.id, data = hexData
|
|
|
|
if $hexData.hash in p.recvdRpcCache:
|
|
|
|
trace "message already received, skipping", peer = p.id
|
2019-10-03 19:14:04 +00:00
|
|
|
continue
|
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
let msg = decodeRpcMsg(data)
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "decoded msg from peer", peer = p.id, msg = msg
|
2019-09-10 02:15:52 +00:00
|
|
|
await p.handler(p, @[msg])
|
2019-12-06 02:16:18 +00:00
|
|
|
p.recvdRpcCache.put($hexData.hash)
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "an exception occured while processing pubsub rpc requests", exc = exc.msg
|
2019-09-10 02:15:52 +00:00
|
|
|
finally:
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "exiting pubsub peer read loop", peer = p.id
|
2019-09-10 02:15:52 +00:00
|
|
|
|
|
|
|
proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async, gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
try:
|
|
|
|
for m in msgs:
|
|
|
|
trace "sending msgs to peer", toPeer = p.id
|
|
|
|
let encoded = encodeRpcMsg(m)
|
|
|
|
let encodedHex = encoded.buffer.toHex()
|
|
|
|
if encoded.buffer.len <= 0:
|
|
|
|
trace "empty message, skipping", peer = p.id
|
|
|
|
return
|
|
|
|
|
|
|
|
if $encodedHex.hash in p.sentRpcCache:
|
|
|
|
trace "message already sent to peer, skipping", peer = p.id
|
|
|
|
continue
|
|
|
|
|
|
|
|
proc sendToRemote() {.async.} =
|
|
|
|
trace "sending encoded msgs to peer", peer = p.id, encoded = encodedHex
|
|
|
|
await p.sendConn.writeLp(encoded.buffer)
|
|
|
|
p.sentRpcCache.put($encodedHex.hash)
|
|
|
|
|
|
|
|
# if no connection has been set,
|
|
|
|
# queue messages untill a connection
|
|
|
|
# becomes available
|
|
|
|
if p.isConnected:
|
|
|
|
await sendToRemote()
|
|
|
|
return
|
|
|
|
|
|
|
|
p.onConnect.wait().addCallback(
|
|
|
|
proc(udata: pointer) =
|
|
|
|
asyncCheck sendToRemote()
|
|
|
|
)
|
|
|
|
trace "enqueued message to send at a later time"
|
|
|
|
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception occured", exc = exc.msg
|
|
|
|
|
|
|
|
proc sendMsg*(p: PubSubPeer,
|
|
|
|
peerId: PeerID,
|
|
|
|
topic: string,
|
|
|
|
data: seq[byte]): Future[void] {.gcsafe.} =
|
|
|
|
p.send(@[RPCMsg(messages: @[newMessage(p.peerInfo.peerId.get(), data, topic)])])
|
|
|
|
|
|
|
|
proc sendGraft*(p: PubSubPeer, topics: seq[string]) {.async, gcsafe.} =
|
|
|
|
for topic in topics:
|
|
|
|
trace "sending graft msg to peer", peer = p.id, topicID = topic
|
|
|
|
await p.send(@[RPCMsg(control: some(ControlMessage(graft: @[ControlGraft(topicID: topic)])))])
|
|
|
|
|
|
|
|
proc sendPrune*(p: PubSubPeer, topics: seq[string]) {.async, gcsafe.} =
|
|
|
|
for topic in topics:
|
|
|
|
trace "sending prune msg to peer", peer = p.id, topicID = topic
|
|
|
|
await p.send(@[RPCMsg(control: some(ControlMessage(prune: @[ControlPrune(topicID: topic)])))])
|
|
|
|
|
|
|
|
proc newPubSubPeer*(peerInfo: PeerInfo,
|
|
|
|
proto: string): PubSubPeer =
|
2019-09-10 02:15:52 +00:00
|
|
|
new result
|
2019-12-06 02:16:18 +00:00
|
|
|
result.proto = proto
|
|
|
|
result.peerInfo = peerInfo
|
|
|
|
result.sentRpcCache = newTimedCache[string](2.minutes)
|
|
|
|
result.recvdRpcCache = newTimedCache[string](2.minutes)
|
|
|
|
result.onConnect = newAsyncEvent()
|