Skip to content

feat: add dynamic direct peer management#541

Open
lodekeeper wants to merge 3 commits intoChainSafe:masterfrom
lodekeeper:feat/dynamic-direct-peers
Open

feat: add dynamic direct peer management#541
lodekeeper wants to merge 3 commits intoChainSafe:masterfrom
lodekeeper:feat/dynamic-direct-peers

Conversation

@lodekeeper
Copy link

@lodekeeper lodekeeper commented Feb 4, 2026

Description

Add methods to dynamically manage direct peers at runtime, enabling applications to add/remove trusted peers without restarting.

New Methods

  • addDirectPeer(peerId: PeerId, addrs: Multiaddr[]): Promise<PeerIdStr | null> - Add a peer to the direct peers set
  • removeDirectPeer(peerId: PeerId | string): boolean - Remove a peer from direct peers
  • getDirectPeers(): PeerIdStr[] - Get current direct peer IDs

Behavior

  • Self-check: Prevents adding self as a direct peer
  • Addresses required: Direct peers need addresses for connection
  • Return value: Returns peer ID string on success, null on failure
  • Error handling: Catches peerStore errors gracefully
  • Auto-connect: Attempts to connect when gossipsub is running

Motivation

This aligns with the Lodestar implementation in ChainSafe/lodestar#8853, allowing runtime management of direct peers for:

  • Adding trusted peers discovered during operation
  • Removing misbehaving peers from the direct list
  • Hot-adding peers without downtime

Authored with AI assistance (Lodekeeper 🌟)

Add methods to add/remove direct peers at runtime without requiring
restart:

- addDirectPeer(peerId, addrs?): Add a peer to the direct peers set
- removeDirectPeer(peerId): Remove a peer from the direct peers set
- getDirectPeers(): Get list of current direct peer IDs

Direct peers maintain permanent mesh connections without GRAFT/PRUNE
negotiation. Previously they could only be configured at initialization
via the directPeers option.

This enables runtime management for:
- Adding trusted peers discovered during operation
- Removing misbehaving peers from the direct list
- Hot-adding bootstrap peers without downtime

Closes ChainSafe/lodestar#7559
- Add self-check to prevent adding self as direct peer
- Make addresses required (needed for connection)
- Return PeerIdStr | null instead of void
- Add try/catch around peerStore.merge
- Add to direct set only after addresses are stored
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant