Skip to content

Commit

Permalink
Add endpoint to return all online addresses
Browse files Browse the repository at this point in the history
This makes it possible to run the Raiden explorer efficiently.

Closes #1007.
  • Loading branch information
karlb committed May 17, 2021
1 parent ce7f75e commit cadc24b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
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)]
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

0 comments on commit cadc24b

Please sign in to comment.