Skip to content

Commit

Permalink
create validateMessageAndUpdateLog function
Browse files Browse the repository at this point in the history
  • Loading branch information
alrevuelta committed Aug 31, 2023
1 parent 9d59d9d commit c130158
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
20 changes: 10 additions & 10 deletions tests/waku_rln_relay/test_waku_rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ suite "Waku rln relay":
# it is a duplicate
result3.value == true

asyncTest "validateMessage test":
asyncTest "validateMessageAndUpdateLog test":
let index = MembershipIndex(5)

let rlnConf = WakuRlnConfig(rlnRelayDynamic: false,
Expand Down Expand Up @@ -695,13 +695,13 @@ suite "Waku rln relay":
# validate messages
# validateMessage proc checks the validity of the message fields and adds it to the log (if valid)
let
msgValidate1 = wakuRlnRelay.validateMessage(wm1, some(time))
msgValidate1 = wakuRlnRelay.validateMessageAndUpdateLog(wm1, some(time))
# wm2 is published within the same Epoch as wm1 and should be found as spam
msgValidate2 = wakuRlnRelay.validateMessage(wm2, some(time))
msgValidate2 = wakuRlnRelay.validateMessageAndUpdateLog(wm2, some(time))
# a valid message should be validated successfully
msgValidate3 = wakuRlnRelay.validateMessage(wm3, some(time))
msgValidate3 = wakuRlnRelay.validateMessageAndUpdateLog(wm3, some(time))
# wm4 has no rln proof and should not be validated
msgValidate4 = wakuRlnRelay.validateMessage(wm4, some(time))
msgValidate4 = wakuRlnRelay.validateMessageAndUpdateLog(wm4, some(time))


check:
Expand Down Expand Up @@ -750,13 +750,13 @@ suite "Waku rln relay":
# validateMessage proc checks the validity of the message fields and adds it to the log (if valid)
let
# this should be no verification, Valid
msgValidate1 = wakuRlnRelay.validateMessage(wm1, some(time))
msgValidate1 = wakuRlnRelay.validateMessageAndUpdateLog(wm1, some(time))
# this should be verification, Valid
msgValidate2 = wakuRlnRelay.validateMessage(wm2, some(time))
msgValidate2 = wakuRlnRelay.validateMessageAndUpdateLog(wm2, some(time))
# this should be verification, Invalid
msgValidate3 = wakuRlnRelay.validateMessage(wm3, some(time))
msgValidate3 = wakuRlnRelay.validateMessageAndUpdateLog(wm3, some(time))
# this should be verification, Spam
msgValidate4 = wakuRlnRelay.validateMessage(wm4, some(time))
msgValidate4 = wakuRlnRelay.validateMessageAndUpdateLog(wm4, some(time))

check:
msgValidate1 == MessageValidationResult.Valid
Expand Down Expand Up @@ -848,7 +848,7 @@ suite "Waku rln relay":

# getMembershipCredentials returns the credential in the keystore which matches
# the query, in this case the query is =
# chainId = "5" and
# chainId = "5" and
# address = "0x0123456789012345678901234567890123456789" and
# treeIndex = 1
let readKeystoreMembership = readKeystoreRes.get()
Expand Down
36 changes: 26 additions & 10 deletions waku/waku_rln_relay/rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ proc validateMessage*(rlnPeer: WakuRLNRelay,
waku_rln_valid_messages_total.observe(rootIndex.toFloat())
return MessageValidationResult.Valid

proc validateMessageAndUpdateLog*(
rlnPeer: WakuRLNRelay,
msg: WakuMessage,
timeOption = none(float64)): MessageValidationResult =
## validates the message and updates the log to prevent double messaging
## in future messages

let result = rlnPeer.validateMessage(msg, timeOption)

let decodeRes = RateLimitProof.init(msg.proof)
if decodeRes.isErr():
return MessageValidationResult.Invalid

let msgProof = decodeRes.get()
let proofMetadataRes = msgProof.extractMetadata()

if proofMetadataRes.isErr():
return MessageValidationResult.Invalid

# insert the message to the log (never errors)
discard rlnPeer.updateLog(proofMetadataRes.get())

return result

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
Expand Down Expand Up @@ -304,16 +328,8 @@ proc generateRlnValidator*(wakuRlnRelay: WakuRLNRelay,

let msgProof = decodeRes.get()

# validate the message
let validationRes = wakuRlnRelay.validateMessage(wakumessage)

# extract the metadata, already checked should not error
let proofMetadataRes = msgProof.extractMetadata()
if proofMetadataRes.isErr():
return pubsub.ValidationResult.Reject

# insert the message to the log (never errors)
discard wakuRlnRelay.updateLog(proofMetadataRes.get())
# validate the message and update log
let validationRes = wakuRlnRelay.validateMessageAndUpdateLog(wakumessage)

let
proof = toHex(msgProof.proof)
Expand Down

0 comments on commit c130158

Please sign in to comment.