Skip to main content

HashPicker

Struct HashPicker 

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

Coordinates which Merkle hash requests to send to peers.

Priority: (1) block hashes for urgent/failed pieces, (2) piece-layer hashes in 512-chunk batches.

Implementations§

Source§

impl HashPicker

Source

pub fn new(files: &[FileHashInfo], blocks_per_piece: u32) -> Self

Create a new hash picker for the given files.

blocks_per_piece is piece_length / 16384.

Source

pub fn pick_hashes( &self, has_piece: impl Fn(u32) -> bool, ) -> Option<HashRequest>

Pick the next hash request to send.

has_piece checks if the peer has a given piece index. Priority: block requests (for failed/urgent pieces) > piece-layer requests.

Source

pub fn add_hashes( &mut self, req: &HashRequest, hashes: &[Id32], ) -> Result<AddHashesResult, Error>

Process received hashes from a peer.

For piece-layer hashes, validates the uncle proof against the known file root before accepting (Gap 1 fix). For block-layer hashes, stores them directly in the tree state.

§Errors

Returns an error if the hash request references an unknown file root.

Source

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

Record a computed block hash. Delegates to MerkleTreeState.

Gap 10 fix: removed unused offset parameter from original plan.

Source

pub fn verify_block_hashes(&mut self, piece: u32)

Request block hashes for a piece that failed verification.

Source

pub fn hashes_rejected(&mut self, req: &HashRequest)

Mark a hash request as rejected (peer couldn’t serve it).

Source

pub fn have_piece_hash(&self, file_index: usize, piece: u32) -> bool

Do we have the piece-layer hash for a specific piece?

Source

pub fn piece_verified(&self, file_index: usize, piece: u32) -> bool

Are all blocks in a piece verified?

Source

pub fn tree_depth(&self, file_index: usize) -> Option<u32>

Tree depth for a file (number of layers from root to block leaves).

Source

pub fn load_piece_layers( &mut self, piece_layers: &BTreeMap<Id32, Vec<u8>>, ) -> Vec<u32>

Pre-load piece-layer hashes from the .torrent file’s piece_layers map.

This is used when full metadata is available at torrent creation (not from a magnet link). Each entry maps a file’s Merkle root to the concatenated piece-layer hashes (32 bytes each). After loading, any blocks that were already hashed are retroactively verified.

Returns the list of piece indices that were fully verified during loading.

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.