コード例 #1
0
def test_get_paths(api_url: str, addresses: List[Address],
                   token_network_model: TokenNetwork):
    hex_addrs = [to_checksum_address(addr) for addr in addresses]
    url = api_url + "/v1/" + to_checksum_address(
        token_network_model.address) + "/paths"

    data = {
        "from": hex_addrs[0],
        "to": hex_addrs[2],
        "value": 10,
        "max_paths": DEFAULT_MAX_PATHS
    }
    response = requests.post(url, json=data)
    assert response.status_code == 200
    paths = response.json()["result"]
    assert len(paths) == 1
    assert paths == [{
        "path": [hex_addrs[0], hex_addrs[1], hex_addrs[2]],
        "address_metadata": {
            hex_addrs[0]: get_address_metadata(hex_addrs[0]),
            hex_addrs[1]: get_address_metadata(hex_addrs[1]),
            hex_addrs[2]: get_address_metadata(hex_addrs[2]),
        },
        "estimated_fee": 0,
    }]

    # check default value for num_path
    data = {"from": hex_addrs[0], "to": hex_addrs[2], "value": 10}
    default_response = requests.post(url, json=data)
    assert default_response.json()["result"] == response.json()["result"]

    # impossible routes
    for source, dest in [
        (hex_addrs[0], hex_addrs[5]),  # no connection between 0 and 5
        ("0x" + "1" * 40, hex_addrs[5]),  # source not in graph
        (hex_addrs[0], "0x" + "1" * 40),  # dest not in graph
    ]:
        data = {"from": source, "to": dest, "value": 10, "max_paths": 3}
        response = requests.post(url, json=data)
        assert response.status_code == 404
        assert response.json(
        )["error_code"] == exceptions.NoRouteFound.error_code
コード例 #2
0
def test_get_paths_via_debug_endpoint_a(api_url: str, addresses: List[Address],
                                        token_network_model: TokenNetwork):  # pylint: disable=too-many-locals
    # `last_failed_requests` is a module variable, so it might have entries
    # from tests that ran earlier.
    last_failed_requests.clear()
    hex_addrs = [to_checksum_address(addr) for addr in addresses]
    token_network_address = to_checksum_address(token_network_model.address)

    # Make two requests, so we can test the `request_count` as well
    for _ in range(2):
        response = requests.post(
            api_url + f"/v1/{token_network_address}/paths",
            json={
                "from": hex_addrs[0],
                "to": hex_addrs[2],
                "value": 10,
                "max_paths": DEFAULT_MAX_PATHS,
            },
        )
        assert response.status_code == 200
        paths = response.json()["result"]
        assert len(paths) == 1
        assert paths == [{
            "path": [hex_addrs[0], hex_addrs[1], hex_addrs[2]],
            "estimated_fee": 0,
            "address_metadata": {
                hex_addrs[0]: get_address_metadata(hex_addrs[0]),
                hex_addrs[1]: get_address_metadata(hex_addrs[1]),
                hex_addrs[2]: get_address_metadata(hex_addrs[2]),
            },
        }]

    # now there must be a debug endpoint for that specific route
    url_debug = api_url + f"/v1/_debug/routes/{token_network_address}/{hex_addrs[0]}"
    response_debug = requests.get(url_debug)
    assert response_debug.status_code == 200
    request_count = response_debug.json()["request_count"]
    assert request_count == 2
    responses = response_debug.json()["responses"]
    assert responses == [
        {
            "source":
            hex_addrs[0],
            "target":
            hex_addrs[2],
            "routes": [{
                "path": [hex_addrs[0], hex_addrs[1], hex_addrs[2]],
                "estimated_fee": 0
            }],
        },
        {
            "source":
            hex_addrs[0],
            "target":
            hex_addrs[2],
            "routes": [{
                "path": [hex_addrs[0], hex_addrs[1], hex_addrs[2]],
                "estimated_fee": 0
            }],
        },
    ]

    # now there must be a debug endpoint for that specific route and that specific target
    url_debug_incl_requested_target = (
        api_url +
        f"/v1/_debug/routes/{token_network_address}/{hex_addrs[0]}/{hex_addrs[2]}"
    )
    response_debug_incl_target = requests.get(url_debug_incl_requested_target)
    assert response_debug_incl_target.status_code == 200
    request_count = response_debug_incl_target.json()["request_count"]
    assert request_count == 2
    responses = response_debug.json()["responses"]
    assert responses == [
        {
            "source":
            hex_addrs[0],
            "target":
            hex_addrs[2],
            "routes": [{
                "path": [hex_addrs[0], hex_addrs[1], hex_addrs[2]],
                "estimated_fee": 0
            }],
        },
        {
            "source":
            hex_addrs[0],
            "target":
            hex_addrs[2],
            "routes": [{
                "path": [hex_addrs[0], hex_addrs[1], hex_addrs[2]],
                "estimated_fee": 0
            }],
        },
    ]

    # when requesting info for a target that was no path requested for
    url_debug_incl_unrequested_target = (
        api_url +
        f"/v1/_debug/routes/{token_network_address}/{hex_addrs[0]}/{hex_addrs[3]}"
    )
    response_debug_incl_unrequested_target = requests.get(
        url_debug_incl_unrequested_target)
    assert response_debug_incl_unrequested_target.status_code == 200
    request_count = response_debug_incl_unrequested_target.json(
    )["request_count"]
    assert request_count == 0
    responses = response_debug_incl_unrequested_target.json()["responses"]
    assert responses == []