def test_events_for_close_hold_for_unpaid_payee():
    """ If the secret is known but the payee transfer has not being paid the
    node must not settle on-chain, otherwise the payee can burn tokens to
    induce the mediator to close a channel.
    """

    transfers_pair = make_transfers_pair(
        factories.HOP1,
        [factories.HOP2, factories.HOP3],
        factories.HOP6,
        amount=10,
        secret=factories.UNIT_SECRET,
    )

    pair = transfers_pair[0]

    # preconditions
    assert pair.payer_transfer.secret == factories.UNIT_SECRET
    assert pair.payee_transfer.secret == factories.UNIT_SECRET
    assert pair.payee_state not in mediator.STATE_TRANSFER_PAID

    # do not generate events if the secret is known AND the payee is not paid
    first_unsafe_block = pair.payer_transfer.expiration - pair.payer_route.reveal_timeout
    events = mediator.events_for_close(
        transfers_pair,
        first_unsafe_block,
    )

    assert len(events) == 0
    assert pair.payee_state not in mediator.STATE_TRANSFER_PAID
    assert pair.payer_state not in mediator.STATE_TRANSFER_PAID

    payer_expiration_block = pair.payer_transfer.expiration
    events = mediator.events_for_close(
        transfers_pair,
        payer_expiration_block,
    )
    assert len(events) == 0
    assert pair.payee_state not in mediator.STATE_TRANSFER_PAID
    assert pair.payer_state not in mediator.STATE_TRANSFER_PAID

    payer_expiration_block = pair.payer_transfer.expiration
    events = mediator.events_for_close(
        transfers_pair,
        payer_expiration_block + 1,
    )
    assert len(events) == 0
    assert pair.payee_state not in mediator.STATE_TRANSFER_PAID
    assert pair.payer_state not in mediator.STATE_TRANSFER_PAID
def test_events_for_close_hold_for_unpaid_payee():
    """ If the secret is known but the payee transfer has not being paid the
    node must not settle on-chain, otherwise the payee can burn tokens to
    induce the mediator to close a channel.
    """

    transfers_pair = make_transfers_pair(
        factories.HOP1,
        [factories.HOP2, factories.HOP3],
        factories.HOP6,
        amount=10,
        secret=factories.UNIT_SECRET,
    )

    pair = transfers_pair[0]

    # preconditions
    assert pair.payer_transfer.secret == factories.UNIT_SECRET
    assert pair.payee_transfer.secret == factories.UNIT_SECRET
    assert pair.payee_state not in mediator.STATE_TRANSFER_PAID

    # do not generate events if the secret is known AND the payee is not paid
    first_unsafe_block = pair.payer_transfer.expiration - pair.payer_route.reveal_timeout
    events = mediator.events_for_close(
        transfers_pair,
        first_unsafe_block,
    )

    assert len(events) == 0
    assert pair.payee_state not in mediator.STATE_TRANSFER_PAID
    assert pair.payer_state not in mediator.STATE_TRANSFER_PAID

    payer_expiration_block = pair.payer_transfer.expiration
    events = mediator.events_for_close(
        transfers_pair,
        payer_expiration_block,
    )
    assert len(events) == 0
    assert pair.payee_state not in mediator.STATE_TRANSFER_PAID
    assert pair.payer_state not in mediator.STATE_TRANSFER_PAID

    payer_expiration_block = pair.payer_transfer.expiration
    events = mediator.events_for_close(
        transfers_pair,
        payer_expiration_block + 1,
    )
    assert len(events) == 0
    assert pair.payee_state not in mediator.STATE_TRANSFER_PAID
    assert pair.payer_state not in mediator.STATE_TRANSFER_PAID
def test_events_for_close():
    """ The node must close to unlock on-chain if the payee was paid. """

    for payee_state in ('payee_balance_proof', 'payee_contract_withdraw'):
        transfers_pair = make_transfers_pair(
            factories.HOP1,
            [factories.HOP2, factories.HOP3],
            factories.HOP6,
            amount=10,
            secret=factories.UNIT_SECRET,
        )

        pair = transfers_pair[0]
        pair.payee_state = payee_state

        block_number = (
            pair.payer_transfer.expiration - pair.payer_route.reveal_timeout
        )

        events = mediator.events_for_close(
            transfers_pair,
            block_number,
        )

        assert isinstance(events[0], ContractSendChannelClose)
        assert events[0].channel_address == pair.payer_route.channel_address
        assert pair.payer_state == 'payer_waiting_close'
Exemplo n.º 4
0
def test_events_for_close():
    """ The node must close to unlock on-chain if the payee was paid. """

    for payee_state in ('payee_balance_proof', 'payee_contract_withdraw'):
        transfers_pair = make_transfers_pair(
            [factories.HOP1, factories.HOP2],
            factories.HOP6,
            amount=10,
            secret=factories.UNIT_SECRET,
        )

        pair = transfers_pair[0]
        pair.payee_state = payee_state

        block_number = (
            pair.payer_transfer.expiration - pair.payer_route.reveal_timeout
        )

        events = mediator.events_for_close(
            transfers_pair,
            block_number,
        )

        assert isinstance(events[0], ContractSendChannelClose)
        assert events[0].channel_address == pair.payer_route.channel_address
        assert pair.payer_state == 'payer_waiting_close'