pub struct MerkleTree { /* private fields */ }Expand description
A complete binary Merkle tree stored in a flat array (1-indexed heap layout).
Index 1 is the root. For a node at index i, its left child is at 2*i
and its right child is at 2*i + 1.
Implementations§
Source§impl MerkleTree
impl MerkleTree
Sourcepub fn from_leaves(leaves: &[Id32]) -> Self
pub fn from_leaves(leaves: &[Id32]) -> Self
Build a Merkle tree from leaf hashes.
Pads to the next power of two with zero hashes, then builds bottom-up
by hashing pairs: parent = SHA-256(left || right).
Sourcepub fn leaf_count(&self) -> usize
pub fn leaf_count(&self) -> usize
Number of actual (non-padding) leaves.
Sourcepub fn depth(&self) -> usize
pub fn depth(&self) -> usize
Tree depth (number of layers below the root). A single leaf has depth 0.
Sourcepub fn layer(&self, depth: usize) -> &[Id32]
pub fn layer(&self, depth: usize) -> &[Id32]
Get all hashes at a given depth (0 = root, depth() = leaves).
Returns a slice of the internal array at that tree level.
Sourcepub fn piece_layer(&self, blocks_per_piece: usize) -> &[Id32]
pub fn piece_layer(&self, blocks_per_piece: usize) -> &[Id32]
Get the piece layer — the tree layer whose nodes correspond to pieces.
blocks_per_piece is piece_length / 16384 (number of 16 KiB blocks per piece).
The piece layer is at depth depth() - log2(blocks_per_piece).
Sourcepub fn root_from_hashes(hashes: &[Id32]) -> Id32
pub fn root_from_hashes(hashes: &[Id32]) -> Id32
Compute a Merkle root from a list of hashes (e.g., verify piece layer → root).
Pads to power of two and builds a temporary tree. Useful for validating that a received piece layer matches a known root.
Sourcepub fn proof_path(&self, leaf_index: usize) -> Vec<Id32>
pub fn proof_path(&self, leaf_index: usize) -> Vec<Id32>
Extract the Merkle proof path for a specific leaf.
Returns the sibling hashes from leaf to root needed to verify the leaf against the root. Used by M34’s hash request/response (BEP 52 wire messages 21-23) for block-level verification.
Trait Implementations§
Source§impl Clone for MerkleTree
impl Clone for MerkleTree
Source§fn clone(&self) -> MerkleTree
fn clone(&self) -> MerkleTree
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more