Skip to content

Commit

Permalink
Merge cec881b into 46e28a2
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc authored Jun 5, 2024
2 parents 46e28a2 + cec881b commit 4b22da1
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 158 deletions.
109 changes: 56 additions & 53 deletions tests/waku_rln_relay/test_rln_group_manager_onchain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ proc generateCredentials(rlnInstance: ptr RLN): IdentityCredential =

proc getRateCommitment(
idCredential: IdentityCredential, userMessageLimit: UserMessageLimit
): RateCommitment =
): RlnRelayResult[RawRateCommitment] =
return RateCommitment(
idCommitment: idCredential.idCommitment, userMessageLimit: userMessageLimit
)
).toLeaf()

proc generateCredentials(rlnInstance: ptr RLN, n: int): seq[IdentityCredential] =
var credentials: seq[IdentityCredential]
Expand All @@ -60,24 +60,38 @@ proc uploadRLNContract*(ethClientAddress: string): Future[Address] {.async.} =
let balance = await web3.provider.eth_getBalance(web3.defaultAccount, "latest")
debug "Initial account balance: ", balance

# deploy registry contract with its constructor inputs
let receipt = await web3.deployContract(RegistryContractCode)
let contractAddress = receipt.contractAddress.get()
# deploy poseidon hasher bytecode
let poseidonT3Receipt = await web3.deployContract(PoseidonT3)
let poseidonT3Address = poseidonT3Receipt.contractAddress.get()
let poseidonAddressStripped = strip0xPrefix($poseidonT3Address)

# deploy lazy imt bytecode
let lazyImtReceipt = await web3.deployContract(LazyIMT.replace("__$PoseidonT3$__", poseidonAddressStripped))
let lazyImtAddress = lazyImtReceipt.contractAddress.get()
let lazyImtAddressStripped = strip0xPrefix($lazyImtAddress)

debug "Address of the deployed registry contract: ", contractAddress
# deploy waku rlnv2 contract
let wakuRlnContractReceipt = await web3.deployContract(WakuRlnV2Contract.replace("__$PoseidonT3$__", poseidonAddressStripped).replace("__$LazyIMT$__", lazyImtAddressStripped))
let wakuRlnContractAddress = wakuRlnContractReceipt.contractAddress.get()
let wakuRlnAddressStripped = strip0xPrefix($wakuRlnContractAddress)

let registryContract = web3.contractSender(WakuRlnRegistry, contractAddress)
let initReceipt = await registryContract.initialize().send()
let newStorageReceipt = await registryContract.newStorage(20.u256).send()
debug "Address of the deployed rlnv2 contract: ", wakuRlnContractAddress

# need to send concat: impl & init_bytes
let contractInput = encode(wakuRlnContractAddress).data & Erc1967ProxyContractInput
debug "contractInput", contractInput
let proxyReceipt = await web3.deployContract(Erc1967Proxy, contractInput = contractInput)

debug "proxy receipt", proxyReceipt
let proxyAddress = proxyReceipt.contractAddress.get()

debug "Receipt of the newStorage transaction: ", newStorageReceipt
let newBalance = await web3.provider.eth_getBalance(web3.defaultAccount, "latest")
debug "Account balance after the contract deployment: ", newBalance

await web3.close()
debug "disconnected from ", ethClientAddress

return contractAddress
return proxyAddress

proc createEthAccount(): Future[(keys.PrivateKey, Address)] {.async.} =
let web3 = await newWeb3(EthClient)
Expand Down Expand Up @@ -161,7 +175,7 @@ proc stopAnvil(runAnvil: Process) {.used.} =
proc setup(): Future[OnchainGroupManager] {.async.} =
let rlnInstanceRes =
createRlnInstance(tree_path = genTempPath("rln_tree", "group_manager_onchain"))
require:
check:
rlnInstanceRes.isOk()

let rlnInstance = rlnInstanceRes.get()
Expand Down Expand Up @@ -197,8 +211,7 @@ suite "Onchain group manager":

check:
manager.ethRpc.isSome()
manager.rlnContract.isSome()
manager.membershipFee.isSome()
manager.wakuRlnContract.isSome()
manager.initialized
manager.rlnContractDeployedBlockNumber > 0

Expand Down Expand Up @@ -261,6 +274,8 @@ suite "Onchain group manager":
asyncTest "startGroupSync: should sync to the state of the group":
let manager = await setup()
let credentials = generateCredentials(manager.rlnInstance)
let rateCommitment = getRateCommitment(credentials, UserMessageLimit(1)).valueOr:
raiseAssert $error
(await manager.init()).isOkOr:
raiseAssert $error

Expand All @@ -271,15 +286,10 @@ suite "Onchain group manager":

proc generateCallback(fut: Future[void]): OnRegisterCallback =
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
require:
check:
registrations.len == 1
registrations[0].index == 0
require:
registrations[0].rateCommitment ==
getRateCommitment(credentials, UserMessageLimit(1))

require:
registrations[0].index == 0
registrations[0].rateCommitment == rateCommitment
fut.complete()

return callback
Expand Down Expand Up @@ -323,9 +333,9 @@ suite "Onchain group manager":
): OnRegisterCallback =
var futureIndex = 0
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
let rateCommitment = getRateCommitment(credentials[futureIndex], UserMessageLimit(1))
if registrations.len == 1 and
registrations[0].rateCommitment ==
getRateCommitment(credentials[futureIndex], UserMessageLimit(1)) and
registrations[0].rateCommitment == rateCommitment.get() and
registrations[0].index == MembershipIndex(futureIndex):
futs[futureIndex].complete()
futureIndex += 1
Expand Down Expand Up @@ -400,18 +410,16 @@ suite "Onchain group manager":
asyncTest "register: callback is called":
let manager = await setup()

let idCommitment = generateCredentials(manager.rlnInstance).idCommitment
let idCredentials = generateCredentials(manager.rlnInstance)
let idCommitment = idCredentials.idCommitment

let fut = newFuture[void]()

proc callback(registrations: seq[Membership]): Future[void] {.async.} =
require:
let rateCommitment = getRateCommitment(idCredentials, UserMessageLimit(1))
check:
registrations.len == 1
require:
registrations[0].rateCommitment ==
RateCommitment(
idCommitment: idCommitment, userMessageLimit: UserMessageLimit(1)
)
registrations[0].rateCommitment == rateCommitment.get()
registrations[0].index == 0
fut.complete()

Expand All @@ -429,7 +437,7 @@ suite "Onchain group manager":
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()

check await fut.withTimeout(5.seconds)
await fut

await manager.stop()

Expand Down Expand Up @@ -457,7 +465,7 @@ suite "Onchain group manager":
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
if registrations.len == 1 and
registrations[0].rateCommitment ==
getRateCommitment(credentials, UserMessageLimit(1)) and
getRateCommitment(credentials, UserMessageLimit(1)).get() and
registrations[0].index == 0:
manager.idCredentials = some(credentials)
fut.complete()
Expand Down Expand Up @@ -485,7 +493,7 @@ suite "Onchain group manager":
data = messageBytes, epoch = epoch, messageId = MessageId(1)
)

require:
check:
validProofRes.isOk()
let validProof = validProofRes.get()

Expand Down Expand Up @@ -517,12 +525,10 @@ suite "Onchain group manager":
debug "epoch in bytes", epochHex = epoch.inHex()

# generate proof
let validProofRes = manager.generateProof(
let validProof = manager.generateProof(
data = messageBytes, epoch = epoch, messageId = MessageId(0)
)
require:
validProofRes.isOk()
let validProof = validProofRes.get()
).valueOr:
raiseAssert $error

# validate the root (should be false)
let validated = manager.validateRoot(validProof.merkleRoot)
Expand All @@ -542,7 +548,7 @@ suite "Onchain group manager":
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
if registrations.len == 1 and
registrations[0].rateCommitment ==
getRateCommitment(credentials, UserMessageLimit(1)) and
getRateCommitment(credentials, UserMessageLimit(1)).get() and
registrations[0].index == 0:
manager.idCredentials = some(credentials)
fut.complete()
Expand All @@ -565,20 +571,16 @@ suite "Onchain group manager":
debug "epoch in bytes", epochHex = epoch.inHex()

# generate proof
let validProofRes = manager.generateProof(
let validProof = manager.generateProof(
data = messageBytes, epoch = epoch, messageId = MessageId(0)
)
require:
validProofRes.isOk()
let validProof = validProofRes.get()
).valueOr:
raiseAssert $error

# verify the proof (should be true)
let verifiedRes = manager.verifyProof(messageBytes, validProof)
require:
verifiedRes.isOk()
let verified = manager.verifyProof(messageBytes, validProof).valueOr:
raiseAssert $error

check:
verifiedRes.get()
check: verified
await manager.stop()

asyncTest "verifyProof: should reject invalid proof":
Expand All @@ -591,7 +593,8 @@ suite "Onchain group manager":
let idCredential = generateCredentials(manager.rlnInstance)

try:
await manager.register(getRateCommitment(idCredential, UserMessageLimit(1)))
await manager.register(RateCommitment(idCommitment: idCredential.idCommitment,
userMessageLimit: UserMessageLimit(1)))
except Exception, CatchableError:
assert false,
"exception raised when calling startGroupSync: " & getCurrentExceptionMsg()
Expand All @@ -614,7 +617,7 @@ suite "Onchain group manager":
data = messageBytes, epoch = epoch, messageId = MessageId(0)
)

require:
check:
invalidProofRes.isOk()
let invalidProof = invalidProofRes.get()

Expand Down Expand Up @@ -645,7 +648,7 @@ suite "Onchain group manager":
proc callback(registrations: seq[Membership]): Future[void] {.async.} =
if registrations.len == 1 and
registrations[0].rateCommitment ==
getRateCommitment(credentials[futureIndex], UserMessageLimit(1)) and
getRateCommitment(credentials[futureIndex], UserMessageLimit(1)).get() and
registrations[0].index == MembershipIndex(futureIndex):
futs[futureIndex].complete()
futureIndex += 1
Expand All @@ -665,7 +668,7 @@ suite "Onchain group manager":
await allFutures(futures)

# At this point, we should have a full root queue, 5 roots, and partial buffer of 1 root
require:
check:
manager.validRoots.len() == credentialCount - 1
manager.validRootBuffer.len() == 1

Expand Down
4 changes: 2 additions & 2 deletions tests/waku_rln_relay/test_rln_group_manager_static.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ suite "Static group manager":
registrations[0].rateCommitment ==
RateCommitment(
idCommitment: idCommitment, userMessageLimit: DefaultUserMessageLimit
)
).toLeaf().get()
callbackCalled = true
fut.complete()

Expand Down Expand Up @@ -204,7 +204,7 @@ suite "Static group manager":
withdrawals[0].rateCommitment ==
RateCommitment(
idCommitment: idCommitment, userMessageLimit: DefaultUserMessageLimit
)
).toLeaf().get()

callbackCalled = true
fut.complete()
Expand Down
15 changes: 12 additions & 3 deletions waku/waku_rln_relay/contract.nim

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions waku/waku_rln_relay/group_manager/group_manager_base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export options, chronos, results, protocol_types, protocol_metrics, deques

type Membership* = object
index*: MembershipIndex
rateCommitment*: RateCommitment
rateCommitment*: RawRateCommitment

type OnRegisterCallback* = proc(registrations: seq[Membership]): Future[void] {.gcsafe.}
type OnWithdrawCallback* = proc(withdrawals: seq[Membership]): Future[void] {.gcsafe.}
Expand Down Expand Up @@ -73,7 +73,7 @@ method register*(
# The user may or may not have the identity secret to these commitments
# It should be used when detecting a batch of new members in the group, and syncing the group state
method registerBatch*(
g: GroupManager, rateCommitments: seq[RateCommitment]
g: GroupManager, rateCommitments: seq[RawRateCommitment]
): Future[void] {.base, async: (raises: [Exception]).} =
raise newException(
CatchableError, "registerBatch proc for " & $g.type & " is not implemented yet"
Expand Down
Loading

0 comments on commit 4b22da1

Please sign in to comment.