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

Add endpoint to return all online addresses #1010

Merged
merged 1 commit into from
May 17, 2021
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
16 changes: 15 additions & 1 deletion src/pathfinding_service/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from werkzeug.exceptions import NotFound
from werkzeug.middleware.dispatcher import DispatcherMiddleware

import raiden.utils.typing as raiden_typing
from pathfinding_service import exceptions, metrics
from pathfinding_service.constants import (
API_PATH,
Expand All @@ -38,7 +39,7 @@
from pathfinding_service.model.token_network import Path, TokenNetwork
from pathfinding_service.service import PathfindingService
from raiden.exceptions import InvalidSignature
from raiden.network.transport.matrix.utils import UserPresence
from raiden.network.transport.matrix.utils import AddressReachability, UserPresence
from raiden.utils.signer import recover
from raiden.utils.typing import (
Address,
Expand Down Expand Up @@ -510,6 +511,18 @@ def get(self, token_network_address: str) -> Tuple[List[Dict[str, Any]], int]:
return suggestions, 200


class OnlineAddressesResource(PathfinderResource):
def get(self) -> Tuple[List[raiden_typing.ChecksumAddress], int]:
user_manager = self.pathfinding_service.matrix_listener.user_manager
online_addresses = [
to_checksum_address(address)
for address in user_manager.known_addresses
if user_manager.get_address_reachability(address) == AddressReachability.REACHABLE
]

return online_addresses, 200


class DebugPathResource(PathfinderResource):
def get( # pylint: disable=no-self-use
self,
Expand Down Expand Up @@ -666,6 +679,7 @@ def after_request(response: Response) -> Response: # pylint: disable=unused-var
{},
"suggest_partner",
),
("/v1/online_addresses", OnlineAddressesResource, {}, "online_addresses"),
("/v1/info", InfoResource, {}, "info"),
("/v2/info", InfoResource2, {}, "info2"),
(
Expand Down
15 changes: 14 additions & 1 deletion tests/pathfinding/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pathfinding_service.model import IOU, TokenNetwork
from pathfinding_service.model.feedback import FeedbackToken
from raiden.network.transport.matrix import AddressReachability
from raiden.tests.utils.factories import make_signer
from raiden.tests.utils.factories import make_address, make_signer
from raiden.utils.signer import LocalSigner
from raiden.utils.typing import (
Address,
Expand Down Expand Up @@ -722,3 +722,16 @@ def test_suggest_partner_api(api_url: str, token_network_model: TokenNetwork):
url = api_url + f"/v1/{token_network_address_hex}/suggest_partner"
response = requests.get(url)
assert response.status_code == 200


@pytest.mark.usefixtures("api_sut")
def test_online_addresses(api_url: str, reachability_state):
online_addr = make_address()
reachability_state.reachabilities = {
online_addr: AddressReachability.REACHABLE,
make_address(): AddressReachability.UNREACHABLE,
}
url = api_url + "/v1/online_addresses"
response = requests.get(url)
assert response.status_code == 200
assert response.json() == [to_checksum_address(online_addr)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to want to have n>=2 examples for tests involving a "many" thing, but I guess this is easy enough...

4 changes: 4 additions & 0 deletions tests/pathfinding/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def __init__(self, reachabilities: Dict[Address, AddressReachability]) -> None:
self._address_to_userids.__getitem__ = lambda self, key: {get_user_id_from_address(key)}
self._displayname_cache = DisplayNameCache()

@property
def known_addresses(self) -> Set[Address]:
return set(self.reachabilities.keys())

def get_address_reachability(self, address: Address) -> AddressReachability:
return self.reachabilities.get(address, AddressReachability.UNKNOWN)

Expand Down