From 1fa78b97c55ef67e540cf6aaa70ba222e119ba39 Mon Sep 17 00:00:00 2001 From: darshankabariya Date: Wed, 2 Apr 2025 22:57:49 +0530 Subject: [PATCH 1/3] chore: update datatype for matching solidity api --- waku/waku_rln_relay/group_manager/on_chain/group_manager.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim index 99526716d..58be8d25e 100644 --- a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim +++ b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim @@ -29,6 +29,7 @@ export group_manager_base logScope: topics = "waku rln_relay onchain_group_manager" +type UInt40* = StUint[40] # using the when predicate does not work within the contract macro, hence need to dupe contract(WakuRlnContract): # this serves as an entrypoint into the rln membership set @@ -46,7 +47,7 @@ contract(WakuRlnContract): # this constant describes max message limit of rln contract proc MAX_MESSAGE_LIMIT(): UInt256 {.view.} # this function returns the merkleProof for a given index - proc merkleProofElements(index: Uint256): seq[Uint256] {.view.} + proc merkleProofElements(index: UInt256): seq[UInt256] {.view.} # this function returns the Merkle root proc root(): Uint256 {.view.} From c1e952110ea7af7f642eb4827fd24c0f1551cad0 Mon Sep 17 00:00:00 2001 From: darshankabariya Date: Wed, 2 Apr 2025 23:45:59 +0530 Subject: [PATCH 2/3] chore: update datatype for matching solidity api uint40 --- .../group_manager/on_chain/group_manager.nim | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim index 58be8d25e..648a79e54 100644 --- a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim +++ b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim @@ -47,7 +47,7 @@ contract(WakuRlnContract): # this constant describes max message limit of rln contract proc MAX_MESSAGE_LIMIT(): UInt256 {.view.} # this function returns the merkleProof for a given index - proc merkleProofElements(index: UInt256): seq[UInt256] {.view.} + proc merkleProofElements(index: UInt40): seq[UInt256] {.view.} # this function returns the Merkle root proc root(): Uint256 {.view.} @@ -93,13 +93,17 @@ proc setMetadata*( proc fetchMerkleProofElements*( g: OnchainGroupManager ): Future[Result[seq[Uint256], string]] {.async.} = - let index = stuint(g.membershipIndex.get(), 256) + let membershipIndex = g.membershipIndex.get() + debug "Fetching merkle proof", index = membershipIndex try: + let index = stuint(membershipIndex, 40) + let merkleProofInvocation = g.wakuRlnContract.get().merkleProofElements(index) let merkleProof = await merkleProofInvocation.call() + debug "Successfully fetched merkle proof", elementsCount = merkleProof.len return ok(merkleProof) except CatchableError: - error "Failed to fetch merkle proof - 1", errMsg = getCurrentExceptionMsg() + error "Failed to fetch merkle proof", errMsg = getCurrentExceptionMsg() proc fetchMerkleRoot*( g: OnchainGroupManager @@ -325,12 +329,12 @@ method generateProof*( try: let proofResult = waitFor g.fetchMerkleProofElements() if proofResult.isErr(): - return err("Failed to fetch Merkle proof - 2: " & $proofResult.error) + return err("Failed to fetch Merkle proof" & $proofResult.error) g.merkleProofCache = proofResult.get() debug "Merkle proof fetched", membershipIndex = g.membershipIndex.get(), elementCount = g.merkleProofCache.len except CatchableError: - error "Failed to fetch merkle proof - 3", error = getCurrentExceptionMsg() + error "Failed to fetch merkle proof", error = getCurrentExceptionMsg() let witness = Witness( identity_secret: g.idCredentials.get().idSecretHash.toArray32(), From bbcbb9fec6fd9d1886ae03da5aac4d966c32c2b4 Mon Sep 17 00:00:00 2001 From: darshankabariya Date: Thu, 3 Apr 2025 01:15:24 +0530 Subject: [PATCH 3/3] chore: check membershipIndex isn;t bigger than currentCommentment --- .../group_manager/on_chain/group_manager.nim | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim index 648a79e54..403f60c1f 100644 --- a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim +++ b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim @@ -94,17 +94,45 @@ proc fetchMerkleProofElements*( g: OnchainGroupManager ): Future[Result[seq[Uint256], string]] {.async.} = let membershipIndex = g.membershipIndex.get() - debug "Fetching merkle proof", index = membershipIndex + debug " ------ Fetching merkle proof", index = membershipIndex try: - let index = stuint(membershipIndex, 40) + # First check if the index is valid + let commitmentIndexInvocation = g.wakuRlnContract.get().commitmentIndex() + let currentCommitmentIndex = await commitmentIndexInvocation.call() + let membershipIndexUint256 = stuint(membershipIndex, 256) - let merkleProofInvocation = g.wakuRlnContract.get().merkleProofElements(index) + debug " ------ Checking membership index validity", + membershipIndex = membershipIndex, + membershipIndexAsUint256 = membershipIndexUint256.toHex(), + currentCommitmentIndex = currentCommitmentIndex.toHex() + + # Convert to UInt40 for contract call (merkleProofElements takes UInt40) + let indexUint40 = stuint(membershipIndex, 40) + debug " ------ Converting membershipIndex to UInt40", + originalIndex = membershipIndex, asUint40 = indexUint40.toHex() + + let merkleProofInvocation = g.wakuRlnContract.get().merkleProofElements(indexUint40) let merkleProof = await merkleProofInvocation.call() debug "Successfully fetched merkle proof", elementsCount = merkleProof.len return ok(merkleProof) except CatchableError: error "Failed to fetch merkle proof", errMsg = getCurrentExceptionMsg() +# proc fetchMerkleProofElements*( +# g: OnchainGroupManager +# ): Future[Result[seq[Uint256], string]] {.async.} = +# let membershipIndex = g.membershipIndex.get() +# debug "Fetching merkle proof", index = membershipIndex +# try: +# let index = stuint(membershipIndex, 40) +# +# let merkleProofInvocation = g.wakuRlnContract.get().merkleProofElements(index) +# let merkleProof = await merkleProofInvocation.call() +# debug "Successfully fetched merkle proof", elementsCount = merkleProof.len +# return ok(merkleProof) +# except CatchableError: +# error "Failed to fetch merkle proof", errMsg = getCurrentExceptionMsg() + proc fetchMerkleRoot*( g: OnchainGroupManager ): Future[Result[Uint256, string]] {.async.} =