fix(merkle): use checkpoint root when local tree is ahead of contract#8
Conversation
AmortizedMerkleTree commits in 32-element batches. When the local tree
has merged N batches but the contract has only committed M < N batches,
tree.root represents a future merkle root that doesn't exist on-chain yet,
causing "Remote merkle root not found on-chain".
Fix:
- In ingestEntryMemos: after each 32-element batch merge, store a
checkpoint-{N} node recording the committed root at that element count.
- In getProofByCids: when tree.totalElements > contractTreeElements,
look up checkpoint-{contractTreeElements} and use its hash as the
proof's merkle_root instead of the ahead-of-contract tree.root.
Also adds payIncludesFee passthrough in ops.prepareTransfer.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical synchronization problem between the local Merkle tree state and the on-chain contract's committed Merkle root. By implementing a checkpointing system, the application can now correctly generate proofs even when its local tree has processed more elements than the contract has officially acknowledged. Additionally, a minor enhancement was made to the transfer preparation logic to better handle fee inclusion. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the issue where the local Merkle tree state can be ahead of the on-chain contract state, leading to proof validation failures. The proposed solution of creating and using checkpoints for historical Merkle roots is well-implemented. The changes in merkleEngine.ts correctly store these checkpoints during leaf ingestion and look them up when generating proofs.
I have one suggestion in src/merkle/merkleEngine.ts to make the error handling more robust when a checkpoint is expected but not found.
The change in src/ops/ops.ts to pass payIncludesFee through prepareTransfer is a straightforward and correct addition.
| if (tree.totalElements > contractTreeElements && contractTreeElements > 0) { | ||
| const checkpoint = await this.storage!.getMerkleNode!(input.chainId, `checkpoint-${contractTreeElements}`); | ||
| if (checkpoint) effectiveRoot = checkpoint.hash; | ||
| } |
There was a problem hiding this comment.
The logic to use a checkpoint root is correct. However, if the checkpoint is not found, the current implementation silently falls back to using tree.root. This will likely cause the original Remote merkle root not found on-chain error downstream, making debugging difficult.
Since a checkpoint is expected to exist if tree.totalElements > contractTreeElements, its absence indicates a data inconsistency. It would be more robust to throw an error in this case to provide a clear signal that the local Merkle tree state is corrupted.
if (tree.totalElements > contractTreeElements && contractTreeElements > 0) {
const checkpoint = await this.storage!.getMerkleNode!(input.chainId, `checkpoint-${contractTreeElements}`);
if (!checkpoint) {
throw new SdkError('MERKLE', `Local merkle checkpoint not found for ${contractTreeElements} elements`, {
chainId: input.chainId,
contractTreeElements,
});
}
effectiveRoot = checkpoint.hash;
}
Problem
AmortizedMerkleTreecommits to the main tree in 32-element batches. WhentotalElements=64, the contract has committedmerkleRoots[1](after the first 32 elements). But the local tree keeps merging all received leaves eagerly — so after ingesting all 64 leaves,tree.rootrepresents the root after both batches, which would becomemerkleRoots[2]only when element 65 triggers the next commit.pickMerkleRootIndexsearchesmerkleRoots[0..9]for the proof's Merkle root and fails:Fix
src/merkle/merkleEngine.tsCheckpoint storage (
ingestEntryMemos): after each 32-element batch merge, store acheckpoint-{N}node with the committed root at exactly N merged elements:Checkpoint lookup (
getProofByCids): whentree.totalElements > contractTreeElements, look upcheckpoint-{contractTreeElements}and use its hash as the proof'smerkle_rootinstead of the ahead-of-contracttree.root:src/ops/ops.ts: passpayIncludesFeethroughprepareTransfer.Verified
Tested on Sepolia testnet with
totalElements=64(merkleRoots[1]active). Transfer-to-self loop runs continuously without errors.