I want to cache torrent blocks based on their content hash. In v2 instead of simple block hashes there's a merkle tree, whose exact composition I can't quite comprehend.
I am working with MonoTorrent library, and it has IPieceHashes.GetHash(hashIndex) function where hashIndex varies from from 0 to piece count - 1. But there's no similar function to get hash of individual blocks (in case piece consists of multiple blocks).
In v2 the easiest way to obtain them is to download a whole piece instead of a chunk, compute the leaf hashes from the data you have received and then verify the leaf hashes against the
piece layersin the.torrentmetadata file.Alternatively you can
hash requestthe leaf hashes while downloading chunks from a peer and verify them against the file root.Both approaches involve merkle tree verification.
In v1 you're limited to downloading a whole piece and verifying its hash against the
pieceslist in the metadata file.