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
impl MerkleTreeState
Sourcepub fn new(root: Id32, num_blocks: u32, num_pieces: u32) -> Self
pub fn new(root: Id32, num_blocks: u32, num_pieces: u32) -> Self
Create a new empty tree state for a file.
Sourcepub fn num_pieces(&self) -> u32
pub fn num_pieces(&self) -> u32
Total number of pieces.
Sourcepub fn num_blocks(&self) -> u32
pub fn num_blocks(&self) -> u32
Total number of blocks.
Sourcepub fn has_piece_hash(&self, piece_index: u32) -> bool
pub fn has_piece_hash(&self, piece_index: u32) -> bool
Whether the piece-layer hash is available for a given piece.
Sourcepub fn set_piece_hashes(&mut self, hashes: Vec<Id32>)
pub fn set_piece_hashes(&mut self, hashes: Vec<Id32>)
Set piece-layer hashes (from hash response).
Sourcepub fn set_piece_hashes_and_verify(
&mut self,
hashes: Vec<Id32>,
blocks_per_piece: u32,
) -> Vec<u32>
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.
Sourcepub fn set_block_hash(&mut self, block_index: u32, hash: Id32) -> SetBlockResult
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.
Sourcepub fn is_block_verified(&self, block_index: u32) -> bool
pub fn is_block_verified(&self, block_index: u32) -> bool
Check if a specific block has been verified.
Sourcepub fn piece_verified(&self, piece_index: u32, blocks_per_piece: u32) -> bool
pub fn piece_verified(&self, piece_index: u32, blocks_per_piece: u32) -> bool
Check if all blocks in a piece have been verified.
Sourcepub fn piece_hashes(&self) -> Option<&[Id32]>
pub fn piece_hashes(&self) -> Option<&[Id32]>
Get a reference to the piece hashes, if available.