Exemplo n.º 1
0
def subdispatch_targettask(node_state, state_change,
                           payment_network_identifier,
                           token_network_identifier, channel_identifier,
                           hashlock):

    block_number = node_state.block_number
    sub_task = node_state.payment_mapping.hashlocks_to_task.get(hashlock)

    if not sub_task:
        is_valid_subtask = True
        target_state = None

    elif sub_task and isinstance(sub_task, PaymentMappingState.TargetTask):
        is_valid_subtask = (
            payment_network_identifier == sub_task.payment_network_identifier
            and token_network_identifier == sub_task.token_network_identifier)
        target_state = sub_task.target_state
    else:
        is_valid_subtask = False

    events = list()
    channel_state = None
    if is_valid_subtask:
        channel_state = views.get_channelstate_by_tokenaddress(
            node_state,
            payment_network_identifier,
            token_network_identifier,
            channel_identifier,
        )

    if channel_state:
        iteration = target.state_transition2(
            target_state,
            state_change,
            channel_state,
            block_number,
        )
        events = iteration.events

        if iteration.new_state:
            sub_task = PaymentMappingState.TargetTask(
                payment_network_identifier,
                token_network_identifier,
                channel_identifier,
                iteration.new_state,
            )
            node_state.payment_mapping.hashlocks_to_task[hashlock] = sub_task

    return TransitionResult(node_state, events)
Exemplo n.º 2
0
def test_handle_block_lower_block_number():
    """ Nothing changes. """
    initiator = factories.HOP6
    our_address = factories.ADDR
    amount = 3
    block_number = 10

    from_channel, state = make_target_state(
        our_address,
        amount,
        block_number,
        initiator,
    )

    new_block = Block(block_number - 1)
    iteration = target.state_transition2(
        state,
        new_block,
        from_channel,
        new_block.block_number,
    )
    assert iteration.new_state
    assert not iteration.events
Exemplo n.º 3
0
def test_handle_block():
    """ Increase the block number. """
    initiator = factories.HOP6
    our_address = factories.ADDR
    amount = 3
    block_number = 1

    from_channel, state = make_target_state(
        our_address,
        amount,
        block_number,
        initiator,
    )

    new_block = Block(block_number + 1)
    iteration = target.state_transition2(
        state,
        new_block,
        from_channel,
        new_block.block_number,
    )
    assert iteration.new_state
    assert not iteration.events
Exemplo n.º 4
0
def subdispatch_to_paymenttask(node_state, state_change, hashlock):
    block_number = node_state.block_number
    sub_task = node_state.payment_mapping.hashlocks_to_task.get(hashlock)
    events = list()

    if sub_task:
        if isinstance(sub_task, PaymentMappingState.InitiatorTask):
            payment_network_identifier = sub_task.payment_network_identifier
            token_network_identifier = sub_task.token_network_identifier

            token_network_state = get_token_network(
                node_state,
                payment_network_identifier,
                token_network_identifier,
            )

            if token_network_state:
                sub_iteration = initiator_manager.state_transition(
                    sub_task.manager_state,
                    state_change,
                    token_network_state.channelidentifiers_to_channels,
                    block_number,
                )
                events = sub_iteration.events

        elif isinstance(sub_task, PaymentMappingState.MediatorTask):
            payment_network_identifier = sub_task.payment_network_identifier
            token_network_identifier = sub_task.token_network_identifier

            token_network_state = get_token_network(
                node_state,
                payment_network_identifier,
                token_network_identifier,
            )

            if token_network_state:
                sub_iteration = mediator.state_transition2(
                    sub_task.mediator_state,
                    state_change,
                    token_network_state.channelidentifiers_to_channels,
                    block_number,
                )
                events = sub_iteration.events

        elif isinstance(sub_task, PaymentMappingState.TargetTask):
            payment_network_identifier = sub_task.payment_network_identifier
            token_network_identifier = sub_task.token_network_identifier
            channel_identifier = sub_task.channel_identifier

            channel_state = views.get_channelstate_by_tokenaddress(
                node_state,
                payment_network_identifier,
                token_network_identifier,
                channel_identifier,
            )

            if channel_state:
                sub_iteration = target.state_transition2(
                    sub_task.target_state,
                    state_change,
                    channel_state,
                    block_number,
                )
                events = sub_iteration.events

    return TransitionResult(node_state, events)
Exemplo n.º 5
0
def test_state_transition():
    """ Happy case testing. """
    lock_amount = 7
    block_number = 1
    initiator = factories.HOP6
    payment_network_identifier = factories.make_address()

    our_balance = 100
    our_address = factories.make_address()
    partner_balance = 130

    from_channel = factories.make_channel(
        our_address=our_address,
        our_balance=our_balance,
        partner_address=UNIT_TRANSFER_SENDER,
        partner_balance=partner_balance,
    )
    from_route = factories.route_from_channel(from_channel)
    expiration = block_number + from_channel.settle_timeout

    from_transfer = factories.make_signed_transfer_for(
        from_channel,
        lock_amount,
        initiator,
        our_address,
        expiration,
        UNIT_SECRET,
    )

    init = ActionInitTarget2(
        payment_network_identifier,
        from_route,
        from_transfer,
    )

    init_transition = target.state_transition2(
        None,
        init,
        from_channel,
        block_number,
    )
    assert init_transition.new_state is not None
    assert init_transition.new_state.route == from_route
    assert init_transition.new_state.transfer == from_transfer

    first_new_block = Block(block_number + 1)
    first_block_iteration = target.state_transition2(
        init_transition.new_state,
        first_new_block,
        from_channel,
        first_new_block.block_number,
    )

    secret_reveal = ReceiveSecretReveal(factories.UNIT_SECRET, initiator)
    reveal_iteration = target.state_transition2(
        first_block_iteration.new_state,
        secret_reveal,
        from_channel,
        first_new_block,
    )
    assert reveal_iteration.events

    second_new_block = Block(block_number + 2)
    iteration = target.state_transition2(
        init_transition.new_state,
        second_new_block,
        from_channel,
        second_new_block.block_number,
    )
    assert not iteration.events

    nonce = from_transfer.balance_proof.nonce + 1
    transferred_amount = lock_amount
    locksroot = EMPTY_MERKLE_ROOT
    invalid_message_hash = b'\x00' * 32

    balance_proof = factories.make_signed_balance_proof(
        nonce,
        transferred_amount,
        from_route.channel_identifier,
        locksroot,
        invalid_message_hash,
        UNIT_TRANSFER_PKEY,
        UNIT_TRANSFER_SENDER,
    )

    payment_network_identifier = factories.make_address()
    balance_proof_state_change = ReceiveUnlock(
        UNIT_SECRET,
        balance_proof,
    )

    proof_iteration = target.state_transition2(
        init_transition.new_state,
        balance_proof_state_change,
        from_channel,
        block_number + 2,
    )
    assert proof_iteration.new_state is None