2019-09-10 02:14:24 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-09-10 02:14:24 +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-05-06 16:31:47 +00:00
|
|
|
import sequtils, tables, sets, strutils
|
2020-06-07 07:15:21 +00:00
|
|
|
import chronos, chronicles, metrics
|
2019-09-10 02:15:52 +00:00
|
|
|
import pubsub,
|
|
|
|
pubsubpeer,
|
2019-12-06 02:16:18 +00:00
|
|
|
timedcache,
|
|
|
|
rpc/[messages, message],
|
2019-09-28 19:55:35 +00:00
|
|
|
../../crypto/crypto,
|
2019-09-10 02:15:52 +00:00
|
|
|
../../connection,
|
2020-03-23 06:03:36 +00:00
|
|
|
../../peer,
|
2020-05-06 16:31:47 +00:00
|
|
|
../../peerinfo,
|
2020-04-11 04:08:25 +00:00
|
|
|
../../utility,
|
|
|
|
../../errors
|
2019-09-10 02:14:24 +00:00
|
|
|
|
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "floodsub"
|
2019-09-10 02:14:24 +00:00
|
|
|
|
|
|
|
const FloodSubCodec* = "/floodsub/1.0.0"
|
|
|
|
|
|
|
|
type
|
2019-10-03 22:22:49 +00:00
|
|
|
FloodSub* = ref object of PubSub
|
2019-12-06 02:16:18 +00:00
|
|
|
floodsub*: Table[string, HashSet[string]] # topic to remote peer map
|
2019-12-10 20:50:35 +00:00
|
|
|
seen*: TimedCache[string] # list of messages forwarded to peers
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
method subscribeTopic*(f: FloodSub,
|
|
|
|
topic: string,
|
|
|
|
subscribe: bool,
|
2020-05-27 18:33:49 +00:00
|
|
|
peerId: string) {.gcsafe, async.} =
|
|
|
|
await procCall PubSub(f).subscribeTopic(topic, subscribe, peerId)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
if topic notin f.floodsub:
|
|
|
|
f.floodsub[topic] = initHashSet[string]()
|
|
|
|
|
|
|
|
if subscribe:
|
|
|
|
trace "adding subscription for topic", peer = peerId, name = topic
|
|
|
|
# subscribe the peer to the topic
|
|
|
|
f.floodsub[topic].incl(peerId)
|
|
|
|
else:
|
|
|
|
trace "removing subscription for topic", peer = peerId, name = topic
|
|
|
|
# unsubscribe the peer from the topic
|
|
|
|
f.floodsub[topic].excl(peerId)
|
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
method handleDisconnect*(f: FloodSub, peer: PubSubPeer) {.async.} =
|
2020-06-11 14:45:59 +00:00
|
|
|
await procCall PubSub(f).handleDisconnect(peer)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
## handle peer disconnects
|
|
|
|
for t in f.floodsub.keys:
|
|
|
|
f.floodsub[t].excl(peer.id)
|
|
|
|
|
|
|
|
method rpcHandler*(f: FloodSub,
|
|
|
|
peer: PubSubPeer,
|
2019-12-17 05:24:03 +00:00
|
|
|
rpcMsgs: seq[RPCMsg]) {.async.} =
|
|
|
|
await procCall PubSub(f).rpcHandler(peer, rpcMsgs)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-01-07 08:04:02 +00:00
|
|
|
for m in rpcMsgs: # for all RPC messages
|
2019-12-06 02:16:18 +00:00
|
|
|
if m.messages.len > 0: # if there are any messages
|
|
|
|
var toSendPeers: HashSet[string] = initHashSet[string]()
|
|
|
|
for msg in m.messages: # for every message
|
|
|
|
if msg.msgId notin f.seen:
|
|
|
|
f.seen.put(msg.msgId) # add the message to the seen cache
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
if f.verifySignature and not msg.verify(peer.peerInfo):
|
2019-12-17 05:24:03 +00:00
|
|
|
trace "dropping message due to failed signature verification"
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not (await f.validate(msg)):
|
|
|
|
trace "dropping message due to failed validation"
|
|
|
|
continue
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
for t in msg.topicIDs: # for every topic in the message
|
|
|
|
if t in f.floodsub:
|
|
|
|
toSendPeers.incl(f.floodsub[t]) # get all the peers interested in this topic
|
|
|
|
if t in f.topics: # check that we're subscribed to it
|
|
|
|
for h in f.topics[t].handler:
|
2019-12-23 18:45:12 +00:00
|
|
|
trace "calling handler for message", msg = msg.msgId,
|
|
|
|
topicId = t,
|
|
|
|
localPeer = f.peerInfo.id,
|
|
|
|
fromPeer = msg.fromPeerId().pretty
|
2019-12-06 02:16:18 +00:00
|
|
|
await h(t, msg.data) # trigger user provided handler
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2019-09-14 13:56:02 +00:00
|
|
|
# forward the message to all peers interested in it
|
2020-01-07 08:04:02 +00:00
|
|
|
var sent: seq[Future[void]]
|
2020-04-11 04:08:25 +00:00
|
|
|
# start the future but do not wait yet
|
2019-09-10 02:15:52 +00:00
|
|
|
for p in toSendPeers:
|
2019-10-03 19:29:28 +00:00
|
|
|
if p in f.peers and f.peers[p].id != peer.id:
|
2020-01-07 08:04:02 +00:00
|
|
|
sent.add(f.peers[p].send(@[RPCMsg(messages: m.messages)]))
|
2020-04-11 04:08:25 +00:00
|
|
|
# wait for all the futures now
|
|
|
|
sent = await allFinished(sent)
|
|
|
|
checkFutures(sent)
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2020-06-12 23:54:12 +00:00
|
|
|
method init*(f: FloodSub) =
|
2019-12-17 05:24:03 +00:00
|
|
|
proc handler(conn: Connection, proto: string) {.async.} =
|
2019-09-12 10:08:11 +00:00
|
|
|
## main protocol handler that gets triggered on every
|
2019-09-10 02:15:52 +00:00
|
|
|
## connection for a protocol string
|
|
|
|
## e.g. ``/floodsub/1.0.0``, etc...
|
|
|
|
##
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
await f.handleConn(conn, proto)
|
2019-09-10 02:14:24 +00:00
|
|
|
|
|
|
|
f.handler = handler
|
2019-09-10 02:15:52 +00:00
|
|
|
f.codec = FloodSubCodec
|
|
|
|
|
2020-05-21 17:33:48 +00:00
|
|
|
method subscribeToPeer*(p: FloodSub,
|
|
|
|
conn: Connection) {.async.} =
|
|
|
|
await procCall PubSub(p).subscribeToPeer(conn)
|
|
|
|
asyncCheck p.handleConn(conn, FloodSubCodec)
|
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
method publish*(f: FloodSub,
|
|
|
|
topic: string,
|
2019-12-17 05:24:03 +00:00
|
|
|
data: seq[byte]) {.async.} =
|
2019-10-03 22:22:49 +00:00
|
|
|
await procCall PubSub(f).publish(topic, data)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
if data.len <= 0 or topic.len <= 0:
|
|
|
|
trace "topic or data missing, skipping publish"
|
|
|
|
return
|
|
|
|
|
|
|
|
if topic notin f.floodsub:
|
|
|
|
trace "missing peers for topic, skipping publish"
|
|
|
|
return
|
|
|
|
|
|
|
|
trace "publishing on topic", name = topic
|
2020-05-06 09:26:08 +00:00
|
|
|
let msg = newMessage(f.peerInfo, data, topic, f.sign)
|
2020-01-07 08:04:02 +00:00
|
|
|
var sent: seq[Future[void]]
|
2020-04-11 04:08:25 +00:00
|
|
|
# start the future but do not wait yet
|
2019-12-06 02:16:18 +00:00
|
|
|
for p in f.floodsub[topic]:
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "publishing message", name = topic, peer = p, data = data.shortLog
|
2020-01-07 08:04:02 +00:00
|
|
|
sent.add(f.peers[p].send(@[RPCMsg(messages: @[msg])]))
|
2020-04-11 04:08:25 +00:00
|
|
|
# wait for all the futures now
|
|
|
|
sent = await allFinished(sent)
|
|
|
|
checkFutures(sent)
|
2019-09-10 02:14:24 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method unsubscribe*(f: FloodSub,
|
2019-12-17 05:24:03 +00:00
|
|
|
topics: seq[TopicPair]) {.async.} =
|
2019-09-10 02:15:52 +00:00
|
|
|
await procCall PubSub(f).unsubscribe(topics)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
for p in f.peers.values:
|
2019-09-24 16:16:39 +00:00
|
|
|
await f.sendSubs(p, topics.mapIt(it.topic).deduplicate(), false)
|
2019-09-10 02:14:24 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
method initPubSub*(f: FloodSub) =
|
2020-04-30 13:22:31 +00:00
|
|
|
procCall PubSub(f).initPubSub()
|
2019-10-03 22:22:49 +00:00
|
|
|
f.peers = initTable[string, PubSubPeer]()
|
|
|
|
f.topics = initTable[string, Topic]()
|
2019-12-06 02:16:18 +00:00
|
|
|
f.floodsub = initTable[string, HashSet[string]]()
|
|
|
|
f.seen = newTimedCache[string](2.minutes)
|
2019-10-03 22:22:49 +00:00
|
|
|
f.init()
|