9 breaking releases

0.10.0 Mar 23, 2024
0.9.0 Oct 16, 2023
0.8.0 Oct 3, 2023
0.6.0 May 24, 2023
0.0.0 Jan 10, 2022

#7 in #leaf-node

Download history 834/week @ 2024-07-13 978/week @ 2024-07-20 879/week @ 2024-07-27 380/week @ 2024-08-03 468/week @ 2024-08-10 934/week @ 2024-08-17 823/week @ 2024-08-24 754/week @ 2024-08-31 864/week @ 2024-09-07 843/week @ 2024-09-14 808/week @ 2024-09-21 595/week @ 2024-09-28 734/week @ 2024-10-05 715/week @ 2024-10-12 679/week @ 2024-10-19 734/week @ 2024-10-26

2,942 downloads per month
Used in 14 crates (5 directly)

Apache-2.0

470KB
8K SLoC

Penumbra's Jellyfish Merkle Tree

This repository is a fork of the Diem Jellyfish Merkle Tree crate, modified to inline dependencies and trim the parts not needed for Penumbra's uses.


lib.rs:

This module implements JellyfishMerkleTree backed by storage module. The tree itself doesn't persist anything, but realizes the logic of R/W only. The write path will produce all the intermediate results in a batch for storage layer to commit and the read path will return results directly. The public APIs are only new, put_value_sets, put_value_set and get_with_proof. After each put with a value_set based on a known version, the tree will return a new root hash with a TreeUpdateBatch containing all the new nodes and indices of stale nodes.

A Jellyfish Merkle Tree itself logically is a 256-bit sparse Merkle tree with an optimization that any subtree containing 0 or 1 leaf node will be replaced by that leaf node or a placeholder node with default hash value. With this optimization we can save CPU by avoiding hashing on many sparse levels in the tree. Physically, the tree is structurally similar to the modified Patricia Merkle tree of Ethereum but with some modifications. A standard Jellyfish Merkle tree will look like the following figure:

                                    .──────────────────────.
                            _.─────'                        `──────.
                       _.──'                                        `───.
                   _.─'                                                  `──.
               _.─'                                                          `──.
             ,'                                                                  `.
          ,─'                                                                      '─.
        ,'                                                                            `.
      ,'                                                                                `.
     ╱                                                                                    ╲
    ╱                                                                                      ╲
   ╱                                                                                        ╲
  ╱                                                                                          ╲
 ;                                                                                            :
 ;                                                                                            :
;                                                                                              :
│                                                                                              │
+──────────────────────────────────────────────────────────────────────────────────────────────+
 .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.  .''.
/    \/    \/    \/    \/    \/    \/    \/    \/    \/    \/    \/    \/    \/    \/    \/    \
+----++----++----++----++----++----++----++----++----++----++----++----++----++----++----++----+
 (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (
  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )
 (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (
  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )
 (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (
  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )
 (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (
  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )  )
 (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (  (
 ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■  ■

 ■: the [`Value`] type this tree stores.

A Jellyfish Merkle Tree consists of InternalNode and LeafNode. InternalNode is like branch node in ethereum patricia merkle with 16 children to represent a 4-level binary tree and LeafNode is similar to that in patricia merkle too. In the above figure, each bell in the jellyfish is an InternalNode while each tentacle is a LeafNode. It is noted that Jellyfish merkle doesn't have a counterpart for extension node of ethereum patricia merkle.

Dependencies

~4–10MB
~112K SLoC