getHashes

fun getHashes(base: Int, index: Int, count: Int, proofLayers: Int): List<Sha256Hash>?

Extracts the hashes needed to answer a BitTorrent v2 hash_request (BEP-52) from this tree. This is the inverse of addHashesWithProof / Merkle.verifyProof. Direct port of merkle_tree::get_hashes (src/merkle_tree.cpp).

A request names count hashes at layer base, starting at index within that layer, plus proofLayers of uncle hashes to anchor the run up toward the root. Layers are counted up from the block layer: base == 0 is the block/leaf layer, and base == blocksPerPieceLog is the piece layer. This returns the run of count hashes followed by the uncle hashes, exactly the PeerMessage.Hashes.hashes layout the requester folds with Merkle.verifyProof.

Returns null (libtorrent's empty-vector decline) when we cannot answer with hashes that are all present in our tree: any requested run node is missing (and is not an implicit zero pad), or any required proof node / sibling is missing, or the request indices fall outside the tree. We never fabricate hashes. We return only nodes actually present in tree, so anything returned verifies against our known root.

Return

the run hashes followed by the proof hashes, or null if we cannot fully answer.

Parameters

base

the requested layer, counted up from the block layer (0 = block leaves).

index

offset of the first hash within the base layer (>= 0).

count

number of hashes to return from the base layer (> 0).

proofLayers

number of uncle-hash layers to append above the run.