From 7313a2ce28a0dee4ceef68ef424f61523f84c77f Mon Sep 17 00:00:00 2001 From: darshankabariya Date: Fri, 14 Mar 2025 03:17:41 +0530 Subject: [PATCH] feat: no need to indexing of sync strategy --- .../group_manager/on_chain/group_manager.nim | 21 -------------- .../on_chain_sync/group_manager.nim | 29 ++++++++----------- 2 files changed, 12 insertions(+), 38 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 b1fa8bb79..50df20cf0 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 @@ -68,7 +68,6 @@ type validRootBuffer*: Deque[MerkleNode] # interval loop to shut down gracefully blockFetchingActive*: bool - merkleProofsByIndex*: Table[Uint256, seq[Uint256]] const DefaultKeyStorePath* = "rlnKeystore.json" const DefaultKeyStorePassword* = "password" @@ -92,16 +91,6 @@ template retryWrapper( retryWrapper(res, RetryStrategy.new(), errStr, g.onFatalErrorAction): body -proc fetchMerkleProof*(g: OnchainGroupManager, index: Uint256) {.async.} = - ## Fetches and caches the Merkle proof elements for a given index - try: - let merkleProofInvocation = g.wakuRlnContract.get().merkleProofElements(index) - let merkleProof = await merkleProofInvocation.call() - # Await the contract call and extract the result - g.merkleProofsByIndex[index] = merkleProof - except CatchableError: - error "Failed to fetch merkle proof: " & getCurrentExceptionMsg() - proc setMetadata*( g: OnchainGroupManager, lastProcessedBlock = none(BlockNumber) ): GroupManagerResult[void] = @@ -361,10 +350,6 @@ proc handleEvents( toRemoveIndices = removalIndices, ) - for i in 0 ..< rateCommitments.len: - let index = startIndex + MembershipIndex(i) - await g.fetchMerkleProof(stuint(index, 256)) - g.latestIndex = startIndex + MembershipIndex(rateCommitments.len) trace "new members added to the Merkle tree", commitments = rateCommitments.mapIt(it.inHex) @@ -385,12 +370,6 @@ proc handleRemovedEvents( if members.anyIt(it[1]): numRemovedBlocks += 1 - # Remove cached merkleProof for each removed member - for member in members: - if member[1]: # Check if the member is removed - let index = member[0].index - g.merkleProofsByIndex.del(stuint(index, 256)) - await g.backfillRootQueue(numRemovedBlocks) proc getAndHandleEvents( diff --git a/waku/waku_rln_relay/group_manager/on_chain_sync/group_manager.nim b/waku/waku_rln_relay/group_manager/on_chain_sync/group_manager.nim index bb7aad2e3..4ee58f1f4 100644 --- a/waku/waku_rln_relay/group_manager/on_chain_sync/group_manager.nim +++ b/waku/waku_rln_relay/group_manager/on_chain_sync/group_manager.nim @@ -13,8 +13,16 @@ logScope: topics = "waku rln_relay onchain_sync_group_manager" type OnChainSyncGroupManager* = ref object of OnchainGroupManager - # Cache for merkle proofs by index - merkleProofsByIndex*: Table[Uint256, seq[Uint256]] + +proc fetchMerkleProof*(g: OnchainSyncGroupManager) {.async.} = + let index = stuint(g.membershipIndex.get(), 256) + try: + let merkleProofInvocation = g.wakuRlnContract.get().merkleProofElements(index) + let merkleProof = await merkleProofInvocation.call() + # Await the contract call and extract the result + return merkleProof + except CatchableError: + error "Failed to fetch merkle proof: " & getCurrentExceptionMsg() method generateProof*( g: OnChainSyncGroupManager, @@ -32,20 +40,7 @@ method generateProof*( if g.userMessageLimit.isNone(): return err("user message limit is not set") - # Retrieve the cached Merkle proof for the membership index - let index = stuint(g.membershipIndex.get(), 256) - - if not g.merkleProofsByIndex.hasKey(index): - try: - let merkleProofInvocation = g.wakuRlnContract.get().merkleProofElements(index) - let merkleProof = await merkleProofInvocation.call() - g.merkleProofsByIndex[index] = merkleProof - except CatchableError: - return err("Failed to fetch merkle proof: " & getCurrentExceptionMsg()) - - let merkleProof = g.merkleProofsByIndex[index] - if merkleProof.len == 0: - return err("Merkle proof not found") + let merkleProof = g.fetchMerkleProof() # Prepare the witness let witness = Witness( @@ -105,4 +100,4 @@ method generateProof*( shareY: shareY, nullifier: nullifier, ) - return ok(output) \ No newline at end of file + return ok(output)