2021-01-06 09:35:05 +00:00
|
|
|
## Waku Relay module. Thin layer on top of GossipSub.
|
2020-04-29 05:02:42 +00:00
|
|
|
##
|
2021-01-06 09:35:05 +00:00
|
|
|
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-relay.md
|
|
|
|
## for spec.
|
2021-06-09 14:37:08 +00:00
|
|
|
{.push raises: [Defect].}
|
2020-04-29 05:02:42 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
import
|
2021-06-09 14:37:08 +00:00
|
|
|
std/[tables, sets],
|
2020-08-31 03:32:41 +00:00
|
|
|
chronos, chronicles, metrics,
|
2020-11-24 09:14:18 +00:00
|
|
|
libp2p/protocols/pubsub/[pubsub, gossipsub],
|
2020-09-01 15:20:38 +00:00
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
2021-01-06 09:35:05 +00:00
|
|
|
libp2p/stream/connection
|
2020-08-31 03:32:41 +00:00
|
|
|
|
2020-05-01 05:00:00 +00:00
|
|
|
logScope:
|
2020-09-16 04:23:10 +00:00
|
|
|
topics = "wakurelay"
|
2020-05-01 05:00:00 +00:00
|
|
|
|
2021-04-30 12:07:46 +00:00
|
|
|
const
|
2021-06-29 14:29:04 +00:00
|
|
|
WakuRelayCodec* = "/vac/waku/relay/2.0.0"
|
2020-05-01 05:00:00 +00:00
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
type
|
|
|
|
WakuRelay* = ref object of GossipSub
|
2021-07-22 09:46:54 +00:00
|
|
|
defaultTopics*: seq[string] # Default configured PubSub topics
|
2021-01-06 09:35:05 +00:00
|
|
|
|
2020-09-01 15:20:38 +00:00
|
|
|
method init*(w: WakuRelay) =
|
2021-07-22 09:46:54 +00:00
|
|
|
debug "init WakuRelay"
|
|
|
|
|
2020-05-11 04:05:28 +00:00
|
|
|
proc handler(conn: Connection, proto: string) {.async.} =
|
|
|
|
## main protocol handler that gets triggered on every
|
|
|
|
## connection for a protocol string
|
|
|
|
## e.g. ``/wakusub/0.0.1``, etc...
|
|
|
|
##
|
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
debug "Incoming WakuRelay connection"
|
2021-06-09 14:37:08 +00:00
|
|
|
try:
|
|
|
|
await w.handleConn(conn, proto)
|
|
|
|
except CancelledError:
|
|
|
|
# This is top-level procedure which will work as separate task, so it
|
|
|
|
# do not need to propogate CancelledError.
|
|
|
|
trace "Unexpected cancellation in relay handler", conn
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "WakuRelay handler leaks an error", exc = exc.msg, conn
|
2020-05-11 04:05:28 +00:00
|
|
|
|
2020-06-01 03:42:16 +00:00
|
|
|
# XXX: Handler hijack GossipSub here?
|
2020-05-11 04:05:28 +00:00
|
|
|
w.handler = handler
|
2020-08-26 11:28:24 +00:00
|
|
|
w.codec = WakuRelayCodec
|
2020-05-11 04:05:28 +00:00
|
|
|
|
2021-06-09 14:37:08 +00:00
|
|
|
method initPubSub*(w: WakuRelay) {.raises: [Defect, InitializationError].} =
|
2020-08-26 11:28:24 +00:00
|
|
|
debug "initWakuRelay"
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2020-12-17 16:06:59 +00:00
|
|
|
# after discussions with @sinkingsugar, this is essentially what is needed for
|
|
|
|
# the libp2p `StrictNoSign` policy
|
|
|
|
w.anonymize = true
|
|
|
|
w.verifySignature = false
|
|
|
|
w.sign = false
|
|
|
|
|
2020-11-24 09:14:18 +00:00
|
|
|
procCall GossipSub(w).initPubSub()
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2020-05-11 04:05:28 +00:00
|
|
|
w.init()
|
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
method subscribe*(w: WakuRelay,
|
2020-09-10 04:18:35 +00:00
|
|
|
pubSubTopic: string,
|
2021-02-02 11:33:59 +00:00
|
|
|
handler: TopicHandler) =
|
2020-09-10 04:18:35 +00:00
|
|
|
debug "subscribe", pubSubTopic=pubSubTopic
|
2020-11-24 09:14:18 +00:00
|
|
|
|
2021-02-02 11:33:59 +00:00
|
|
|
procCall GossipSub(w).subscribe(pubSubTopic, handler)
|
2020-05-22 07:35:31 +00:00
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
method publish*(w: WakuRelay,
|
2020-09-10 04:18:35 +00:00
|
|
|
pubSubTopic: string,
|
2020-09-02 03:15:25 +00:00
|
|
|
message: seq[byte]
|
2020-09-01 15:20:38 +00:00
|
|
|
): Future[int] {.async.} =
|
2022-02-28 17:16:50 +00:00
|
|
|
trace "publish", pubSubTopic=pubSubTopic, message=message
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2020-11-24 09:14:18 +00:00
|
|
|
return await procCall GossipSub(w).publish(pubSubTopic, message)
|
2020-05-15 03:29:01 +00:00
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
method unsubscribe*(w: WakuRelay,
|
2021-02-02 11:33:59 +00:00
|
|
|
topics: seq[TopicPair]) =
|
2020-05-15 03:29:01 +00:00
|
|
|
debug "unsubscribe"
|
2020-11-24 09:14:18 +00:00
|
|
|
|
2021-02-02 11:33:59 +00:00
|
|
|
procCall GossipSub(w).unsubscribe(topics)
|
2020-06-01 03:42:16 +00:00
|
|
|
|
2020-10-27 01:13:56 +00:00
|
|
|
method unsubscribeAll*(w: WakuRelay,
|
2021-02-02 11:33:59 +00:00
|
|
|
pubSubTopic: string) =
|
2020-10-27 01:13:56 +00:00
|
|
|
debug "unsubscribeAll"
|
2020-11-24 09:14:18 +00:00
|
|
|
|
2021-02-02 11:33:59 +00:00
|
|
|
procCall GossipSub(w).unsubscribeAll(pubSubTopic)
|
2020-10-27 01:13:56 +00:00
|
|
|
|
2020-09-01 15:20:38 +00:00
|
|
|
# GossipSub specific methods --------------------------------------------------
|
2020-08-26 11:28:24 +00:00
|
|
|
method start*(w: WakuRelay) {.async.} =
|
2020-06-01 03:42:16 +00:00
|
|
|
debug "start"
|
2020-11-24 09:14:18 +00:00
|
|
|
await procCall GossipSub(w).start()
|
2020-06-01 03:42:16 +00:00
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
method stop*(w: WakuRelay) {.async.} =
|
2020-06-01 03:42:16 +00:00
|
|
|
debug "stop"
|
2021-04-30 12:07:46 +00:00
|
|
|
|
2020-11-24 09:14:18 +00:00
|
|
|
await procCall GossipSub(w).stop()
|