|
| 1 | +"""Client for the logos-delivery store REST API. |
| 2 | +
|
| 3 | +The transport that status-go currently runs on (and that backs the functional |
| 4 | +test fleet) is historically called "waku". The project term going forward is |
| 5 | +"logos delivery", so new code uses that name. The fleet store node exposes the |
| 6 | +nwaku REST API (`/store/v3/messages`); this client queries it so tests can |
| 7 | +assert that messages were actually persisted by the store node, instead of |
| 8 | +sleeping and hoping. |
| 9 | +""" |
| 10 | + |
| 11 | +import logging |
| 12 | +import time |
| 13 | + |
| 14 | +import docker |
| 15 | +import requests |
| 16 | +from tenacity import retry, stop_after_attempt, wait_fixed |
| 17 | + |
| 18 | +from utils.config import Config |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +# Container port the nwaku store node serves its REST API on (mapped to an |
| 23 | +# ephemeral host port by docker-compose.waku.yml). |
| 24 | +STORE_REST_CONTAINER_PORT = "8645/tcp" |
| 25 | + |
| 26 | + |
| 27 | +class LogosDeliveryClient: |
| 28 | + def __init__(self, base_url: str | None = None, timeout: float = 10.0): |
| 29 | + # The store REST port is published on an ephemeral host port to avoid |
| 30 | + # collisions on shared CI hosts, so discover it at runtime via the |
| 31 | + # Docker API (same approach as clients/anvil.py). An explicit base_url |
| 32 | + # can still be passed (e.g. for --status_backend_url style setups). |
| 33 | + self.base_url = (base_url or self._discover_store_url()).rstrip("/") |
| 34 | + self.timeout = timeout |
| 35 | + |
| 36 | + @retry(stop=stop_after_attempt(15), wait=wait_fixed(0.2), reraise=True) |
| 37 | + def _discover_store_url(self) -> str: |
| 38 | + docker_client = docker.from_env() |
| 39 | + project = Config.docker_project_name |
| 40 | + network = docker_client.networks.get(f"{project}_default") |
| 41 | + prefix = f"{project}-store" |
| 42 | + store = next((c for c in network.containers if c.name and prefix in c.name), None) |
| 43 | + if store is None: |
| 44 | + raise RuntimeError(f"store container ('{prefix}*') not found") |
| 45 | + mappings = store.attrs["NetworkSettings"]["Ports"].get(STORE_REST_CONTAINER_PORT) or [] |
| 46 | + if not mappings: |
| 47 | + raise RuntimeError("store REST port is not exposed") |
| 48 | + host_ip = mappings[0]["HostIp"] or "127.0.0.1" |
| 49 | + if host_ip == "0.0.0.0": |
| 50 | + host_ip = "127.0.0.1" |
| 51 | + return f"http://{host_ip}:{mappings[0]['HostPort']}" |
| 52 | + |
| 53 | + def get_messages( |
| 54 | + self, |
| 55 | + *, |
| 56 | + content_topics: list[str] | None = None, |
| 57 | + pubsub_topic: str | None = None, |
| 58 | + start_time: int | None = None, |
| 59 | + end_time: int | None = None, |
| 60 | + hashes: list[str] | None = None, |
| 61 | + page_size: int = 100, |
| 62 | + ascending: bool = True, |
| 63 | + include_data: bool = True, |
| 64 | + ) -> list[dict]: |
| 65 | + """Return all stored messages matching the filters, following pagination. |
| 66 | +
|
| 67 | + Times are Unix nanoseconds (matching the store node's timestamps). |
| 68 | + """ |
| 69 | + params: dict[str, str] = { |
| 70 | + "pageSize": str(page_size), |
| 71 | + "ascending": "true" if ascending else "false", |
| 72 | + "includeData": "true" if include_data else "false", |
| 73 | + } |
| 74 | + if content_topics: |
| 75 | + params["contentTopics"] = ",".join(content_topics) |
| 76 | + if pubsub_topic: |
| 77 | + params["pubsubTopic"] = pubsub_topic |
| 78 | + if start_time is not None: |
| 79 | + params["startTime"] = str(start_time) |
| 80 | + if end_time is not None: |
| 81 | + params["endTime"] = str(end_time) |
| 82 | + if hashes: |
| 83 | + params["hashes"] = ",".join(hashes) |
| 84 | + |
| 85 | + messages: list[dict] = [] |
| 86 | + cursor = None |
| 87 | + while True: |
| 88 | + page_params = dict(params) |
| 89 | + if cursor: |
| 90 | + page_params["cursor"] = cursor |
| 91 | + response = requests.get(f"{self.base_url}/store/v3/messages", params=page_params, timeout=self.timeout) |
| 92 | + response.raise_for_status() |
| 93 | + body = response.json() |
| 94 | + messages.extend(body.get("messages", []) or []) |
| 95 | + cursor = body.get("paginationCursor") or body.get("cursor") |
| 96 | + if not cursor: |
| 97 | + break |
| 98 | + return messages |
| 99 | + |
| 100 | + def count_messages(self, **kwargs) -> int: |
| 101 | + return len(self.get_messages(**kwargs)) |
| 102 | + |
| 103 | + def get_peers(self) -> list[dict]: |
| 104 | + response = requests.get(f"{self.base_url}/admin/v1/peers", timeout=self.timeout) |
| 105 | + response.raise_for_status() |
| 106 | + return response.json() |
| 107 | + |
| 108 | + def wait_for_message_count(self, expected_count: int, *, timeout: float = 60.0, poll_interval: float = 2.0, **kwargs) -> list[dict]: |
| 109 | + """Poll the store until at least *expected_count* messages match, or time out.""" |
| 110 | + deadline = time.time() + timeout |
| 111 | + messages: list[dict] = [] |
| 112 | + while time.time() < deadline: |
| 113 | + messages = self.get_messages(**kwargs) |
| 114 | + if len(messages) >= expected_count: |
| 115 | + return messages |
| 116 | + time.sleep(poll_interval) |
| 117 | + return messages |
0 commit comments