2021-01-06 17:35:05 +08:00
|
|
|
## Waku Relay module. Thin layer on top of GossipSub.
|
2020-04-29 13:02:42 +08:00
|
|
|
##
|
2021-01-06 17:35:05 +08:00
|
|
|
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-relay.md
|
|
|
|
## for spec.
|
2021-06-09 16:37:08 +02:00
|
|
|
{.push raises: [Defect].}
|
2020-04-29 13:02:42 +08:00
|
|
|
|
2020-08-31 05:32:41 +02:00
|
|
|
import
|
2021-06-09 16:37:08 +02:00
|
|
|
std/[tables, sets],
|
2020-08-31 05:32:41 +02:00
|
|
|
chronos, chronicles, metrics,
|
2020-11-24 10:14:18 +01:00
|
|
|
libp2p/protocols/pubsub/[pubsub, gossipsub],
|
2020-09-01 23:20:38 +08:00
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
2021-01-06 17:35:05 +08:00
|
|
|
libp2p/stream/connection
|
2020-08-31 05:32:41 +02:00
|
|
|
|
2020-05-01 13:00:00 +08:00
|
|
|
logScope:
|
2020-09-16 12:23:10 +08:00
|
|
|
topics = "wakurelay"
|
2020-05-01 13:00:00 +08:00
|
|
|
|
2021-04-30 14:07:46 +02:00
|
|
|
const
|
2021-06-29 16:29:04 +02:00
|
|
|
WakuRelayCodec* = "/vac/waku/relay/2.0.0"
|
2020-05-01 13:00:00 +08:00
|
|
|
|
2021-01-06 17:35:05 +08:00
|
|
|
type
|
|
|
|
WakuRelay* = ref object of GossipSub
|
2021-07-22 11:46:54 +02:00
|
|
|
defaultTopics*: seq[string] # Default configured PubSub topics
|
2021-01-06 17:35:05 +08:00
|
|
|
|
2020-09-01 23:20:38 +08:00
|
|
|
method init*(w: WakuRelay) =
|
2021-07-22 11:46:54 +02:00
|
|
|
debug "init WakuRelay"
|
|
|
|
|
2020-05-11 12:05:28 +08: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 19:28:24 +08:00
|
|
|
debug "Incoming WakuRelay connection"
|
2021-06-09 16:37:08 +02: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 12:05:28 +08:00
|
|
|
|
2020-06-01 11:42:16 +08:00
|
|
|
# XXX: Handler hijack GossipSub here?
|
2020-05-11 12:05:28 +08:00
|
|
|
w.handler = handler
|
2020-08-26 19:28:24 +08:00
|
|
|
w.codec = WakuRelayCodec
|
2020-05-11 12:05:28 +08:00
|
|
|
|
2021-06-09 16:37:08 +02:00
|
|
|
method initPubSub*(w: WakuRelay) {.raises: [Defect, InitializationError].} =
|
2020-08-26 19:28:24 +08:00
|
|
|
debug "initWakuRelay"
|
2020-06-02 19:27:53 +08:00
|
|
|
|
2020-12-17 17:06:59 +01: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 10:14:18 +01:00
|
|
|
procCall GossipSub(w).initPubSub()
|
2020-06-02 19:27:53 +08:00
|
|
|
|
2020-05-11 12:05:28 +08:00
|
|
|
w.init()
|
|
|
|
|
2020-08-26 19:28:24 +08:00
|
|
|
method subscribe*(w: WakuRelay,
|
2020-09-10 12:18:35 +08:00
|
|
|
pubSubTopic: string,
|
2021-02-02 13:33:59 +02:00
|
|
|
handler: TopicHandler) =
|
2020-09-10 12:18:35 +08:00
|
|
|
debug "subscribe", pubSubTopic=pubSubTopic
|
2020-11-24 10:14:18 +01:00
|
|
|
|
2021-02-02 13:33:59 +02:00
|
|
|
procCall GossipSub(w).subscribe(pubSubTopic, handler)
|
2020-05-22 15:35:31 +08:00
|
|
|
|
2020-08-26 19:28:24 +08:00
|
|
|
method publish*(w: WakuRelay,
|
2020-09-10 12:18:35 +08:00
|
|
|
pubSubTopic: string,
|
2020-09-02 05:15:25 +02:00
|
|
|
message: seq[byte]
|
2020-09-01 23:20:38 +08:00
|
|
|
): Future[int] {.async.} =
|
2022-02-28 18:16:50 +01:00
|
|
|
trace "publish", pubSubTopic=pubSubTopic, message=message
|
2020-06-02 19:27:53 +08:00
|
|
|
|
2020-11-24 10:14:18 +01:00
|
|
|
return await procCall GossipSub(w).publish(pubSubTopic, message)
|
2020-05-15 11:29:01 +08:00
|
|
|
|
2020-08-26 19:28:24 +08:00
|
|
|
method unsubscribe*(w: WakuRelay,
|
2021-02-02 13:33:59 +02:00
|
|
|
topics: seq[TopicPair]) =
|
2020-05-15 11:29:01 +08:00
|
|
|
debug "unsubscribe"
|
2020-11-24 10:14:18 +01:00
|
|
|
|
2021-02-02 13:33:59 +02:00
|
|
|
procCall GossipSub(w).unsubscribe(topics)
|
2020-06-01 11:42:16 +08:00
|
|
|
|
2020-10-27 03:13:56 +02:00
|
|
|
method unsubscribeAll*(w: WakuRelay,
|
2021-02-02 13:33:59 +02:00
|
|
|
pubSubTopic: string) =
|
2020-10-27 03:13:56 +02:00
|
|
|
debug "unsubscribeAll"
|
2020-11-24 10:14:18 +01:00
|
|
|
|
2021-02-02 13:33:59 +02:00
|
|
|
procCall GossipSub(w).unsubscribeAll(pubSubTopic)
|
2020-10-27 03:13:56 +02:00
|
|
|
|
2020-09-01 23:20:38 +08:00
|
|
|
# GossipSub specific methods --------------------------------------------------
|
2020-08-26 19:28:24 +08:00
|
|
|
method start*(w: WakuRelay) {.async.} =
|
2020-06-01 11:42:16 +08:00
|
|
|
debug "start"
|
2020-11-24 10:14:18 +01:00
|
|
|
await procCall GossipSub(w).start()
|
2020-06-01 11:42:16 +08:00
|
|
|
|
2020-08-26 19:28:24 +08:00
|
|
|
method stop*(w: WakuRelay) {.async.} =
|
2020-06-01 11:42:16 +08:00
|
|
|
debug "stop"
|
2021-04-30 14:07:46 +02:00
|
|
|
|
2020-11-24 10:14:18 +01:00
|
|
|
await procCall GossipSub(w).stop()
|