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-06-11 18:09:34 +00:00
|
|
|
import chronos, chronicles, nimcrypto/sha2, metrics
|
2019-12-10 20:50:35 +00:00
|
|
|
import rpc/[messages, message, protobuf],
|
2019-10-03 21:34:12 +00:00
|
|
|
timedcache,
|
2020-07-01 06:25:09 +00:00
|
|
|
../../peerid,
|
2019-09-24 16:16:39 +00:00
|
|
|
../../peerinfo,
|
2020-06-19 17:29:43 +00:00
|
|
|
../../stream/connection,
|
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:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "pubsubpeer"
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2020-06-15 23:39:03 +00:00
|
|
|
declareCounter(libp2p_pubsub_sent_messages, "number of messages sent", labels = ["id", "topic"])
|
|
|
|
declareCounter(libp2p_pubsub_received_messages, "number of messages received", labels = ["id", "topic"])
|
2020-06-19 17:29:43 +00:00
|
|
|
declareCounter(libp2p_pubsub_skipped_received_messages, "number of received skipped messages", labels = ["id"])
|
|
|
|
declareCounter(libp2p_pubsub_skipped_sent_messages, "number of sent skipped messages", labels = ["id"])
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
type
|
2020-04-30 13:22:31 +00:00
|
|
|
PubSubObserver* = ref object
|
2020-05-29 16:46:27 +00:00
|
|
|
onRecv*: proc(peer: PubSubPeer; msgs: var RPCMsg) {.gcsafe, raises: [Defect].}
|
|
|
|
onSend*: proc(peer: PubSubPeer; msgs: var RPCMsg) {.gcsafe, raises: [Defect].}
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2020-04-30 13:22:31 +00:00
|
|
|
PubSubPeer* = ref object of RootObj
|
2020-07-08 00:33:05 +00:00
|
|
|
proto*: string # the protocol that this peer joined from
|
2020-04-30 13:22:31 +00:00
|
|
|
sendConn: Connection
|
|
|
|
peerInfo*: PeerInfo
|
|
|
|
handler*: RPCHandler
|
|
|
|
sentRpcCache: TimedCache[string] # cache for already sent messages
|
|
|
|
recvdRpcCache: TimedCache[string] # cache for already received messages
|
2020-07-08 00:33:05 +00:00
|
|
|
onConnect*: AsyncEvent
|
2020-04-30 13:22:31 +00:00
|
|
|
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
|
|
|
|
|
2020-06-29 15:15:31 +00:00
|
|
|
proc connected*(p: PubSubPeer): bool =
|
|
|
|
not(isNil(p.sendConn))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
proc `conn=`*(p: PubSubPeer, conn: Connection) =
|
2020-06-06 00:17:05 +00:00
|
|
|
if not(isNil(conn)):
|
2020-06-08 16:40:08 +00:00
|
|
|
trace "attaching send connection for peer", peer = p.id
|
2020-06-06 00:17:05 +00:00
|
|
|
p.sendConn = conn
|
|
|
|
p.onConnect.fire()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
proc conn*(p: PubSubPeer): Connection =
|
|
|
|
p.sendConn
|
|
|
|
|
2020-05-29 16:46:27 +00:00
|
|
|
proc recvObservers(p: PubSubPeer, msg: var RPCMsg) =
|
|
|
|
# trigger hooks
|
|
|
|
if not(isNil(p.observers)) and p.observers[].len > 0:
|
|
|
|
for obs in p.observers[]:
|
2020-06-24 15:08:44 +00:00
|
|
|
if not(isNil(obs)): # TODO: should never be nil, but...
|
|
|
|
obs.onRecv(p, msg)
|
2020-05-29 16:46:27 +00:00
|
|
|
|
|
|
|
proc sendObservers(p: PubSubPeer, msg: var RPCMsg) =
|
|
|
|
# trigger hooks
|
|
|
|
if not(isNil(p.observers)) and p.observers[].len > 0:
|
|
|
|
for obs in p.observers[]:
|
2020-06-24 15:08:44 +00:00
|
|
|
if not(isNil(obs)): # TODO: should never be nil, but...
|
|
|
|
obs.onSend(p, msg)
|
2020-05-29 16:46:27 +00:00
|
|
|
|
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:
|
2020-05-25 14:33:24 +00:00
|
|
|
try:
|
|
|
|
while not conn.closed:
|
|
|
|
trace "waiting for data", peer = p.id, closed = conn.closed
|
|
|
|
let data = await conn.readLp(64 * 1024)
|
|
|
|
let digest = $(sha256.digest(data))
|
|
|
|
trace "read data from peer", peer = p.id, data = data.shortLog
|
|
|
|
if digest in p.recvdRpcCache:
|
2020-06-19 17:29:43 +00:00
|
|
|
libp2p_pubsub_skipped_received_messages.inc(labelValues = [p.id])
|
2020-05-25 14:33:24 +00:00
|
|
|
trace "message already received, skipping", peer = p.id
|
|
|
|
continue
|
|
|
|
|
|
|
|
var msg = decodeRpcMsg(data)
|
|
|
|
trace "decoded msg from peer", peer = p.id, msg = msg.shortLog
|
|
|
|
# trigger hooks
|
2020-06-02 23:53:38 +00:00
|
|
|
p.recvObservers(msg)
|
|
|
|
|
2020-06-15 23:39:03 +00:00
|
|
|
for m in msg.messages:
|
|
|
|
for t in m.topicIDs:
|
|
|
|
# metrics
|
|
|
|
libp2p_pubsub_received_messages.inc(labelValues = [p.id, t])
|
2020-06-11 18:09:34 +00:00
|
|
|
|
2020-05-25 14:33:24 +00:00
|
|
|
await p.handler(p, @[msg])
|
|
|
|
p.recvdRpcCache.put(digest)
|
|
|
|
finally:
|
|
|
|
trace "exiting pubsub peer read loop", peer = p.id
|
|
|
|
await conn.close()
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
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
|
2020-07-08 00:33:05 +00:00
|
|
|
raise exc
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} =
|
2020-07-08 00:33:05 +00:00
|
|
|
logScope:
|
|
|
|
peer = p.id
|
|
|
|
msgs = $msgs
|
|
|
|
|
2020-05-23 16:50:29 +00:00
|
|
|
for m in msgs.items:
|
2020-05-23 17:15:47 +00:00
|
|
|
trace "sending msgs to peer", toPeer = p.id, msgs = $msgs
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2020-05-29 16:46:27 +00:00
|
|
|
# trigger send hooks
|
|
|
|
var mm = m # hooks can modify the message
|
|
|
|
p.sendObservers(mm)
|
|
|
|
|
|
|
|
let encoded = encodeRpcMsg(mm)
|
2020-05-23 16:50:29 +00:00
|
|
|
if encoded.buffer.len <= 0:
|
|
|
|
trace "empty message, skipping", peer = p.id
|
|
|
|
return
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-23 16:50:29 +00:00
|
|
|
let digest = $(sha256.digest(encoded.buffer))
|
|
|
|
if digest in p.sentRpcCache:
|
|
|
|
trace "message already sent to peer, skipping", peer = p.id
|
2020-06-19 17:29:43 +00:00
|
|
|
libp2p_pubsub_skipped_sent_messages.inc(labelValues = [p.id])
|
2020-05-23 16:50:29 +00:00
|
|
|
continue
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
try:
|
|
|
|
trace "about to send message", peer = p.id,
|
|
|
|
encoded = digest
|
|
|
|
if p.connected: # this can happen if the remote disconnected
|
|
|
|
trace "sending encoded msgs to peer", peer = p.id,
|
|
|
|
encoded = encoded.buffer.shortLog
|
|
|
|
await p.sendConn.writeLp(encoded.buffer)
|
|
|
|
p.sentRpcCache.put(digest)
|
|
|
|
|
|
|
|
for m in msgs:
|
|
|
|
for mm in m.messages:
|
|
|
|
for t in mm.topicIDs:
|
|
|
|
# metrics
|
|
|
|
libp2p_pubsub_sent_messages.inc(labelValues = [p.id, t])
|
|
|
|
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "unable to send to remote", exc = exc.msg
|
|
|
|
if not(isNil(p.sendConn)):
|
|
|
|
await p.sendConn.close()
|
|
|
|
p.sendConn = nil
|
|
|
|
p.onConnect.clear()
|
|
|
|
|
|
|
|
raise exc
|
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.} =
|
2020-06-28 15:56:38 +00:00
|
|
|
p.send(@[RPCMsg(messages: @[Message.init(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.} =
|
2020-07-09 17:16:46 +00:00
|
|
|
try:
|
|
|
|
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)])))])
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Could not send graft", msg = exc.msg
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc sendPrune*(p: PubSubPeer, topics: seq[string]) {.async.} =
|
2020-07-09 17:16:46 +00:00
|
|
|
try:
|
|
|
|
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)])))])
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Could not send prune", msg = exc.msg
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
proc `$`*(p: PubSubPeer): string =
|
|
|
|
p.id
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
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()
|