2021-01-06 17:35:05 +08:00
|
|
|
|
## Waku Relay module. Thin layer on top of GossipSub.
|
2020-04-29 13:02:42 +08:00
|
|
|
|
##
|
2021-01-06 17:35:05 +08:00
|
|
|
|
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-relay.md
|
|
|
|
|
## for spec.
|
2022-10-28 12:51:46 +03:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
2020-04-29 13:02:42 +08:00
|
|
|
|
|
2020-08-31 05:32:41 +02:00
|
|
|
|
import
|
2023-02-10 15:17:50 +01:00
|
|
|
|
std/[tables, sequtils, hashes],
|
2022-11-18 14:50:56 +01:00
|
|
|
|
stew/results,
|
2022-11-03 16:36:24 +01:00
|
|
|
|
chronos,
|
2023-01-11 09:10:46 +01:00
|
|
|
|
chronicles,
|
2022-11-03 16:36:24 +01:00
|
|
|
|
metrics,
|
2022-11-18 14:50:56 +01:00
|
|
|
|
libp2p/multihash,
|
2022-11-03 16:36:24 +01:00
|
|
|
|
libp2p/protocols/pubsub/pubsub,
|
2022-11-18 14:50:56 +01:00
|
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
2022-11-03 16:36:24 +01:00
|
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
2023-01-11 09:10:46 +01:00
|
|
|
|
libp2p/stream/connection,
|
|
|
|
|
libp2p/switch
|
2022-11-09 09:55:47 +01:00
|
|
|
|
import
|
|
|
|
|
./waku_message
|
2020-08-31 05:32:41 +02:00
|
|
|
|
|
2020-05-01 13:00:00 +08:00
|
|
|
|
logScope:
|
2022-11-03 16:36:24 +01:00
|
|
|
|
topics = "waku relay"
|
2020-05-01 13:00:00 +08:00
|
|
|
|
|
2021-04-30 14:07:46 +02:00
|
|
|
|
const
|
2021-06-29 16:29:04 +02:00
|
|
|
|
WakuRelayCodec* = "/vac/waku/relay/2.0.0"
|
2020-05-01 13:00:00 +08:00
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
|
|
|
|
|
type WakuRelayResult*[T] = Result[T, string]
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
PubsubRawHandler* = proc(pubsubTopic: PubsubTopic, data: seq[byte]): Future[void] {.gcsafe, raises: [Defect].}
|
2023-02-08 16:09:59 +01:00
|
|
|
|
SubscriptionHandler* = proc(pubsubTopic: PubsubTopic, message: WakuMessage): Future[void] {.gcsafe, raises: [Defect].}
|
2022-11-18 14:50:56 +01:00
|
|
|
|
|
2021-01-06 17:35:05 +08:00
|
|
|
|
type
|
|
|
|
|
WakuRelay* = ref object of GossipSub
|
|
|
|
|
|
2023-02-08 16:09:59 +01:00
|
|
|
|
WakuRelayHandler* = PubsubRawHandler|SubscriptionHandler
|
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
|
|
|
|
|
proc initProtocolHandler(w: WakuRelay) =
|
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...
|
2022-11-18 14:50:56 +01:00
|
|
|
|
debug "Incoming WakuRelay connection", connection=conn, protocol=proto
|
2020-05-11 12:05:28 +08:00
|
|
|
|
|
2021-06-09 16:37:08 +02: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 14:50:56 +01: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 12:05:28 +08:00
|
|
|
|
|
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
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
method initPubSub(w: WakuRelay) {.raises: [InitializationError].} =
|
2023-01-11 09:10:46 +01:00
|
|
|
|
## NOTE: This method overrides GossipSub initPubSub method; it called by the
|
2022-11-18 14:50:56 +01:00
|
|
|
|
## parent protocol, PubSub.
|
|
|
|
|
debug "init waku relay"
|
2020-06-02 19:27:53 +08:00
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
# After discussions with @sinkingsugar: This is essentially what is needed for
|
2020-12-17 17:06:59 +01:00
|
|
|
|
# the libp2p `StrictNoSign` policy
|
|
|
|
|
w.anonymize = true
|
|
|
|
|
w.verifySignature = false
|
|
|
|
|
w.sign = false
|
|
|
|
|
|
2020-11-24 10:14:18 +01:00
|
|
|
|
procCall GossipSub(w).initPubSub()
|
2020-06-02 19:27:53 +08:00
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
w.initProtocolHandler()
|
|
|
|
|
|
|
|
|
|
|
2023-02-10 15:17:50 +01:00
|
|
|
|
proc new*(T: type WakuRelay, switch: Switch, triggerSelf: bool = true): WakuRelayResult[T] =
|
2023-01-11 09:10:46 +01:00
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
proc msgIdProvider(msg: messages.Message): Result[MessageID, ValidationResult] =
|
|
|
|
|
let hash = MultiHash.digest("sha2-256", msg.data)
|
|
|
|
|
if hash.isErr():
|
2023-02-10 15:17:50 +01:00
|
|
|
|
ok(toBytes($hashes.hash(msg.data)))
|
2022-11-18 14:50:56 +01:00
|
|
|
|
else:
|
|
|
|
|
ok(hash.value.data.buffer)
|
|
|
|
|
|
|
|
|
|
var wr: WakuRelay
|
|
|
|
|
try:
|
|
|
|
|
wr = WakuRelay.init(
|
2023-01-11 09:10:46 +01:00
|
|
|
|
switch = switch,
|
2022-11-18 14:50:56 +01:00
|
|
|
|
msgIdProvider = msgIdProvider,
|
|
|
|
|
triggerSelf = triggerSelf,
|
|
|
|
|
sign = false,
|
|
|
|
|
verifySignature = false,
|
|
|
|
|
maxMessageSize = MaxWakuMessageSize
|
|
|
|
|
)
|
|
|
|
|
except InitializationError:
|
|
|
|
|
return err("initialization error: " & getCurrentExceptionMsg())
|
|
|
|
|
|
2023-02-10 15:17:50 +01:00
|
|
|
|
# TODO: Add a function to validate the WakuMessage integrity
|
|
|
|
|
# # Rejects messages that are not WakuMessage
|
|
|
|
|
# proc validator(topic: string, message: messages.Message): Future[ValidationResult] {.async.} =
|
|
|
|
|
# let msg = WakuMessage.decode(message.data)
|
|
|
|
|
# if msg.isOk():
|
|
|
|
|
# return ValidationResult.Accept
|
|
|
|
|
# return ValidationResult.Reject
|
|
|
|
|
|
|
|
|
|
# # Add validator to all default pubsub topics
|
|
|
|
|
# for pubSubTopic in defaultPubsubTopics:
|
|
|
|
|
# wr.addValidator(pubSubTopic, validator)
|
2022-11-18 14:50:56 +01:00
|
|
|
|
|
|
|
|
|
ok(wr)
|
|
|
|
|
|
|
|
|
|
|
2023-02-10 15:17:50 +01:00
|
|
|
|
method addValidator*(w: WakuRelay, topic: varargs[string], handler: ValidatorHandler) {.gcsafe.} =
|
|
|
|
|
procCall GossipSub(w).addValidator(topic, handler)
|
|
|
|
|
|
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
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 12:05:28 +08:00
|
|
|
|
|
2020-11-24 10:14:18 +01:00
|
|
|
|
|
2023-02-08 16:09:59 +01:00
|
|
|
|
proc isSubscribed*(w: WakuRelay, topic: PubsubTopic): bool =
|
|
|
|
|
GossipSub(w).topics.hasKey(topic)
|
|
|
|
|
|
|
|
|
|
iterator subscribedTopics*(w: WakuRelay): lent PubsubTopic =
|
|
|
|
|
for topic in GossipSub(w).topics.keys():
|
|
|
|
|
yield topic
|
|
|
|
|
|
|
|
|
|
method subscribe*(w: WakuRelay, pubsubTopic: PubsubTopic, handler: WakuRelayHandler) =
|
2022-11-18 14:50:56 +01:00
|
|
|
|
debug "subscribe", pubsubTopic=pubsubTopic
|
2020-05-22 15:35:31 +08:00
|
|
|
|
|
2023-01-11 09:10:46 +01:00
|
|
|
|
var subsHandler: PubsubRawHandler
|
2023-02-08 16:09:59 +01:00
|
|
|
|
when handler is SubscriptionHandler:
|
|
|
|
|
subsHandler = proc(pubsubTopic: PubsubTopic, data: seq[byte]): Future[void] {.gcsafe.} =
|
2022-11-18 14:50:56 +01:00
|
|
|
|
let decodeRes = WakuMessage.decode(data)
|
|
|
|
|
if decodeRes.isErr():
|
|
|
|
|
debug "message decode failure", pubsubTopic=pubsubTopic, error=decodeRes.error
|
|
|
|
|
return
|
2020-06-02 19:27:53 +08:00
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
handler(pubsubTopic, decodeRes.value)
|
|
|
|
|
else:
|
|
|
|
|
subsHandler = handler
|
2020-05-15 11:29:01 +08:00
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
procCall GossipSub(w).subscribe(pubsubTopic, subsHandler)
|
|
|
|
|
|
|
|
|
|
method unsubscribe*(w: WakuRelay, topics: seq[TopicPair]) =
|
2023-02-08 16:09:59 +01:00
|
|
|
|
debug "unsubscribe", pubsubTopic=topics.mapIt(it[0])
|
2020-11-24 10:14:18 +01:00
|
|
|
|
|
2021-02-02 13:33:59 +02:00
|
|
|
|
procCall GossipSub(w).unsubscribe(topics)
|
2020-06-01 11:42:16 +08:00
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
method unsubscribeAll*(w: WakuRelay, pubsubTopic: PubsubTopic) =
|
|
|
|
|
debug "unsubscribeAll", pubsubTopic=pubsubTopic
|
2020-11-24 10:14:18 +01:00
|
|
|
|
|
2022-11-18 14:50:56 +01:00
|
|
|
|
procCall GossipSub(w).unsubscribeAll(pubsubTopic)
|
2020-10-27 03:13:56 +02:00
|
|
|
|
|
2020-06-01 11:42:16 +08:00
|
|
|
|
|
2022-11-18 14:50:56 +01: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:
|
2023-01-25 14:35:38 +01:00
|
|
|
|
data = message.encode().buffer
|
2022-11-18 14:50:56 +01:00
|
|
|
|
else:
|
|
|
|
|
data = message
|
|
|
|
|
|
2023-01-25 14:35:38 +01:00
|
|
|
|
return await procCall GossipSub(w).publish(pubsubTopic, data)
|
2022-11-18 14:50:56 +01:00
|
|
|
|
|
2021-04-30 14:07:46 +02:00
|
|
|
|
|