Skip to content

Commit

Permalink
fix(rln-relay): decouple waku-relay and waku-rln-relay
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jan 22, 2023
1 parent 714c9bc commit 316c7af
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
19 changes: 15 additions & 4 deletions waku/v2/node/waku_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 11 additions & 27 deletions waku/v2/protocol/waku_rln_relay/rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import
import
../../utils/time,
../../utils/keyfile,
../waku_message,
../waku_relay
../waku_message

logScope:
topics = "waku rln_relay"
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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.} =
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 316c7af

Please sign in to comment.