Each time we add a leaf, it should be imperative to check the state of our local tree against the state on-chain. If it matches, we can proceed. If it doesn't match we need to:
Clear our cache
Start over (fetch all the events using the fast sync for events)
Rebuild the tree
Validate and continue until we get this right.
Use a local Merkle tree instance in the relayer to validate the leaves against the Merkle root on the chain. You can use the Merkle tree found in: https://github.com/webb-tools/zero-knowledge-gadgets/blob/master/arkworks/native-gadgets/src/merkle_tree.rs.
Each time we add a leaf, it should be imperative to check the state of our local tree against the state on-chain. If it matches, we can proceed. If it doesn't match we need to:
Clear our cache Start over (fetch all the events using the fast sync for events) Rebuild the tree Validate and continue until we get this right.
Evm implementation for reference: https://github.com/webb-tools/relayer/pull/354