Limit active pieces and expose file list

This commit is contained in:
ookami125 2026-06-17 02:23:15 -04:00
parent ff8e0f5f5b
commit 6dc711cf57
6 changed files with 117 additions and 3 deletions

View file

@ -90,6 +90,36 @@ int main(void) {
naut_download_destroy(d);
/* The picker should not open the entire torrent at once. A large swarm can
* keep many requests in flight, but new-piece fanout is bounded so the
* piece map does not show most pieces "downloading" while few verify. */
enum { CAP_NP = 80 };
uint64_t cap_total = (uint64_t)CAP_NP * NAUT_BLOCK;
uint8_t *cap_hashes = malloc(CAP_NP * NAUT_SHA1_LEN);
CHECK(cap_hashes != NULL);
for (int p = 0; p < CAP_NP; p++)
naut_sha1(data, NAUT_BLOCK, cap_hashes + p * NAUT_SHA1_LEN);
naut_file cap_file[1] = { { (char *)"cap.bin", (int64_t)cap_total } };
naut_metainfo cap_mi; memset(&cap_mi, 0, sizeof cap_mi);
cap_mi.has_v1 = true; cap_mi.num_pieces = CAP_NP;
cap_mi.piece_length = NAUT_BLOCK; cap_mi.total_length = cap_total;
cap_mi.piece_hashes = cap_hashes; cap_mi.files = cap_file;
cap_mi.num_files = 1; cap_mi.name = (char *)"cap";
d = naut_download_create(&cap_mi, st);
CHECK(d != NULL);
naut_bitfield all;
naut_bitfield_init(&all, CAP_NP);
for (int p = 0; p < CAP_NP; p++) naut_bitfield_set(&all, p);
naut_download_add_bitfield(d, &all);
int opened = 0;
while (naut_download_pick(d, &all, &idx, &begin, &len))
opened++;
CHECK(opened > 0);
CHECK(opened < CAP_NP);
naut_bitfield_free(&all);
naut_download_destroy(d);
free(cap_hashes);
/* full multi-peer download: alternate peers, all pieces verify */
d = naut_download_create(&mi, st);
naut_download_add_bitfield(d, &hb); /* one peer that has everything */