Skip to content

Commit

Permalink
chore(rln-relay-v2): added tests for static rln-relay-v2 (#2484)
Browse files Browse the repository at this point in the history
* chore(rln-relay-v2): added tests for onchain rln-relay-v2

* chore(rln-relay): added tests for static rln-relay-v2

* Update waku/waku_rln_relay/group_manager/static/group_manager.nim

Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>

* fix: split lines

* fix: remove redundant require

* fix: remove redundant require

* fix: bad await

---------

Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>
  • Loading branch information
rymnc and Ivansete-status authored Mar 4, 2024
1 parent 361fe2c commit 5b174fb
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 31 deletions.
5 changes: 2 additions & 3 deletions tests/waku_rln_relay/test_rln_group_manager_onchain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -433,17 +433,16 @@ suite "Onchain group manager":
let fut = newFuture[void]()

proc callback(registrations: seq[Membership]): Future[void] {.async.} =

require:
registrations.len == 1
when defined(rln_v2):
require:
registrations.len == 1
registrations[0].rateCommitment == RateCommitment(idCommitment: idCommitment, userMessageLimit: UserMessageLimit(1))
else:
require:
registrations[0].idCommitment == idCommitment
require:
registrations[0].index == 0
registrations.len == 1
fut.complete()

manager.onRegister(callback)
Expand Down
32 changes: 27 additions & 5 deletions tests/waku_rln_relay/test_rln_group_manager_static.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ suite "Static group manager":
let dummyCommitment = default(IDCommitment)

try:
await manager.register(dummyCommitment)
when defined(rln_v2):
await manager.register(RateCommitment(idCommitment: dummyCommitment,
userMessageLimit: DefaultUserMessageLimit))
else:
await manager.register(dummyCommitment)
except ValueError:
assert true
except Exception, CatchableError:
Expand All @@ -119,7 +123,11 @@ suite "Static group manager":
merkleRootBeforeRes.isOk()
let merkleRootBefore = merkleRootBeforeRes.get()
try:
await manager.register(idCommitment)
when defined(rln_v2):
await manager.register(RateCommitment(idCommitment: idCommitment,
userMessageLimit: DefaultUserMessageLimit))
else:
await manager.register(idCommitment)
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()
let merkleRootAfterRes = manager.rlnInstance.getMerkleRoot()
Expand All @@ -139,16 +147,25 @@ suite "Static group manager":
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
require:
registrations.len == 1
registrations[0].idCommitment == idCommitment
registrations[0].index == 10
when defined(rln_v2):
require:
registrations[0].rateCommitment == RateCommitment(idCommitment: idCommitment, userMessageLimit: DefaultUserMessageLimit)
else:
require:
registrations[0].idCommitment == idCommitment
callbackCalled = true
fut.complete()

try:
manager.onRegister(callback)
await manager.init()
await manager.startGroupSync()
await manager.register(idCommitment)
when defined(rln_v2):
await manager.register(RateCommitment(idCommitment: idCommitment,
userMessageLimit: DefaultUserMessageLimit))
else:
await manager.register(idCommitment)
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()

Expand Down Expand Up @@ -198,8 +215,13 @@ suite "Static group manager":
proc callback(withdrawals: seq[Membership]): Future[void] {.async.} =
require:
withdrawals.len == 1
withdrawals[0].idCommitment == idCommitment
withdrawals[0].index == 0
when defined(rln_v2):
require:
withdrawals[0].rateCommitment == RateCommitment(idCommitment: idCommitment, userMessageLimit: DefaultUserMessageLimit)
else:
require:
withdrawals[0].idCommitment == idCommitment
callbackCalled = true
fut.complete()

Expand Down
7 changes: 5 additions & 2 deletions waku/waku_keystore/protocol_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type
# hash of identity key as defined ed in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Membership
IDCommitment* = seq[byte] #array[32, byte]

when defined(rln_v2):
type UserMessageLimit* = uint64

type IdentityCredential* = object
idTrapdoor*: IdentityTrapdoor
idNullifier*: IdentityNullifier
Expand All @@ -44,7 +47,7 @@ type MembershipIndex* = uint
proc toMembershipIndex*(v: UInt256): MembershipIndex =
return cast[MembershipIndex](v)

# Converts a sequence of tuples containing 4 string (i.e. identity trapdoor, nullifier, secret hash and commitment) to an IndentityCredential
# Converts a sequence of tuples containing 4 string (i.e. identity trapdoor, nullifier, secret hash, commitment) to an IdentityCredential
type RawMembershipCredentials* = (string, string, string, string)
proc toIdentityCredentials*(groupKeys: seq[RawMembershipCredentials]): Result[seq[
IdentityCredential], string] =
Expand Down Expand Up @@ -93,7 +96,7 @@ type KeystoreMembership* = ref object of RootObj
treeIndex*: MembershipIndex
identityCredential*: IdentityCredential
when defined(rln_v2):
userMessageLimit*: uint64
userMessageLimit*: UserMessageLimit

when defined(rln_v2):
proc `$`*(m: KeystoreMembership): string =
Expand Down
19 changes: 11 additions & 8 deletions waku/waku_rln_relay/constants.nim

Large diffs are not rendered by default.

52 changes: 40 additions & 12 deletions waku/waku_rln_relay/group_manager/static/group_manager.nim
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import
../group_manager_base,
../../constants,
../../rln,
std/sequtils

export
group_manager_base

type
StaticGroupManager* = ref object of GroupManager
groupKeys*: seq[IdentityCredential]
groupSize*: uint
StaticGroupManager* = ref object of GroupManager
groupKeys*: seq[IdentityCredential]
groupSize*: uint

template initializedGuard*(g: StaticGroupManager): untyped =
if not g.initialized:
Expand All @@ -24,17 +25,26 @@ method init*(g: StaticGroupManager): Future[void] {.async.} =

if membershipIndex < MembershipIndex(0) or membershipIndex >= MembershipIndex(groupSize):
raise newException(ValueError, "Invalid membership index. Must be within 0 and " & $(groupSize - 1) & "but was " & $membershipIndex)
g.idCredentials = some(groupKeys[membershipIndex])
when defined(rln_v2):
g.userMessageLimit = some(DefaultUserMessageLimit)

g.idCredentials = some(groupKeys[membershipIndex])
# Seed the received commitments into the merkle tree
let idCommitments = groupKeys.mapIt(it.idCommitment)
let membersInserted = g.rlnInstance.insertMembers(g.latestIndex, idCommitments)
if not membersInserted:
raise newException(ValueError, "Failed to insert members into the merkle tree")
when defined(rln_v2):
let rateCommitments = groupKeys.mapIt(RateCommitment(idCommitment: it.idCommitment,
userMessageLimit: g.userMessageLimit.get()))
let leaves = rateCommitments.toLeaves().valueOr:
raise newException(ValueError, "Failed to convert rate commitments to leaves: " & $error)
let membersInserted = g.rlnInstance.insertMembers(g.latestIndex, leaves)
else:
let idCommitments = groupKeys.mapIt(it.idCommitment)
let membersInserted = g.rlnInstance.insertMembers(g.latestIndex, idCommitments)
if not membersInserted:
raise newException(ValueError, "Failed to insert members into the merkle tree")

discard g.slideRootQueue()

g.latestIndex += MembershipIndex(idCommitments.len - 1)
g.latestIndex += MembershipIndex(groupKeys.len - 1)

g.initialized = true

Expand Down Expand Up @@ -107,8 +117,25 @@ else:
when defined(rln_v2):
method withdraw*(g: StaticGroupManager,
idSecretHash: IdentitySecretHash): Future[void] {.async: (raises: [Exception]).} =
# No-op
return
initializedGuard(g)

let groupKeys = g.groupKeys

for i in 0..<groupKeys.len:
if groupKeys[i].idSecretHash == idSecretHash:
let idCommitment = groupKeys[i].idCommitment
let index = MembershipIndex(i)
let rateCommitment = RateCommitment(idCommitment: idCommitment,
userMessageLimit: g.userMessageLimit.get())
let memberRemoved = g.rlnInstance.removeMember(index)
if not memberRemoved:
raise newException(ValueError, "Failed to remove member from the merkle tree")

if g.withdrawCb.isSome():
let withdrawCb = g.withdrawCb.get()
await withdrawCb(@[Membership(rateCommitment: rateCommitment, index: index)])

return
else:
method withdraw*(g: StaticGroupManager, idSecretHash: IdentitySecretHash):
Future[void] {.async: (raises: [Exception]).} =
Expand All @@ -125,7 +152,8 @@ else:
raise newException(ValueError, "Failed to remove member from the merkle tree")

if g.withdrawCb.isSome():
await g.withdrawCb.get()(@[Membership(idCommitment: idCommitment, index: index)])
let withdrawCb = g.withdrawCb.get()
await withdrawCb((@[Membership(idCommitment: idCommitment, index: index)]))

return

Expand Down
1 change: 0 additions & 1 deletion waku/waku_rln_relay/protocol_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type

when defined(rln_v2):
type
UserMessageLimit* = uint64
MessageId* = uint64
ExternalNullifier* = array[32, byte]

Expand Down

0 comments on commit 5b174fb

Please sign in to comment.