hs-libp2p-0.2.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

LibP2P.Discovery.Rendezvous

Description

Rendezvous protocol (specsrendezvousREADME.md), protocol id rendezvous1.0.0.

Peers connect to a rendezvous point and register their presence in one or more namespaces with a signed peer record (RFC 0003, see "LibP2P.Crypto.PeerRecord"); other peers discover registrations by namespace with cookie-based pagination for progressive refresh.

This module implements both sides:

  • server — registerRendezvousPoint registers a stream handler on the Switch, turning this node into a rendezvous point. Registrations carry signed peer record envelopes, expire by TTL, and are bounded by the rendezvous-point recommendations (namespace length, TTL range, per-peer and per-query limits). Only the peer initiating a registration may register itself.
  • client — registerSelf, unregisterSelf and discover operate over an existing connection to a rendezvous point.

On the wire every message is varint-length-delimited protobuf (uvarint(len) ++ protobuf), the framing go-libp2p, rust-libp2p and js-libp2p use for rendezvous streams, and the same framing this codebase uses for Identify.

Synopsis

Types

data RendezvousPoint Source #

The rendezvous point's mutable state.

data RendezvousConfig Source #

Rendezvous point configuration. Defaults follow the spec's "Recommendations for Rendezvous Points configurations": minimum TTL 2h, maximum TTL 72h, namespace length 255, 1000 registrations per peer, 1000 registrations returned per namespace query.

Constructors

RendezvousConfig 

Fields

data RendezvousError Source #

Client-side failure. Rejections carry the rendezvous point's status (ResponseStatus) so the application can distinguish, say, an invalid TTL from an unauthorized registration.

Constructors

RendezvousTimeout

No answer within the exchange timeout

RendezvousTransport !String

Stream/negotiation failure

RendezvousRejected !ResponseStatus !(Maybe Text)

Rendezvous point rejected the request, with its status text

RendezvousDecode !String

Malformed response

Events

data DiscoveryEvent Source #

A peer found by a discovery mechanism, with the addresses it advertised. Deduplication is the consumer's job, not the queue's: discovery runs concurrently with the Switch's peer store, and suppressing re-announcements would hide refreshed address lists. A TQueue is unbounded, so producing never blocks; applications are expected to drain or poll at their own cadence.

Constructors

DiscoveryEvent 

Fields

newDiscoveryQueue :: IO (TQueue DiscoveryEvent) Source #

Create a discovery event queue.

tryReadDiscoveryQueue :: TQueue DiscoveryEvent -> IO (Maybe DiscoveryEvent) Source #

Read one event if any are queued.

drainDiscoveryQueue :: TQueue DiscoveryEvent -> IO [DiscoveryEvent] Source #

Drain every currently queued event, in order.

Server (rendezvous point)

newRendezvousPoint :: RendezvousConfig -> IO RendezvousPoint Source #

Create an in-memory rendezvous point.

registerRendezvousPoint :: Switch -> RendezvousConfig -> IO RendezvousPoint Source #

Serve rendezvous1.0.0 on the Switch, making this node act as a rendezvous point. Returns the point so callers can query state (registeredCount) for tests and observability.

handleRendezvousStream :: RendezvousPoint -> StreamIO -> PeerId -> IO () Source #

Handle an inbound rendezvous stream (server side).

Clients open one stream per operation in practice (go-libp2p), but the stream is a message channel: pipelined messages are served until the stream ends. Framing or decode errors stop serving the stream; a disconnect (EOF, reset) ends it too. Either way the local side is closed so a half-open Yamux stream cannot leak (handleDHTRequest follows the same loop shape).

registeredCount :: RendezvousPoint -> IO Int Source #

Number of currently valid registrations across all namespaces, excluding expired ones. Expired entries are pruned lazily (on discovery; nothing scans in the background), so rcMaxPerPeer counts live registrations: a peer that keeps re-registering a fresh record keeps one slot per namespace.

Client

rendezvousProtocolId :: ProtocolId Source #

Protocol ID (specsrendezvousREADME.md §The Protocol).

rendezvousTimeoutMicros :: Int Source #

Timeout for one rendezvous exchange: 5 seconds, the same bound as this codebase's Identify interaction. Discovery calls are interactive; a rendezvous point that negotiates and never answers must not pin a stream and a thread forever.

registerSelf :: Switch -> Connection -> Text -> Maybe Word64 -> IO (Either RendezvousError Word64) Source #

Register ourselves in a namespace (client side of REGISTER). A signed peer record over our current listen addresses is built here; the granted TTL is returned so the application knows when to refresh.

unregisterSelf :: Switch -> Connection -> Text -> IO (Either RendezvousError ()) Source #

Cancel our registration in a namespace (client side of UNREGISTER). No reply is sent by the rendezvous point; the write succeeding is the outcome.

discover :: Switch -> Connection -> Maybe Text -> DiscoverLimit -> ByteString -> IO (Either RendezvousError ([RegistrationInfo], ByteString)) Source #

Discover registrations (client side of DISCOVER). Pass Nothing for the namespace to query every namespace; pass the cookie from a previous response to list only registrations made since (progressive refresh, specs/rendezvous §The Protocol). Returns the registrations and the cookie for the next poll.

inspectRegistration :: RegistrationInfo -> Either String DiscoveryEvent Source #

Validate a received registration's signed peer record envelope and extract the peer identity and dialable addresses it advertises.

pushDiscoverEvents :: TQueue DiscoveryEvent -> [RegistrationInfo] -> IO () Source #

Push decoded discovery events for registrations onto a queue; registrations whose record does not verify are dropped rather than poisoning the feed.

Wire framing

encodeFramedMessage :: RendezvousMessage -> ByteString Source #

Encode a message with its uvarint length prefix, as written on the wire: uvarint(len) ++ protobuf.

readFramedMessage :: StreamIO -> Int -> IO (Either String RendezvousMessage) Source #

Read one varint-length-prefixed message from a stream.

Reads the uvarint length prefix, then exactly that many payload bytes, and decodes the protobuf. Rejects messages larger than maxSize before reading the payload.