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
|
2020-05-18 20:49:49 +00:00
|
|
|
import chronos, chronicles, nimcrypto/sha2
|
2019-12-10 20:50:35 +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,
|
2020-03-23 06:03:36 +00:00
|
|
|
../../protobuf/minprotobuf,
|
|
|
|
../../utility
|
2019-09-10 02:15:52 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topic = "PubSubPeer"
|
|
|
|
|
|
|
|
type
|
2020-04-30 13:22:31 +00:00
|
|
|
PubSubObserver* = ref object
|
|
|
|
onRecv*: proc(peer: PubSubPeer; msgs: var RPCMsg) {.gcsafe.}
|
|
|
|
onSend*: proc(peer: PubSubPeer; msgs: var RPCMsg) {.gcsafe.}
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2020-04-30 13:22:31 +00:00
|
|
|
PubSubPeer* = ref object of RootObj
|
|
|
|
proto: string # the protocol that this peer joined from
|
|
|
|
sendConn: Connection
|
|
|
|
peerInfo*: PeerInfo
|
|
|
|
handler*: RPCHandler
|
|
|
|
topics*: seq[string]
|
|
|
|
sentRpcCache: TimedCache[string] # cache for already sent messages
|
|
|
|
recvdRpcCache: TimedCache[string] # cache for already received messages
|
|
|
|
refs*: int # refcount of the connections this peer is handling
|
|
|
|
onConnect: AsyncEvent
|
|
|
|
observers*: ref seq[PubSubObserver] # ref as in smart_ptr
|
|
|
|
|
|
|
|
RPCHandler* = proc(peer: PubSubPeer, msg: seq[RPCMsg]): Future[void] {.gcsafe.}
|
2019-09-10 02:15:52 +00:00
|
|
|
|
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()
|
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc handle*(p: PubSubPeer, conn: Connection) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
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
|
2020-05-08 20:58:23 +00:00
|
|
|
let data = await conn.readLp(64 * 1024)
|
2020-05-18 20:49:49 +00:00
|
|
|
let digest = $(sha256.digest(data))
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "read data from peer", peer = p.id, data = data.shortLog
|
2020-05-18 20:49:49 +00:00
|
|
|
if digest in p.recvdRpcCache:
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "message already received, skipping", peer = p.id
|
2019-10-03 19:14:04 +00:00
|
|
|
continue
|
|
|
|
|
2020-04-30 13:22:31 +00:00
|
|
|
var msg = decodeRpcMsg(data)
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "decoded msg from peer", peer = p.id, msg = msg.shortLog
|
2020-04-30 13:22:31 +00:00
|
|
|
# trigger hooks
|
|
|
|
for obs in p.observers[]:
|
|
|
|
obs.onRecv(p, msg)
|
2019-09-10 02:15:52 +00:00
|
|
|
await p.handler(p, @[msg])
|
2020-05-18 20:49:49 +00:00
|
|
|
p.recvdRpcCache.put(digest)
|
2019-12-06 02:16:18 +00:00
|
|
|
except CatchableError as exc:
|
2020-03-27 14:25:52 +00:00
|
|
|
trace "Exception occurred in PubSubPeer.handle", 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
|
2020-01-07 08:04:02 +00:00
|
|
|
if not conn.closed():
|
|
|
|
await conn.close()
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
try:
|
2020-04-30 13:22:31 +00:00
|
|
|
for m in msgs.items:
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "sending msgs to peer", toPeer = p.id
|
|
|
|
let encoded = encodeRpcMsg(m)
|
2020-04-30 13:22:31 +00:00
|
|
|
# trigger hooks
|
2020-05-08 21:44:18 +00:00
|
|
|
if not(isNil(p.observers)) and p.observers[].len > 0:
|
2020-04-30 13:22:31 +00:00
|
|
|
var mm = m
|
|
|
|
for obs in p.observers[]:
|
|
|
|
obs.onSend(p, mm)
|
2020-05-18 20:49:49 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
if encoded.buffer.len <= 0:
|
|
|
|
trace "empty message, skipping", peer = p.id
|
|
|
|
return
|
|
|
|
|
2020-05-18 20:49:49 +00:00
|
|
|
let digest = $(sha256.digest(encoded.buffer))
|
|
|
|
if digest in p.sentRpcCache:
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "message already sent to peer, skipping", peer = p.id
|
|
|
|
continue
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
proc sendToRemote() {.async.} =
|
2020-05-18 20:49:49 +00:00
|
|
|
trace "sending encoded msgs to peer", peer = p.id,
|
|
|
|
encoded = encoded.buffer.shortLog
|
2019-12-06 02:16:18 +00:00
|
|
|
await p.sendConn.writeLp(encoded.buffer)
|
2020-05-18 20:49:49 +00:00
|
|
|
p.sentRpcCache.put(digest)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
# if no connection has been set,
|
|
|
|
# queue messages untill a connection
|
2019-12-06 02:16:18 +00:00
|
|
|
# becomes available
|
|
|
|
if p.isConnected:
|
|
|
|
await sendToRemote()
|
|
|
|
return
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
p.onConnect.wait().addCallback do (udata: pointer):
|
2019-12-06 02:16:18 +00:00
|
|
|
asyncCheck sendToRemote()
|
2020-01-07 08:04:02 +00:00
|
|
|
trace "enqueued message to send at a later time", peer = p.id,
|
|
|
|
encoded = encodedHex
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
except CatchableError as exc:
|
2020-03-27 14:25:52 +00:00
|
|
|
trace "Exception occurred in PubSubPeer.send", exc = exc.msg
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
proc sendMsg*(p: PubSubPeer,
|
|
|
|
peerId: PeerID,
|
|
|
|
topic: string,
|
2020-05-06 09:26:08 +00:00
|
|
|
data: seq[byte],
|
|
|
|
sign: bool): Future[void] {.gcsafe.} =
|
|
|
|
p.send(@[RPCMsg(messages: @[newMessage(p.peerInfo, data, topic, sign)])])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc sendGraft*(p: PubSubPeer, topics: seq[string]) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
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)])))])
|
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc sendPrune*(p: PubSubPeer, topics: seq[string]) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
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)
|
2019-12-07 16:36:39 +00:00
|
|
|
result.onConnect = newAsyncEvent()
|