# TypeScript BitTorrent #ND2: Receiving invalid message types

**URL:** <https://forum.codecrafters.io/t/typescript-bittorrent-nd2-receiving-invalid-message-types/4098>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [January 10, 2025, 12:10pm UTC](https://forum.codecrafters.io/t/typescript-bittorrent-nd2-receiving-invalid-message-types/4098 "2025-01-10T12:10:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorgecamilowu](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/jorgecamilowu/32/4155_2.png) [@jorgecamilowu](https://forum.codecrafters.io/u/jorgecamilowu)\
**Post date:** [January 10, 2025, 12:10pm UTC](https://forum.codecrafters.io/t/typescript-bittorrent-nd2-receiving-invalid-message-types/4098/1 "2025-01-10T12:10:40Z")

</div>

I’m stuck on Stage #ND2.

I’m receiving a message type which is not defined in the spec:  
Buffer(17) [0, 0, 0, 13, 16, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 64, 0]

As you can see, the message id here would be ‘16’, am I supposed to just discard these messages?

Here are my logs:

```auto
[tester::#ND2] Running tests for Stage #ND2 (Download a piece)
[tester::#ND2] Running ./your_program.sh download_piece -o /tmp/torrents2253464248/piece-0 /tmp/torrents2253464248/codercat.gif.torrent 0
[your_program] Peer ID: 2d524e302e302e302d89d20d086fea9fcedc2c9d
[your_program] Buffer(7) [0, 0, 0, 3, 5, 255, 240]
[your_program] Buffer(5) [0, 0, 0, 1, 1]
[your_program] Buffer(16397) [0, 0, 64, 9, 7, 0, 0, 0, 0, 0, 0, 0, 0, 71, 73, 70, 56, 57, 97, 104, 1, 122, 1, 247, 255, 0, 10, 10, 6, 11, 16, 21, 12, 11, 8, 12, 12, 9, 12, 19, 6, 13, 20, 30, 15, 19, 11, 16, 21, 8, 17, 13, 9, 18, 24, 22, 18, 26, 39, 19, 20, 17, 20, 18, 11, 21, 23, 11, 21, 30, 11, 23, 24, 17, 26, 19, 11, 26, 20, 12, 26, 23, 12, 27, 26, 15, 27, 27, 19, 28, 33, 21, 28, 37, 49, 29, 36, 38, 29, 37, 19, 30, 29, 22, 33, 25, 13, 33, 34, 27, 34, 28, 14, 34, 36, 28, 36, 24, 13, 36, 33, 18, 36, 44, 61, 38, 37, 32, 38, 39, 32, 38, 43, 28, 39, 47, 53, 40, 37, 20, 41, 41, 35, 41, 51, 31, 42, 42, 37, 42, 51, 69, 43, 31, 17, 43, 39, 23, 43, 42, 36, 43, 44, 43, 44, 45, 40, 49, 48, 43, 50, 50, 51, 50, 59, 78, 51, 38, 20, 51, 43, 29, 51, 47, 31, 54, 62, 43, 55, 56, 58, 57, 54, 40, 57, 63, 66, 58, 49, 30, 58, 57, 51, 59, 66, 84, 62, 45, 24, 64, 51, 32, 64, 75, 100, 65, 64, 59, 65, 65, 51, 67, 66, 66, 68, 74, 75, 71, 58, 38, 72, 52, 29, 72, 62, 42, 72, 72, 58, 73, 57, 35, 73, 74, 67, 75, 82, 101, 80, 82, 74, 81, 66, 42, 81, 76, 63, 81, 84, 83, 82, 56, 30, 83, 72, 51, 85, 51, 23, 85, 65, 41, 90, 73, 48, 90, 80, 63, 90, 86, 76, 91, 75, 51, 94, 93, 85, 94, 102, 97, 95, 56, 26, 96, 76, 49, 97, 81, 56, 98, 65, 33, 98, 84, 64, 101, 58, 26, 102, 92, 74, 104, 96, 80, 105, 84, 57, 105, 104, 95, 107, 64, 30, 107, 75, 41, 107, 114, 107, 113, 68, 33, 114, 104, 84, 115, 101, 75, 116, 68, 31, 117, 119, 109, 118, 75, 38, 118, 88, 54, 119, 128, 122, 120, 83, 44, 121, 112, 94, 123, 75, 34, 123, 82, 41, 127, 90, 51, 128, 79, 36, 128, 106, 75, 129, 87, 43, 130, 115, 84, 131, 84, 42, 131, 100, 62, 132, 80, 36, 132, 119, 95, 133, 91, 47, 133, 128, 113, 133, 132, 123, 135, 99, 56, 136, 93, 50, 137, 85, 39, 137, 126, 102, 139, 91, 44, 140, 98, 51, 142, 88, 41, 142, 104, 57, 142, 124, 94, 143, 119, 82, 144, 94, 45, 144, 110, 67, 145, 100, 52, 145, 110, 64, 146, 116, 75, 147, 99, 49, 147, 105, 53, 147, 108, 59, 147, 132, 103, 151, 158, 153, 151, 169, 168, 152, 93, 43, 152, 113, 61, 153, 151, 138, 154, 108, 58, 154, 124, 78, 155, 100, 48, 155, 106, 52, 155, 116, 66, 156, 114, 59, 156, 119, 67, 157, 132, 93, 157, 171, 168, 158, 126, 77, 159, 108, 55, 159, 141, 107, 160, 146, 117, 162, ... 15884 more]
[your_program] Buffer(16397) [0, 0, 64, 9, 7, 0, 0, 0, 0, 0, 0, 64, 0, 238, 67, 251, 39, 69, 67, 140, 70, 147, 130, 38, 76, 194, 254, 166, 30, 93, 95, 244, 116, 27, 51, 156, 242, 245, 106, 254, 111, 57, 82, 40, 130, 152, 74, 9, 2, 95, 1, 127, 140, 159, 94, 165, 10, 26, 197, 120, 215, 140, 121, 7, 255, 231, 122, 39, 185, 146, 183, 183, 123, 71, 192, 58, 128, 67, 189, 202, 247, 55, 131, 3, 125, 115, 246, 198, 230, 55, 112, 185, 67, 58, 83, 147, 81, 159, 0, 14, 8, 248, 168, 50, 198, 95, 74, 111, 190, 44, 220, 187, 124, 167, 13, 224, 192, 85, 11, 1, 123, 138, 242, 40, 243, 1, 36, 176, 2, 25, 252, 51, 17, 59, 196, 198, 94, 222, 34, 224, 147, 37, 88, 194, 64, 158, 47, 77, 230, 228, 72, 28, 96, 252, 230, 228, 68, 159, 117, 176, 50, 183, 230, 106, 174, 92, 115, 177, 93, 3, 48, 141, 91, 119, 168, 24, 165, 25, 167, 163, 96, 179, 87, 41, 133, 25, 143, 163, 110, 240, 113, 153, 210, 182, 206, 42, 29, 65, 111, 178, 37, 100, 219, 244, 146, 51, 121, 147, 135, 243, 6, 131, 67, 45, 28, 129, 43, 117, 118, 21, 96, 121, 212, 152, 128, 150, 111, 185, 49, 92, 1, 49, 178, 243, 118, 50, 192, 194, 129, 128, 129, 15, 30, 155, 148, 201, 21, 158, 170, 57, 137, 0, 25, 64, 2, 36, 196, 194, 227, 181, 231, 148, 90, 1, 13, 172, 64, 63, 235, 201, 16, 88, 248, 134, 115, 56, 159, 131, 56, 160, 203, 111, 203, 186, 181, 145, 69, 163, 182, 231, 114, 128, 54, 122, 162, 191, 56, 70, 139, 49, 141, 219, 248, 117, 155, 227, 208, 77, 168, 10, 65, 208, 209, 249, 169, 100, 41, 69, 73, 111, 255, 250, 51, 203, 76, 145, 215, 140, 136, 122, 86, 38, 150, 186, 5, 155, 250, 130, 61, 57, 140, 130, 3, 148, 163, 250, 80, 181, 58, 57, 84, 129, 243, 68, 145, 59, 96, 195, 34, 115, 249, 21, 100, 231, 11, 228, 122, 5, 144, 192, 14, 144, 167, 56, 61, 7, 25, 104, 21, 87, 169, 45, 66, 236, 65, 26, 100, 156, 21, 196, 106, 99, 158, 213, 95, 20, 198, 86, 215, 83, 253, 112, 248, 34, 236, 57, 39, 76, 68, 182, 23, 32, 126, 78, 230, 181, 83, 244, 233, 149, 236, 116, 135, 123, 162, 99, 180, 163, 243, 47, 205, 90, 43, 186, 87, 130, 186, 175, 123, 159, 78, 69, 98, 43, 155, 135, 136, 107, 61, 164, 160, 97, 3, 182, 36, 2, 79, 37, 30, 185, 175, 164, 183, 122, 235, 59, 191, 227, 146, 168, 190, 43, 148, 7, 60, 192, 227, 238, 69, 128, 131, 52, 72, 1, 193, 95, 75, 150, 43, 252, 194, 215, 186, 2, 152, 192, 61, 60, 60, 106, 167, 11, 116, 144, 19, 114, 36, 1, 27, 76, 7, 120, 126, ... 15884 more]
[your_program] Buffer(17) [0, 0, 0, 13, 16, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 64, 0]
[your_program] 24 | 
[your_program] 25 | const tag = bytes[4];
[your_program] 26 | const payload = bytes.subarray(5);
[your_program] 27 | 
[your_program] 28 | if (!Object.values(Tag).includes(tag)) {
[your_program] 29 | throw new Error(`Unsupported message tag ${tag}`);
[your_program] ^
[your_program] error: Unsupported message tag 16
[your_program] at decode (/app/app/peer/Message.ts:29:13)
[your_program] at <anonymous> (/app/app/main.ts:137:29)
[your_program] at flush (/app/app/torrent/download/MessageBuffer.ts:15:10)
[your_program] at receive (/app/app/torrent/download/MessageBuffer.ts:47:12)
[your_program] at data (/app/app/main.ts:157:27)
[your_program] 
[tester::#ND2] timed out, test exceeded 10 seconds
[tester::#ND2] Test failed

```

And here’s a snippet of my code:

```TypeScript
export enum Tag {
  CHOKE,
  UNCHOKE,
  INTERESTED,
  NOT_INTERESTED,
  HAVE,
  BITFIELD,
  REQUEST,
  PIECE,
  CANCEL,
}

export class Message {
  readonly length: number;

  constructor(readonly tag: Tag, readonly payload: Buffer) {
    this.length = 1 + this.payload.length;
  }

  static decode(bytes: Buffer): Message {
    if (bytes.length < 5) {
      throw new Error("Invalid peer message length");
    }

    const tag = bytes[4];
    const payload = bytes.subarray(5);

    if (!Object.values(Tag).includes(tag)) {
      throw new Error(`Unsupported message tag ${tag}`);
    }

    return new Message(tag, payload);
  }

  encode(): Buffer {
    // the length prefix is a four byte big endian encoded number
    const encodedLength = Buffer.alloc(4);
    encodedLength.writeUInt32BE(this.length);

    return Buffer.from([...encodedLength, this.tag, ...this.payload]);
  }
}

```

---

<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:** [January 10, 2025, 12:59pm UTC](https://forum.codecrafters.io/t/typescript-bittorrent-nd2-receiving-invalid-message-types/4098/2 "2025-01-10T12:59:18Z")

</div>

Hi @jorgecamilowu, I’m not sure about the “16” message either.

However, it seems like `nextEmptyBlock` isn’t working, as the first two requests are exactly the same:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/2/28f05394f749a1ca7ae3f7331364b6c9461f53ab.jpeg)

---

<div class="post-metadata">

**Author:** ![jorgecamilowu](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/jorgecamilowu/32/4155_2.png) [@jorgecamilowu](https://forum.codecrafters.io/u/jorgecamilowu)\
**Post date:** [January 10, 2025, 1:32pm UTC](https://forum.codecrafters.io/t/typescript-bittorrent-nd2-receiving-invalid-message-types/4098/3 "2025-01-10T13:32:31Z")

</div>

Hey Andy, thanks for looking into this. Interesting! I’ll double check there, still kidna weird that there is a properly formatted message with an id of 16 tho. I’ll double check the nextEmptyBlock implementation 🙏

---

<div class="post-metadata">

**Author:** ![jorgecamilowu](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/jorgecamilowu/32/4155_2.png) [@jorgecamilowu](https://forum.codecrafters.io/u/jorgecamilowu)\
**Post date:** [January 10, 2025, 3:59pm UTC](https://forum.codecrafters.io/t/typescript-bittorrent-nd2-receiving-invalid-message-types/4098/4 "2025-01-10T15:59:48Z")

</div>

The method was indeed the issue, after fixing that all data flowed in without problem

---

<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:** [January 15, 2025, 4:00pm UTC](https://forum.codecrafters.io/t/typescript-bittorrent-nd2-receiving-invalid-message-types/4098/5 "2025-01-15T16:00:33Z")

</div>

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