| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
LibP2P.Protocol.GossipSub.Handler
Description
GossipSub Switch integration handler (Phase 10b).
Bridges the GossipSub Router with the Switch by: 1. Registering StreamHandlers for inbound GossipSub protocol versions 2. Providing a sendRPC callback that opens/reuses outbound streams 3. Managing lifecycle (heartbeat start/stop)
GossipSub maintains persistent bidirectional RPC streams, unlike Identify/Ping which are one-shot. Each peer has at most one cached outbound stream.
Synopsis
- data GossipSubNode = GossipSubNode {
- gsnRouter :: !GossipSubRouter
- gsnSwitch :: !Switch
- gsnHeartbeat :: !(TVar (Maybe (Async ())))
- gsnStreams :: !(TVar (Map PeerId StreamIO))
- gsnStreamMeta :: !(TVar (Map PeerId GossipStreamMeta))
- gsnConnectHook :: !(IORef (Maybe (Connection -> IO ())))
- gsnDisconnectHook :: !(IORef (Maybe (Connection -> IO ())))
- gsnNotifierInstalled :: !(TVar Bool)
- gsnStarted :: !(TVar Bool)
- gsnLifecycleLock :: !(MVar ())
- gsnDirectSlots :: !DirectDialSlots
- gsnDirectConnect :: !(TVar (Maybe (Async ())))
- newGossipSubNode :: Switch -> GossipSubParams -> IO GossipSubNode
- handleGossipSubStream :: GossipSubNode -> StreamIO -> PeerId -> PeerProtocol -> Maybe ByteString -> IO ()
- sendCurrentSubscriptions :: GossipSubNode -> StreamIO -> IO ()
- startGossipSub :: GossipSubNode -> IO ()
- stopGossipSub :: GossipSubNode -> IO ()
- enablePartialMessages :: GossipSubNode -> PartialMessagesExtension state -> IO (Either PartialMessagesError ())
- gossipJoin :: GossipSubNode -> Topic -> IO ()
- gossipJoinPartial :: GossipSubNode -> Topic -> PartialTopicMode -> IO (Either PartialMessagesError ())
- gossipLeave :: GossipSubNode -> Topic -> IO ()
- gossipPublish :: GossipSubNode -> Topic -> ByteString -> IO ()
- gossipSubProtocolId :: ProtocolId
- gossipSubProtocolIdV12 :: ProtocolId
- gossipSubProtocolIdV11 :: ProtocolId
- gossipSubProtocolIdV10 :: ProtocolId
- floodSubProtocolId :: ProtocolId
Types
data GossipSubNode Source #
A GossipSub node: Router + Switch integration.
Constructors
| GossipSubNode | |
Fields
| |
Construction
newGossipSubNode :: Switch -> GossipSubParams -> IO GossipSubNode Source #
Create a new GossipSub node with a Router wired to the Switch.
The Router's gsSendRPC callback opens/reuses outbound streams to peers via the Switch's connection pool. Accepted peer-exchange records are stored and dialed through the same Switch (#247).
Stream handling
handleGossipSubStream :: GossipSubNode -> StreamIO -> PeerId -> PeerProtocol -> Maybe ByteString -> IO () Source #
Handle an inbound GossipSub stream.
Reads framed RPCs in a loop and dispatches each to the Router's handleRPC. The peer's negotiated protocol version gates v1.1 control extensions. On error or EOF, removes the inbound peer state. The independent outbound stream stays cached until its own loop fails or its connection closes.
sendCurrentSubscriptions :: GossipSubNode -> StreamIO -> IO () Source #
Send current topic subscriptions to a newly connected peer. This ensures peers joining after we've already subscribed still learn about our subscriptions (standard GossipSub behavior). Writes directly to the stream to avoid any routing issues.
Lifecycle
startGossipSub :: GossipSubNode -> IO () Source #
Start the GossipSub node: register stream handler, notifier, and start heartbeat.
Idempotent. A second start does not stack connection notifiers or
heartbeats. stopGossipSub clears the notifier body so a later
connection does not open a stream (#283).
stopGossipSub :: GossipSubNode -> IO () Source #
Stop the GossipSub node: disable the connection notifier, cancel heartbeat, and unregister handlers.
The wrapper stays on swNotifiers but reads a cleared hook, so a
connection established after stop does not open a stream or call
addPeer. A later start re-arms the same wrapper (#283).
Convenience API
enablePartialMessages :: GossipSubNode -> PartialMessagesExtension state -> IO (Either PartialMessagesError ()) Source #
Enable partial messages before starting the node.
gossipJoin :: GossipSubNode -> Topic -> IO () Source #
Subscribe to a topic.
gossipJoinPartial :: GossipSubNode -> Topic -> PartialTopicMode -> IO (Either PartialMessagesError ()) Source #
Subscribe while supporting or requesting partial messages.
gossipLeave :: GossipSubNode -> Topic -> IO () Source #
Unsubscribe from a topic.
gossipPublish :: GossipSubNode -> Topic -> ByteString -> IO () Source #
Publish a message to a topic (signed with the Switch's identity key).
Constants
gossipSubProtocolId :: ProtocolId Source #
GossipSub v1.3 protocol ID (preferred).
gossipSubProtocolIdV12 :: ProtocolId Source #
GossipSub v1.2 protocol ID, retained for backwards compatibility.
gossipSubProtocolIdV11 :: ProtocolId Source #
GossipSub v1.1 protocol ID, retained for backwards compatibility.
gossipSubProtocolIdV10 :: ProtocolId Source #
GossipSub v1.0 protocol ID, retained for backwards compatibility.
floodSubProtocolId :: ProtocolId Source #
FloodSub protocol ID, advertised alongside the meshsub protocols so that floodsub-only peers still get a pubsub stream (#157, gossipsub-v1.0.md "Compatibility with FloodSub").