コード例 #1
0
def test_receive_direct_before_deposit(raiden_network):
    """Regression test that ensures we accept incoming direct transfers, even if we don't have
    any back channel balance.  """
    app0, app1, _ = raiden_network

    token_address = app0.raiden.default_registry.token_addresses()[0]
    channel_0_1 = channel(app0, app1, token_address)
    back_channel = channel(app1, app0, token_address)

    assert not channel_0_1.can_transfer
    assert not back_channel.can_transfer

    deposit_amount = 2
    transfer_amount = 1
    api0 = RaidenAPI(app0.raiden)
    api0.deposit(token_address, app1.raiden.address, deposit_amount)
    app0.raiden.chain.next_block()
    gevent.sleep(app0.raiden.alarm.wait_time)

    assert channel_0_1.can_transfer
    assert not back_channel.can_transfer
    assert back_channel.distributable == 0

    api0.transfer_and_wait(token_address, transfer_amount, app1.raiden.address)
    gevent.sleep(app1.raiden.alarm.wait_time)

    assert back_channel.can_transfer
    assert back_channel.distributable == transfer_amount
コード例 #2
0
def test_snapshotting(raiden_network, token_addresses):
    app0, app1, app2 = raiden_network

    api0 = RaidenAPI(app0.raiden)
    api1 = RaidenAPI(app1.raiden)

    channel_0_1 = api0.get_channel_list(token_addresses[0], app1.raiden.address)
    channel_0_2 = api0.get_channel_list(token_addresses[0], app2.raiden.address)

    assert not api1.get_channel_list(token_addresses[0], app2.raiden.address)

    assert len(channel_0_1) == 1
    assert len(channel_0_2) == 1
    api1.transfer_and_wait(token_addresses[0], 5, app2.raiden.address)

    app0.stop()
    app1.stop()
    app2.stop()

    for app in [app0, app1, app2]:
        data = load_snapshot(app.raiden.serialization_file)

        for serialized_channel in data['channels']:
            network = app.raiden.token_to_channelgraph[serialized_channel.token_address]
            running_channel = network.address_to_channel[serialized_channel.channel_address]
            assert running_channel.serialize() == serialized_channel

        for queue in data['queues']:
            key = (queue['receiver_address'], queue['token_address'])
            assert app.raiden.protocol.channel_queue[key].copy() == queue['messages']

        assert data['receivedhashes_to_acks'] == app.raiden.protocol.receivedhashes_to_acks
        assert data['nodeaddresses_to_nonces'] == app.raiden.protocol.nodeaddresses_to_nonces
        assert data['transfers'] == app.raiden.identifier_to_statemanagers
コード例 #3
0
def test_receive_direct_before_deposit(raiden_network):
    """Regression test that ensures we accept incoming direct transfers, even if we don't have
    any back channel balance.  """
    app0, app1, _ = raiden_network

    token_address = app0.raiden.default_registry.token_addresses()[0]
    channel_0_1 = channel(app0, app1, token_address)
    back_channel = channel(app1, app0, token_address)

    assert not channel_0_1.can_transfer
    assert not back_channel.can_transfer

    deposit_amount = 2
    transfer_amount = 1
    api0 = RaidenAPI(app0.raiden)
    api0.deposit(token_address, app1.raiden.address, deposit_amount)
    app0.raiden.chain.next_block()
    gevent.sleep(app0.raiden.alarm.wait_time)

    assert channel_0_1.can_transfer
    assert not back_channel.can_transfer
    assert back_channel.distributable == 0

    api0.transfer_and_wait(token_address, transfer_amount, app1.raiden.address)
    gevent.sleep(app1.raiden.alarm.wait_time)

    assert back_channel.can_transfer
    assert back_channel.distributable == transfer_amount
コード例 #4
0
def test_receive_mediated_before_deposit(raiden_network):
    """Regression test that ensures we accept incoming mediated transfers, even if we don't have
    any back channel balance. """
    app_bob, app_alice, app_charlie = raiden_network

    chain = app_bob.raiden.chain

    token_address = app_bob.raiden.chain.default_registry.token_addresses()[0]
    # path alice -> bob -> charlie
    alice_bob = channel(app_alice, app_bob, token_address)
    bob_alice = channel(app_bob, app_alice, token_address)
    bob_charlie = channel(app_bob, app_charlie, token_address)
    charlie_bob = channel(app_charlie, app_bob, token_address)

    all_channels = dict(alice_bob=alice_bob,
                        bob_alice=bob_alice,
                        bob_charlie=bob_charlie,
                        charlie_bob=charlie_bob)
    # ensure alice charlie is mediated
    with pytest.raises(KeyError):
        channel(app_alice, app_charlie, token_address)

    assert not alice_bob.can_transfer
    assert not bob_charlie.can_transfer
    assert not bob_alice.can_transfer

    deposit_amount = 3
    transfer_amount = 1

    api_alice = RaidenAPI(app_alice.raiden)
    api_alice.deposit(token_address, app_bob.raiden.address, deposit_amount)
    chain.next_block()
    gevent.sleep(app_alice.raiden.alarm.wait_time)

    api_bob = RaidenAPI(app_bob.raiden)
    api_bob.deposit(token_address, app_charlie.raiden.address, deposit_amount)
    chain.next_block()
    gevent.sleep(app_bob.raiden.alarm.wait_time)

    assert alice_bob.can_transfer
    assert alice_bob.distributable == deposit_amount
    assert bob_charlie.can_transfer
    assert bob_charlie.distributable == deposit_amount
    assert not bob_alice.can_transfer

    api_alice.transfer_and_wait(token_address, transfer_amount,
                                app_charlie.raiden.address)
    gevent.sleep(app_alice.raiden.alarm.wait_time)

    assert alice_bob.distributable == deposit_amount - transfer_amount
    assert bob_charlie.distributable == deposit_amount - transfer_amount
    assert bob_alice.distributable == transfer_amount, channel_balances(
        all_channels)
    assert bob_alice.can_transfer
    assert charlie_bob.distributable == transfer_amount, channel_balances(
        all_channels)
    assert charlie_bob.can_transfer
コード例 #5
0
def test_same_addresses_for_payment(raiden_network: List[RaidenService],
                                    token_addresses):
    app0, _ = raiden_network
    api0 = RaidenAPI(app0)
    registry_address = app0.default_registry.address
    token_address = token_addresses[0]

    with pytest.raises(SamePeerAddress):
        api0.transfer_and_wait(
            registry_address=registry_address,
            token_address=token_address,
            target=TargetAddress(app0.address),
            amount=PaymentAmount(1),
        )
コード例 #6
0
def test_close_regression(raiden_network, deposit, token_addresses):
    """ The python api was using the wrong balance proof to close the channel,
    thus the close was failing if a transfer was made.
    """
    app0, app1 = raiden_network
    token_address = token_addresses[0]

    api1 = RaidenAPI(app0.raiden)
    api2 = RaidenAPI(app1.raiden)

    registry_address = app0.raiden.default_registry.address
    channel_list = api1.get_channel_list(registry_address, token_address,
                                         app1.raiden.address)
    channel12 = channel_list[0]

    token_proxy = app0.raiden.proxy_manager.token(token_address,
                                                  BLOCK_ID_LATEST)
    node1_balance_before = token_proxy.balance_of(api1.address)
    node2_balance_before = token_proxy.balance_of(api2.address)

    # Initialize app2 balance proof and close the channel
    amount = PaymentAmount(10)
    identifier = PaymentID(42)
    secret, secrethash = factories.make_secret_with_hash()
    timeout = block_offset_timeout(app1.raiden, "Transfer timed out.")
    with watch_for_unlock_failures(*raiden_network), timeout:
        assert api1.transfer_and_wait(
            registry_address=registry_address,
            token_address=token_address,
            amount=amount,
            target=TargetAddress(api2.address),
            identifier=identifier,
            secret=secret,
        )
        timeout.exception_to_throw = ValueError(
            "Waiting for transfer received success in the WAL timed out.")
        result = waiting.wait_for_received_transfer_result(
            raiden=app1.raiden,
            payment_identifier=identifier,
            amount=amount,
            retry_timeout=app1.raiden.alarm.sleep_time,
            secrethash=secrethash,
        )

    msg = f"Unexpected transfer result: {str(result)}"
    assert result == waiting.TransferWaitResult.UNLOCKED, msg

    api2.channel_close(registry_address, token_address, api1.address)

    waiting.wait_for_settle(
        app0.raiden,
        app0.raiden.default_registry.address,
        token_address,
        [channel12.identifier],
        app0.raiden.alarm.sleep_time,
    )
    node1_expected_balance = node1_balance_before + deposit - amount
    node2_expected_balance = node2_balance_before + deposit + amount
    assert token_proxy.balance_of(api1.address) == node1_expected_balance
    assert token_proxy.balance_of(api2.address) == node2_expected_balance
コード例 #7
0
def test_close_regression(raiden_network, deposit, token_addresses):
    """ The python api was using the wrong balance proof to close the channel,
    thus the close was failing if a transfer was made.
    """
    app0, app1 = raiden_network
    token_address = token_addresses[0]

    api1 = RaidenAPI(app0.raiden)
    api2 = RaidenAPI(app1.raiden)

    registry_address = app0.raiden.default_registry.address
    channel_list = api1.get_channel_list(registry_address, token_address,
                                         app1.raiden.address)
    channel12 = channel_list[0]

    token_proxy = app0.raiden.proxy_manager.token(token_address)
    node1_balance_before = token_proxy.balance_of(api1.address)
    node2_balance_before = token_proxy.balance_of(api2.address)

    # Initialize app2 balance proof and close the channel
    amount = 10
    identifier = 42
    secret, secrethash = factories.make_secret_with_hash()
    assert api1.transfer_and_wait(
        registry_address=registry_address,
        token_address=token_address,
        amount=amount,
        target=api2.address,
        identifier=identifier,
        secret=secret,
        transfer_timeout=10,
    )
    exception = ValueError(
        "Waiting for transfer received success in the WAL timed out")
    with gevent.Timeout(seconds=5, exception=exception):
        result = waiting.wait_for_received_transfer_result(
            raiden=app1.raiden,
            payment_identifier=identifier,
            amount=amount,
            retry_timeout=app1.raiden.alarm.sleep_time,
            secrethash=secrethash,
        )
        msg = f"Unexpected transfer result: {str(result)}"
        assert result == waiting.TransferWaitResult.UNLOCKED, msg

    api2.channel_close(registry_address, token_address, api1.address)

    waiting.wait_for_settle(
        app0.raiden,
        app0.raiden.default_registry.address,
        token_address,
        [channel12.identifier],
        app0.raiden.alarm.sleep_time,
    )
    node1_expected_balance = node1_balance_before + deposit - amount
    node2_expected_balance = node2_balance_before + deposit + amount
    assert token_proxy.balance_of(api1.address) == node1_expected_balance
    assert token_proxy.balance_of(api2.address) == node2_expected_balance
コード例 #8
0
def test_transfer_after_connect_works(raiden_network, token_addresses):
    """
    Test that payments work after joining a channel. This makes sure that the
    connection manager does not leave any partners in a half-healthchecked
    state that causes problems during payments.

    Test for https://github.com/raiden-network/raiden/issues/5918
    """
    registry_address = raiden_network[0].raiden.default_registry.address
    token_address = token_addresses[0]
    app0, app1, app2 = raiden_network
    api0 = RaidenAPI(app0.raiden)
    api1 = RaidenAPI(app1.raiden)

    # Open channel between node0 and node2 to not run into the bootstrapping
    # case when joining the token network
    api0.channel_open(registry_address, token_address, app2.raiden.address)
    # Make sure that app1 processed the block where channel open
    # happened. Otherwise the test becomes flaky because it does not see
    # potential participants in the network
    current_block = app0.raiden.get_block_number()
    wait_for_block(app1.raiden, current_block, 1)

    api1.token_network_connect(
        registry_address=registry_address,
        token_address=token_address,
        funds=TokenAmount(100),
        initial_channel_target=2,
    )

    payment_result = api1.transfer_and_wait(
        registry_address=registry_address,
        token_address=token_address,
        amount=PaymentAmount(1),
        target=app0.raiden.address,
    ).payment_done.get()
    assert isinstance(payment_result, EventPaymentSentSuccess)