Exemplo n.º 1
0
def subdispatch_initiatortask(
    chain_state: ChainState,
    state_change: StateChange,
    token_network_identifier: TokenNetworkID,
    secrethash: SecretHash,
) -> TransitionResult[ChainState]:

    block_number = chain_state.block_number
    sub_task = chain_state.payment_mapping.secrethashes_to_task.get(secrethash)

    if not sub_task:
        is_valid_subtask = True
        manager_state = None

    elif sub_task and isinstance(sub_task, InitiatorTask):
        is_valid_subtask = (
            token_network_identifier == sub_task.token_network_identifier)
        manager_state = sub_task.manager_state
    else:
        is_valid_subtask = False

    events = list()
    if is_valid_subtask:
        pseudo_random_generator = chain_state.pseudo_random_generator

        token_network_state = get_token_network_by_address(
            chain_state,
            token_network_identifier,
        )

        if token_network_state:
            iteration = initiator_manager.state_transition(
                manager_state,
                state_change,
                token_network_state.channelidentifiers_to_channels,
                pseudo_random_generator,
                block_number,
            )
            events = iteration.events

            if iteration.new_state:
                sub_task = InitiatorTask(
                    token_network_identifier,
                    iteration.new_state,
                )
                chain_state.payment_mapping.secrethashes_to_task[
                    secrethash] = sub_task
            elif secrethash in chain_state.payment_mapping.secrethashes_to_task:
                del chain_state.payment_mapping.secrethashes_to_task[
                    secrethash]

    return TransitionResult(chain_state, events)
Exemplo n.º 2
0
def test_initiator_task_view():
    """Test transfer_tasks_view(), which is used to generate the output of the
    pending transfers API, with an initiator task.
    """
    channel_id = factories.UNIT_CHANNEL_ID
    secret = factories.make_secret()
    payment_hash_invoice = factories.make_payment_hash_invoice()
    transfer = factories.create(
        factories.LockedTransferUnsignedStateProperties(
            secret=secret, payment_hash_invoice=payment_hash_invoice))
    secrethash = transfer.lock.secrethash
    transfer_description = TransferDescriptionWithSecretState(
        payment_network_identifier=factories.UNIT_PAYMENT_NETWORK_IDENTIFIER,
        payment_identifier=transfer.payment_identifier,
        payment_hash_invoice=transfer.payment_hash_invoice,
        amount=transfer.balance_proof.locked_amount,
        allocated_fee=0,
        token_network_identifier=factories.UNIT_TOKEN_NETWORK_ADDRESS,
        initiator=transfer.initiator,
        target=transfer.target,
        secret=secret,
    )
    transfer_state = InitiatorTransferState(
        transfer_description=transfer_description,
        channel_identifier=channel_id,
        transfer=transfer,
        revealsecret=None,
    )
    payment_state = InitiatorPaymentState({secrethash: transfer_state})
    task = InitiatorTask(
        token_network_identifier=factories.UNIT_TOKEN_NETWORK_ADDRESS,
        manager_state=payment_state)
    payment_mapping = {secrethash: task}

    view = transfer_tasks_view(payment_mapping)

    assert len(view) == 1
    pending_transfer = view[0]
    assert pending_transfer.get("role") == "initiator"
    balance_proof = transfer.balance_proof
    assert pending_transfer.get("channel_identifier") == str(
        balance_proof.channel_identifier)
    assert pending_transfer.get("locked_amount") == str(
        balance_proof.locked_amount)
    assert pending_transfer.get("transferred_amount") == str(
        balance_proof.transferred_amount)