#ND2 - Not receiving unchoke message

Hey folks. I’m stuck on #ND2 and would like some help.

I’m currently on the step for receiving the unchoke message, right after sending the interested message. I’m not getting any data back from the peer though, and tests time out.

FYI I’m doing a sequential implementation for starters.

Here’s the relevant piece of code:

            peers = get_peers(torrent_filename)
            peer_id, reader, writer = await perform_handshake(
                torrent_filename, peer=peers[0]
            )

            # Receive bitfield message
            bitfield_size = int.from_bytes(await reader.readexactly(4))
            bitfield_type = int.from_bytes(await reader.readexactly(1))
            if bitfield_type != 0x05:
                raise Exception("Expected bitfield type")
            _bitfield_payload = await reader.read(bitfield_size)

            # Send interested message
            # Payload size is 0, interested type is 2
            message = bytes().join(int.to_bytes(0) for _ in range(4)) + int.to_bytes(2)
            writer.write(message)
            await writer.drain()

            # Receive unchoke message
            unchoke_size = int.from_bytes(await reader.readexactly(4))
            unchoke_type = int.from_bytes(await reader.readexactly(1))
            if unchoke_type != 0x01 or unchoke_size != 0x00:
                raise Exception("Expected unchoke type with 0 size")
            print("foo!")

And here is the repo with all of the code:

I figured it out by looking at some code examples:

The “message length prefix” for the interested message should be 1, not 0, as it should include the type byte itself, not just the payload.

With a message length prefix of 1 I was able to continue and receive the unchoke message :tada:

1 Like

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