Beispiel #1
0
def handle_unlock(mediator_state, state_change: ReceiveUnlock,
                  channelidentifiers_to_channels):
    """ Handle a ReceiveUnlock state change. """
    events = list()
    balance_proof_sender = state_change.balance_proof.sender
    channel_identifier = state_change.balance_proof.channel_identifier

    for pair in mediator_state.transfers_pair:
        if pair.payer_transfer.balance_proof.sender == balance_proof_sender:
            channel_state = channelidentifiers_to_channels.get(
                channel_identifier)

            if channel_state:
                is_valid, channel_events, _ = channel.handle_unlock(
                    channel_state,
                    state_change,
                )
                events.extend(channel_events)

                if is_valid:
                    unlock = EventUnlockClaimSuccess(
                        pair.payee_transfer.payment_identifier,
                        pair.payee_transfer.lock.secrethash,
                    )
                    events.append(unlock)

                    send_processed = SendProcessed(
                        recipient=balance_proof_sender,
                        channel_identifier=CHANNEL_IDENTIFIER_GLOBAL_QUEUE,
                        message_identifier=state_change.message_identifier,
                    )
                    events.append(send_processed)

                    pair.payer_state = 'payer_balance_proof'

    iteration = TransitionResult(mediator_state, events)
    return iteration
Beispiel #2
0
def handle_unlock(
    target_state: TargetTransferState,
    state_change: ReceiveUnlock,
    channel_state: NettingChannelState,
) -> TransitionResult[TargetTransferState]:
    """ Handles a ReceiveUnlock state change. """
    balance_proof_sender = state_change.balance_proof.sender

    is_valid, events, _ = channel.handle_unlock(channel_state, state_change)
    next_target_state: Optional[TargetTransferState] = target_state

    if is_valid:
        transfer = target_state.transfer
        payment_received_success = EventPaymentReceivedSuccess(
            payment_network_identifier=channel_state.
            payment_network_identifier,
            token_network_identifier=TokenNetworkID(
                channel_state.token_network_identifier),
            identifier=transfer.payment_identifier,
            amount=TokenAmount(transfer.lock.amount),
            initiator=transfer.initiator,
        )

        unlock_success = EventUnlockClaimSuccess(transfer.payment_identifier,
                                                 transfer.lock.secrethash)

        send_processed = SendProcessed(
            recipient=balance_proof_sender,
            channel_identifier=CHANNEL_IDENTIFIER_GLOBAL_QUEUE,
            message_identifier=state_change.message_identifier,
        )

        events.extend(
            [payment_received_success, unlock_success, send_processed])
        next_target_state = None

    return TransitionResult(next_target_state, events)
Beispiel #3
0
def handle_unlock(target_state, state_change: ReceiveUnlock, channel_state):
    """ Handles a ReceiveUnlock state change. """
    iteration = TransitionResult(target_state, list())
    balance_proof_sender = state_change.balance_proof.sender

    if balance_proof_sender == target_state.route.node_address:
        is_valid, events, _ = channel.handle_unlock(
            channel_state,
            state_change,
        )

        if is_valid:
            transfer = target_state.transfer
            payment_received_success = EventPaymentReceivedSuccess(
                payment_network_identifier=channel_state.payment_network_identifier,
                token_network_identifier=channel_state.token_network_identifier,
                identifier=transfer.payment_identifier,
                amount=transfer.lock.amount,
                initiator=transfer.initiator,
            )

            unlock_success = EventUnlockClaimSuccess(
                transfer.payment_identifier,
                transfer.lock.secrethash,
            )

            send_processed = SendProcessed(
                recipient=balance_proof_sender,
                channel_identifier=CHANNEL_IDENTIFIER_GLOBAL_QUEUE,
                message_identifier=state_change.message_identifier,
            )

            events.extend([payment_received_success, unlock_success, send_processed])
            iteration = TransitionResult(None, events)

    return iteration
Beispiel #4
0
def handle_unlock_light(
    target_state: TargetTransferState,
    state_change: ReceiveUnlockLight,
    channel_state: NettingChannelState,
) -> TransitionResult[TargetTransferState]:
    """ Handles a ReceiveUnlockLight state change. """

    is_valid, events, _ = channel.handle_unlock_light(channel_state, state_change)
    next_target_state: Optional[TargetTransferState] = target_state

    if is_valid:
        transfer = target_state.transfer
        payment_received_success = EventPaymentReceivedSuccess(
            payment_network_identifier=channel_state.payment_network_identifier,
            token_network_identifier=TokenNetworkID(channel_state.token_network_identifier),
            identifier=transfer.payment_identifier,
            amount=TokenAmount(transfer.lock.amount),
            initiator=transfer.initiator,
        )

        unlock_success = EventUnlockClaimSuccess(
            transfer.payment_identifier, transfer.lock.secrethash
        )

        store_unlock_message = StoreMessageEvent(
            state_change.signed_unlock.message_identifier,
            state_change.signed_unlock.payment_identifier,
            11,
            state_change.signed_unlock,
            True
        )

        events.extend([payment_received_success, unlock_success, store_unlock_message])
        next_target_state = None

    return TransitionResult(next_target_state, events)
Beispiel #5
0
def handle_contractunlock(
    state,
    state_change,
    channelidentifiers_to_channels,
    pseudo_random_generator,
    block_number,
):
    """ Handle a NettingChannelUnlock state change. """
    assert sha3(
        state.secret
    ) == state.secrethash, 'secret must be validated by the smart contract'

    # For all but the last pair in transfer pair a refund transfer ocurred,
    # meaning the same channel was used twice, once when this node sent the
    # mediated transfer and once when the refund transfer was received. A
    # ContractReceiveChannelUnlock state change may be used for each.

    events = list()

    # This node withdrew the refund
    if state_change.receiver == state.our_address:
        for previous_pos, pair in enumerate(state.transfers_pair, -1):
            payer_channel = get_payer_channel(channelidentifiers_to_channels,
                                              pair)
            if payer_channel.identifier == state_change.channel_identifier:
                # always set the contract_unlock regardless of the previous
                # state (even expired)
                pair.payer_state = 'payer_contract_unlock'

                unlock = EventUnlockClaimSuccess(
                    pair.payer_transfer.payment_identifier,
                    pair.payer_transfer.lock.secrethash,
                )
                events.append(unlock)

                # if the current pair is backed by a refund set the sent
                # mediated transfer to a 'secret known' state
                if previous_pos > -1:
                    previous_pair = state.transfers_pair[previous_pos]

                    if previous_pair.payee_state not in STATE_TRANSFER_FINAL:
                        previous_pair.payee_state = 'payee_refund_unlock'

    # A partner withdrew the mediated transfer
    else:
        for pair in state.transfers_pair:
            payee_channel = get_payee_channel(channelidentifiers_to_channels,
                                              pair)
            if payee_channel.identifier == state_change.channel_identifier:
                unlock = EventUnlockSuccess(
                    pair.payee_transfer.payment_identifier,
                    pair.payee_transfer.lock.secrethash,
                )
                events.append(unlock)

                pair.payee_state = 'payee_contract_unlock'

    iteration = secret_learned(
        state,
        channelidentifiers_to_channels,
        pseudo_random_generator,
        block_number,
        state_change.secret,
        state_change.secrethash,
        state_change.receiver,
        'payee_contract_unlock',
        False,
    )

    iteration.events.extend(events)

    return iteration