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

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

Construction

newRouter Source #

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

handleRPC :: GossipSubRouter -> PeerId -> RPC -> IO () Source #

Handle an inbound RPC from a peer.

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.

Scoring

peerScore :: GossipSubRouter -> PeerId -> IO Double Source #

Compute peer score using Score.computeScore (P1-P4, P6, P7). P5 (application-specific) is not included here.