feat(gossipsub): add addDirectPeer/removeDirectPeer/getDirectPeers - #3529
Open
paschal533 wants to merge 12 commits into
Open
feat(gossipsub): add addDirectPeer/removeDirectPeer/getDirectPeers#3529paschal533 wants to merge 12 commits into
paschal533 wants to merge 12 commits into
Conversation
Add addDirectPeer, removeDirectPeer, and getDirectPeers methods to the GossipSub class and its public interface, enabling applications to manage direct peers at runtime without restarting gossipsub. Direct peers bypass GRAFT/PRUNE negotiation and maintain permanent mesh connections. This mirrors the Lodestar implementation (ChainSafe/lodestar#8853) and ports the design from ChainSafe/js-libp2p-gossipsub#541 into the monorepo. Also adds a benchmark module type declaration to fix a pre-existing TypeScript build error that was blocking the test runner.
…agement Benchmarks measure: - BEFORE vs AFTER: stop+reconstruct+start (6ms) vs addDirectPeer (1ms) — 6x speedup - addDirectPeer throughput: ~2813 ops/s - removeDirectPeer throughput: ~3.3M ops/s (pure Set.delete) - getDirectPeers throughput: ~1.3M ops/s (Array.from on Set) - Integration benchmark on a live 3-node connected network Demo (dist/test/demo-direct-peers.js) runs 11 end-to-end verification steps covering all edge cases and the integration scenario.
- Fix import order (external packages sorted alphabetically) - Replace .to.be.true/false/null with .to.equal() — required by @typescript-eslint/no-unused-expressions - Remove unused imports (connectPubsubNodes, StubbedInstance) - Replace console.log with process.stdout.write in benchmark - Remove demo-direct-peers.ts (replaced by benchmark coverage)
dozyio
approved these changes
Jun 24, 2026
Collaborator
|
Thanks @paschal533 - made a few changes. Hopefully will get this merged over the next few days |
wemeetagain
reviewed
Jul 22, 2026
| this.log('addDirectPeer: added %s', peerIdStr) | ||
|
|
||
| if (this.status.code === GossipStatusCode.started) { | ||
| this.connect(peerIdStr).catch((err) => { |
Member
There was a problem hiding this comment.
should we await this.connect?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ports ChainSafe/js-libp2p-gossipsub#541 into the monorepo, adding three new public methods that let applications manage direct peers at runtime — without restarting GossipSub.
Before this PR, the only way to change the
directPeersset was to stop GossipSub, construct a new instance with the updateddirectPeersconfig array, and start it again (~6 ms, all mesh state lost).After this PR, three methods handle it at runtime in ~1 ms with no disruption to existing mesh connections.
New API
Motivation
Implementation notes
addDirectPeerstores addresses viapeerStore.mergebefore adding tothis.direct, so a peerStore failure leaves the set clean.connect()is fire-and-forget (.catch()only) — consistent with the existingdirectConnect()heartbeat path.removeDirectPeerintentionally does not close the existing connection; it expires naturally through mesh pruning. This matches GossipSub spec behaviour.this.directremainspublic readonlyfor backwards compatibility with existing tests that read it directly.Benchmark results
addDirectPeer(new API)removeDirectPeergetDirectPeers(100-peer set)~6× faster than the restart approach for adding a single peer.
Files changed
packages/gossipsub/src/gossipsub.tspackages/gossipsub/src/index.tsGossipSubinterface + importspackages/gossipsub/test/direct-peers.spec.tspackages/gossipsub/test/benchmark/direct-peers.benchmark.tspackages/gossipsub/test/demo-direct-peers.tspackages/gossipsub/test/types.d.tsbenchmarkmodule type declaration (fixes pre-existing TS build error)Test plan
addDirectPeeradds peer to direct set and stores addresses in peerStoreaddDirectPeerattempts connection when GossipSub is runningaddDirectPeerreturnsnullwhen adding selfaddDirectPeerreturnsnullwhen no addresses providedaddDirectPeerdoes not connect when GossipSub is stopped (but still adds to set)removeDirectPeerremoves peer and returnstrue(PeerId form)removeDirectPeerremoves peer and returnstrue(string form)removeDirectPeerreturnsfalsefor non-membergetDirectPeersreturns all current entriesgetDirectPeersreflects removals immediately