2021-01-06 09:35:05 +00:00
|
|
|
## Waku Relay module. Thin layer on top of GossipSub.
|
2020-04-29 05:02:42 +00:00
|
|
|
##
|
2021-01-06 09:35:05 +00:00
|
|
|
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-relay.md
|
|
|
|
## for spec.
|
2022-10-28 09:51:46 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2020-04-29 05:02:42 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
import
|
2022-11-18 13:50:56 +00:00
|
|
|
stew/results,
|
2022-11-03 15:36:24 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
|
|
|
metrics,
|
2022-11-18 13:50:56 +00:00
|
|
|
libp2p/multihash,
|
2022-11-03 15:36:24 +00:00
|
|
|
libp2p/protocols/pubsub/pubsub,
|
2022-11-18 13:50:56 +00:00
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
2022-11-03 15:36:24 +00:00
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
2021-01-06 09:35:05 +00:00
|
|
|
libp2p/stream/connection
|
2022-11-09 08:55:47 +00:00
|
|
|
import
|
2022-11-18 13:50:56 +00:00
|
|
|
../node/peer_manager/peer_manager,
|
2022-11-09 08:55:47 +00:00
|
|
|
./waku_message
|
2020-08-31 03:32:41 +00:00
|
|
|
|
2020-05-01 05:00:00 +00:00
|
|
|
logScope:
|
2022-11-03 15:36:24 +00:00
|
|
|
topics = "waku relay"
|
2020-05-01 05:00:00 +00:00
|
|
|
|
2021-04-30 12:07:46 +00:00
|
|
|
const
|
2021-06-29 14:29:04 +00:00
|
|
|
WakuRelayCodec* = "/vac/waku/relay/2.0.0"
|
2020-05-01 05:00:00 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
|
|
|
|
type WakuRelayResult*[T] = Result[T, string]
|
|
|
|
|
|
|
|
type
|
|
|
|
PubsubRawHandler* = proc(pubsubTopic: PubsubTopic, data: seq[byte]): Future[void] {.gcsafe, raises: [Defect].}
|
|
|
|
SubsciptionHandler* = proc(pubsubTopic: PubsubTopic, message: WakuMessage): Future[void] {.gcsafe, raises: [Defect].}
|
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
type
|
|
|
|
WakuRelay* = ref object of GossipSub
|
2022-11-18 13:50:56 +00:00
|
|
|
peerManager: PeerManager
|
2022-11-09 14:00:11 +00:00
|
|
|
defaultPubsubTopics*: seq[PubsubTopic] # Default configured PubSub topics
|
2021-01-06 09:35:05 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
|
|
|
|
proc initProtocolHandler(w: WakuRelay) =
|
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...
|
2022-11-18 13:50:56 +00:00
|
|
|
debug "Incoming WakuRelay connection", connection=conn, protocol=proto
|
2020-05-11 04:05:28 +00:00
|
|
|
|
2021-06-09 14:37:08 +00: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.
|
2022-11-18 13:50:56 +00:00
|
|
|
error "Unexpected cancellation in relay handler", conn=conn, error=getCurrentExceptionMsg()
|
|
|
|
except CatchableError:
|
|
|
|
error "WakuRelay handler leaks an error", conn=conn, error=getCurrentExceptionMsg()
|
2020-05-11 04:05:28 +00:00
|
|
|
|
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
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
method initPubSub(w: WakuRelay) {.raises: [InitializationError].} =
|
|
|
|
## NOTE: This method overrides GossipSub initPubSub method; it called by the
|
|
|
|
## parent protocol, PubSub.
|
|
|
|
debug "init waku relay"
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
# After discussions with @sinkingsugar: This is essentially what is needed for
|
2020-12-17 16:06:59 +00:00
|
|
|
# the libp2p `StrictNoSign` policy
|
|
|
|
w.anonymize = true
|
|
|
|
w.verifySignature = false
|
|
|
|
w.sign = false
|
|
|
|
|
2020-11-24 09:14:18 +00:00
|
|
|
procCall GossipSub(w).initPubSub()
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
w.initProtocolHandler()
|
|
|
|
|
|
|
|
|
|
|
|
proc new*(T: type WakuRelay,
|
|
|
|
peerManager: PeerManager,
|
|
|
|
defaultPubsubTopics: seq[PubsubTopic] = @[],
|
|
|
|
triggerSelf: bool = true): WakuRelayResult[T] =
|
|
|
|
|
|
|
|
proc msgIdProvider(msg: messages.Message): Result[MessageID, ValidationResult] =
|
|
|
|
let hash = MultiHash.digest("sha2-256", msg.data)
|
|
|
|
if hash.isErr():
|
|
|
|
ok(($msg.data.hash).toBytes())
|
|
|
|
else:
|
|
|
|
ok(hash.value.data.buffer)
|
|
|
|
|
|
|
|
var wr: WakuRelay
|
|
|
|
try:
|
|
|
|
wr = WakuRelay.init(
|
|
|
|
switch = peerManager.switch,
|
|
|
|
msgIdProvider = msgIdProvider,
|
|
|
|
triggerSelf = triggerSelf,
|
|
|
|
sign = false,
|
|
|
|
verifySignature = false,
|
|
|
|
maxMessageSize = MaxWakuMessageSize
|
|
|
|
)
|
|
|
|
except InitializationError:
|
|
|
|
return err("initialization error: " & getCurrentExceptionMsg())
|
|
|
|
|
|
|
|
wr.peerManager = peerManager
|
|
|
|
wr.defaultPubsubTopics = defaultPubsubTopics
|
|
|
|
|
|
|
|
ok(wr)
|
|
|
|
|
|
|
|
|
|
|
|
method start*(w: WakuRelay) {.async.} =
|
|
|
|
debug "start"
|
|
|
|
await procCall GossipSub(w).start()
|
|
|
|
|
|
|
|
method stop*(w: WakuRelay) {.async.} =
|
|
|
|
debug "stop"
|
|
|
|
await procCall GossipSub(w).stop()
|
2020-05-11 04:05:28 +00:00
|
|
|
|
2020-11-24 09:14:18 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
method subscribe*(w: WakuRelay, pubsubTopic: PubsubTopic, handler: SubsciptionHandler|PubsubRawHandler) =
|
|
|
|
debug "subscribe", pubsubTopic=pubsubTopic
|
2020-05-22 07:35:31 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
var subsHandler: PubsubRawHandler
|
|
|
|
when handler is SubsciptionHandler:
|
|
|
|
subsHandler = proc(pubsubTopic: PubsubTopic, data: seq[byte]): Future[void] {.gcsafe, raises: [Defect].} =
|
|
|
|
let decodeRes = WakuMessage.decode(data)
|
|
|
|
if decodeRes.isErr():
|
|
|
|
debug "message decode failure", pubsubTopic=pubsubTopic, error=decodeRes.error
|
|
|
|
return
|
2020-06-02 11:27:53 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
handler(pubsubTopic, decodeRes.value)
|
|
|
|
else:
|
|
|
|
subsHandler = handler
|
2020-05-15 03:29:01 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
procCall GossipSub(w).subscribe(pubsubTopic, subsHandler)
|
|
|
|
|
|
|
|
method unsubscribe*(w: WakuRelay, topics: seq[TopicPair]) =
|
|
|
|
debug "unsubscribe", topics=topics
|
2020-11-24 09:14:18 +00:00
|
|
|
|
2021-02-02 11:33:59 +00:00
|
|
|
procCall GossipSub(w).unsubscribe(topics)
|
2020-06-01 03:42:16 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
method unsubscribeAll*(w: WakuRelay, pubsubTopic: PubsubTopic) =
|
|
|
|
debug "unsubscribeAll", pubsubTopic=pubsubTopic
|
2020-11-24 09:14:18 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
procCall GossipSub(w).unsubscribeAll(pubsubTopic)
|
2020-10-27 01:13:56 +00:00
|
|
|
|
2020-06-01 03:42:16 +00:00
|
|
|
|
2022-11-18 13:50:56 +00:00
|
|
|
method publish*(w: WakuRelay, pubsubTopic: PubsubTopic, message: WakuMessage|seq[byte]): Future[int] {.async.} =
|
|
|
|
trace "publish", pubsubTopic=pubsubTopic
|
|
|
|
|
|
|
|
var data: seq[byte]
|
|
|
|
when message is WakuMessage:
|
|
|
|
data = message.encode()
|
|
|
|
else:
|
|
|
|
data = message
|
|
|
|
|
|
|
|
return await procCall GossipSub(w).publish(pubsubTopic, message)
|
|
|
|
|
2021-04-30 12:07:46 +00:00
|
|
|
|