Skip to content

Add: get byte ranges from piece indexes#6

Merged
sneakers-the-rat merged 4 commits intomainfrom
byte-ranges
Jul 29, 2025
Merged

Add: get byte ranges from piece indexes#6
sneakers-the-rat merged 4 commits intomainfrom
byte-ranges

Conversation

@sneakers-the-rat
Copy link
Contributor

When we want to do partial validations of data (e.g. when testing if a webseed is legit, or... yno... downloading torrents), we need to get byte ranges associated with piece hashes. The behavior differs somewhat significantly between v1 and v2, so we made separate implementations for both

  • v1: a piece may correspond to a range within a single file or across several, and may include padfiles that shouldn't really "exist" on a filesystem
  • v2: much simpler, a file either has a single root hash or a set of hashes from a lower level of the merkle tree, both are computed identically. pieces are always either a whole file or a part of a single file.

So we have two methods to get v1 and v2 ranges, which return a PieceRange object that can validate data passed to validate_data

so e.g. if we have a v1 torrent of 5 10KiB files of all zeros, and a piece size of 32 KiB, we might do somethign like this

piece_range = torrent.v1_piece_range(0)
piece_range.validate_data([bytes(10), bytes(10), bytes(10), bytes(2)])

and v2 torrents work at the block level, as they usually do, so if we had a single-file v2 torrent with an empty 64 KiB file with a piece size of 64KiB, we would do

piece_range = torrent.v2_piece_range('filename')
piece_range.validate_data([bytes(16 * KiB) for _ in range(4)])

additionally

  • changed the behavior of v2 piece layers dict to match v1 pieces: when in memory, we split up the pieces into a list of hashes, rather than one bigass bytestring, and then split again on serialization.
  • reordered methods in Torrent to put validators at end, props up front, then methods in the middle.

@sneakers-the-rat sneakers-the-rat merged commit c8b2db6 into main Jul 29, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant