Peer‑to‑peer (P2P) stack for Strata‑based systems.
This library provides a modular, feature‑gated LibP2P implementation that supports pub/sub (gossipsub), direct request/response, QUIC/TCP transports, and optional Kademlia DHT. It can run either with a built‑in signer (non‑BYOS) or with an application‑provided signer (BYOS).
- Modular architecture with clear feature flags.
- Signed envelopes for both gossip and request/response:
- Your app passes/receives raw bytes; the library wraps and verifies a signed JSON envelope (protocol/version/pubkey/timestamp/signature).
- BYOS setup/handshake also uses JSON.
- BYOS mode (bring your own signer) with an explicit handshake that exchanges application public keys and enforces an application‑key allowlist.
- Request/response framing uses raw bytes at the transport layer; the content is the signed JSON envelope.
- Optional Kademlia DHT.
- Configurable identifiers: protocol_name for identify/request-response (default
"/strata"), and gossipsub_topic (default"strata"). - Tunables:
envelope_max_age(drop stale envelopes; default 300s),max_clock_skew(reject future‑dated envelopes; default 0s),gossipsub_max_transmit_size(default 512 KiB), and logging viaRUST_LOG.
gossipsub— pub/sub messaging.request-response— direct request/response messaging.quic— QUIC transport (enabled by default).kad— Kademlia DHT (server mode).byos— Bring Your Own Signer: application‑key handshake + allowlist; messages are signed with your provided signer.
Default features: quic, gossipsub, request-response.
Non‑BYOS uses the transport keypair to sign messages transparently. Typical steps:
-
Generate a transport keypair (libp2p Ed25519).
-
Build
P2PConfig:- Set
transport_keypair. - Provide one or more
listening_addrs(e.g.,/ip4/127.0.0.1/udp/0/quic-v1or/ip4/127.0.0.1/tcp/0). - Optionally set
connect_topeers (multiaddrs). - Optional timeouts, buffer sizes, and limits (
envelope_max_age,max_clock_skew,gossipsub_max_transmit_size,request_max_bytes,response_max_bytes,handle_default_timeout). - Optional identifiers: override
protocol_name(default"/strata") andgossipsub_topic(default"strata").
- Set
-
Construct a swarm with either the in‑memory or default transport helper (in‑memory if your listening address is
/memory/*). -
Build the
P2Pinstance. -
Get handles:
CommandHandlefor query/connect/disconnect.GossipHandlefor pub/sub (ifgossipsub).ReqRespHandlefor request/response (ifrequest-response).
-
Spawn
p2p.listen()in a task. -
Send/receive:
- Gossip: send raw bytes via the gossip handle; you’ll receive raw bytes back from events.
- Req/Resp: send a request with raw bytes; handle incoming requests via events and reply with raw bytes using the provided one‑shot.
Notes:
- QUIC is preferred automatically if present in the address list; the dialer will fall back to TCP if QUIC fails (when both are provided).
- Envelopes older than envelope_max_age are dropped (default 300s). Gossip messages larger than
gossipsub_max_transmit_sizeare rejected (default 512 KiB). - Use
CommandHandle::is_connected/get_connected_peers/QueryP2PState::GetMyListeningAddressesfor runtime state.
BYOS lets your app control signing with its own keys and enforces an application‑key allowlist.
-
Implement the
ApplicationSignertrait:- Provide a
sign(&[u8]) -> [u8; 64]backed by your app private key(s).
- Provide a
-
Build
P2PConfig:- Set
app_public_key(the public half of your app signing key). - Set
transport_keypair(libp2p Ed25519). - Provide
listening_addrsand optionalconnect_to. - Optional identifiers and limits: override
protocol_name(default"/strata") andgossipsub_topic(default"strata"); tuneenvelope_max_age,max_clock_skew,gossipsub_max_transmit_size,request_max_bytes,response_max_bytes.
- Set
-
Supply an application allowlist (
Vecof application public keys) and your signer when callingP2P::from_config. -
Start
p2p.listen(); the setup handshake will exchange/verify application public keys and enforce the allowlist before allowing traffic. -
Use gossip/request‑response handles as in non‑BYOS; the library will sign/verify envelopes using your signer and the peer’s app public key.
- Enable the
kadfeature to include a Kademlia behaviour (server mode) for discovery. P2PConfigexposes akad_protocol_nameoption (defaults to v1).
Use cases:
- Dynamic peer discovery.
- Using
FindMultiaddresscommand in CommandHandle to find peer's multiaddresses by application public key ( or in case of non-BYOS by transport id ). Result of using such command will be in event channel in command handle.
Dual‑licensed under Apache‑2.0 and MIT.