Exemplo n.º 1
0
def test_pfs_rejects_capacity_update_with_wrong_channel_identifier(
    pathfinding_service_web3_mock, ):
    pathfinding_service_web3_mock.chain_id = ChainID(1)

    token_network = TokenNetwork(
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS)
    pathfinding_service_web3_mock.token_networks[
        token_network.address] = token_network

    token_network.handle_channel_opened_event(
        channel_identifier=ChannelID(0),
        participant1=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        participant2=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        settle_timeout=15,
    )

    # Check that the new channel has id == 0
    assert token_network.channel_id_to_addresses[ChannelID(0)] == (
        private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
    )

    message = get_updatepfs_message(
        channel_identifier=ChannelID(35),
        updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        privkey_signer=PRIVAT_KEY_EXAMPLE_1,
    )

    with pytest.raises(InvalidCapacityUpdate) as exinfo:
        pathfinding_service_web3_mock.on_pfs_update(message)
    assert "unknown channel identifier in token network" in str(exinfo.value)
Exemplo n.º 2
0
def test_pfs_rejects_capacity_update_with_wrong_nonces(
    addresses: List[Address],
    pathfinding_service_mocked_listeners: PathfindingService,
):
    pathfinding_service_mocked_listeners.chain_id = 1

    token_network = TokenNetwork(
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS,
        token_address=DEFAULT_TOKEN_ADDRESS,
    )

    pathfinding_service_mocked_listeners.token_networks[
        token_network.address] = token_network

    token_network.handle_channel_opened_event(
        channel_identifier=ChannelIdentifier(0),
        participant1=addresses[0],
        participant2=addresses[1],
        settle_timeout=15,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelIdentifier(0),
        receiver=addresses[0],
        total_deposit=100,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelIdentifier(0),
        receiver=addresses[1],
        total_deposit=100,
    )

    # Check that the new channel has id == 0
    assert token_network.channel_id_to_addresses[ChannelIdentifier(0)] == (
        addresses[0],
        addresses[1],
    )

    message = get_updatepfs_message(
        updating_participant=addresses[0],
        other_participant=addresses[1],
    )

    # Check first capacity update succeeded
    pathfinding_service_mocked_listeners.on_pfs_update(message)
    view_to_partner, view_from_partner = token_network.get_channel_views_for_partner(
        channel_identifier=ChannelIdentifier(0),
        updating_participant=addresses[0],
        other_participant=addresses[1],
    )
    assert view_to_partner.capacity == 90
    assert view_to_partner.update_nonce == 1
    assert view_from_partner.capacity == 110
    assert view_from_partner.update_nonce == 0

    # Send the same Capacity Update again - leads to an exception
    with pytest.raises(InvalidCapacityUpdate) as exinfo:
        pathfinding_service_mocked_listeners.on_pfs_update(message)
    assert 'Capacity Update already received' in str(exinfo.value)
Exemplo n.º 3
0
def test_path_without_capacity(token_network_model: TokenNetwork,
                               addresses: List[Address]):
    """Channels without capacity must not cause unexpected exceptions.

    Regression test for https://github.com/raiden-network/raiden-services/issues/636
    """
    token_network_model.handle_channel_opened_event(
        channel_identifier=ChannelID(1),
        participant1=addresses[0],
        participant2=addresses[1],
    )
    token_network_model.handle_channel_opened_event(
        channel_identifier=ChannelID(2),
        participant1=addresses[1],
        participant2=addresses[2],
    )

    token_network_model.G[addresses[1]][
        addresses[2]]["view"].channel.capacity1 = 100
    path = Path(
        G=token_network_model.G,
        nodes=[addresses[0], addresses[1], addresses[2]],
        value=PaymentAmount(10),
        reachability_state=SimpleReachabilityContainer({}),
    )
    assert not path.is_valid
Exemplo n.º 4
0
def test_pfs_rejects_capacity_update_with_wrong_nonces(
    pathfinding_service_web3_mock, ):
    pathfinding_service_web3_mock.chain_id = ChainID(1)

    token_network = TokenNetwork(
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS)

    pathfinding_service_web3_mock.token_networks[
        token_network.address] = token_network

    token_network.handle_channel_opened_event(
        channel_identifier=ChannelID(0),
        participant1=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        participant2=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        settle_timeout=15,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelID(0),
        receiver=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        total_deposit=100,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelID(0),
        receiver=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        total_deposit=100,
    )

    # Check that the new channel has id == 0
    assert token_network.channel_id_to_addresses[ChannelID(0)] == (
        private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
    )

    message = get_updatepfs_message(
        updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        privkey_signer=PRIVAT_KEY_EXAMPLE_1,
    )

    # Check first capacity update succeeded
    pathfinding_service_web3_mock.on_pfs_update(message)
    view_to_partner, view_from_partner = token_network.get_channel_views_for_partner(
        channel_identifier=ChannelID(0),
        updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
    )
    assert view_to_partner.capacity == 90
    assert view_to_partner.update_nonce == 1
    assert view_from_partner.capacity == 110
    assert view_from_partner.update_nonce == 0

    # Send the same Capacity Update again - leads to an exception
    with pytest.raises(InvalidCapacityUpdate) as exinfo:
        pathfinding_service_web3_mock.on_pfs_update(message)
    assert "Capacity Update already received" in str(exinfo.value)
Exemplo n.º 5
0
def test_pfs_rejects_capacity_update_with_impossible_updating_capacity(
    pathfinding_service_web3_mock, ):
    pathfinding_service_web3_mock.chain_id = ChainID(1)

    token_network = TokenNetwork(
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS)

    pathfinding_service_web3_mock.token_networks[
        token_network.address] = token_network

    token_network.handle_channel_opened_event(
        channel_identifier=ChannelID(0),
        participant1=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        participant2=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        settle_timeout=15,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelID(0),
        receiver=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        total_deposit=100,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelID(0),
        receiver=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
        total_deposit=100,
    )

    # Check that the new channel has id == 0
    assert token_network.channel_id_to_addresses[ChannelID(0)] == (
        private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
        private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
    )

    with patch(
            "pathfinding_service.service.recover_signer_from_capacity_update",
            private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
    ):
        message = get_updatepfs_message(
            updating_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_1),
            other_participant=private_key_to_address(PRIVAT_KEY_EXAMPLE_2),
            updating_capacity=TokenAmount(UINT256_MAX),
            privkey_signer=PRIVAT_KEY_EXAMPLE_1,
        )
        message.updating_capacity = TokenAmount(UINT256_MAX + 1)

        with pytest.raises(InvalidCapacityUpdate) as exinfo:
            pathfinding_service_web3_mock.on_pfs_update(message)
        assert "with impossible updating_capacity" in str(exinfo.value)
Exemplo n.º 6
0
def test_pfs_rejects_capacity_update_with_wrong_other_participant(
    addresses: List[Address],
    pathfinding_service_mocked_listeners: PathfindingService,
):
    pathfinding_service_mocked_listeners.chain_id = 1

    token_network = TokenNetwork(
        token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS,
        token_address=DEFAULT_TOKEN_ADDRESS,
    )

    pathfinding_service_mocked_listeners.token_networks[
        token_network.address] = token_network

    token_network.handle_channel_opened_event(
        channel_identifier=ChannelIdentifier(0),
        participant1=addresses[0],
        participant2=addresses[1],
        settle_timeout=15,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelIdentifier(0),
        receiver=addresses[0],
        total_deposit=100,
    )

    token_network.handle_channel_new_deposit_event(
        channel_identifier=ChannelIdentifier(0),
        receiver=addresses[1],
        total_deposit=100,
    )

    # Check that the new channel has id == 0
    assert token_network.channel_id_to_addresses[ChannelIdentifier(0)] == (
        addresses[0],
        addresses[1],
    )

    message = get_updatepfs_message(
        updating_participant=addresses[0],
        other_participant=addresses[2],
    )

    with pytest.raises(InvalidCapacityUpdate) as exinfo:
        pathfinding_service_mocked_listeners.on_pfs_update(message)
    assert 'Other Participant of Capacity Update does not match' in str(
        exinfo.value)
Exemplo n.º 7
0
def setup_channel(service: PathfindingService) -> TokenNetwork:
    token_network = TokenNetwork(token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS)
    service.token_networks[token_network.address] = token_network

    token_network.handle_channel_opened_event(
        channel_identifier=DEFAULT_CHANNEL_ID,
        participant1=PRIVATE_KEY_1_ADDRESS,
        participant2=PRIVATE_KEY_2_ADDRESS,
        settle_timeout=15,
    )

    # Check that the new channel has id == 0
    assert token_network.channel_id_to_addresses[DEFAULT_CHANNEL_ID] == (
        PRIVATE_KEY_1_ADDRESS,
        PRIVATE_KEY_2_ADDRESS,
    )

    return token_network
Exemplo n.º 8
0
def test_tn_idempotency_of_channel_openings(token_network_model: TokenNetwork,
                                            addresses: List[Address]):
    # create same channel 5 times
    for _ in range(5):
        token_network_model.handle_channel_opened_event(
            channel_identifier=ChannelID(1),
            participant1=addresses[0],
            participant2=addresses[1],
        )
    # there should only be one channel
    assert len(token_network_model.channel_id_to_addresses) == 1

    # now close the channel
    token_network_model.handle_channel_removed_event(
        channel_identifier=ChannelID(1))

    # there should be no channels
    assert len(token_network_model.channel_id_to_addresses) == 0
Exemplo n.º 9
0
def test_tn_multiple_channels_for_two_participants_opened(
        token_network_model: TokenNetwork, addresses: List[Address]):
    token_network_model.handle_channel_opened_event(
        channel_identifier=ChannelID(1),
        participant1=addresses[0],
        participant2=addresses[1],
    )
    token_network_model.handle_channel_opened_event(
        channel_identifier=ChannelID(2),
        participant1=addresses[0],
        participant2=addresses[1],
    )

    # now there should be two channels
    assert len(token_network_model.channel_id_to_addresses) == 2

    # now close one channel
    token_network_model.handle_channel_removed_event(
        channel_identifier=ChannelID(1))

    # there should be one channel left
    assert len(token_network_model.channel_id_to_addresses) == 1