| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Network.LibP2P.Protocol.GossipSub.Router
Description
GossipSub mesh management: JOIN, LEAVE, GRAFT/PRUNE, message forwarding.
The router manages the mesh overlay and handles inbound/outbound RPC messages. For testability, peer communication is injectable via gsSendRPC on GossipSubRouter.
Synopsis
- newRouter :: GossipSubParams -> PeerId -> (PeerId -> RPC -> IO ()) -> IO UTCTime -> IO GossipSubRouter
- addPeer :: GossipSubRouter -> PeerId -> PeerProtocol -> Bool -> UTCTime -> IO ()
- removePeer :: GossipSubRouter -> PeerId -> IO ()
- join :: GossipSubRouter -> Topic -> IO ()
- leave :: GossipSubRouter -> Topic -> IO ()
- publish :: GossipSubRouter -> Topic -> ByteString -> Maybe KeyPair -> IO ()
- handleRPC :: GossipSubRouter -> PeerId -> RPC -> IO ()
- handleGraft :: GossipSubRouter -> PeerId -> [Graft] -> IO ()
- handlePrune :: GossipSubRouter -> PeerId -> [Prune] -> IO ()
- handleIHave :: GossipSubRouter -> PeerId -> [IHave] -> IO ()
- handleIWant :: GossipSubRouter -> PeerId -> [IWant] -> IO ()
- handleSubscriptions :: GossipSubRouter -> PeerId -> [SubOpts] -> IO ()
- forwardMessage :: GossipSubRouter -> PeerId -> PubSubMessage -> IO ()
- peerScore :: GossipSubRouter -> PeerId -> IO Double
Construction
Arguments
| :: GossipSubParams | |
| -> PeerId | |
| -> (PeerId -> RPC -> IO ()) | RPC sender |
| -> IO UTCTime | Time source |
| -> IO GossipSubRouter |
Create a new GossipSub router with empty state.
Peer management
addPeer :: GossipSubRouter -> PeerId -> PeerProtocol -> Bool -> UTCTime -> IO () Source #
Register a connected peer. If the peer already exists, preserves accumulated state (topics, scores) to avoid overwriting subscriptions.
removePeer :: GossipSubRouter -> PeerId -> IO () Source #
Remove a disconnected peer and clean up mesh/fanout membership.
Topic subscription
join :: GossipSubRouter -> Topic -> IO () Source #
Subscribe to a topic (JOIN): announce, fanout→mesh transition, fill to D, GRAFT.
leave :: GossipSubRouter -> Topic -> IO () Source #
Unsubscribe from a topic (LEAVE): announce, PRUNE with backoff, delete mesh.
Publishing
publish :: GossipSubRouter -> Topic -> ByteString -> Maybe KeyPair -> IO () Source #
Publish a message to a topic. In StrictSign mode, signs the message and populates fromseqnosignature/key. With FloodPublish=True (default), sends to ALL topic peers above PublishThreshold. Otherwise, sends via mesh (or fanout if not subscribed).
Inbound RPC handling
Control message handlers
handleGraft :: GossipSubRouter -> PeerId -> [Graft] -> IO () Source #
Handle GRAFT: accept if subscribed, non-negative score, and no backoff.
handlePrune :: GossipSubRouter -> PeerId -> [Prune] -> IO () Source #
Handle PRUNE: remove from mesh and start backoff.
handleIHave :: GossipSubRouter -> PeerId -> [IHave] -> IO () Source #
Handle IHAVE: request unseen messages via IWANT.
handleIWant :: GossipSubRouter -> PeerId -> [IWant] -> IO () Source #
Handle IWANT: respond with cached messages from the message cache.
handleSubscriptions :: GossipSubRouter -> PeerId -> [SubOpts] -> IO () Source #
Handle subscription changes from a peer.
Message forwarding
forwardMessage :: GossipSubRouter -> PeerId -> PubSubMessage -> IO () Source #
Forward a message to mesh peers for its topic, excluding the sender.