mirror of https://github.com/waku-org/nwaku.git
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:
parent
361fe2cdc7
commit
5b174fb392
|
@ -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.} =
|
||||||
|
require:
|
||||||
|
registrations.len == 1
|
||||||
when defined(rln_v2):
|
when defined(rln_v2):
|
||||||
require:
|
require:
|
||||||
registrations.len == 1
|
|
||||||
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)
|
||||||
|
|
|
@ -100,7 +100,11 @@ suite "Static group manager":
|
||||||
let dummyCommitment = default(IDCommitment)
|
let dummyCommitment = default(IDCommitment)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await manager.register(dummyCommitment)
|
when defined(rln_v2):
|
||||||
|
await manager.register(RateCommitment(idCommitment: dummyCommitment,
|
||||||
|
userMessageLimit: DefaultUserMessageLimit))
|
||||||
|
else:
|
||||||
|
await manager.register(dummyCommitment)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
assert true
|
assert true
|
||||||
except Exception, CatchableError:
|
except Exception, CatchableError:
|
||||||
|
@ -119,7 +123,11 @@ suite "Static group manager":
|
||||||
merkleRootBeforeRes.isOk()
|
merkleRootBeforeRes.isOk()
|
||||||
let merkleRootBefore = merkleRootBeforeRes.get()
|
let merkleRootBefore = merkleRootBeforeRes.get()
|
||||||
try:
|
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:
|
except Exception, CatchableError:
|
||||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||||
let merkleRootAfterRes = manager.rlnInstance.getMerkleRoot()
|
let merkleRootAfterRes = manager.rlnInstance.getMerkleRoot()
|
||||||
|
@ -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,7 +161,11 @@ suite "Static group manager":
|
||||||
manager.onRegister(callback)
|
manager.onRegister(callback)
|
||||||
await manager.init()
|
await manager.init()
|
||||||
await manager.startGroupSync()
|
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:
|
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()
|
||||||
|
|
||||||
|
|
|
@ -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
|
@ -1,5 +1,6 @@
|
||||||
import
|
import
|
||||||
../group_manager_base,
|
../group_manager_base,
|
||||||
|
../../constants,
|
||||||
../../rln,
|
../../rln,
|
||||||
std/sequtils
|
std/sequtils
|
||||||
|
|
||||||
|
@ -7,9 +8,9 @@ export
|
||||||
group_manager_base
|
group_manager_base
|
||||||
|
|
||||||
type
|
type
|
||||||
StaticGroupManager* = ref object of GroupManager
|
StaticGroupManager* = ref object of GroupManager
|
||||||
groupKeys*: seq[IdentityCredential]
|
groupKeys*: seq[IdentityCredential]
|
||||||
groupSize*: uint
|
groupSize*: uint
|
||||||
|
|
||||||
template initializedGuard*(g: StaticGroupManager): untyped =
|
template initializedGuard*(g: StaticGroupManager): untyped =
|
||||||
if not g.initialized:
|
if not g.initialized:
|
||||||
|
@ -24,17 +25,26 @@ 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
|
||||||
let idCommitments = groupKeys.mapIt(it.idCommitment)
|
when defined(rln_v2):
|
||||||
let membersInserted = g.rlnInstance.insertMembers(g.latestIndex, idCommitments)
|
let rateCommitments = groupKeys.mapIt(RateCommitment(idCommitment: it.idCommitment,
|
||||||
if not membersInserted:
|
userMessageLimit: g.userMessageLimit.get()))
|
||||||
raise newException(ValueError, "Failed to insert members into the merkle tree")
|
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()
|
discard g.slideRootQueue()
|
||||||
|
|
||||||
g.latestIndex += MembershipIndex(idCommitments.len - 1)
|
g.latestIndex += MembershipIndex(groupKeys.len - 1)
|
||||||
|
|
||||||
g.initialized = true
|
g.initialized = true
|
||||||
|
|
||||||
|
@ -107,8 +117,25 @@ 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)
|
||||||
return
|
|
||||||
|
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:
|
else:
|
||||||
method withdraw*(g: StaticGroupManager, idSecretHash: IdentitySecretHash):
|
method withdraw*(g: StaticGroupManager, idSecretHash: IdentitySecretHash):
|
||||||
Future[void] {.async: (raises: [Exception]).} =
|
Future[void] {.async: (raises: [Exception]).} =
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue