Source code for fleetmesh_node.transport.relay_rung.core

"""RelayRung: the relay rung's state and construction. Behaviour is in the
sibling mixins; this file is what an operator or a test instantiates.
"""
from __future__ import annotations

import collections
import queue
import threading
import time
from typing import Any, Callable, Counter, Deque, Mapping, Optional, Sequence

from ...interfaces import NostrEvent, RelayHealthRow
from .. import const, didcomm_inbound
from ..ladder import Fault, FaultLog
from .cooldown import RelayCooldown
from ..const import Inbound
from . import wire
from .types import AckVerdict, AwaitingEntry, Partial
from .inbound import InboundMixin
from .ledger import LedgerMixin
from .listen import ListenMixin
from .outbound import OutboundMixin


[docs]class RelayRung(OutboundMixin, ListenMixin, InboundMixin, LedgerMixin): scheme: str = 'nostr'
[docs] def __init__( self, keystore: Any, peers: Any, relay_urls: Sequence[str], clock: Optional[Callable[[], int]] = None, timeout: float = 15.0, seen_store: Optional[tuple[Callable[[str], bool], Callable[[str, int], None]]] = None, ack_store: Optional[Any] = None, seen_max: int = const.SEEN_MAX, ) -> None: self.ks: Any = keystore self.seen_max: int = seen_max # wrap ids remembered for dedup before the oldest is forgotten self.peers: Any = peers # Stored messages sent and not yet acknowledged, by message id. On # disk when the node backs it with its accounts (put_awaiting / # drop_awaiting / awaiting), because the peer may be offline longer # than this process lives and the question survives a restart. self.ack_store: Optional[Any] = ack_store self.awaiting: dict[str, AwaitingEntry] = {} if ack_store is not None: for mid, peer, mtype, at, relays in ack_store.awaiting(): self.awaiting[mid] = AwaitingEntry(peer=peer, type=mtype, at=at, relays=relays, heard_at=None) self.heard: dict[str, int] = {} # peer -> when it was last heard from self.acked: Counter[str] = collections.Counter() # type -> stored messages acknowledged self.acks_in: int = 0 self.acks_out: int = 0 self.unacked: Deque[AckVerdict] = collections.deque(maxlen=const.DROPPED_KEPT) # verdicts, newest last self._unacked_new: list[AckVerdict] = [] # verdicts not yet taken by the compartment self._acks: queue.Queue[tuple[str, str]] = queue.Queue() # (message id, peer) to acknowledge # Stored wraps (kind 1059) are replayed by relays on every reconnect, # for two days; a node that forgets what it opened across a restart # would act on an old terminate after re-peering. The store is a # pair (has(wid), add(wid, at)) the node backs with its accounts. self.seen_store: Optional[tuple[Callable[[str], bool], Callable[[str, int], None]]] = seen_store self.wraps_in: Counter[Optional[int]] = collections.Counter() # inbound wraps by kind self.wraps_seen_again: Counter[Optional[int]] = collections.Counter() # and those a dedup discarded self.relays: list[str] = list(relay_urls) self.clock: Callable[[], int] = clock or (lambda: int(time.time())) # peer -> (its kind 10050 relays, when read). Read once an hour, not # per message: a list that changed under us costs one hour of wraps # going to the old inbox, which the relay replay horizon covers. self._dm_relays: dict[str, tuple[list[str], int]] = {} self.timeout: float = timeout self.inbox: queue.Queue[Inbound] = queue.Queue(maxsize=const.INBOX_MAX) self._waiting: dict[str, queue.Queue[Inbound]] = {} # thid -> Queue self._seen: set[str] = set() # wrap ids, so two relays deliver once self._seen_order: Deque[str] = collections.deque() # the same ids, oldest first, for the cap self._partials: dict[tuple[str, str], Partial] = {} # (sender, fragment id) -> pieces of a large envelope # Replay and freshness for control/1.0 sealed by a person's own key # (plan D25): the wrap-id dedup above does not cover a seal re-wrapped # in a new wrap, so these messages are guarded by their own id. self.sealed_guard: didcomm_inbound.InboundGuard = didcomm_inbound.InboundGuard() self._lock: threading.Lock = threading.Lock() self._stop: threading.Event = threading.Event() self._threads: list[threading.Thread] = [] # What this rung refused or lost, with reasons; the ladder reads it # through faults, Link exports it, and `meshctl watch` reds on it. self.faults: FaultLog = FaultLog('nostr', clock=self.clock, keep=const.DROPPED_KEPT) # Relays that asked to be left alone for a while; see cooldown.py. self.cooldown: RelayCooldown = RelayCooldown(self.clock) # One entry per relay, for the operator: whether the subscription is up, # when it last delivered, how often it has had to reconnect and why. self.health: dict[str, RelayHealthRow] = { url: { 'connected': False, # Whether the relay ANSWERED the subscription, which is not the # same as having sent it. A kind 21059 is ephemeral: a wrap a # peer sends before the REQ is registered is fanned out to # nobody and no relay kept a copy, so a node that believes it # is listening one round trip early loses exactly the live # control traffic it cannot ask for again. 'confirmed': False, 'since': None, 'last_event_at': None, 'reconnects': 0, 'last_error': None, } for url in self.relays }
[docs] def unfinished(self) -> int: """Envelopes half-arrived: fragments held with pieces still missing. A number the operator reads, so it is asked for rather than reached in for. The compartment that reports it has no business in `_partials`. """ return len(self._partials)
def _sign_auth(self, relay_url: str, challenge: str) -> NostrEvent: """Sign a NIP-42 kind 22242 authentication event using the node's key.""" return wire.auth_event(self.ks.node_sk, int(self.clock()), relay_url, challenge) def _drop(self, reason: str, wrap_id: Optional[str]) -> None: self.faults.record(reason, wrap_id)
[docs] def recent_faults(self, since: Optional[int] = None) -> list[Fault]: """What this rung refused or lost, newest last, with reasons.""" return self.faults.recent(since)
@property def drop_counts(self) -> Counter[str]: """Fault family -> how many, ever.""" return self.faults.counts @property def dropped(self) -> Deque[tuple[str, Optional[str]]]: """(reason, wrap_id) of the most recent faults, oldest first.""" return collections.deque(((f['reason'], f['ident']) for f in self.faults.recent()), maxlen=self.faults.maxlen) @staticmethod def _fragments(jwe: Mapping[str, Any]) -> list[Any]: return wire.fragments(jwe)