# Buggy code
def on_balance_request(self, payload):
    """
    We received a balance request from a relay or exit node. Respond with the latest block in our chain.
    """
    if not self.bandwidth_wallet:
        self.logger.warning(
            "Bandwidth wallet is not available, not sending balance response!"
        )
        return

    # Get the latest block
    latest_block = self.bandwidth_wallet.trustchain.persistence.get_latest(
        self.my_peer.public_key.key_to_bin(), block_type=b"tribler_bandwidth"
    )
    if not latest_block:
        latest_block = TriblerBandwidthBlock()
    latest_block.public_key = EMPTY_PK  # We hide the public key

    # We either send the response directly or relay the response to the last verified hop
    circuit = self.circuits[payload.circuit_id]
    if not circuit.hops:
        self.increase_bytes_sent(
            circuit,
            self.send_cell(
                circuit.peer,
                "balance-response",
                BalanceResponsePayload.from_half_block(
                    latest_block, circuit.circuit_id
                ),
            ),
        )
    else:
        self.increase_bytes_sent(
            circuit,
            self.send_cell(
                circuit.peer,
                "relay-balance-response",
                BalanceResponsePayload.from_half_block(
                    latest_block, circuit.circuit_id
                ),
            ),
        )


# Traceback (RuntimeError)
Traceback (most recent call last):
File "src\tribler-gui\tribler_gui\event_request_manager.py", line 112, in on_read_data
RuntimeError: bytes must be in range(0, 256)
--LONG TEXT--
Traceback (most recent call last):
File "lib\asyncio\events.py", line 88, in _run
File "lib\asyncio\selector_events.py", line 965, in _read_ready
File "src\tribler-core\tribler_core\modules\tunnel\socks5\udp_connection.py", line 51, in datagram_received
File "src\tribler-core\tribler_core\modules\tunnel\community\dispatcher.py", line 91, in on_socks5_udp_data
File "ipv8\messaging\anonymization\community.py", line 485, in send_data
File "ipv8\messaging\anonymization\community.py", line 471, in send_cell
File "ipv8\messaging\anonymization\payload.py", line 77, in to_bin
File "ipv8\messaging\anonymization\payload.py", line 32, in encode_address
File "ipv8\util.py", line 24, in cast_to_bin
ValueError: bytes must be in range(0, 256)

--CONTEXT--
{'message': 'Exception in callback _SelectorDatagramTransport._read_ready()', 'exception': ValueError('bytes must be in range(0, 256)'), 'handle': <Handle _SelectorDatagramTransport._read_ready()>}

# Fixed code
def on_balance_request(self, payload):
    """
    We received a balance request from a relay or exit node. Respond with the latest block in our chain.
    """
    if not self.bandwidth_wallet:
        self.logger.warning(
            "Bandwidth wallet is not available, not sending balance response!"
        )
        return

    # Get the latest block
    latest_block = self.bandwidth_wallet.trustchain.persistence.get_latest(
        self.my_peer.public_key.key_to_bin(), block_type=b"tribler_bandwidth"
    )
    if not latest_block:
        latest_block = TriblerBandwidthBlock()
    latest_block.public_key = EMPTY_PK  # We hide the public key

    # We either send the response directly or relay the response to the last verified hop
    circuit = self.circuits[payload.circuit_id]
    if not circuit.hops:
        self.increase_bytes_sent(
            circuit,
            self.send_cell(
                circuit.peer,
                "balance-response",
                BalanceResponsePayload.from_half_block(
                    latest_block, circuit.circuit_id
                ),
            ),
        )
    else:
        self.increase_bytes_sent(
            circuit,
            self.send_cell(
                circuit.peer,
                "relay-balance-response",
                BalanceResponsePayload.from_half_block(
                    latest_block, circuit.circuit_id
                ),
            ),
        )