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
impl HashPicker
Sourcepub fn new(files: &[FileHashInfo], blocks_per_piece: u32) -> Self
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.
Sourcepub fn pick_hashes(
&self,
has_piece: impl Fn(u32) -> bool,
) -> Option<HashRequest>
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.
Sourcepub fn add_hashes(
&mut self,
req: &HashRequest,
hashes: &[Id32],
) -> Result<AddHashesResult, Error>
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.
Sourcepub fn set_block_hash(
&mut self,
file_index: usize,
block_index: u32,
hash: Id32,
) -> SetBlockResult
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.
Sourcepub fn verify_block_hashes(&mut self, piece: u32)
pub fn verify_block_hashes(&mut self, piece: u32)
Request block hashes for a piece that failed verification.
Sourcepub fn hashes_rejected(&mut self, req: &HashRequest)
pub fn hashes_rejected(&mut self, req: &HashRequest)
Mark a hash request as rejected (peer couldn’t serve it).
Sourcepub fn have_piece_hash(&self, file_index: usize, piece: u32) -> bool
pub fn have_piece_hash(&self, file_index: usize, piece: u32) -> bool
Do we have the piece-layer hash for a specific piece?
Sourcepub fn piece_verified(&self, file_index: usize, piece: u32) -> bool
pub fn piece_verified(&self, file_index: usize, piece: u32) -> bool
Are all blocks in a piece verified?
Sourcepub fn tree_depth(&self, file_index: usize) -> Option<u32>
pub fn tree_depth(&self, file_index: usize) -> Option<u32>
Tree depth for a file (number of layers from root to block leaves).
Sourcepub fn load_piece_layers(
&mut self,
piece_layers: &BTreeMap<Id32, Vec<u8>>,
) -> Vec<u32>
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.