Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rln-relay-v2): added tests for onchain rln-relay-v2 #2482

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 146 additions & 43 deletions tests/waku_rln_relay/test_rln_group_manager_onchain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ proc generateCredentials(rlnInstance: ptr RLN): IdentityCredential =
let credRes = membershipKeyGen(rlnInstance)
return credRes.get()

when defined(rln_v2):
proc getRateCommitment(idCredential: IdentityCredential, userMessageLimit: UserMessageLimit): RateCommitment =
return RateCommitment(idCommitment: idCredential.idCommitment,
userMessageLimit: userMessageLimit)

proc generateCredentials(rlnInstance: ptr RLN, n: int): seq[IdentityCredential] =
var credentials: seq[IdentityCredential]
for i in 0 ..< n:
Expand Down Expand Up @@ -84,7 +89,11 @@ proc uploadRLNContract*(ethClientAddress: string): Future[Address] {.async.} =
debug "Address of the deployed registry contract: ", contractAddress

let registryContract = web3.contractSender(WakuRlnRegistry, contractAddress)
let newStorageReceipt = await registryContract.newStorage().send()
when defined(rln_v2):
let initReceipt = await registryContract.initialize().send()
let newStorageReceipt = await registryContract.newStorage(20.u256).send()
else:
let newStorageReceipt = await registryContract.newStorage().send()

debug "Receipt of the newStorage transaction: ", newStorageReceipt
let newBalance = await web3.provider.eth_getBalance(web3.defaultAccount, "latest")
Expand Down Expand Up @@ -278,14 +287,24 @@ suite "Onchain group manager":
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
require:
registrations.len == 1
registrations[0].idCommitment == credentials.idCommitment
registrations[0].index == 0
when defined(rln_v2):
require:
registrations[0].rateCommitment == getRateCommitment(credentials, UserMessageLimit(1))
else:
require:
registrations[0].idCommitment == credentials.idCommitment
require:
registrations[0].index == 0
fut.complete()
return callback

try:
manager.onRegister(generateCallback(fut))
await manager.register(credentials)
when defined(rln_v2):
await manager.register(credentials, UserMessageLimit(1))
else:
await manager.register(credentials)
await manager.startGroupSync()
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()
Expand Down Expand Up @@ -319,19 +338,29 @@ suite "Onchain group manager":
proc generateCallback(futs: TestGroupSyncFuts, credentials: seq[IdentityCredential]): OnRegisterCallback =
var futureIndex = 0
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
if registrations.len == 1 and
registrations[0].idCommitment == credentials[futureIndex].idCommitment and
registrations[0].index == MembershipIndex(futureIndex):
futs[futureIndex].complete()
futureIndex += 1
when defined(rln_v2):
if registrations.len == 1 and
registrations[0].rateCommitment == getRateCommitment(credentials[futureIndex], UserMessageLimit(1)) and
registrations[0].index == MembershipIndex(futureIndex):
futs[futureIndex].complete()
futureIndex += 1
else:
if registrations.len == 1 and
registrations[0].idCommitment == credentials[futureIndex].idCommitment and
registrations[0].index == MembershipIndex(futureIndex):
futs[futureIndex].complete()
futureIndex += 1
return callback

try:
manager.onRegister(generateCallback(futures, credentials))
await manager.startGroupSync()

for i in 0 ..< credentials.len():
await manager.register(credentials[i])
when defined(rln_v2):
await manager.register(credentials[i], UserMessageLimit(1))
else:
await manager.register(credentials[i])
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()

Expand All @@ -352,7 +381,11 @@ suite "Onchain group manager":
let dummyCommitment = default(IDCommitment)

try:
await manager.register(dummyCommitment)
when defined(rln_v2):
await manager.register(RateCommitment(idCommitment: dummyCommitment,
userMessageLimit: UserMessageLimit(1)))
else:
await manager.register(dummyCommitment)
except CatchableError:
assert true
except Exception:
Expand All @@ -375,7 +408,11 @@ suite "Onchain group manager":
let merkleRootBefore = merkleRootBeforeRes.get()

try:
await manager.register(idCommitment)
when defined(rln_v2):
await manager.register(RateCommitment(idCommitment: idCommitment,
userMessageLimit: UserMessageLimit(1)))
else:
await manager.register(idCommitment)
except Exception, CatchableError:
assert false, "exception raised when calling register: " & getCurrentExceptionMsg()

Expand All @@ -396,17 +433,26 @@ suite "Onchain group manager":
let fut = newFuture[void]()

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

when defined(rln_v2):
require:
registrations[0].rateCommitment == RateCommitment(idCommitment: idCommitment, userMessageLimit: UserMessageLimit(1))
rymnc marked this conversation as resolved.
Show resolved Hide resolved
else:
require:
registrations[0].idCommitment == idCommitment
require:
registrations.len == 1
registrations[0].idCommitment == idCommitment
registrations[0].index == 0
registrations.len == 1
fut.complete()

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

Expand Down Expand Up @@ -438,18 +484,27 @@ suite "Onchain group manager":
let fut = newFuture[void]()

proc callback(registrations: seq[Membership]): Future[void] {.async.} =
if registrations.len == 1 and
registrations[0].idCommitment == credentials.idCommitment and
registrations[0].index == 0:
manager.idCredentials = some(credentials)
manager.membershipIndex = some(registrations[0].index)
fut.complete()
when defined(rln_v2):
if registrations.len == 1 and
registrations[0].rateCommitment == getRateCommitment(credentials, UserMessageLimit(1)) and
registrations[0].index == 0:
manager.idCredentials = some(credentials)
fut.complete()
else:
if registrations.len == 1 and
registrations[0].idCommitment == credentials.idCommitment and
registrations[0].index == 0:
manager.idCredentials = some(credentials)
fut.complete()

manager.onRegister(callback)

try:
await manager.startGroupSync()
await manager.register(credentials)
when defined(rln_v2):
await manager.register(credentials, UserMessageLimit(1))
else:
await manager.register(credentials)
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()

Expand All @@ -462,8 +517,14 @@ suite "Onchain group manager":
debug "epoch in bytes", epochHex = epoch.inHex()

# generate proof
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch)
when defined(rln_v2):
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch,
messageId = MessageId(1))
else:
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch)

require:
validProofRes.isOk()
let validProof = validProofRes.get()
Expand All @@ -488,6 +549,8 @@ suite "Onchain group manager":
## Assume the registration occured out of band
manager.idCredentials = some(credentials)
manager.membershipIndex = some(MembershipIndex(0))
when defined(rln_v2):
manager.userMessageLimit = some(UserMessageLimit(1))

let messageBytes = "Hello".toBytes()

Expand All @@ -496,8 +559,13 @@ suite "Onchain group manager":
debug "epoch in bytes", epochHex = epoch.inHex()

# generate proof
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch)
when defined(rln_v2):
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch,
messageId = MessageId(0))
else:
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch)
require:
validProofRes.isOk()
let validProof = validProofRes.get()
Expand All @@ -517,18 +585,27 @@ suite "Onchain group manager":
let fut = newFuture[void]()

proc callback(registrations: seq[Membership]): Future[void] {.async.} =
if registrations.len == 1 and
registrations[0].idCommitment == credentials.idCommitment and
registrations[0].index == 0:
manager.idCredentials = some(credentials)
manager.membershipIndex = some(registrations[0].index)
fut.complete()
when defined(rln_v2):
if registrations.len == 1 and
registrations[0].rateCommitment == getRateCommitment(credentials, UserMessageLimit(1)) and
registrations[0].index == 0:
manager.idCredentials = some(credentials)
fut.complete()
else:
if registrations.len == 1 and
registrations[0].idCommitment == credentials.idCommitment and
registrations[0].index == 0:
manager.idCredentials = some(credentials)
fut.complete()

manager.onRegister(callback)

try:
await manager.startGroupSync()
await manager.register(credentials)
when defined(rln_v2):
await manager.register(credentials, UserMessageLimit(1))
else:
await manager.register(credentials)
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()
await fut
Expand All @@ -540,8 +617,13 @@ suite "Onchain group manager":
debug "epoch in bytes", epochHex = epoch.inHex()

# generate proof
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch)
when defined(rln_v2):
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch,
messageId = MessageId(0))
else:
let validProofRes = manager.generateProof(data = messageBytes,
epoch = epoch)
require:
validProofRes.isOk()
let validProof = validProofRes.get()
Expand All @@ -566,7 +648,10 @@ suite "Onchain group manager":
let idCredential = generateCredentials(manager.rlnInstance)

try:
await manager.register(idCredential.idCommitment)
when defined(rln_v2):
await manager.register(getRateCommitment(idCredential, UserMessageLimit(1)))
else:
await manager.register(idCredential.idCommitment)
except Exception, CatchableError:
assert false, "exception raised when calling startGroupSync: " & getCurrentExceptionMsg()

Expand All @@ -575,6 +660,8 @@ suite "Onchain group manager":
## Assume the registration occured out of band
manager.idCredentials = some(idCredential2)
manager.membershipIndex = some(MembershipIndex(0))
when defined(rln_v2):
manager.userMessageLimit = some(UserMessageLimit(1))

let messageBytes = "Hello".toBytes()

Expand All @@ -583,8 +670,14 @@ suite "Onchain group manager":
debug "epoch in bytes", epochHex = epoch.inHex()

# generate proof
let invalidProofRes = manager.generateProof(data = messageBytes,
epoch = epoch)
when defined(rln_v2):
let invalidProofRes = manager.generateProof(data = messageBytes,
epoch = epoch,
messageId = MessageId(0))
else:
let invalidProofRes = manager.generateProof(data = messageBytes,
epoch = epoch)

require:
invalidProofRes.isOk()
let invalidProof = invalidProofRes.get()
Expand Down Expand Up @@ -613,18 +706,28 @@ suite "Onchain group manager":
proc generateCallback(futs: TestBackfillFuts, credentials: seq[IdentityCredential]): OnRegisterCallback =
var futureIndex = 0
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
if registrations.len == 1 and
registrations[0].idCommitment == credentials[futureIndex].idCommitment and
registrations[0].index == MembershipIndex(futureIndex):
futs[futureIndex].complete()
futureIndex += 1
when defined(rln_v2):
if registrations.len == 1 and
registrations[0].rateCommitment == getRateCommitment(credentials[futureIndex], UserMessageLimit(1)) and
registrations[0].index == MembershipIndex(futureIndex):
futs[futureIndex].complete()
futureIndex += 1
else:
if registrations.len == 1 and
registrations[0].idCommitment == credentials[futureIndex].idCommitment and
registrations[0].index == MembershipIndex(futureIndex):
futs[futureIndex].complete()
futureIndex += 1
return callback

try:
manager.onRegister(generateCallback(futures, credentials))
await manager.startGroupSync()
for i in 0 ..< credentials.len():
await manager.register(credentials[i])
when defined(rln_v2):
await manager.register(credentials[i], UserMessageLimit(1))
else:
await manager.register(credentials[i])
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()

Expand Down
11 changes: 9 additions & 2 deletions tests/waku_rln_relay/test_wakunode_rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,15 @@ procSuite "WakuNode - RLN relay":
contentTopicBytes = contentTopic.toBytes
input = concat(payload, contentTopicBytes)
extraBytes: seq[byte] = @[byte(1),2,3]
rateLimitProofRes = node1.wakuRlnRelay.groupManager.generateProof(concat(input, extraBytes), # we add extra bytes to invalidate proof verification against original payload
epoch)

when defined(rln_v2):
let nonceManager = node1.wakuRlnRelay.nonceManager
let rateLimitProofRes = node1.wakuRlnRelay.groupManager.generateProof(input,
epoch,
MessageId(0))
else:
let rateLimitProofRes = node1.wakuRlnRelay.groupManager.generateProof(concat(input, extraBytes), # we add extra bytes to invalidate proof verification against original payload
epoch)
require:
rateLimitProofRes.isOk()
let rateLimitProof = rateLimitProofRes.get().encode().buffer
Expand Down
Loading
Loading