nwaku/waku/protocol/v2/waku_relay.nim

92 lines
2.5 KiB
Nim
Raw Normal View History

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.
import
std/strutils,
chronos, chronicles, metrics,
libp2p/protocols/pubsub/[pubsub, floodsub, gossipsub],
libp2p/protocols/pubsub/rpc/messages,
libp2p/stream/connection,
../../node/v2/waku_types
2020-05-27 04:00:50 +00:00
declarePublicGauge total_messages, "number of messages received"
2020-05-01 05:00:00 +00:00
logScope:
topics = "wakurelay"
2020-05-01 05:00:00 +00:00
const WakuRelayCodec* = "/vac/waku/relay/2.0.0-beta1"
2020-05-01 05:00:00 +00:00
method init*(w: WakuRelay) =
debug "init"
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...
##
debug "Incoming WakuRelay connection"
await w.handleConn(conn, proto)
# XXX: Handler hijack GossipSub here?
w.handler = handler
w.codec = WakuRelayCodec
method initPubSub*(w: WakuRelay) =
debug "initWakuRelay"
2020-06-02 11:27:53 +00:00
# Not using GossipSub
# XXX: FloodSub subscribe doesn't work
w.gossipEnabled = true
2020-06-02 11:27:53 +00:00
if w.gossipEnabled:
2020-06-02 11:27:53 +00:00
procCall GossipSub(w).initPubSub()
else:
procCall FloodSub(w).initPubSub()
w.init()
method subscribe*(w: WakuRelay,
pubSubTopic: string,
handler: TopicHandler) {.async.} =
debug "subscribe", pubSubTopic=pubSubTopic
if w.gossipEnabled:
await procCall GossipSub(w).subscribe(pubSubTopic, handler)
2020-06-02 11:27:53 +00:00
else:
await procCall FloodSub(w).subscribe(pubSubTopic, handler)
method publish*(w: WakuRelay,
pubSubTopic: string,
message: seq[byte]
): Future[int] {.async.} =
debug "publish", pubSubTopic=pubSubTopic, message=message
2020-06-02 11:27:53 +00:00
if w.gossipEnabled:
return await procCall GossipSub(w).publish(pubSubTopic, message)
2020-06-02 11:27:53 +00:00
else:
return await procCall FloodSub(w).publish(pubSubTopic, message)
method unsubscribe*(w: WakuRelay,
topics: seq[TopicPair]) {.async.} =
debug "unsubscribe"
if w.gossipEnabled:
2020-06-02 11:27:53 +00:00
await procCall GossipSub(w).unsubscribe(topics)
else:
await procCall FloodSub(w).unsubscribe(topics)
# GossipSub specific methods --------------------------------------------------
method start*(w: WakuRelay) {.async.} =
debug "start"
if w.gossipEnabled:
2020-06-02 11:27:53 +00:00
await procCall GossipSub(w).start()
else:
await procCall FloodSub(w).start()
method stop*(w: WakuRelay) {.async.} =
debug "stop"
if w.gossipEnabled:
2020-06-02 11:27:53 +00:00
await procCall GossipSub(w).stop()
else:
await procCall FloodSub(w).stop()