Esempio n. 1
0
def test_event_operators():
    a = EventPaymentSentSuccess(1, 4, 2, 5, sha3(b"target"), b"0", [])
    b = EventPaymentSentSuccess(1, 4, 2, 5, sha3(b"target"), b"0", [])
    c = EventPaymentSentSuccess(2, 7, 3, 4, sha3(b"target"), b"0", [])
    d = EventPaymentSentSuccess(2, 7, 3, 4, sha3(b"differenttarget"), b"0", [])

    # pylint: disable=unneeded-not
    assert a == b
    assert not a != b
    assert a != c
    assert not a == c
    assert not c == d

    a = EventPaymentSentFailed(1, 7, 2, "target", "BECAUSE")
    b = EventPaymentSentFailed(1, 7, 2, "target", "BECAUSE")
    c = EventPaymentSentFailed(3, 3, 3, "target", "UNKNOWN")

    assert a == b
    assert not a != b
    assert a != c
    assert not a == c

    a = EventPaymentReceivedSuccess(4, 4, 2, 5, sha3(b"initiator"))
    b = EventPaymentReceivedSuccess(4, 4, 2, 5, sha3(b"initiator"))
    c = EventPaymentReceivedSuccess(1, 2, 3, 5, sha3(b"initiator"))
    d = EventPaymentReceivedSuccess(1, 2, 3, 5, sha3(b"other initiator"))

    assert a == b
    assert not a != b
    assert a != c
    assert not a == c
    assert c != d
    assert not c == d
Esempio n. 2
0
def test_event_operators():
    a = EventPaymentSentSuccess(1, 4, 2, 5, sha3(b'target'))
    b = EventPaymentSentSuccess(1, 4, 2, 5, sha3(b'target'))
    c = EventPaymentSentSuccess(2, 7, 3, 4, sha3(b'target'))
    d = EventPaymentSentSuccess(2, 7, 3, 4, sha3(b'differenttarget'))

    # pylint: disable=unneeded-not
    assert a == b
    assert not a != b
    assert a != c
    assert not a == c
    assert not c == d

    a = EventPaymentSentFailed(1, 7, 2, 'target', 'BECAUSE')
    b = EventPaymentSentFailed(1, 7, 2, 'target', 'BECAUSE')
    c = EventPaymentSentFailed(3, 3, 3, 'target', 'UNKNOWN')

    assert a == b
    assert not a != b
    assert a != c
    assert not a == c

    a = EventPaymentReceivedSuccess(4, 4, 2, 5, sha3(b'initiator'))
    b = EventPaymentReceivedSuccess(4, 4, 2, 5, sha3(b'initiator'))
    c = EventPaymentReceivedSuccess(1, 2, 3, 5, sha3(b'initiator'))
    d = EventPaymentReceivedSuccess(1, 2, 3, 5, sha3(b'other initiator'))

    assert a == b
    assert not a != b
    assert a != c
    assert not a == c
    assert c != d
    assert not c == d
Esempio n. 3
0
def handle_processed(
        chain_state: ChainState,
        state_change: ReceiveProcessed,
) -> TransitionResult:
    events = list()

    for queue in chain_state.queueids_to_queues.values():
        for message in queue:
            message_found = (
                message.message_identifier == state_change.message_identifier and
                message.recipient == state_change.sender
            )
            if message_found:
                if type(message) == SendDirectTransfer:
                    channel_state = views.get_channelstate_by_token_network_and_partner(
                        chain_state,
                        message.balance_proof.token_network_identifier,
                        message.recipient,
                    )
                    events.append(EventPaymentSentSuccess(
                        channel_state.payment_network_identifier,
                        channel_state.token_network_identifier,
                        message.payment_identifier,
                        message.balance_proof.transferred_amount,
                        message.recipient,
                    ))

    # Clean up message queue
    for queueid in list(chain_state.queueids_to_queues.keys()):
        inplace_delete_message_queue(chain_state, state_change, queueid)

    return TransitionResult(chain_state, events)
Esempio n. 4
0
def events_for_unlock_lock(
    initiator_state: InitiatorTransferState,
    channel_state: NettingChannelState,
    secret: typing.Secret,
    secrethash: typing.SecretHash,
    pseudo_random_generator: random.Random,
):
    # next hop learned the secret, unlock the token locally and send the
    # lock claim message to next hop
    transfer_description = initiator_state.transfer_description

    message_identifier = message_identifier_from_prng(pseudo_random_generator)
    unlock_lock = channel.send_unlock(
        channel_state=channel_state,
        message_identifier=message_identifier,
        payment_identifier=transfer_description.payment_identifier,
        secret=secret,
        secrethash=secrethash,
    )

    payment_sent_success = EventPaymentSentSuccess(
        payment_network_identifier=channel_state.payment_network_identifier,
        token_network_identifier=channel_state.token_network_identifier,
        identifier=transfer_description.payment_identifier,
        amount=transfer_description.amount,
        target=transfer_description.target,
    )

    unlock_success = EventUnlockSuccess(
        transfer_description.payment_identifier,
        transfer_description.secrethash,
    )

    return [unlock_lock, payment_sent_success, unlock_success]
Esempio n. 5
0
def handle_processed(
    chain_state: ChainState,
    state_change: ReceiveProcessed,
) -> TransitionResult:
    # TODO: improve the complexity of this algorithm
    events = list()
    for queue in chain_state.queueids_to_queues.values():
        remove = []

        # TODO: ensure Processed message came from the correct peer
        for pos, message in enumerate(queue):
            if message.message_identifier == state_change.message_identifier:
                if type(message) == SendDirectTransfer:
                    channel_state = views.get_channelstate_by_token_network_and_partner(
                        chain_state,
                        message.balance_proof.token_network_identifier,
                        message.recipient,
                    )
                    events.append(
                        EventPaymentSentSuccess(
                            channel_state.payment_network_identifier,
                            channel_state.token_network_identifier,
                            message.payment_identifier,
                            message.balance_proof.transferred_amount,
                            message.recipient,
                        ))
                remove.append(pos)

        for removepos in reversed(remove):
            queue.pop(removepos)

    return TransitionResult(chain_state, events)
def test_pfs_handler_handle_paymentsentsuccess_without_feedback_token():
    (
        raiden,
        pfs_handler,
        token_network_registry_address,
        token_network_address,
        route,
        _,
    ) = setup_pfs_handler_test(set_feedback_token=False)

    payment_id = make_payment_id()
    amount = PaymentAmount(123)
    target = TargetAddress(route[-1])
    raiden.targets_to_identifiers_to_statuses[target][payment_id] = Mock()

    route_failed_event = EventPaymentSentSuccess(
        token_network_registry_address=token_network_registry_address,
        token_network_address=token_network_address,
        identifier=payment_id,
        amount=amount,
        target=TargetAddress(target),
        secret=make_secret(),
        route=route,
    )

    with patch("raiden.raiden_event_handler.post_pfs_feedback"
               ) as pfs_feedback_handler:
        pfs_handler.on_raiden_events(
            raiden=raiden,
            chain_state=cast(
                ChainState,
                raiden.wal.state_manager.current_state),  # type: ignore
            events=[route_failed_event],
        )
    assert not pfs_feedback_handler.called
Esempio n. 7
0
def handle_secretreveal(
    initiator_state: InitiatorTransferState,
    state_change: ReceiveSecretReveal,
    channel_state: NettingChannelState,
    pseudo_random_generator: random.Random,
) -> TransitionResult:
    """ Send a balance proof to the next hop with the current mediated transfer
    lock removed and the balance updated.
    """
    is_valid_secret_reveal = (
        state_change.sender == channel_state.partner_state.address
        and state_change.secrethash
        == initiator_state.transfer_description.secrethash)

    # If the channel is closed the balance proof must not be sent
    is_channel_open = channel.get_status(channel_state) == CHANNEL_STATE_OPENED

    if is_valid_secret_reveal and is_channel_open:
        # next hop learned the secret, unlock the token locally and send the
        # lock claim message to next hop
        transfer_description = initiator_state.transfer_description

        message_identifier = message_identifier_from_prng(
            pseudo_random_generator)
        unlock_lock = channel.send_unlock(
            channel_state,
            transfer_description.payment_identifier,
            message_identifier,
            state_change.secret,
            state_change.secrethash,
        )

        # TODO: Emit these events after on-chain unlock
        payment_sent_success = EventPaymentSentSuccess(
            payment_network_identifier=channel_state.
            payment_network_identifier,
            token_network_identifier=channel_state.token_network_identifier,
            identifier=transfer_description.payment_identifier,
            amount=transfer_description.amount,
            target=transfer_description.target,
        )

        unlock_success = EventUnlockSuccess(
            transfer_description.payment_identifier,
            transfer_description.secrethash,
        )

        iteration = TransitionResult(
            None, [payment_sent_success, unlock_success, unlock_lock])
    else:
        iteration = TransitionResult(initiator_state, list())

    return iteration
Esempio n. 8
0
def test_event_filter_for_payments():
    secret = factories.make_secret()
    identifier = PaymentID(1)
    target = TargetAddress(factories.make_address())
    event1 = EventPaymentSentSuccess(
        token_network_registry_address=UNIT_TOKEN_NETWORK_REGISTRY_ADDRESS,
        token_network_address=UNIT_TOKEN_NETWORK_ADDRESS,
        identifier=identifier,
        amount=PaymentAmount(5),
        target=target,
        secret=secret,
        route=[],
    )
    assert event_filter_for_payments(event=event1, partner_address=None)
    assert event_filter_for_payments(event=event1,
                                     partner_address=Address(target))
    assert not event_filter_for_payments(
        event=event1, partner_address=factories.make_address())

    initiator = InitiatorAddress(factories.make_address())
    event2 = EventPaymentReceivedSuccess(
        token_network_registry_address=UNIT_TOKEN_NETWORK_REGISTRY_ADDRESS,
        token_network_address=UNIT_TOKEN_NETWORK_ADDRESS,
        identifier=identifier,
        amount=TokenAmount(5),
        initiator=initiator,
    )
    assert event_filter_for_payments(event=event2, partner_address=None)
    assert event_filter_for_payments(event=event2,
                                     partner_address=Address(initiator))
    assert not event_filter_for_payments(
        event=event2, partner_address=factories.make_address())

    event3 = EventPaymentSentFailed(
        token_network_registry_address=UNIT_TOKEN_NETWORK_REGISTRY_ADDRESS,
        token_network_address=UNIT_TOKEN_NETWORK_ADDRESS,
        identifier=identifier,
        target=target,
        reason="whatever",
    )
    assert event_filter_for_payments(event=event3, partner_address=None)
    assert event_filter_for_payments(event=event3,
                                     partner_address=Address(target))
    assert not event_filter_for_payments(
        event=event3, partner_address=factories.make_address())
Esempio n. 9
0
def test_event_filter_for_payments():
    token_network_address = factories.make_address()
    secret = factories.make_secret()
    token_network_registry_address = factories.make_token_network_registry_address(
    )
    identifier = 1
    target = factories.make_address()
    event = EventPaymentSentSuccess(
        token_network_registry_address=token_network_registry_address,
        token_network_address=token_network_address,
        identifier=identifier,
        amount=5,
        target=target,
        secret=secret,
        route=[],
    )
    assert event_filter_for_payments(event=event, partner_address=None)
    assert event_filter_for_payments(event=event, partner_address=target)
    assert not event_filter_for_payments(
        event=event, partner_address=factories.make_address())

    event = EventPaymentReceivedSuccess(
        token_network_registry_address=token_network_registry_address,
        token_network_address=token_network_address,
        identifier=identifier,
        amount=5,
        initiator=target,
    )
    assert event_filter_for_payments(event=event, partner_address=None)
    assert event_filter_for_payments(event=event, partner_address=target)
    assert not event_filter_for_payments(
        event=event, partner_address=factories.make_address())

    event = EventPaymentSentFailed(
        token_network_registry_address=factories.
        make_token_network_registry_address(),
        token_network_address=token_network_address,
        identifier=identifier,
        target=target,
        reason="whatever",
    )
    assert event_filter_for_payments(event=event, partner_address=None)
    assert event_filter_for_payments(event=event, partner_address=target)
    assert not event_filter_for_payments(
        event=event, partner_address=factories.make_address())
Esempio n. 10
0
def test_pfs_handler_handle_paymentsentsuccess_with_feedback_token():
    (
        raiden,
        pfs_handler,
        token_network_registry_address,
        token_network_address,
        route,
        feedback_uuid,
    ) = setup_pfs_handler_test(set_feedback_token=True)

    payment_id = make_payment_id()
    amount = PaymentAmount(123)
    target = TargetAddress(route[-1])
    raiden.targets_to_identifiers_to_statuses[target][payment_id] = Mock()

    route_failed_event = EventPaymentSentSuccess(
        token_network_registry_address=token_network_registry_address,
        token_network_address=token_network_address,
        identifier=payment_id,
        amount=amount,
        target=TargetAddress(target),
        secret=make_secret(),
        route=route,
    )

    with patch("raiden.raiden_event_handler.post_pfs_feedback"
               ) as pfs_feedback_handler:
        pfs_handler.on_raiden_event(
            raiden=raiden,
            chain_state=cast(
                ChainState,
                raiden.wal.state_manager.current_state),  # type: ignore
            event=route_failed_event,
        )
    assert pfs_feedback_handler.called
    assert pfs_feedback_handler.call_args == call(
        pfs_config=raiden.config.pfs_config,
        route=route,
        routing_mode=RoutingMode.PRIVATE,
        successful=True,
        token=feedback_uuid,
        token_network_address=token_network_address,
    )
Esempio n. 11
0
def events_for_unlock_base(
    initiator_state: InitiatorTransferState,
    channel_state: NettingChannelState,
    secret: Secret
) -> List[Event]:
    transfer_description = initiator_state.transfer_description
    payment_sent_success = EventPaymentSentSuccess(
        payment_network_identifier=channel_state.payment_network_identifier,
        token_network_identifier=TokenNetworkID(channel_state.token_network_identifier),
        identifier=transfer_description.payment_identifier,
        amount=transfer_description.amount,
        target=transfer_description.target,
        secret=secret,
    )

    unlock_success = EventUnlockSuccess(
        transfer_description.payment_identifier, transfer_description.secrethash
    )

    return [payment_sent_success, unlock_success]
Esempio n. 12
0
def test_event_filter_for_payments():
    token_network_identifier = factories.make_address()
    payment_network_identifier = factories.make_payment_network_identifier()
    identifier = 1
    target = factories.make_address()
    event = EventPaymentSentSuccess(
        payment_network_identifier=payment_network_identifier,
        token_network_identifier=token_network_identifier,
        identifier=identifier,
        amount=5,
        target=target,
    )
    assert event_filter_for_payments(event, token_network_identifier, None)
    assert event_filter_for_payments(event, token_network_identifier, target)
    assert not event_filter_for_payments(event, token_network_identifier,
                                         factories.make_address())

    event = EventPaymentReceivedSuccess(
        payment_network_identifier=payment_network_identifier,
        token_network_identifier=token_network_identifier,
        identifier=identifier,
        amount=5,
        initiator=target,
    )
    assert event_filter_for_payments(event, token_network_identifier, None)
    assert event_filter_for_payments(event, token_network_identifier, target)
    assert not event_filter_for_payments(event, token_network_identifier,
                                         factories.make_address())

    event = EventPaymentSentFailed(
        payment_network_identifier=factories.make_payment_network_identifier(),
        token_network_identifier=token_network_identifier,
        identifier=identifier,
        target=target,
        reason="whatever",
    )
    assert event_filter_for_payments(event, token_network_identifier, None)
    assert event_filter_for_payments(event, token_network_identifier, target)
    assert not event_filter_for_payments(event, token_network_identifier,
                                         factories.make_address())
Esempio n. 13
0
def events_for_unlock_lock(
    initiator_state: InitiatorTransferState,
    channel_state: NettingChannelState,
    secret: Secret,
    secrethash: SecretHash,
    pseudo_random_generator: random.Random,
    block_number: BlockNumber,
) -> List[Event]:
    """ Unlocks the lock offchain, and emits the events for the successful payment. """
    # next hop learned the secret, unlock the token locally and send the
    # lock claim message to next hop
    transfer_description = initiator_state.transfer_description

    message_identifier = message_identifier_from_prng(pseudo_random_generator)
    unlock_lock = channel.send_unlock(
        channel_state=channel_state,
        message_identifier=message_identifier,
        payment_identifier=transfer_description.payment_identifier,
        secret=secret,
        secrethash=secrethash,
        block_number=block_number,
    )

    payment_sent_success = EventPaymentSentSuccess(
        token_network_registry_address=channel_state.
        token_network_registry_address,
        token_network_address=channel_state.token_network_address,
        identifier=transfer_description.payment_identifier,
        amount=transfer_description.amount,
        target=transfer_description.target,
        secret=secret,
        route=initiator_state.route.route,
    )

    unlock_success = EventUnlockSuccess(
        transfer_description.payment_identifier,
        transfer_description.secrethash)

    return [unlock_lock, payment_sent_success, unlock_success]