feat: no need to indexing of sync strategy

This commit is contained in:
darshankabariya 2025-03-14 03:17:41 +05:30
parent 521aca37aa
commit 7313a2ce28
2 changed files with 12 additions and 38 deletions

View File

@ -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(

View File

@ -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)
return ok(output)