Skip to main content

MerkleTree

Struct MerkleTree 

Source
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

Source

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).

Source

pub fn root(&self) -> Id32

The Merkle root hash.

Source

pub fn leaf_count(&self) -> usize

Number of actual (non-padding) leaves.

Source

pub fn depth(&self) -> usize

Tree depth (number of layers below the root). A single leaf has depth 0.

Source

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.

Source

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).

Source

pub fn leaves(&self) -> &[Id32]

Get the leaf hashes.

Source

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.

Source

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.

Source

pub fn verify_proof( root: Id32, leaf: Id32, leaf_index: usize, proof: &[Id32], ) -> bool

Verify a leaf hash against a root using a proof path.

Recomputes the root from the leaf and its sibling hashes, then compares.

Trait Implementations§

Source§

impl Clone for MerkleTree

Source§

fn clone(&self) -> MerkleTree

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MerkleTree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.