perf(shielded-pool): replace O(n) root recomputation with incremental frontier#78
Merged
Merged
Conversation
… frontier insert_leaf formerly called get_all_leaves() and recomputed the full Poseidon Merkle root from scratch on every insert (O(n) reads, O(n log n) hashes). At 10k commitments this meant ~10k storage reads per shield or private_transfer extrinsic. Replace with an incremental frontier algorithm: persist a 20-slot array (MerkleTreeFrontier storage item) that stores the last left-sibling at each tree level. Each insert now takes exactly 20 hashes and 1 storage read/write, regardless of tree size. Complexity is O(depth) = O(20) — constant at any scale. Changes: - lib.rs: add MerkleTreeFrontier<T> StorageValue<_, [[u8;32]; 20]> - storage.rs: add get_frontier / set_frontier to MerkleRepository - merkle.rs: rewrite MerkleTreeService::insert_leaf to use frontier; remove compute_poseidon_merkle_root (now unused); fix old_root in MerkleRootUpdated event (was always [0u8;32], now carries real value) - genesis.rs: initialize MerkleTreeFrontier to all-zeros at genesis Add 5 tests: - incremental_root_matches_batch_root_after_single_insert - incremental_root_matches_batch_root_after_multiple_inserts - incremental_proof_verifies_against_incremental_root - merkle_root_updated_event_carries_correct_old_root - frontier_survives_storage_round_trip_across_separate_calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
insert_leaf formerly called get_all_leaves() and recomputed the full Poseidon Merkle root from scratch on every insert (O(n) reads, O(n log n) hashes). At 10k commitments this meant ~10k storage reads per shield or private_transfer extrinsic.
Replace with an incremental frontier algorithm: persist a 20-slot array (MerkleTreeFrontier storage item) that stores the last left-sibling at each tree level. Each insert now takes exactly 20 hashes and 1 storage read/write, regardless of tree size. Complexity is O(depth) = O(20) — constant at any scale.
Changes:
Add 5 tests: