diff --git a/tests/waku_rln_relay/test_waku_rln_relay.nim b/tests/waku_rln_relay/test_waku_rln_relay.nim index c8f655ad7d..611f3d80f8 100644 --- a/tests/waku_rln_relay/test_waku_rln_relay.nim +++ b/tests/waku_rln_relay/test_waku_rln_relay.nim @@ -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) + diff --git a/waku/waku_rln_relay/rln_relay.nim b/waku/waku_rln_relay/rln_relay.nim index 139c7aaf99..ab4ba10a6a 100644 --- a/waku/waku_rln_relay/rln_relay.nim +++ b/waku/waku_rln_relay/rln_relay.nim @@ -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)