2020-04-29 13:02:42 +08: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 05:32:41 +02:00
|
|
|
import
|
|
|
|
chronos, chronicles, metrics,
|
2020-09-02 16:52:00 +02:00
|
|
|
libp2p/protocols/pubsub/[pubsub, floodsub, gossipsub],
|
2020-09-01 23:20:38 +08:00
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
2020-09-16 12:23:10 +08:00
|
|
|
libp2p/stream/connection,
|
|
|
|
../../node/v2/waku_types
|
2020-08-31 05:32:41 +02:00
|
|
|
|
2020-05-27 12:00:50 +08:00
|
|
|
declarePublicGauge total_messages, "number of messages received"
|
|
|
|
|
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
|
|
|
|
2020-09-17 19:24:27 +08:00
|
|
|
const WakuRelayCodec* = "/vac/waku/relay/2.0.0-beta1"
|
2020-05-01 13:00:00 +08:00
|
|
|
|
2020-09-01 23:20:38 +08:00
|
|
|
method init*(w: WakuRelay) =
|
2020-05-15 11:29:01 +08:00
|
|
|
debug "init"
|
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"
|
2020-05-11 12:05:28 +08:00
|
|
|
await w.handleConn(conn, proto)
|
|
|
|
|
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
|
|
|
|
2020-08-26 19:28:24 +08:00
|
|
|
method initPubSub*(w: WakuRelay) =
|
|
|
|
debug "initWakuRelay"
|
2020-06-02 19:27:53 +08:00
|
|
|
|
2020-07-24 09:39:58 +08:00
|
|
|
# Not using GossipSub
|
2020-09-16 12:23:10 +08:00
|
|
|
# XXX: FloodSub subscribe doesn't work
|
|
|
|
w.gossipEnabled = true
|
2020-06-02 19:27:53 +08:00
|
|
|
|
2020-08-31 05:32:41 +02:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 19:27:53 +08:00
|
|
|
procCall GossipSub(w).initPubSub()
|
|
|
|
else:
|
|
|
|
procCall FloodSub(w).initPubSub()
|
|
|
|
|
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,
|
2020-05-22 15:35:31 +08:00
|
|
|
handler: TopicHandler) {.async.} =
|
2020-09-10 12:18:35 +08:00
|
|
|
debug "subscribe", pubSubTopic=pubSubTopic
|
2020-08-31 05:32:41 +02:00
|
|
|
if w.gossipEnabled:
|
2020-09-10 12:18:35 +08:00
|
|
|
await procCall GossipSub(w).subscribe(pubSubTopic, handler)
|
2020-06-02 19:27:53 +08:00
|
|
|
else:
|
2020-09-10 12:18:35 +08:00
|
|
|
await procCall FloodSub(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.} =
|
2020-09-10 12:18:35 +08:00
|
|
|
debug "publish", pubSubTopic=pubSubTopic, message=message
|
2020-06-02 19:27:53 +08:00
|
|
|
|
2020-08-31 05:32:41 +02:00
|
|
|
if w.gossipEnabled:
|
2020-09-10 12:18:35 +08:00
|
|
|
return await procCall GossipSub(w).publish(pubSubTopic, message)
|
2020-06-02 19:27:53 +08:00
|
|
|
else:
|
2020-09-10 12:18:35 +08:00
|
|
|
return await procCall FloodSub(w).publish(pubSubTopic, message)
|
2020-05-15 11:29:01 +08:00
|
|
|
|
2020-08-26 19:28:24 +08:00
|
|
|
method unsubscribe*(w: WakuRelay,
|
2020-05-15 11:29:01 +08:00
|
|
|
topics: seq[TopicPair]) {.async.} =
|
|
|
|
debug "unsubscribe"
|
2020-08-31 05:32:41 +02:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 19:27:53 +08:00
|
|
|
await procCall GossipSub(w).unsubscribe(topics)
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).unsubscribe(topics)
|
2020-06-01 11:42:16 +08: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-08-31 05:32:41 +02:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 19:27:53 +08:00
|
|
|
await procCall GossipSub(w).start()
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(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"
|
2020-08-31 05:32:41 +02:00
|
|
|
if w.gossipEnabled:
|
2020-06-02 19:27:53 +08:00
|
|
|
await procCall GossipSub(w).stop()
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).stop()
|