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

* 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>
This commit is contained in:
Aaryamann Challani 2024-03-04 17:41:33 +05:30 committed by GitHub
parent 361fe2cdc7
commit 5b174fb392
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 85 additions and 31 deletions

View File

@ -433,17 +433,16 @@ suite "Onchain group manager":
let fut = newFuture[void]() let fut = newFuture[void]()
proc callback(registrations: seq[Membership]): Future[void] {.async.} = proc callback(registrations: seq[Membership]): Future[void] {.async.} =
when defined(rln_v2):
require: require:
registrations.len == 1 registrations.len == 1
when defined(rln_v2):
require:
registrations[0].rateCommitment == RateCommitment(idCommitment: idCommitment, userMessageLimit: UserMessageLimit(1)) registrations[0].rateCommitment == RateCommitment(idCommitment: idCommitment, userMessageLimit: UserMessageLimit(1))
else: else:
require: require:
registrations[0].idCommitment == idCommitment registrations[0].idCommitment == idCommitment
require: require:
registrations[0].index == 0 registrations[0].index == 0
registrations.len == 1
fut.complete() fut.complete()
manager.onRegister(callback) manager.onRegister(callback)

View File

@ -100,6 +100,10 @@ suite "Static group manager":
let dummyCommitment = default(IDCommitment) let dummyCommitment = default(IDCommitment)
try: try:
when defined(rln_v2):
await manager.register(RateCommitment(idCommitment: dummyCommitment,
userMessageLimit: DefaultUserMessageLimit))
else:
await manager.register(dummyCommitment) await manager.register(dummyCommitment)
except ValueError: except ValueError:
assert true assert true
@ -119,6 +123,10 @@ suite "Static group manager":
merkleRootBeforeRes.isOk() merkleRootBeforeRes.isOk()
let merkleRootBefore = merkleRootBeforeRes.get() let merkleRootBefore = merkleRootBeforeRes.get()
try: try:
when defined(rln_v2):
await manager.register(RateCommitment(idCommitment: idCommitment,
userMessageLimit: DefaultUserMessageLimit))
else:
await manager.register(idCommitment) await manager.register(idCommitment)
except Exception, CatchableError: except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg() assert false, "exception raised: " & getCurrentExceptionMsg()
@ -139,8 +147,13 @@ suite "Static group manager":
proc callback(registrations: seq[Membership]): Future[void] {.async.} = proc callback(registrations: seq[Membership]): Future[void] {.async.} =
require: require:
registrations.len == 1 registrations.len == 1
registrations[0].idCommitment == idCommitment
registrations[0].index == 10 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 callbackCalled = true
fut.complete() fut.complete()
@ -148,6 +161,10 @@ suite "Static group manager":
manager.onRegister(callback) manager.onRegister(callback)
await manager.init() await manager.init()
await manager.startGroupSync() await manager.startGroupSync()
when defined(rln_v2):
await manager.register(RateCommitment(idCommitment: idCommitment,
userMessageLimit: DefaultUserMessageLimit))
else:
await manager.register(idCommitment) await manager.register(idCommitment)
except Exception, CatchableError: except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg() assert false, "exception raised: " & getCurrentExceptionMsg()
@ -198,8 +215,13 @@ suite "Static group manager":
proc callback(withdrawals: seq[Membership]): Future[void] {.async.} = proc callback(withdrawals: seq[Membership]): Future[void] {.async.} =
require: require:
withdrawals.len == 1 withdrawals.len == 1
withdrawals[0].idCommitment == idCommitment
withdrawals[0].index == 0 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 callbackCalled = true
fut.complete() fut.complete()

View File

@ -20,6 +20,9 @@ type
# hash of identity key as defined ed in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Membership # hash of identity key as defined ed in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Membership
IDCommitment* = seq[byte] #array[32, byte] IDCommitment* = seq[byte] #array[32, byte]
when defined(rln_v2):
type UserMessageLimit* = uint64
type IdentityCredential* = object type IdentityCredential* = object
idTrapdoor*: IdentityTrapdoor idTrapdoor*: IdentityTrapdoor
idNullifier*: IdentityNullifier idNullifier*: IdentityNullifier
@ -44,7 +47,7 @@ type MembershipIndex* = uint
proc toMembershipIndex*(v: UInt256): MembershipIndex = proc toMembershipIndex*(v: UInt256): MembershipIndex =
return cast[MembershipIndex](v) 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) type RawMembershipCredentials* = (string, string, string, string)
proc toIdentityCredentials*(groupKeys: seq[RawMembershipCredentials]): Result[seq[ proc toIdentityCredentials*(groupKeys: seq[RawMembershipCredentials]): Result[seq[
IdentityCredential], string] = IdentityCredential], string] =
@ -93,7 +96,7 @@ type KeystoreMembership* = ref object of RootObj
treeIndex*: MembershipIndex treeIndex*: MembershipIndex
identityCredential*: IdentityCredential identityCredential*: IdentityCredential
when defined(rln_v2): when defined(rln_v2):
userMessageLimit*: uint64 userMessageLimit*: UserMessageLimit
when defined(rln_v2): when defined(rln_v2):
proc `$`*(m: KeystoreMembership): string = proc `$`*(m: KeystoreMembership): string =

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
import import
../group_manager_base, ../group_manager_base,
../../constants,
../../rln, ../../rln,
std/sequtils std/sequtils
@ -24,9 +25,18 @@ method init*(g: StaticGroupManager): Future[void] {.async.} =
if membershipIndex < MembershipIndex(0) or membershipIndex >= MembershipIndex(groupSize): if membershipIndex < MembershipIndex(0) or membershipIndex >= MembershipIndex(groupSize):
raise newException(ValueError, "Invalid membership index. Must be within 0 and " & $(groupSize - 1) & "but was " & $membershipIndex) 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 # Seed the received commitments 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 idCommitments = groupKeys.mapIt(it.idCommitment)
let membersInserted = g.rlnInstance.insertMembers(g.latestIndex, idCommitments) let membersInserted = g.rlnInstance.insertMembers(g.latestIndex, idCommitments)
if not membersInserted: if not membersInserted:
@ -34,7 +44,7 @@ method init*(g: StaticGroupManager): Future[void] {.async.} =
discard g.slideRootQueue() discard g.slideRootQueue()
g.latestIndex += MembershipIndex(idCommitments.len - 1) g.latestIndex += MembershipIndex(groupKeys.len - 1)
g.initialized = true g.initialized = true
@ -107,7 +117,24 @@ else:
when defined(rln_v2): when defined(rln_v2):
method withdraw*(g: StaticGroupManager, method withdraw*(g: StaticGroupManager,
idSecretHash: IdentitySecretHash): Future[void] {.async: (raises: [Exception]).} = idSecretHash: IdentitySecretHash): Future[void] {.async: (raises: [Exception]).} =
# No-op 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 return
else: else:
method withdraw*(g: StaticGroupManager, idSecretHash: IdentitySecretHash): method withdraw*(g: StaticGroupManager, idSecretHash: IdentitySecretHash):
@ -125,7 +152,8 @@ else:
raise newException(ValueError, "Failed to remove member from the merkle tree") raise newException(ValueError, "Failed to remove member from the merkle tree")
if g.withdrawCb.isSome(): 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 return

View File

@ -33,7 +33,6 @@ type
when defined(rln_v2): when defined(rln_v2):
type type
UserMessageLimit* = uint64
MessageId* = uint64 MessageId* = uint64
ExternalNullifier* = array[32, byte] ExternalNullifier* = array[32, byte]