Expand description
Jellyfish Merkle Tree for Tari.
§Hash schemes
Node hashing is selected by JmtHashScheme, passed explicitly to JellyfishMerkleTree::new and to every
SparseMerkleProofExt::verify_* call. There is no default. A verifier takes the scheme from its authenticated
context (e.g. the protocol version in a signed header), never from a proof, which carries no scheme tag.
| Scheme | Domain (name, version) | Leaf label | Internal label | First crate version |
|---|---|---|---|---|
JmtHashScheme::V1 | com.tari.jmt, 0 | "Leaf" | "Internal" | 6.0.1-pre.2 |
Earlier crate versions hashed leaf and internal nodes with the single label "Node". That scheme is not
supported: no live root uses it. jmt_node_hash and jmt_node_hash2 still use the "Node" label but are
general-purpose helpers (e.g. key derivation), independent of the scheme.
A store does not record its scheme: opening an existing store under a different scheme silently yields roots that verify under neither scheme.
A scheme never changes once released. A new scheme is a new variant with its own domain version and distinct leaf and internal labels, and callers gate it on their own protocol version.
§Encoding
Tree, node and proof types implement serde and borsh. The minicbor feature adds native minicbor Encode,
Decode and CborLen impls with a compact array-based format, documented in src/cbor.rs. Proof types are
validated on decode. Tree node types are not, for speed, and must only be decoded from trusted storage.
§Version pinning
command_merkle_root in a sidechain block header is built by the validator node and verified by
tari_sidechain, so a build that links a different tari_jellyfish into each, or maps a protocol version to
a different scheme in each, produces roots the other side rejects.
Structs§
- Child
- Each child of
InternalNodeencapsulates a nibble forking at this node. - Child
Nibbles - Iterates the occupied nibbles of
Childrenin ascending order. - Children
- The children of an
InternalNode, indexed by nibble (0 to 15, inclusive) and always in ascending nibble order. - Domain
Separated Borsh Hasher - A domain-separated hasher that feeds the Borsh encoding of its input to the digest, so hashing is canonical.
- Internal
Node - Represents a 4-level subtree with 16 children at the bottom level. Theoretically, this reduces IOPS to query a tree by 4x since we compress 4 levels in a standard Merkle tree into 1 node. Though we choose the same internal node structure as that of Patricia Merkle tree, the root hash computation logic is similar to a 4-level sparse Merkle tree except for some customizations.
- Jellyfish
Merkle Tree - The Jellyfish Merkle tree data structure. See
cratefor description. - JmtHash
Domain - A hashing domain instance
- LeafKey
- A leaf key (i.e. a complete nibble path).
- Leaf
KeyBit Iterator - An iterator over
LeafKeythat generates one bit for each iteration. - Leaf
KeyRef - A leaf key (i.e. a complete nibble path).
- Leaf
Node - Leaf node, capturing the value hash and carrying an arbitrary payload.
- Nibble
- Nibble
BitIterator - BitIterator iterates a nibble path by bit.
- Nibble
Iterator - NibbleIterator iterates a nibble path by nibble.
- Nibble
OutOf Range - Nibble
Path - NibblePath defines a path in Merkle tree in the unit of nibble (4 bits).
- NodeKey
- The unique key of each node.
- Sorted
Leaf Root Builder - Computes the root hash of the tree holding a set of leaves, from the leaves in strictly ascending key order, without storing the tree.
- Sparse
Merkle Internal Node - Sparse
Merkle Leaf Node - Sparse
Merkle Proof - A proof that can be used to authenticate an element in a Sparse Merkle Tree given trusted root
hash. For example,
TransactionInfoToAccountProofcan be constructed on top of this structure. - Sparse
Merkle Proof Ext - A more detailed version of
SparseMerkleProofwith the only difference that all the leaf siblings are explicitly set asSparseMerkleLeafNodeinstead of its hash value. - Sparse
Merkle Range Proof - Note: this is not a range proof in the sense that a range of nodes is verified! Instead, it verifies the entire left part of the tree up to a known rightmost node. See the description below.
- Stale
Node Index - Indicates a node becomes stale since
stale_since_version. - Tree
Hash - Tree
Hash Size Error - Tree
Update Batch - The nodes to write and the nodes made stale by one
batch_put_value_setcall.
Enums§
- Internal
Node Error - JmtHash
Scheme - The scheme used to hash JMT leaf and internal nodes, and so every root and proof.
- JmtProof
Verify Error - JmtStorage
Error - Error originating from underlying storage failure / inconsistency.
- Nibble
Path Error - Node
- The concrete node type of [
JellyfishMerkleTree]. - Node
InProof - A sibling in a
SparseMerkleProofExt. - Node
Type - Sorted
Leaf Root Error - Stale
Tree Node - A part of a tree that may become stale (i.e. need eventual pruning).
- Tree
Node
Constants§
- MAX_
NIBBLE_ PATH_ BYTES - The maximum number of bytes in a
NibblePath. - MAX_
NIBBLE_ PATH_ LEN - The maximum number of nibbles in a
NibblePath, i.e. the nibble length of aLeafKey. - MAX_
PROOF_ SIBLINGS - The maximum number of siblings in a proof, i.e. the bit length of a
LeafKey. - SPARSE_
MERKLE_ PLACEHOLDER_ HASH
Traits§
- Iterated
Leaf Key - Peekable
- Tree
Store - Implementers are able to read and write nodes to a tree store.
- Tree
Store Reader - Implementers are able to read nodes from a tree store.
- Tree
Store Writer - Implementers are able to insert nodes to a tree store.
Functions§
- jmt_
node_ hash - jmt_
node_ hash2 - tari_
hasher32 - Returns a 32-byte
TariDomainHasherfor the domainMand the given label.
Type Aliases§
- JmtHasher
- Proof
Value - Tari
Domain Hasher - A
DomainSeparatedBorshHasherover Blake2b with an output ofOutSizebytes. - Version