# Buggy code
def on_token_balance(self, circuit_id, balance):
    """
    We received the token balance of a circuit initiator. Check whether we can allocate a slot to this user.
    """
    if not self.request_cache.has("balance-request", circuit_id):
        self.logger.warning("Received token balance without associated request cache!")
        return

    cache = self.request_cache.pop("balance-request", circuit_id)

    lowest_balance = sys.maxsize
    lowest_index = -1
    for ind, tup in enumerate(self.competing_slots):
        if not tup[1]:
            # The slot is empty, take it
            self.competing_slots[ind] = (balance, circuit_id)
            cache.balance_future.set_result(True)
            return

        if tup[0] < lowest_balance:
            lowest_balance = tup[0]
            lowest_index = ind

    if balance > lowest_balance:
        # We kick this user out
        old_circuit_id = self.competing_slots[lowest_index][1]
        self.logger.info(
            "Kicked out circuit %s (balance: %s) in favor of %s (balance: %s)",
            old_circuit_id,
            lowest_balance,
            circuit_id,
            balance,
        )
        self.competing_slots[lowest_index] = (balance, circuit_id)

        self.remove_relay(old_circuit_id, destroy=DESTROY_REASON_BALANCE)
        self.remove_exit_socket(old_circuit_id, destroy=DESTROY_REASON_BALANCE)

        cache.balance_future.set_result(True)
    else:
        # We can't compete with the balances in the existing slots
        if self.reject_callback:
            self.reject_callback(time.time(), balance)
        cache.balance_future.set_result(False)


# 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_token_balance(self, circuit_id, balance):
    """
    We received the token balance of a circuit initiator. Check whether we can allocate a slot to this user.
    """
    if not self.request_cache.has("balance-request", circuit_id):
        self.logger.warning("Received token balance without associated request cache!")
        return

    cache = self.request_cache.pop("balance-request", circuit_id)

    lowest_balance = sys.maxsize
    lowest_index = -1
    for ind, tup in enumerate(self.competing_slots):
        if not tup[1]:
            # The slot is empty, take it
            self.competing_slots[ind] = (balance, circuit_id)
            cache.balance_future.set_result(True)
            return

        if tup[0] < lowest_balance:
            lowest_balance = tup[0]
            lowest_index = ind

    if balance > lowest_balance:
        # We kick this user out
        old_circuit_id = self.competing_slots[lowest_index][1]
        self.logger.info(
            "Kicked out circuit %s (balance: %s) in favor of %s (balance: %s)",
            old_circuit_id,
            lowest_balance,
            circuit_id,
            balance,
        )
        self.competing_slots[lowest_index] = (balance, circuit_id)

        self.remove_relay(old_circuit_id, destroy=DESTROY_REASON_BALANCE)
        self.remove_exit_socket(old_circuit_id, destroy=DESTROY_REASON_BALANCE)

        cache.balance_future.set_result(True)
    else:
        # We can't compete with the balances in the existing slots
        if self.reject_callback:
            self.reject_callback(time.time(), balance)
        cache.balance_future.set_result(False)