Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide specific error message for common errors #674

Merged
merged 1 commit into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ disable=
fixme,
unused-wildcard-import,
len-as-condition,
too-many-return-statements,

# checks that are done with higher accuracy by mypy
no-member,
Expand Down
15 changes: 15 additions & 0 deletions src/pathfinding_service/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ def post(self, token_network_address: str) -> Tuple[dict, int]:
one_to_n_address=self.service_api.one_to_n_address,
)

# check for common error cases to provide clear error messages
error = token_network.check_path_request_errors(
source=path_req.from_,
target=path_req.to,
value=path_req.value,
address_to_reachability=self.pathfinding_service.address_to_reachability,
)
if error:
raise exceptions.NoRouteFound(
from_=to_checksum_address(path_req.from_),
to=to_checksum_address(path_req.to),
value=path_req.value,
msg=error,
)

# only add optional args if not None, so we can use defaults
optional_args = {}
for arg in ["diversity_penalty", "fee_penalty"]:
Expand Down
34 changes: 34 additions & 0 deletions src/pathfinding_service/model/token_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,40 @@ def _get_single_path( # pylint: disable=too-many-arguments, too-many-locals
except StopIteration:
return None

def check_path_request_errors(
self,
source: Address,
target: Address,
value: PaymentAmount,
address_to_reachability: Dict[Address, AddressReachability],
) -> Optional[str]:
""" Checks for basic problems with the path requests. Returns error message or `None` """

if (
address_to_reachability.get(source, AddressReachability.UNREACHABLE)
!= AddressReachability.REACHABLE
):
return "Source not online"
if (
address_to_reachability.get(target, AddressReachability.UNREACHABLE)
!= AddressReachability.REACHABLE
):
return "Target not online"

if not any(self.G.edges(source)):
return "No channel from source"
if not any(self.G.edges(target)):
return "No channel to target"

source_capacities = [view.capacity for _, _, view in self.G.out_edges(source, data="view")]
if max(source_capacities) < value:
return "Source does not have a channel with sufficient capacity"
target_capacities = [view.capacity for _, _, view in self.G.in_edges(target, data="view")]
if max(target_capacities) < value:
return "Target does not have a channel with sufficient capacity"

return None

def get_paths( # pylint: disable=too-many-arguments, too-many-locals
self,
source: Address,
Expand Down
56 changes: 56 additions & 0 deletions tests/pathfinding/test_token_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,59 @@ def test_path_without_capacity(token_network_model: TokenNetwork, addresses: Lis
address_to_reachability=dict(),
)
assert not path.is_valid


def test_check_path_request_errors(token_network_model, addresses):
a = addresses # pylint: disable=invalid-name

# Not online checks
assert (
token_network_model.check_path_request_errors(a[0], a[2], 100, {}) == "Source not online"
)
assert (
token_network_model.check_path_request_errors(
a[0], a[2], 100, {a[0]: AddressReachability.REACHABLE}
)
== "Target not online"
)

# No channel checks
reachability = {
a[0]: AddressReachability.REACHABLE,
a[2]: AddressReachability.REACHABLE,
}
assert (
token_network_model.check_path_request_errors(a[0], a[2], 100, reachability)
== "No channel from source"
)
token_network_model.handle_channel_opened_event(
channel_identifier=ChannelID(1),
participant1=a[0],
participant2=a[1],
settle_timeout=BlockTimeout(15),
)
assert (
token_network_model.check_path_request_errors(a[0], a[2], 100, reachability)
== "No channel to target"
)
token_network_model.handle_channel_opened_event(
channel_identifier=ChannelID(1),
participant1=a[1],
participant2=a[2],
settle_timeout=BlockTimeout(15),
)

# Check capacities
assert (
token_network_model.check_path_request_errors(a[0], a[2], 100, reachability)
== "Source does not have a channel with sufficient capacity"
)
token_network_model.G.edges[a[0], a[1]]["view"].capacity = 100
assert (
token_network_model.check_path_request_errors(a[0], a[2], 100, reachability)
== "Target does not have a channel with sufficient capacity"
)
token_network_model.G.edges[a[1], a[2]]["view"].capacity = 100

# Must return `None` when no errors could be found
assert token_network_model.check_path_request_errors(a[0], a[2], 100, reachability) is None