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-05-15 03:35:32 +00:00
|
|
|
import strutils
|
2020-05-01 05:00:00 +00:00
|
|
|
import chronos, chronicles
|
2020-05-11 04:05:28 +00:00
|
|
|
import libp2p/protocols/pubsub/pubsub,
|
|
|
|
libp2p/protocols/pubsub/pubsubpeer,
|
2020-06-02 11:27:53 +00:00
|
|
|
libp2p/protocols/pubsub/floodsub,
|
2020-06-01 03:42:16 +00:00
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
2020-05-15 03:35:32 +00:00
|
|
|
libp2p/protocols/pubsub/rpc/[messages],
|
2020-07-13 10:08:03 +00:00
|
|
|
libp2p/stream/connection
|
2020-05-01 05:00:00 +00:00
|
|
|
|
2020-05-27 04:00:50 +00:00
|
|
|
import metrics
|
|
|
|
|
|
|
|
declarePublicGauge connected_peers, "number of peers in the pool" # XXX
|
|
|
|
declarePublicGauge total_messages, "number of messages received"
|
|
|
|
|
2020-05-01 05:00:00 +00:00
|
|
|
logScope:
|
|
|
|
topic = "WakuSub"
|
|
|
|
|
2020-07-07 03:23:20 +00:00
|
|
|
const WakuSubCodec* = "/wakusub/2.0.0-alpha1"
|
2020-05-01 05:00:00 +00:00
|
|
|
|
|
|
|
type
|
2020-06-01 03:42:16 +00:00
|
|
|
WakuSub* = ref object of GossipSub
|
2020-05-01 05:00:00 +00:00
|
|
|
# XXX: just playing
|
|
|
|
text*: string
|
2020-06-02 11:27:53 +00:00
|
|
|
gossip_enabled*: bool
|
2020-05-01 05:00:00 +00:00
|
|
|
|
2020-05-11 04:05:28 +00:00
|
|
|
method init(w: WakuSub) =
|
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-05-15 03:35:32 +00:00
|
|
|
debug "Incoming WakuSub connection"
|
2020-05-27 04:00:50 +00:00
|
|
|
# XXX: Increment connectedPeers counter, unclear if this is the right place tho
|
|
|
|
# Where is the disconnect event?
|
|
|
|
connected_peers.inc()
|
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
|
|
|
|
w.codec = WakuSubCodec
|
|
|
|
|
2020-05-11 04:20:07 +00:00
|
|
|
method initPubSub*(w: WakuSub) =
|
2020-05-15 03:29:01 +00:00
|
|
|
debug "initWakuSub"
|
2020-05-11 04:05:28 +00:00
|
|
|
w.text = "Foobar"
|
2020-05-15 03:35:32 +00:00
|
|
|
debug "w.text", text = w.text
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2020-06-15 02:59:09 +00:00
|
|
|
# Using GossipSub
|
|
|
|
w.gossip_enabled = true
|
2020-06-02 11:27:53 +00:00
|
|
|
|
|
|
|
if w.gossip_enabled:
|
|
|
|
procCall GossipSub(w).initPubSub()
|
|
|
|
else:
|
|
|
|
procCall FloodSub(w).initPubSub()
|
|
|
|
|
2020-05-11 04:05:28 +00:00
|
|
|
w.init()
|
|
|
|
|
2020-05-22 07:35:31 +00:00
|
|
|
method subscribe*(w: WakuSub,
|
|
|
|
topic: string,
|
|
|
|
handler: TopicHandler) {.async.} =
|
|
|
|
debug "subscribe", topic=topic
|
|
|
|
# XXX: Pubsub really
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2020-06-03 11:32:11 +00:00
|
|
|
# XXX: This is what is called, I think
|
2020-06-02 11:27:53 +00:00
|
|
|
if w.gossip_enabled:
|
|
|
|
await procCall GossipSub(w).subscribe(topic, handler)
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).subscribe(topic, handler)
|
2020-05-22 07:35:31 +00:00
|
|
|
|
2020-06-03 11:32:11 +00:00
|
|
|
|
2020-05-22 07:35:31 +00:00
|
|
|
# Subscribing a peer to a specified topic
|
2020-05-15 03:29:01 +00:00
|
|
|
method subscribeTopic*(w: WakuSub,
|
|
|
|
topic: string,
|
|
|
|
subscribe: bool,
|
2020-05-29 07:41:13 +00:00
|
|
|
peerId: string) {.async, gcsafe.} =
|
2020-06-03 11:32:11 +00:00
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
info "Hit NOOP handler", topic
|
|
|
|
|
2020-05-26 04:02:25 +00:00
|
|
|
debug "subscribeTopic", topic=topic, subscribe=subscribe, peerId=peerId
|
2020-06-02 11:27:53 +00:00
|
|
|
|
|
|
|
if w.gossip_enabled:
|
|
|
|
await procCall GossipSub(w).subscribeTopic(topic, subscribe, peerId)
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).subscribeTopic(topic, subscribe, peerId)
|
2020-05-15 03:29:01 +00:00
|
|
|
|
2020-06-03 11:32:11 +00:00
|
|
|
# XXX: This should distingish light and etc node
|
|
|
|
# NOTE: Relay subscription
|
|
|
|
# TODO: If peer is light node
|
|
|
|
info "about to call subscribe"
|
|
|
|
await w.subscribe(topic, handler)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-27 04:07:11 +00:00
|
|
|
# TODO: Fix decrement connected peers here or somewhere else
|
2020-05-15 03:29:01 +00:00
|
|
|
method handleDisconnect*(w: WakuSub, peer: PubSubPeer) {.async.} =
|
2020-05-15 03:35:32 +00:00
|
|
|
debug "handleDisconnect (NYI)"
|
2020-05-27 04:07:11 +00:00
|
|
|
#connected_peers.dec()
|
2020-05-15 03:29:01 +00:00
|
|
|
|
|
|
|
method rpcHandler*(w: WakuSub,
|
|
|
|
peer: PubSubPeer,
|
|
|
|
rpcMsgs: seq[RPCMsg]) {.async.} =
|
|
|
|
debug "rpcHandler"
|
2020-05-27 04:00:50 +00:00
|
|
|
|
|
|
|
# XXX: Right place?
|
|
|
|
total_messages.inc()
|
2020-06-02 11:27:53 +00:00
|
|
|
|
|
|
|
if w.gossip_enabled:
|
|
|
|
await procCall GossipSub(w).rpcHandler(peer, rpcMsgs)
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).rpcHandler(peer, rpcMsgs)
|
2020-06-03 11:32:11 +00:00
|
|
|
# XXX: here
|
2020-05-15 03:29:01 +00:00
|
|
|
|
|
|
|
method publish*(w: WakuSub,
|
|
|
|
topic: string,
|
2020-07-13 10:08:03 +00:00
|
|
|
data: seq[byte]): Future[int] {.async.} =
|
2020-05-26 04:02:25 +00:00
|
|
|
debug "publish", topic=topic
|
2020-06-02 11:27:53 +00:00
|
|
|
|
|
|
|
if w.gossip_enabled:
|
2020-07-13 10:08:03 +00:00
|
|
|
return await procCall GossipSub(w).publish(topic, data)
|
2020-06-02 11:27:53 +00:00
|
|
|
else:
|
2020-07-13 10:08:03 +00:00
|
|
|
return await procCall FloodSub(w).publish(topic, data)
|
2020-05-15 03:29:01 +00:00
|
|
|
|
|
|
|
method unsubscribe*(w: WakuSub,
|
|
|
|
topics: seq[TopicPair]) {.async.} =
|
|
|
|
debug "unsubscribe"
|
2020-06-02 11:27:53 +00:00
|
|
|
if w.gossip_enabled:
|
|
|
|
await procCall GossipSub(w).unsubscribe(topics)
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).unsubscribe(topics)
|
2020-06-01 03:42:16 +00:00
|
|
|
|
|
|
|
# GossipSub specific methods
|
|
|
|
method start*(w: WakuSub) {.async.} =
|
|
|
|
debug "start"
|
2020-06-02 11:27:53 +00:00
|
|
|
if w.gossip_enabled:
|
|
|
|
await procCall GossipSub(w).start()
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).start()
|
2020-06-01 03:42:16 +00:00
|
|
|
|
|
|
|
method stop*(w: WakuSub) {.async.} =
|
|
|
|
debug "stop"
|
2020-06-02 11:27:53 +00:00
|
|
|
if w.gossip_enabled:
|
|
|
|
await procCall GossipSub(w).stop()
|
|
|
|
else:
|
|
|
|
await procCall FloodSub(w).stop()
|