# Buggy code
def initiate_payment(
    self,
    registry_address: TokenNetworkRegistryAddress,
    token_address: TokenAddress,
    target_address: TargetAddress,
    amount: PaymentAmount,
    identifier: PaymentID,
    secret: Secret,
    secret_hash: SecretHash,
    lock_timeout: BlockTimeout,
) -> Response:
    log.debug(
        "Initiating payment",
        node=to_checksum_address(self.raiden_api.address),
        registry_address=to_checksum_address(registry_address),
        token_address=to_checksum_address(token_address),
        target_address=to_checksum_address(target_address),
        amount=amount,
        payment_identifier=identifier,
        secret=secret,
        secret_hash=secret_hash,
        lock_timeout=lock_timeout,
    )

    if identifier is None:
        identifier = create_default_identifier()

    try:
        payment_status = self.raiden_api.transfer(
            registry_address=registry_address,
            token_address=token_address,
            target=target_address,
            amount=amount,
            identifier=identifier,
            secret=secret,
            secrethash=secret_hash,
            lock_timeout=lock_timeout,
        )
    except (
        InvalidAmount,
        InvalidBinaryAddress,
        InvalidSecret,
        InvalidSecretHash,
        InvalidPaymentIdentifier,
        PaymentConflict,
        UnknownTokenAddress,
    ) as e:
        return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)
    except InsufficientFunds as e:
        return api_error(errors=str(e), status_code=HTTPStatus.PAYMENT_REQUIRED)

    result = payment_status.payment_done.get()

    if isinstance(result, EventPaymentSentFailed):
        return api_error(
            errors=f"Payment couldn't be completed because: {result.reason}",
            status_code=HTTPStatus.CONFLICT,
        )

    assert isinstance(result, EventPaymentSentSuccess)
    payment = {
        "initiator_address": self.raiden_api.address,
        "registry_address": registry_address,
        "token_address": token_address,
        "target_address": target_address,
        "amount": amount,
        "identifier": identifier,
        "secret": result.secret,
        "secret_hash": sha256(result.secret).digest(),
    }
    result = self.payment_schema.dump(payment)
    return api_response(result=result)


# Traceback (ValueError)
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._greenlet.Greenlet.run
File "site-packages/raiden/network/transport/matrix/transport.py", line 629, in _health_check_worker
File "site-packages/raiden/network/transport/matrix/transport.py", line 606, in immediate_health_check_for
File "site-packages/raiden/network/transport/matrix/transport.py", line 577, in whitelist
File "site-packages/raiden/network/transport/matrix/transport.py", line 1194, in _maybe_create_room_for_address
File "site-packages/raiden/network/transport/matrix/utils.py", line 876, in my_place_or_yours
ValueError: Addresses to compare must differ
2020-02-19T14:55:13Z <Greenlet "Greenlet<fn:_health_check_worker>" at 0x7f1f5e02a050: <bound method MatrixTransport._health_check_worker of <MatrixTransport node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9 id:585de50e-f064-4a93-b3f4-df2bf40f8078>>> failed with ValueError

2020-02-19 14:55:13.088967 [error    ] Runnable subtask died!         [raiden.utils.runnable] exc=ValueError('Addresses to compare must differ') running=True subtask=<Greenlet "Greenlet<fn:_health_check_worker>" at 0x7f1f5e02a050: <bound method MatrixTransport._health_check_worker of <MatrixTransport node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9 id:585de50e-f064-4a93-b3f4-df2bf40f8078>>> this=<MatrixTransport node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9 id:585de50e-f064-4a93-b3f4-df2bf40f8078>
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._greenlet.Greenlet.run
File "site-packages/raiden/network/transport/matrix/transport.py", line 496, in _run
File "site-packages/raiden/network/transport/matrix/transport.py", line 718, in _broadcast_worker
File "src/gevent/event.py", line 133, in gevent._event.Event.wait
File "src/gevent/_abstract_linkable.py", line 208, in gevent.__abstract_linkable.AbstractLinkable._wait
File "src/gevent/_abstract_linkable.py", line 178, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 196, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 182, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 65, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
ValueError: Addresses to compare must differ
2020-02-19T14:55:13Z <Greenlet "MatrixTransport._run node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9" at 0x7f1f5f9a3a70: _run> failed with ValueError

2020-02-19 14:55:13.259801 [error    ] Runnable subtask died!         [raiden.utils.runnable] exc=ValueError('Addresses to compare must differ') running=True subtask=<Greenlet "MatrixTransport._run node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9" at 0x7f1f5f9a3a70: _run> this=<RaidenService node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9>
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._greenlet.Greenlet.run
File "site-packages/raiden/raiden_service.py", line 375, in _run
File "src/gevent/event.py", line 133, in gevent._event.Event.wait
File "src/gevent/_abstract_linkable.py", line 208, in gevent.__abstract_linkable.AbstractLinkable._wait
File "src/gevent/_abstract_linkable.py", line 178, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 196, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 182, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 65, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
ValueError: Addresses to compare must differ
2020-02-19T14:55:13Z <Greenlet "RaidenService._run node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9" at 0x7f1f5f625b90: _run> failed with ValueError

FATAL: An unexpected exception occured. A traceback has been written to /tmp/raiden-exception-2020-02-19T14-5542cjz658.txt
Addresses to compare must differ
ReturnCode.FATAL

# Fixed code
def initiate_payment(
    self,
    registry_address: TokenNetworkRegistryAddress,
    token_address: TokenAddress,
    target_address: TargetAddress,
    amount: PaymentAmount,
    identifier: PaymentID,
    secret: Secret,
    secret_hash: SecretHash,
    lock_timeout: BlockTimeout,
) -> Response:
    log.debug(
        "Initiating payment",
        node=to_checksum_address(self.raiden_api.address),
        registry_address=to_checksum_address(registry_address),
        token_address=to_checksum_address(token_address),
        target_address=to_checksum_address(target_address),
        amount=amount,
        payment_identifier=identifier,
        secret=secret,
        secret_hash=secret_hash,
        lock_timeout=lock_timeout,
    )

    if identifier is None:
        identifier = create_default_identifier()

    try:
        payment_status = self.raiden_api.transfer(
            registry_address=registry_address,
            token_address=token_address,
            target=target_address,
            amount=amount,
            identifier=identifier,
            secret=secret,
            secrethash=secret_hash,
            lock_timeout=lock_timeout,
        )
    except (
        InvalidAmount,
        InvalidBinaryAddress,
        InvalidSecret,
        InvalidSecretHash,
        InvalidPaymentIdentifier,
        PaymentConflict,
        SamePeerAddress,
        UnknownTokenAddress,
    ) as e:
        return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)
    except InsufficientFunds as e:
        return api_error(errors=str(e), status_code=HTTPStatus.PAYMENT_REQUIRED)

    result = payment_status.payment_done.get()

    if isinstance(result, EventPaymentSentFailed):
        return api_error(
            errors=f"Payment couldn't be completed because: {result.reason}",
            status_code=HTTPStatus.CONFLICT,
        )

    assert isinstance(result, EventPaymentSentSuccess)
    payment = {
        "initiator_address": self.raiden_api.address,
        "registry_address": registry_address,
        "token_address": token_address,
        "target_address": target_address,
        "amount": amount,
        "identifier": identifier,
        "secret": result.secret,
        "secret_hash": sha256(result.secret).digest(),
    }
    result = self.payment_schema.dump(payment)
    return api_response(result=result)