Repository navigation
[CRITICAL][Tier-A] Reputation lazy Merkle-sum tree with incremental on-chain scoring #387
Description
Tier
Tier A · CRITICAL — Gas-efficient stateful aggregation.
Context
contracts/reputation/src/lib.rs compute_score iterates up to MAX_TRADES (200) sequential trades by calling get_trade_by_index + get_trade on the escrow contract for every single index. On a busy mainnet deployment this is ~200 cross-contract calls per compute_score. The score is recomputed from scratch every time, even when only one new trade changed.
Goal
Replace the linear scan with a Merkle-sum tree (MST) maintained by the escrow contract. Each leaf is a trade's (amount, status, timestamp). The root is updated atomically on release / refund / dispute_resolve. The reputation contract then validates a sparse Merkle proof (≤3 hashes) instead of scanning.
Non-goals
No off-chain indexer that feeds the tree (the root must be derivable purely from on-chain state).
No ZK proof for the score — the score is still computed in Solidity/Rust, just over a smaller tree.
No change to the external compute_score(address) API.
Data model
New types in contracts/reputation/src/lib.rs:
- ReputationLeaf: (trade_id_hash, amount, status_bits, counterparty_hash, ledger)
- ScoreProof: (leaf_index, sibling_hashes[..MAX_DEPTH], score_components)
New storage in contracts/escrow/src/lib.rs:
- ReputationRoot(BytesN<32>) — current MST root, updated on terminal trade state changes.
- ReputationLeaf(BytesN<32>) — leaf data for each completed/refunded trade.
Implementation plan
- Add MerkleSumTree module to contracts/escrow/src/lib.rs (or a shared htlc-core crate). Leaf hash = H(amount || status || counterparty_hash || ledger).
- In release, refund, batch_release, resolve_dispute, and fallback_after_timeout, update the MST root after flipping state. Use a helper update_reputation_root(env, trade_id, old_status, new_status, amount).
- Add a new read-only escrow contract method get_reputation_root() -> BytesN<32> and get_reputation_proof(address, max_trades) -> ScoreProof.
- Rewrite contracts/reputation/src/lib.rs compute_score to accept a ScoreProof from the caller (or fetch it via cross-contract call if the caller is the contract itself). Verify the MST path matches ReputationRoot.
- Add compute_score_incremental that only touches new leaves since the last cached score.
- Write contracts/reputation/src/mst_test.rs with randomized insertion/deletion/verification property tests.
Thresholds
- MAX_TRADES can increase to 10,000 because MST verification is O(log n).
- DECAY_TABLE index must be derived from the MST leaf's ledger field, not a global scan.
Acceptance criteria
- compute_score executes in <500k instructions for 10,000 trades (benchmark under cargo test).
- A proof with a single altered leaf fails MST verification.
- Escrow terminal-state transitions still publish the same events; released / refunded / disp_res / disp_exp events are unchanged.
- Existing get_score and get_score_breakdown read methods continue to work; they internally fetch and verify the MST proof.
Related files
- contracts/reputation/src/lib.rs
- contracts/escrow/src/lib.rs
- contracts/htlc-core/src/lib.rs
- contracts/reputation/src/test.rs
- contracts/reputation/src/benchmarks.rs
- packages/shared/src/index.ts
Contributor notes
A Merkle-sum tree is strictly better than a Merkle tree here because score math is additive. Reuse the Htlc trait's terminal-state transition points to keep MST updates atomic. Benchmark before and after — the gas delta is the selling point for mainnet.
Activity
Muhammadcodes112 commentedon Aug 18, 2026
I’ve assessed the scope of this issue and have a clear roadmap for the implementation. I can have this completed and tested by [Date/Time]. I'll ensure the code is well-documented and includes unit tests. Please go ahead and assign this to me.
OZILSOLAR commentedon Aug 18, 2026
Hi team!!!!
I would like to work on this issue!!!!!
grantfox-oss commentedon Aug 18, 2026
🦊 GrantFox — @Muhammadcodes112 has been assigned to this issue as part of the Third Campaign campaign!
Next steps:
Closes #387)Good luck! Track your progress on GrantFox.
Ranjeet2063 commentedon Sep 12, 2026
Hi @jotel-dev and maintainers,
I would like to resolve this critical issue:
[CRITICAL][Tier-A] Reputation lazy Merkle-sum tree with incremental on-chain scoring.Following my contribution on PR #475 (implementing the deterministic Merkle-Patricia Trie verifier), I have analyzed the gas bottleneck in
contracts/reputation/src/lib.rswhere linear scans across 200 sequential trades trigger cross-contract gas exhaustion.Proposed Architectural Execution Plan:
contracts/reputation/&contracts/escrow/.(amount: i128, status: u32, timestamp: u64)into a canonical 32-byte leaf digest.release,refund,dispute_resolve), the escrow contract updates strictly thecompute_score(address)to consume sparse Merkle-sum inclusion proofs (soroban_sdk::cryptoimplementation without heap allocations.Ready to begin implementation immediately and deliver a production-ready, 100% green CI pull request.
rupesh-kumar-sah commentedon Oct 1, 2026
Hi team,
I would like to take on and implement this issue:
[CRITICAL][Tier-A] Reputation lazy Merkle-sum tree with incremental on-chain scoring.Technical Implementation Roadmap:
Instance,Persistent,Temporary) with strict TTL management and explicitrequire_authguards.tests/asserting authorized vs unauthorized invocations, mock auth environments, and edge value bounds.I have worked extensively on similar architectures and can deliver this cleanly within 24 hours. Please assign to me!
ranjeet150 commentedon Oct 1, 2026
Hi team,
I would like to take on and implement this issue:
[CRITICAL][Tier-A] Reputation lazy Merkle-sum tree with incremental on-chain scoring.Technical Implementation Roadmap:
Instance,Persistent,Temporary) with strict TTL management and explicitrequire_authguards.tests/asserting authorized vs unauthorized invocations, mock auth environments, and edge value bounds.I have worked extensively on similar architectures and can deliver this cleanly within 24 hours. Please assign to me!
ravimahatonp commentedon Oct 1, 2026
Hi maintainers,
I would like to take on and implement this issue:
[CRITICAL][Tier-A] Reputation lazy Merkle-sum tree with incremental on-chain scoring.Technical Implementation Roadmap:
Instance,Persistent,Temporary) with strict TTL management and explicitrequire_authguards.tests/asserting authorized vs unauthorized invocations, mock auth environments, and edge value bounds.Ready to implement and deliver a production-grade PR following all repository standards. Please feel free to assign!