# Buggy code
def validate_userid_signature(user: User) -> Optional[Address]:
    """Validate a userId format and signature on displayName, and return its address"""
    # display_name should be an address in the USERID_RE format
    match = USERID_RE.match(user.user_id)
    if not match:
        return None

    msg = (
        "The User instance provided to validate_userid_signature must have the "
        "displayname attribute set. Make sure to warm the value using the "
        "DisplayNameCache."
    )
    displayname = user.displayname
    assert displayname is not None, msg

    encoded_address = match.group(1)
    address: Address = to_canonical_address(encoded_address)

    try:
        if DISPLAY_NAME_HEX_RE.match(displayname):
            signature_bytes = decode_hex(displayname)
        else:
            return None
        recovered = recover(
            data=user.user_id.encode(), signature=Signature(signature_bytes)
        )
        if not (address and recovered and recovered == address):
            return None
    except (
        DecodeError,
        TypeError,
        InvalidSignature,
        MatrixRequestError,
        json.decoder.JSONDecodeError,
    ):
        return None
    return address


# Traceback (AssertionError)
Traceback (most recent call last):
File "raiden/ui/cli.py", line 617, in run
File "raiden/ui/runners.py", line 21, in run_services
File "raiden/ui/app.py", line 446, in run_app
File "raiden/app.py", line 90, in start
File "raiden/raiden_service.py", line 444, in start
File "raiden/raiden_service.py", line 552, in _start_transport
File "raiden/network/transport/matrix/transport.py", line 467, in start
File "raiden/network/transport/matrix/transport.py", line 790, in _initialize_first_sync
File "raiden/network/transport/matrix/client.py", line 567, in blocking_sync
File "raiden/network/transport/matrix/client.py", line 774, in _handle_responses
File "raiden/network/transport/matrix/transport.py", line 1063, in _handle_member_join
File "raiden/network/transport/matrix/transport.py", line 824, in _leave_unexpected_rooms
File "raiden/network/transport/matrix/transport.py", line 932, in _extract_addresses
File "raiden/network/transport/matrix/transport.py", line 932, in <genexpr>
File "cachetools/decorators.py", line 36, in wrapper
File "raiden/network/transport/matrix/utils.py", line 728, in validate_userid_signature
AssertionError: The User instance provided to validate_userid_signature must have the displayname attribute set. Make sure to warm the value using the DisplayNameCache.
FATAL: An unexpected exception occurred. A traceback has been written to /tmp/raiden-exception-2020-09-21T14-498mov1ija.txt
The User instance provided to validate_userid_signature must have the displayname attribute set. Make sure to warm the value using the DisplayNameCache.

# Fixed code
def validate_userid_signature(user: User) -> Optional[Address]:
    """Validate a userId format and signature on displayName, and return its address"""
    # display_name should be an address in the USERID_RE format
    match = USERID_RE.match(user.user_id)
    if not match:
        log.warning("Invalid user id", user=user.user_id)
        return None

    displayname = user.displayname

    if displayname is None:
        log.warning("Displayname not set", user=user.user_id)
        return None

    encoded_address = match.group(1)
    address: Address = to_canonical_address(encoded_address)

    try:
        if DISPLAY_NAME_HEX_RE.match(displayname):
            signature_bytes = decode_hex(displayname)
        else:
            log.warning(
                "Displayname invalid format", user=user.user_id, displayname=displayname
            )
            return None
        recovered = recover(
            data=user.user_id.encode(), signature=Signature(signature_bytes)
        )
        if not (address and recovered and recovered == address):
            log.warning("Unexpected signer of displayname", user=user.user_id)
            return None
    except (
        DecodeError,
        TypeError,
        InvalidSignature,
        MatrixRequestError,
        json.decoder.JSONDecodeError,
    ):
        return None
    return address