# Go (Receive metadata #zh1): Total sum of the length of all pieces is less than the length of the torrent

**URL:** <https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [March 31, 2025, 7:01pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004 "2025-03-31T19:01:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MlkMahmud](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/mlkmahmud/32/7763_2.png) [@MlkMahmud](https://forum.codecrafters.io/u/MlkMahmud)\
**Post date:** [March 31, 2025, 7:01pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/1 "2025-03-31T19:01:19Z")

</div>

I’m getting a piece length mismatch when running tests for Stage #ZH1, because the total sum of the length of all pieces is less than the length of the torrent file. Is this deliberate?

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [March 31, 2025, 7:17pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/4 "2025-03-31T19:17:16Z")

</div>

> the total sum of the length of all pieces is less than the length of the torrent file. Is this deliberate?

Hey @MlkMahmud, it shouldn’t be the case. The total size of all pieces should match the file size exactly.

Would you mind sharing an example or more context? We’d love to take a closer look.

---

<div class="post-metadata">

**Author:** ![MlkMahmud](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/mlkmahmud/32/7763_2.png) [@MlkMahmud](https://forum.codecrafters.io/u/MlkMahmud)\
**Post date:** [April 1, 2025, 10:58am UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/6 "2025-04-01T10:58:56Z")

</div>

Hi @andy1li

Maybe there’s a problem with the way I’m parsing the “pieces” string, but from my observations, it’s only 20 bytes long (one piece), and this is what the rest of the info dict looks like:

`{ "length": 79752, "name": "magnet2.gif", "piece length": 262144, "pieces": "׊U\ufffd؟\ufffdG{ĝ\ufffd\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd" } `

You should be able to recreate this by running the “magnet\_info” command on this magnet link “magnet2.gif.torrent: magnet:?xt=urn:btih:3f994a835e090238873498636b98a3e78d1c34ca&dn=magnet2.gif&tr=http%3A%2F%[2Fbittorrent-test-tracker.codecrafters.io](http://2Fbittorrent-test-tracker.codecrafters.io)%2Fannounce”

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [April 1, 2025, 3:08pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/7 "2025-04-01T15:08:37Z")

</div>

@MlkMahmud Looks like you’ve resolved the issue. Do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

---

<div class="post-metadata">

**Author:** ![MlkMahmud](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/mlkmahmud/32/7763_2.png) [@MlkMahmud](https://forum.codecrafters.io/u/MlkMahmud)\
**Post date:** [April 1, 2025, 6:02pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/8 "2025-04-01T18:02:57Z")

</div>

I haven’t solved it I found a hacky way around it.

The problem is that the metadata for the torrent I linked to in my previous reply has a single piece, but the “piece length” value of the info dictionary is ‘26211’, which is less than the length of the file ‘79752’.

Ideally, if there is only one piece for the torrent, the length of the piece should be equal to the length of the file.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [April 1, 2025, 7:01pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/10 "2025-04-01T19:01:16Z")

</div>

Hmm, it looks like the nominal piece length for our [torrent files](https://github.com/codecrafters-io/bittorrent-tester/blob/2ab3b588d39a5883b223f39135f8b2d519e93832/internal/stage_magnet_helpers.go#L40-L81) is 256 KB. In this edge case, the total file size is actually smaller than the nominal piece length.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/6/601bdb3cec3286122ecfef452a1bf4b5c0bc5762.png)

In such cases, the torrent contains a **single piece** , and the **actual piece length** will be equal to the **total file size** , not the nominal piece length.

Let me know if you’d like help handling this edge case more cleanly instead of relying on a workaround!

---

<div class="post-metadata">

**Author:** ![MlkMahmud](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/mlkmahmud/32/7763_2.png) [@MlkMahmud](https://forum.codecrafters.io/u/MlkMahmud)\
**Post date:** [April 1, 2025, 7:26pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/12 "2025-04-01T19:26:54Z")

</div>

Aw man, I could’ve sworn that the file lengh was greater than the nominal piece length. I need to increase my font size again. 😭

Thank you for clearing this up. Am I correct in concluding that the test expects the nominal piece length, and not the actual value?

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [April 1, 2025, 7:34pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/13 "2025-04-01T19:34:27Z")

</div>

Haha, totally understandable. We’ve all been there! 😄

And yes, the test in stage #zh1 expects the metadata to be printed as-is, hence the **nominal** piece length.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [April 6, 2025, 7:34pm UTC](https://forum.codecrafters.io/t/go-receive-metadata-zh1-total-sum-of-the-length-of-all-pieces-is-less-than-the-length-of-the-torrent/8004/14 "2025-04-06T19:34:49Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
