From 316c7af184285a2efb79a2a17c72e20f3343f20d Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Sun, 22 Jan 2023 12:27:45 +0530 Subject: [PATCH] fix(rln-relay): decouple waku-relay and waku-rln-relay --- waku/v2/node/waku_node.nim | 19 ++++++++-- waku/v2/protocol/waku_rln_relay/rln_relay.nim | 38 ++++++------------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/waku/v2/node/waku_node.nim b/waku/v2/node/waku_node.nim index 2e4c7c1936..1495c91d38 100644 --- a/waku/v2/node/waku_node.nim +++ b/waku/v2/node/waku_node.nim @@ -811,14 +811,25 @@ when defined(rln): registrationHandler: Option[RegistrationHandler] = none(RegistrationHandler)) {.async.} = info "mounting rln relay" - let rlnRelayRes = await WakuRlnRelay.new(node.wakuRelay, - rlnConf, + if node.wakuRelay.isNil(): + error "WakuRelay protocol is not mounted, cannot mount WakuRlnRelay" + return + # check whether the pubsub topic is supported at the relay level + if rlnConf.rlnRelayPubsubTopic notin node.wakuRelay.defaultPubsubTopics: + error "The relay protocol does not support the configured pubsub topic for WakuRlnRelay" + + let rlnRelayRes = await WakuRlnRelay.new(rlnConf, spamHandler, registrationHandler) if rlnRelayRes.isErr(): - error "failed to mount rln relay", error=rlnRelayRes.error + error "failed to mount WakuRlnRelay", error=rlnRelayRes.error return - node.wakuRlnRelay = rlnRelayRes.get() + let rlnRelay = rlnRelayRes.get() + let validator = generateRlnValidator(rlnRelay) + let pb = PubSub(node.wakuRelay) + pb.addValidator(rlnRelay.pubsubTopic, validator) + node.wakuRlnRelay = rlnRelay + ## Waku peer-exchange diff --git a/waku/v2/protocol/waku_rln_relay/rln_relay.nim b/waku/v2/protocol/waku_rln_relay/rln_relay.nim index c0f1450260..705915e2ba 100644 --- a/waku/v2/protocol/waku_rln_relay/rln_relay.nim +++ b/waku/v2/protocol/waku_rln_relay/rln_relay.nim @@ -26,8 +26,7 @@ import import ../../utils/time, ../../utils/keyfile, - ../waku_message, - ../waku_relay + ../waku_message logScope: topics = "waku rln_relay" @@ -302,7 +301,7 @@ proc toRLNSignal*(wakumessage: WakuMessage): seq[byte] = ## it is a utility proc that prepares the `data` parameter of the proof generation procedure i.e., `proofGen` that resides in the current module ## it extracts the `contentTopic` and the `payload` of the supplied `wakumessage` and serializes them into a byte sequence let - contentTopicBytes = wakumessage.contentTopic.toBytes + contentTopicBytes = wakumessage.contentTopic.toBytes() output = concat(wakumessage.payload, contentTopicBytes) return output @@ -324,15 +323,14 @@ proc appendRLNProof*(rlnPeer: WakuRLNRelay, msg.proof = proofGenRes.get().encode().buffer return true -proc addRLNRelayValidator*(wakuRlnRelay: WakuRLNRelay, - wakuRelay: WakuRelay, - pubsubTopic: PubsubTopic, - contentTopic: ContentTopic, - spamHandler: Option[SpamHandler] = none(SpamHandler)) = +proc generateRlnValidator*(wakuRlnRelay: WakuRLNRelay, + spamHandler: Option[SpamHandler] = none(SpamHandler)): pubsub.ValidatorHandler = ## this procedure is a thin wrapper for the pubsub addValidator method ## it sets a validator for the waku messages published on the supplied pubsubTopic and contentTopic ## if contentTopic is empty, then validation takes place for All the messages published on the given pubsubTopic ## the message validation logic is according to https://rfc.vac.dev/spec/17/ + let contentTopic = wakuRlnRelay.contentTopic + let pubsubTopic = wakuRlnRelay.pubsubTopic proc validator(topic: string, message: messages.Message): Future[pubsub.ValidationResult] {.async.} = trace "rln-relay topic validator is called" let decodeRes = WakuMessage.decode(message.data) @@ -378,12 +376,9 @@ proc addRLNRelayValidator*(wakuRlnRelay: WakuRLNRelay, let handler = spamHandler.get() handler(wakumessage) return pubsub.ValidationResult.Reject - # set a validator for the supplied pubsubTopic - let pb = PubSub(wakuRelay) - pb.addValidator(pubsubTopic, validator) + return validator -proc mount(wakuRelay: WakuRelay, - conf: WakuRlnConfig, +proc mount(conf: WakuRlnConfig, spamHandler: Option[SpamHandler] = none(SpamHandler), registrationHandler: Option[RegistrationHandler] = none(RegistrationHandler) ): Future[WakuRlnRelay] {.async.} = @@ -419,27 +414,16 @@ proc mount(wakuRelay: WakuRelay, await rlnRelay.groupManager.startGroupSync() proc new*(T: type WakuRlnRelay, - wakuRelay: WakuRelay, - conf: WakuRlnConfig, - spamHandler: Option[SpamHandler] = none(SpamHandler), - registrationHandler: Option[RegistrationHandler] = none(RegistrationHandler) + conf: WakuRlnConfig, + spamHandler: Option[SpamHandler] = none(SpamHandler), + registrationHandler: Option[RegistrationHandler] = none(RegistrationHandler) ): Future[RlnRelayResult[WakuRlnRelay]] {.async.} = ## Mounts the rln-relay protocol on the node. ## The rln-relay protocol can be mounted in two modes: on-chain and off-chain. ## Returns an error if the rln-relay protocol could not be mounted. - - # check whether inputs are provided - # relay protocol is the prerequisite of rln-relay - if wakuRelay.isNil(): - return err("WakuRelay protocol is not mounted") - # check whether the pubsub topic is supported at the relay level - if conf.rlnRelayPubsubTopic notin wakuRelay.defaultPubsubTopics: - return err("The relay protocol does not support the configured pubsub topic") - debug "rln-relay input validation passed" waku_rln_relay_mounting_duration_seconds.nanosecondTime: let rlnRelay = await mount( - wakuRelay, conf, spamHandler, registrationHandler