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.
|
|
|
|
|
2020-07-16 12:59:03 +00:00
|
|
|
import std/[hashes, options, sequtils, strutils, tables, hashes, sets]
|
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-08-12 00:05:49 +00:00
|
|
|
../../switch,
|
2020-07-04 02:29:37 +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-08-04 23:27:59 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
declareCounter(libp2p_pubsub_sent_messages, "number of messages sent", labels = ["id", "topic"])
|
|
|
|
declareCounter(libp2p_pubsub_received_messages, "number of messages received", labels = ["id", "topic"])
|
|
|
|
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
|
|
|
|
2020-08-03 05:20:11 +00:00
|
|
|
const
|
|
|
|
DefaultSendTimeout* = 10.seconds
|
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-08-12 00:05:49 +00:00
|
|
|
switch*: Switch # switch instance to dial peers
|
|
|
|
codec*: string # the protocol that this peer joined from
|
2020-04-30 13:22:31 +00:00
|
|
|
sendConn: Connection
|
2020-08-12 00:05:49 +00:00
|
|
|
peerId*: PeerID
|
2020-04-30 13:22:31 +00:00
|
|
|
handler*: RPCHandler
|
2020-07-04 03:15:35 +00:00
|
|
|
topics*: HashSet[string]
|
2020-07-15 19:18:55 +00:00
|
|
|
sentRpcCache: TimedCache[string] # cache for already sent messages
|
|
|
|
recvdRpcCache: TimedCache[string] # cache for already received messages
|
2020-04-30 13:22:31 +00:00
|
|
|
observers*: ref seq[PubSubObserver] # ref as in smart_ptr
|
2020-08-12 00:05:49 +00:00
|
|
|
subscribed*: bool # are we subscribed to this peer
|
|
|
|
sendLock*: AsyncLock # send connection lock
|
2020-07-16 12:53:45 +00:00
|
|
|
|
2020-07-19 03:37:45 +00:00
|
|
|
score*: float64
|
|
|
|
|
2020-04-30 13:22:31 +00:00
|
|
|
RPCHandler* = proc(peer: PubSubPeer, msg: seq[RPCMsg]): Future[void] {.gcsafe.}
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2020-07-18 09:09:40 +00:00
|
|
|
chronicles.formatIt(PubSubPeer): it.peerInfo.id
|
|
|
|
|
2020-07-13 13:32:38 +00:00
|
|
|
func hash*(p: PubSubPeer): Hash =
|
|
|
|
# int is either 32/64, so intptr basically, pubsubpeer is a ref
|
|
|
|
cast[pointer](p).hash
|
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
proc id*(p: PubSubPeer): string =
|
|
|
|
doAssert(not p.isNil, "nil pubsubpeer")
|
|
|
|
p.peerId.pretty
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-04 02:29:37 +00:00
|
|
|
proc connected*(p: PubSubPeer): bool =
|
2020-08-12 00:05:49 +00:00
|
|
|
not p.sendConn.isNil and not
|
|
|
|
(p.sendConn.closed or p.sendConn.atEof)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
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-07-04 02:29:37 +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-07-04 02:29:37 +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.} =
|
2020-07-16 19:26:57 +00:00
|
|
|
logScope:
|
|
|
|
peer = p.id
|
2020-08-12 00:05:49 +00:00
|
|
|
|
2020-07-16 19:26:57 +00:00
|
|
|
debug "starting pubsub read loop for peer", closed = conn.closed
|
2019-09-10 02:15:52 +00:00
|
|
|
try:
|
2020-05-25 14:33:24 +00:00
|
|
|
try:
|
2020-08-03 05:20:11 +00:00
|
|
|
while not conn.atEof:
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "waiting for data", closed = conn.closed
|
2020-08-12 00:05:49 +00:00
|
|
|
let data = await conn.readLp(64 * 1024)
|
2020-05-25 14:33:24 +00:00
|
|
|
let digest = $(sha256.digest(data))
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "read data from peer", data = data.shortLog
|
2020-05-25 14:33:24 +00:00
|
|
|
if digest in p.recvdRpcCache:
|
2020-08-04 23:27:59 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_pubsub_skipped_received_messages.inc(labelValues = [p.id])
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "message already received, skipping"
|
2020-05-25 14:33:24 +00:00
|
|
|
continue
|
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
var rmsg = decodeRpcMsg(data)
|
|
|
|
if rmsg.isErr():
|
2020-07-16 19:26:57 +00:00
|
|
|
notice "failed to decode msg from peer"
|
2020-07-15 08:25:39 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
var msg = rmsg.get()
|
|
|
|
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "decoded msg from peer", msg = msg.shortLog
|
2020-05-25 14:33:24 +00:00
|
|
|
# trigger hooks
|
2020-06-02 23:53:38 +00:00
|
|
|
p.recvObservers(msg)
|
|
|
|
|
2020-08-04 23:27:59 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
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:
|
2020-07-16 19:26:57 +00:00
|
|
|
debug "exiting pubsub peer read loop"
|
2020-05-25 14:33:24 +00:00
|
|
|
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
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2020-08-03 05:20:11 +00:00
|
|
|
proc send*(
|
|
|
|
p: PubSubPeer,
|
|
|
|
msg: RPCMsg,
|
|
|
|
timeout: Duration = DefaultSendTimeout) {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
|
|
|
|
doAssert(not isNil(p), "pubsubpeer nil!")
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
logScope:
|
|
|
|
peer = p.id
|
2020-08-07 22:46:00 +00:00
|
|
|
rpcMsg = shortLog(msg)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-16 10:06:57 +00:00
|
|
|
trace "sending msg to peer"
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2020-07-16 10:06:57 +00:00
|
|
|
# trigger send hooks
|
|
|
|
var mm = msg # hooks can modify the message
|
|
|
|
p.sendObservers(mm)
|
2020-05-29 16:46:27 +00:00
|
|
|
|
2020-07-16 10:06:57 +00:00
|
|
|
let encoded = encodeRpcMsg(mm)
|
|
|
|
if encoded.len <= 0:
|
2020-08-03 05:20:11 +00:00
|
|
|
info "empty message, skipping"
|
2020-07-16 10:06:57 +00:00
|
|
|
return
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-16 10:06:57 +00:00
|
|
|
logScope:
|
|
|
|
encoded = shortLog(encoded)
|
|
|
|
|
|
|
|
let digest = $(sha256.digest(encoded))
|
|
|
|
if digest in p.sentRpcCache:
|
|
|
|
trace "message already sent to peer, skipping"
|
2020-08-04 23:27:59 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_pubsub_skipped_sent_messages.inc(labelValues = [p.id])
|
2020-07-16 10:06:57 +00:00
|
|
|
return
|
2020-05-23 16:50:29 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
try:
|
2020-07-16 10:06:57 +00:00
|
|
|
trace "about to send message"
|
2020-08-12 00:05:49 +00:00
|
|
|
if not p.connected:
|
|
|
|
try:
|
|
|
|
await p.sendLock.acquire()
|
|
|
|
trace "no send connection, dialing peer"
|
|
|
|
# get a send connection if there is none
|
|
|
|
p.sendConn = await p.switch.dial(
|
|
|
|
p.peerId, p.codec)
|
|
|
|
|
|
|
|
if not p.connected:
|
|
|
|
raise newException(CatchableError, "unable to get send pubsub stream")
|
|
|
|
|
|
|
|
# install a reader on the send connection
|
|
|
|
asyncCheck p.handle(p.sendConn)
|
|
|
|
finally:
|
|
|
|
if p.sendLock.locked:
|
|
|
|
p.sendLock.release()
|
|
|
|
|
|
|
|
trace "sending encoded msgs to peer"
|
|
|
|
await p.sendConn.writeLp(encoded).wait(timeout)
|
|
|
|
p.sentRpcCache.put(digest)
|
|
|
|
trace "sent pubsub message to remote"
|
2020-08-03 05:20:11 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
for x in mm.messages:
|
|
|
|
for t in x.topicIDs:
|
|
|
|
# metrics
|
|
|
|
libp2p_pubsub_sent_messages.inc(labelValues = [p.id, t])
|
2020-07-16 10:06:57 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
except CatchableError as exc:
|
2020-07-16 10:06:57 +00:00
|
|
|
trace "unable to send to remote", exc = exc.msg
|
|
|
|
if not(isNil(p.sendConn)):
|
|
|
|
await p.sendConn.close()
|
|
|
|
p.sendConn = nil
|
|
|
|
|
2020-08-03 05:20:11 +00:00
|
|
|
raise exc
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
proc `$`*(p: PubSubPeer): string =
|
|
|
|
p.id
|
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
proc newPubSubPeer*(peerId: PeerID,
|
|
|
|
switch: Switch,
|
|
|
|
codec: string): PubSubPeer =
|
2019-09-10 02:15:52 +00:00
|
|
|
new result
|
2020-08-12 00:05:49 +00:00
|
|
|
result.switch = switch
|
|
|
|
result.codec = codec
|
|
|
|
result.peerId = peerId
|
2019-12-06 02:16:18 +00:00
|
|
|
result.sentRpcCache = newTimedCache[string](2.minutes)
|
|
|
|
result.recvdRpcCache = newTimedCache[string](2.minutes)
|
2020-07-04 03:15:35 +00:00
|
|
|
result.topics = initHashSet[string]()
|
2020-08-12 00:05:49 +00:00
|
|
|
result.sendLock = newAsyncLock()
|