Exemple #1
0
def test_validate_found_nodes_distances_catches_self_reference(local_enr, enr_group):
    enrs, distances = enr_group
    invalid_enrs = enrs + (local_enr,)
    unique_distances = set(distances)
    assert 0 not in unique_distances
    with pytest.raises(ValidationError, match="Invalid response: distance=0"):
        validate_found_nodes_distances(
            invalid_enrs, local_enr.node_id, unique_distances
        )
Exemple #2
0
def validate_found_nodes_response(message: FoundNodesMessage,
                                  request: AnyOutboundMessage,
                                  expected_total: int) -> None:
    if message.total == 0:
        raise ValidationError(
            f"Invalid `total` counter in response: total={message.total}")

    if expected_total != message.total:
        raise ValidationError(
            "Inconsistent message total. Received a FoundNodesMessage with "
            f"a total of {message.total}, expected a total of {expected_total}"
        )

    validate_found_nodes_distances(message.enrs, request.receiver_node_id,
                                   request.message.distances)
Exemple #3
0
    async def find_nodes(
        self,
        node_id: NodeID,
        *distances: int,
        endpoint: Optional[Endpoint] = None,
        request_id: Optional[bytes] = None,
    ) -> Tuple[ENRAPI, ...]:
        if not distances:
            raise TypeError("Must provide at least one distance")

        if endpoint is None:
            endpoint = await self.endpoint_for_node_id(node_id)
        responses = await self.client.find_nodes(
            node_id, endpoint, distances=distances, request_id=request_id
        )

        # Validate that all responses are indeed at one of the
        # specified distances.
        for response in responses:
            validate_found_nodes_distances(response.message.enrs, node_id, distances)

        return tuple(enr for response in responses for enr in response.message.enrs)
Exemple #4
0
def test_validate_found_nodes_distances_catches_invalid_cases(local_enr, enr_group):
    enrs, distances = enr_group
    unique_distances = set(distances)
    unique_distances.remove(distances[0])
    with pytest.raises(ValidationError, match="Invalid response: distance"):
        validate_found_nodes_distances(enrs, local_enr.node_id, unique_distances)
Exemple #5
0
def test_validate_found_nodes_distances(local_enr, enr_group):
    enrs, distances = enr_group
    assert validate_found_nodes_distances(enrs, local_enr.node_id, distances) is None