Skip to main content

MerkleTreeState

Struct MerkleTreeState 

Source
pub struct MerkleTreeState { /* private fields */ }
Expand description

Per-file Merkle tree verification state for BEP 52 downloads.

Tracks which blocks have been verified against the file’s Merkle tree. Handles the case where block data arrives before piece-layer hashes (deferred verification via SetBlockResult::Unknown).

Implementations§

Source§

impl MerkleTreeState

Source

pub fn new(root: Id32, num_blocks: u32, num_pieces: u32) -> Self

Create a new empty tree state for a file.

Source

pub fn root(&self) -> Id32

The file’s Merkle root hash.

Source

pub fn num_pieces(&self) -> u32

Total number of pieces.

Source

pub fn num_blocks(&self) -> u32

Total number of blocks.

Source

pub fn has_piece_hash(&self, piece_index: u32) -> bool

Whether the piece-layer hash is available for a given piece.

Source

pub fn set_piece_hashes(&mut self, hashes: Vec<Id32>)

Set piece-layer hashes (from hash response).

Source

pub fn set_piece_hashes_and_verify( &mut self, hashes: Vec<Id32>, blocks_per_piece: u32, ) -> Vec<u32>

Set piece-layer hashes and retroactively verify any stored block hashes.

Returns a list of piece indices that were fully verified by this operation.

Source

pub fn set_block_hash(&mut self, block_index: u32, hash: Id32) -> SetBlockResult

Record a computed block hash. Returns the verification result.

  • Ok: all blocks in the piece verified against piece-layer hash.
  • Unknown: piece-layer hash unavailable, or not all sibling blocks present yet.
  • HashFailed: block hash doesn’t match the Merkle tree.
Source

pub fn is_block_verified(&self, block_index: u32) -> bool

Check if a specific block has been verified.

Source

pub fn piece_verified(&self, piece_index: u32, blocks_per_piece: u32) -> bool

Check if all blocks in a piece have been verified.

Source

pub fn piece_hashes(&self) -> Option<&[Id32]>

Get a reference to the piece hashes, if available.

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