2020-04-29 05:02:42 +00:00
|
|
|
## Waku on libp2p
|
|
|
|
##
|
|
|
|
## This file should eventually correspond to waku_protocol as RLPx subprotocol.
|
|
|
|
## Instead, it should likely be on top of GossipSub with a similar interface.
|
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
import
|
|
|
|
std/[strutils, tables],
|
|
|
|
chronos, chronicles, metrics,
|
2020-09-01 15:20:38 +00:00
|
|
|
libp2p/protocols/pubsub/[pubsub, pubsubpeer, floodsub, gossipsub],
|
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
2020-08-31 03:32:41 +00:00
|
|
|
libp2p/stream/connection,
|
2020-09-01 15:20:38 +00:00
|
|
|
../../node/v2/waku_types,
|
2020-08-31 03:32:41 +00:00
|
|
|
./filter
|
|
|
|
|
2020-05-27 04:00:50 +00:00
|
|
|
declarePublicGauge total_messages, "number of messages received"
|
|
|
|
|
2020-05-01 05:00:00 +00:00
|
|
|
logScope:
|
2020-08-26 11:28:24 +00:00
|
|
|
topic = "WakuRelay"
|
2020-05-01 05:00:00 +00:00
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
const WakuRelayCodec* = "/vac/waku/relay/2.0.0-alpha2"
|
2020-05-01 05:00:00 +00:00
|
|
|
|
|
|
|
type
|
2020-08-26 11:28:24 +00:00
|
|
|
WakuRelay* = ref object of GossipSub
|
2020-08-31 03:32:41 +00:00
|
|
|
gossipEnabled*: bool
|
2020-05-01 05:00:00 +00:00
|
|
|
|
2020-09-01 15:20:38 +00:00
|
|
|
method init*(w: WakuRelay) =
|
2020-05-15 03:29:01 +00:00
|
|
|
debug "init"
|
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"
|
2020-05-11 04:05:28 +00:00
|
|
|
await w.handleConn(conn, proto)
|
|
|
|
|
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
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
method initPubSub*(w: WakuRelay) =
|
|
|
|
debug "initWakuRelay"
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2020-07-24 01:39:58 +00:00
|
|
|
# Not using GossipSub
|
2020-08-31 03:32:41 +00:00
|
|
|
w.gossipEnabled = false
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 11:27:53 +00:00
|
|
|
procCall GossipSub(w).initPubSub()
|
|
|
|
else:
|
|
|
|
procCall FloodSub(w).initPubSub()
|
|
|
|
|
2020-05-11 04:05:28 +00:00
|
|
|
w.init()
|
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
method subscribe*(w: WakuRelay,
|
2020-05-22 07:35:31 +00:00
|
|
|
topic: string,
|
|
|
|
handler: TopicHandler) {.async.} =
|
|
|
|
debug "subscribe", topic=topic
|
2020-08-31 03:32:41 +00:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 11:27:53 +00:00
|
|
|
await procCall GossipSub(w).subscribe(topic, handler)
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).subscribe(topic, handler)
|
2020-05-22 07:35:31 +00:00
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
method publish*(w: WakuRelay,
|
2020-05-15 03:29:01 +00:00
|
|
|
topic: string,
|
2020-09-02 03:15:25 +00:00
|
|
|
message: seq[byte]
|
2020-09-01 15:20:38 +00:00
|
|
|
): Future[int] {.async.} =
|
2020-09-02 03:15:25 +00:00
|
|
|
debug "publish", topic=topic
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
if w.gossipEnabled:
|
2020-09-02 03:15:25 +00:00
|
|
|
return await procCall GossipSub(w).publish(topic, message)
|
2020-06-02 11:27:53 +00:00
|
|
|
else:
|
2020-09-02 03:15:25 +00:00
|
|
|
return await procCall FloodSub(w).publish(topic, message)
|
2020-05-15 03:29:01 +00:00
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
method unsubscribe*(w: WakuRelay,
|
2020-05-15 03:29:01 +00:00
|
|
|
topics: seq[TopicPair]) {.async.} =
|
|
|
|
debug "unsubscribe"
|
2020-08-31 03:32:41 +00:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 11:27:53 +00:00
|
|
|
await procCall GossipSub(w).unsubscribe(topics)
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).unsubscribe(topics)
|
2020-06-01 03:42:16 +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-08-31 03:32:41 +00:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 11:27:53 +00:00
|
|
|
await procCall GossipSub(w).start()
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(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"
|
2020-08-31 03:32:41 +00:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 11:27:53 +00:00
|
|
|
await procCall GossipSub(w).stop()
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).stop()
|
2020-09-02 03:08:48 +00:00
|
|
|
|
|
|
|
proc addFilter*(w: WakuRelay, name: string, filter: Filter) =
|
|
|
|
w.filters.subscribe(name, filter)
|