Add Gossipsub direct peer support - #211
Conversation
There was a problem hiding this comment.
Pull request overview
Adds Gossipsub “direct peer” support to the pubsub router, enabling explicit peering (reciprocal direct connections) with special routing rules (direct forwarding + isolation from mesh/fanout/gossip).
Changes:
- Introduces direct-peer configuration (addresses + reconnect period) and validates configuration at router startup.
- Updates publish/forwarding logic to send subscribed-topic messages directly to configured direct peers while excluding them from mesh/fanout/gossip selection and graylisting behavior.
- Rejects direct-peer GRAFTs with PRUNE and adds unit tests covering delivery, mesh isolation, startup dialing, and configuration validation.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libp2p/Libp2p.Protocols.Pubsub/PubSubSettings.cs | Adds DirectPeers + DirectConnectPeriod settings for explicit peering and reconnect cadence. |
| src/libp2p/Libp2p.Protocols.Pubsub/PubsubRouter.Topics.cs | Publishes directly to subscribed direct peers and excludes direct peers from fanout selection/publish paths. |
| src/libp2p/Libp2p.Protocols.Pubsub/PubsubRouter.Rpc.cs | Forwards valid messages to direct peers, skips graylist for direct peers, and rejects direct-peer GRAFT with PRUNE. |
| src/libp2p/Libp2p.Protocols.Pubsub/PubsubRouter.cs | Builds direct-peer map, discovers configured direct peers at startup, and periodically reconnects disconnected direct peers. |
| src/libp2p/Libp2p.Protocols.Pubsub.Tests/DirectPeersTests.cs | Adds tests for direct peer forwarding, mesh isolation, GRAFT rejection, startup dialing, and config validation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
859154c to
105400f
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Suppressed comments (1)
src/libp2p/Libp2p.Protocols.Pubsub/PubsubRouter.cs:177
- The
ArgumentOutOfRangeExceptionforDirectConnectPeriodusesnameof(settings)as the parameter name, which makes the exception less actionable for callers (it points at the whole settings object rather than the specific property).
if (_settings.DirectConnectPeriod <= 0)
{
throw new ArgumentOutOfRangeException(nameof(settings), "DirectConnectPeriod must be positive.");
}
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Suppressed comments (2)
Previously missed (2) — in code that hasn't changed since the last review.
src/libp2p/Libp2p.Protocols.Pubsub/PubSubSettings.cs:34
- The XML doc for DirectConnectPeriod doesn't state the units. Since the value is an int and used with AddMilliseconds(), it should explicitly document that this is milliseconds to avoid misconfiguration.
/// <summary>
/// Interval for reconnecting disconnected direct peers. Gossipsub recommends
/// five minutes.
/// </summary>
public int DirectConnectPeriod { get; set; } = 5 * 60 * 1000;
src/libp2p/Libp2p.Protocols.Pubsub/PubsubRouter.cs:212
- Direct peer reconnect timing is currently driven by the LoopReconnect cadence (ReconnectionPeriod), which means DirectConnectPeriod won't be respected if ReconnectionPeriod is larger. Since ReconnectDirectPeers() already self-throttles via nextDirectConnectionAttempt, consider calling it from the heartbeat loop as well so the configured DirectConnectPeriod is honored independently.
nextDirectConnectionAttempt = DateTime.UtcNow.AddMilliseconds(_settings.DirectConnectPeriod);
_ = Task.Run(LoopHeartbeat, token);
_ = Task.Run(LoopReconnect, token);
6f1c889 to
ead5428
Compare
ead5428 to
72d6b84
Compare
cbbc9d0 to
4ccdad0
Compare
Summary
Validation