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

LibP2P.Discovery.Rendezvous.Message

Description

Rendezvous protocol messages (specsrendezvousREADME.md).

Wire format (specsrendezvousREADME.md §Protobuf, proto2):

message Message {
  enum MessageType { REGISTER = 0; REGISTER_RESPONSE = 1;
                     UNREGISTER = 2; DISCOVER = 3; DISCOVER_RESPONSE = 4; }
  enum ResponseStatus { OK = 0; E_INVALID_NAMESPACE = 100;
                        E_INVALID_SIGNED_PEER_RECORD = 101; E_INVALID_TTL = 102;
                        E_INVALID_COOKIE = 103; E_NOT_AUTHORIZED = 200;
                        E_INTERNAL_ERROR = 300; E_UNAVAILABLE = 400; }
  message Register        { optional string ns = 1; optional bytes
                            signedPeerRecord = 2; optional uint64 ttl = 3; }
  message RegisterResponse{ optional ResponseStatus status = 1;
                            optional string statusText = 2;
                            optional uint64 ttl = 3; }
  message Unregister      { optional string ns = 1; }
  message Discover        { optional string ns = 1; optional uint64 limit = 2;
                            optional bytes cookie = 3; }
  message DiscoverResponse{ repeated Register registrations = 1;
                            optional bytes cookie = 2;
                            optional ResponseStatus status = 3;
                            optional string statusText = 4; }
  optional MessageType type = 1;
  optional Register register = 2; ... DiscoverResponse discoverResponse = 6;
}

Uses proto3-wire for protobuf encoding/decoding. On the wire the message is varint-length-delimited; the framing lives in LibP2P.Discovery.Rendezvous (encodeFramedMessage / readFramedMessage).

Synopsis

Message types

data RendezvousMessage Source #

A rendezvous message (specsrendezvousREADME.md §Protobuf).

The five wire message types are separate constructors; the envelope type discriminator (field 1) is checked on decode and set on encode, so a decoded message never disagrees with its type field.

Constructors

RMRegister !Text !ByteString !(Maybe Word64)

A REGISTER: namespace, signed peer-record envelope (RFC 0003), requested TTL (absent = rendezvous point default).

RMRegisterResponse !ResponseStatus !(Maybe Text) !(Maybe Word64)

A REGISTER_RESPONSE: grant or rejection status, optional status text, granted TTL (present when the registration was OK).

RMUnregister !Text

An UNREGISTER: namespace to cancel in.

RMDiscover !(Maybe Text) !DiscoverLimit !ByteString

A DISCOVER: namespace (Nothing or empty = ask for every namespace), result limit, previous response cookie.

RMDiscoverResponse ![RegistrationInfo] !ByteString !ResponseStatus !(Maybe Text)

A DISCOVER_RESPONSE: current registrations, cookie for the next poll, status, optional status text.

data RegistrationInfo Source #

One registration as carried in a DISCOVER response: the namespace it was registered under, its signed peer record (RFC 0003 envelope), and the TTL it was registered with.

Constructors

RegistrationInfo 

Fields

data ResponseStatus Source #

Response status codes (specsrendezvousREADME.md §Protobuf).

data DiscoverLimit Source #

Discovery limit: absent (rendezvous point default applies) or an explicit maximum (possibly 0). Both are meaningful on the wire: a rendezvous point with zero registrations to hand out still answers OK with an empty list, so an absent limit must not be conflated with "zero".

Constructors

LimitDefault

No limit given; rendezvous point applies its default

LimitExact !Word64

Explicit limit, possibly 0

Codec

encodeRendezvousMessage :: RendezvousMessage -> ByteString Source #

Encode a rendezvous message to protobuf wire format. The message type (field 1) is always written; exactly one submessage (fields 2-6) is written, matching the discriminator. Submessage fields that coincide with a proto3 default (empty bytes, status OK code 0) are written explicitly where the spec's response semantics need them (status, ttl), matching go-libp2p's encoder, and omitted elsewhere.

decodeRendezvousMessage :: ByteString -> Either ParseError RendezvousMessage Source #

Decode a rendezvous message from protobuf wire format.

The type discriminator (field 1) selects which submessage shape is parsed; a message whose discriminator names no known type is a decode error rather than a partial guess.

maxRendezvousMessageSize :: Int Source #

Maximum rendezvous message size: 1 MiB. Signed peer records are a few hundred bytes; registrations are bounded by the discovery limits (1000 per query by recommendation), so 1 MiB is far above anything legitimate. Rejecting larger messages before reading the payload keeps a hostile peer from pinning memory.