Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rln-relay): clear nullifier log only if length is over max epoch gap #2836

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 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,40 @@ 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)

proc runTestForEpochSizeSec(rlnEpochSizeSec: uint) {.async.} =
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(rlnEpochSizeSec * i))
wakuRlnRelay.nullifierLog[testEpoch] = testProofMetadataTable
check: wakuRlnRelay.nullifierLog.len().uint == i + 1

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

# clearing it now will remove 1 epoch
wakuRlnRelay.clearNullifierLog()

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

var testEpochSizes: seq[uint] = @[1,5,10,30,60]
for i in testEpochSizes:
await runTestForEpochSizeSec(i)

9 changes: 5 additions & 4 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:
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
Loading