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]()
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)

View File

@ -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:
@ -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()
@ -139,8 +147,13 @@ 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()
@ -148,7 +161,11 @@ suite "Static group manager":
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()
@ -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()

View File

@ -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
@ -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] =
@ -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 =

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
import
../group_manager_base,
../../constants,
../../rln,
std/sequtils
@ -7,9 +8,9 @@ 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:
@ -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
@ -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]).} =
@ -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

View File

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