Skip to content

Commit

Permalink
fix: silent test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Mar 5, 2024
1 parent f010cac commit 2daca1d
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 149 deletions.
26 changes: 25 additions & 1 deletion tests/waku_rln_relay/rln/waku_rln_relay_utils.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import std/tempfiles

import ../../../waku/waku_rln_relay/[rln, protocol_types]
import
../../../waku/waku_rln_relay,
../../../waku/waku_rln_relay/[rln, protocol_types]

proc createRLNInstanceWrapper*(): RLNResult =
return createRlnInstance(tree_path = genTempPath("rln_tree", "waku_rln_relay"))


proc unsafeAppendRLNProof*(rlnPeer: WakuRLNRelay,
msg: var WakuMessage,
senderEpochTime: float64): RlnRelayResult[void] =
## this proc derived from unsafeAppendRLNProof, does not perform nonce check to
## facilitate bad message id generation for testing

let input = msg.toRLNSignal()
let epoch = rlnPeer.calcEpoch(senderEpochTime)

when defined(rln_v2):
# we do not fetch a nonce from the nonce manager,
# instead we use 0 as the nonce
let proof = rlnPeer.groupManager.generateProof(input, epoch, 0).valueOr:
return err("could not generate rln-v2 proof: " & $error)
else:
let proof = rlnPeer.groupManager.generateProof(input, epoch).valueOr:
return err("could not generate rln proof: " & $error)

msg.proof = proof.encode().buffer
return ok()
66 changes: 33 additions & 33 deletions tests/waku_rln_relay/test_waku_rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -639,41 +639,42 @@ suite "Waku rln relay":

# check whether hasDuplicate correctly finds records with the same nullifiers but different secret shares
# no duplicate for proof1 should be found, since the log is empty
let result1 = wakurlnrelay.hasDuplicate(proof1.extractMetadata().tryGet())
require:
result1.isOk()
# no duplicate is found
result1.value == false
let result1 = wakurlnrelay.hasDuplicate(epoch, proof1.extractMetadata().tryGet())
assert result1.isOk(), $result1.error
assert result1.value == false, "no duplicate should be found"
# add it to the log
discard wakurlnrelay.updateLog(proof1.extractMetadata().tryGet())
discard wakurlnrelay.updateLog(epoch, proof1.extractMetadata().tryGet())

# # no duplicate for proof2 should be found, its nullifier differs from proof1
let result2 = wakurlnrelay.hasDuplicate(proof2.extractMetadata().tryGet())
require:
result2.isOk()
# no duplicate is found
result2.value == false
# no duplicate for proof2 should be found, its nullifier differs from proof1
let result2 = wakurlnrelay.hasDuplicate(epoch, proof2.extractMetadata().tryGet())
assert result2.isOk(), $result2.error
# no duplicate is found
assert result2.value == false, "no duplicate should be found"
# add it to the log
discard wakurlnrelay.updateLog(proof2.extractMetadata().tryGet())
discard wakurlnrelay.updateLog(epoch, proof2.extractMetadata().tryGet())

# proof3 has the same nullifier as proof1 but different secret shares, it should be detected as duplicate
let result3 = wakurlnrelay.hasDuplicate(proof3.extractMetadata().tryGet())
require:
result3.isOk()
check:
# it is a duplicate
result3.value == true
let result3 = wakurlnrelay.hasDuplicate(epoch, proof3.extractMetadata().tryGet())
assert result3.isOk(), $result3.error
# it is a duplicate
assert result3.value, "duplicate should be found"

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

let rlnConf = WakuRlnConfig(rlnRelayDynamic: false,
rlnRelayCredIndex: some(index),
rlnEpochSizeSec: 1,
rlnRelayTreePath: genTempPath("rln_tree", "waku_rln_relay_2"))
let wakuRlnRelayRes = await WakuRlnRelay.new(rlnConf)
require:
wakuRlnRelayRes.isOk()
when defined(rln_v2):
let wakuRlnConfig = WakuRlnConfig(rlnRelayDynamic: false,
rlnRelayCredIndex: some(index),
rlnRelayUserMessageLimit: 1,
rlnEpochSizeSec: 1,
rlnRelayTreePath: genTempPath("rln_tree", "waku_rln_relay_2"))
else:
let wakuRlnConfig = WakuRlnConfig(rlnRelayDynamic: false,
rlnRelayCredIndex: some(index),
rlnEpochSizeSec: 1,
rlnRelayTreePath: genTempPath("rln_tree", "waku_rln_relay_2"))
let wakuRlnRelayRes = await WakuRlnRelay.new(wakuRlnConfig)
assert wakuRlnRelayRes.isOk(), $wakuRlnRelayRes.error
let wakuRlnRelay = wakuRlnRelayRes.get()

# get the current epoch time
Expand All @@ -689,15 +690,14 @@ suite "Waku rln relay":
wm4 = WakuMessage(payload: "Invalid message".toBytes())

let
proofAdded1 = wakuRlnRelay.appendRLNProof(wm1, time)
proofAdded2 = wakuRlnRelay.appendRLNProof(wm2, time)
proofAdded3 = wakuRlnRelay.appendRLNProof(wm3, time+float64(wakuRlnRelay.rlnEpochSizeSec))
proofAdded1 = wakuRlnRelay.unsafeAppendRLNProof(wm1, time)
proofAdded2 = wakuRlnRelay.unsafeAppendRLNProof(wm2, time)
proofAdded3 = wakuRlnRelay.unsafeAppendRLNProof(wm3, time+float64(wakuRlnRelay.rlnEpochSizeSec))

# ensure proofs are added
require:
proofAdded1.isOk()
proofAdded2.isOk()
proofAdded3.isOk()
assert proofAdded1.isOk(), $proofAdded1.error
assert proofAdded2.isOk(), $proofAdded2.error
assert proofAdded3.isOk(), $proofAdded3.error

# validate messages
# validateMessage proc checks the validity of the message fields and adds it to the log (if valid)
Expand Down
Loading

0 comments on commit 2daca1d

Please sign in to comment.