Skip to content

Commit

Permalink
test: add unit test for the behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jun 24, 2024
1 parent 32b3f6f commit 6463775
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Binary file added tests/waku_rln_relay/test_waku_rln_relay
Binary file not shown.
43 changes: 43 additions & 0 deletions tests/waku_rln_relay/test_waku_rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -886,3 +886,46 @@ suite "Waku rln relay":
check:
buckets.len == 5
buckets == [2.0, 4.0, 6.0, 8.0, 10.0]

asyncTest "nullifierLog clearing only after epoch has passed":
let index = MembershipIndex(0)
let rlnEpochSizeSec: uint = 1

let wakuRlnConfig = WakuRlnConfig(
rlnRelayDynamic: false,
rlnRelayCredIndex: some(index),
rlnRelayUserMessageLimit: 1,
rlnEpochSizeSec: rlnEpochSizeSec,
rlnRelayTreePath: genTempPath("rln_tree", "waku_rln_relay_4"),
)


let wakuRlnRelay = (await WakuRlnRelay.new(wakuRlnConfig)).valueOr:
raiseAssert $error

let rlnMaxEpochGap = wakuRlnRelay.rlnMaxEpochGap
let testProofMetadata = default(ProofMetadata)
let testProofMetadataTable = {testProofMetadata.nullifier: testProofMetadata}.toTable()

for i in 0..<rlnMaxEpochGap:
# we add epochs to the nullifierLog
let testEpoch = wakuRlnRelay.calcEpoch(epochTime() + float(i))
wakuRlnRelay.nullifierLog[testEpoch] = testProofMetadataTable
check: wakuRlnRelay.nullifierLog.len().uint == i + 1

# clearing it now will be a no-op
wakuRlnRelay.clearNullifierLog()

check: wakuRlnRelay.nullifierLog.len().uint == rlnMaxEpochGap

# append one more now
let testEpoch = wakuRlnRelay.calcEpoch(epochTime() + float(rlnMaxEpochGap))
wakuRlnRelay.nullifierLog[testEpoch] = testProofMetadataTable

check: wakuRlnRelay.nullifierLog.len().uint == rlnMaxEpochGap + 1
# clearing it now will be a no-op
wakuRlnRelay.clearNullifierLog()

check: wakuRlnRelay.nullifierLog.len().uint == rlnMaxEpochGap


7 changes: 4 additions & 3 deletions waku/waku_rln_relay/rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,16 @@ proc appendRLNProof*(
msg.proof = proof.encode().buffer
return ok()

proc clearNullifierLog(rlnPeer: WakuRlnRelay) =
proc clearNullifierLog*(rlnPeer: WakuRlnRelay) =
# clear the first MaxEpochGap epochs of the nullifer log
# if more than MaxEpochGap epochs are in the log
# note: the epochs are ordered ascendingly
if rlnPeer.nullifierLog.len().uint <= rlnPeer.rlnMaxEpochGap:
return

trace "clearing epochs from the nullifier log", count = rlnPeer.rlnMaxEpochGap
let epochsToClear = rlnPeer.nullifierLog.keys().toSeq()[0 ..< rlnPeer.rlnMaxEpochGap]
let countToClear = rlnPeer.nullifierLog.len().uint - rlnPeer.rlnMaxEpochGap
trace "clearing epochs from the nullifier log", count = countToClear
let epochsToClear = rlnPeer.nullifierLog.keys().toSeq()[0 ..< countToClear]
for epoch in epochsToClear:
rlnPeer.nullifierLog.del(epoch)

Expand Down

0 comments on commit 6463775

Please sign in to comment.