Skip to content

feat(api): add runtime direct peer management endpoints#8853

Merged
nflaig merged 14 commits intoChainSafe:unstablefrom
lodekeeper:feat/add-direct-peer-api
Feb 5, 2026
Merged

feat(api): add runtime direct peer management endpoints#8853
nflaig merged 14 commits intoChainSafe:unstablefrom
lodekeeper:feat/add-direct-peer-api

Conversation

@lodekeeper
Copy link
Contributor

@lodekeeper lodekeeper commented Feb 3, 2026

Description

Add HTTP API endpoints to manage GossipSub direct peers at runtime, allowing operators to dynamically add/remove trusted peers without requiring a node restart.

New Endpoints

Method Endpoint Description
POST /eth/v1/lodestar/direct_peers?peer=<multiaddr|enr> Add a direct peer
DELETE /eth/v1/lodestar/direct_peers?peerId=<peer_id> Remove a direct peer
GET /eth/v1/lodestar/direct_peers List current direct peer IDs

Motivation

Direct peers maintain permanent mesh connections without GRAFT/PRUNE negotiation. Currently, they can only be configured via the --directPeers CLI flag at startup.

This PR enables runtime management which is useful for:

  • Adding trusted peers discovered during operation
  • Removing misbehaving peers from the direct list
  • Temporary mesh connections for debugging/testing
  • Hot-adding bootstrap peers without downtime

Implementation

  • Adds addDirectPeer, removeDirectPeer, getDirectPeers methods to Eth2Gossipsub class
  • Reuses existing parseDirectPeers() function for multiaddr/ENR parsing
  • Exposes through NetworkCore → Network → API layer
  • Follows existing patterns for Lodestar-specific debug endpoints
  • Throws ApiError(400) on invalid peer input (instead of returning null)
  • Prevents adding self as a direct peer with appropriate warning log

Error Handling

  • Invalid multiaddr/ENR format → ApiError(400) with descriptive message
  • Missing peer addresses → ApiError(400)
  • Adding self as direct peer → ApiError(400)

Testing

  • Existing directPeers.test.ts covers the parsing logic (11 tests passing)
  • Build and lint pass

Closes #7559


This PR was authored with AI assistance (Lodekeeper 🌟)

Add HTTP API endpoints to manage GossipSub direct peers at runtime:

- POST /eth/v1/lodestar/direct_peer?peer=<multiaddr|enr>
  Adds a direct peer. Accepts multiaddr with peer ID or ENR string.
  Returns {peerId: string | null}

- DELETE /eth/v1/lodestar/direct_peer?peerId=<peer_id>
  Removes a direct peer by peer ID.
  Returns {removed: boolean}

- GET /eth/v1/lodestar/direct_peers
  Lists current direct peer IDs.
  Returns string[]

Direct peers maintain permanent mesh connections without GRAFT/PRUNE
negotiation, making them useful for trusted peer relationships that
should persist regardless of scoring.

This enables operators to dynamically add/remove direct peers without
requiring a node restart, which is particularly useful for:
- Adding trusted peers discovered during operation
- Removing misbehaving peers from the direct list
- Temporary mesh connections for debugging/testing
@lodekeeper lodekeeper requested a review from a team as a code owner February 3, 2026 21:15
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @lodekeeper, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the operational flexibility of the Lodestar beacon node by providing HTTP API endpoints for runtime management of GossipSub direct peers. Previously, direct peers could only be configured at startup, limiting dynamic adjustments. With these changes, operators can now add, remove, and list trusted peers on the fly, which is crucial for adapting to changing network conditions, debugging, and improving overall network resilience without service interruptions.

Highlights

  • New API Endpoints for Direct Peer Management: Introduced three new HTTP API endpoints: POST /eth/v1/lodestar/direct_peer to add a peer, DELETE /eth/v1/lodestar/direct_peer to remove a peer, and GET /eth/v1/lodestar/direct_peers to list current direct peers.
  • Runtime Direct Peer Configuration: Enabled dynamic addition, removal, and listing of GossipSub direct peers at runtime, eliminating the need for node restarts for these operations.
  • Core Network Integration: Integrated direct peer management functionality across the network stack, from the Eth2Gossipsub class to NetworkCore, Network, and the API layer, ensuring consistent handling and exposure.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • packages/api/src/beacon/routes/lodestar.ts
    • Added addDirectPeer, removeDirectPeer, and getDirectPeers endpoint type definitions.
    • Defined routes for the new direct peer management endpoints, including their URL paths, HTTP methods, and request/response schemas.
  • packages/beacon-node/src/api/impl/lodestar/index.ts
    • Implemented the API handler functions for addDirectPeer, removeDirectPeer, and getDirectPeers, delegating the actual logic to the network service.
  • packages/beacon-node/src/network/core/networkCore.ts
    • Added addDirectPeer, removeDirectPeer, and getDirectPeers methods to the NetworkCore class, which forward calls to the GossipSub instance.
  • packages/beacon-node/src/network/core/networkCoreWorker.ts
    • Exposed the new direct peer management methods (addDirectPeer, removeDirectPeer, getDirectPeers) in the libp2pWorkerApi for inter-worker communication.
  • packages/beacon-node/src/network/core/networkCoreWorkerHandler.ts
    • Implemented the addDirectPeer, removeDirectPeer, and getDirectPeers methods in WorkerNetworkCore, utilizing the worker API.
  • packages/beacon-node/src/network/core/types.ts
    • Updated the INetworkCorePublic interface to include the new addDirectPeer, removeDirectPeer, and getDirectPeers methods.
  • packages/beacon-node/src/network/gossip/gossipsub.ts
    • Added a libp2p instance to the Eth2Gossipsub class and initialized it in the constructor.
    • Implemented the core logic for addDirectPeer, which parses the peer string (multiaddr or ENR), adds the peer ID to the direct set, and merges addresses into the libp2p.peerStore.
    • Implemented removeDirectPeer to remove a peer ID from the direct set.
    • Implemented getDirectPeers to return an array of currently configured direct peer IDs.
  • packages/beacon-node/src/network/network.ts
    • Added addDirectPeer, removeDirectPeer, and getDirectPeers methods to the Network class, delegating to the NetworkCore.
  • packages/beacon-node/test/e2e/network/onWorker/dataSerialization.test.ts
    • Extended the workerApiMethods test suite to include the new addDirectPeer, removeDirectPeer, and getDirectPeers methods, ensuring proper data serialization across the worker boundary.
Activity
  • The pull request was authored with AI assistance by lodekeeper.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable feature for managing direct peers at runtime through new API endpoints. The implementation is well-integrated across the API, network, and gossip layers, following existing patterns in the codebase. I have one suggestion to improve the robustness of the addDirectPeer implementation by properly handling an asynchronous operation, which will prevent potential race conditions.

Address review feedback - ensure addresses are stored before adding
peer to direct set to avoid potential race conditions.
@lodekeeper
Copy link
Contributor Author

Addressed review feedback in 9e45760:

  • Made addDirectPeer async and await peerStore.merge before adding to direct set
  • Returns null if address storage fails, avoiding potential race conditions

@lodekeeper
Copy link
Contributor Author

Simplified JSDoc comments in 9a7b843 👍

@nflaig
Copy link
Member

nflaig commented Feb 3, 2026

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces new HTTP API endpoints for managing GossipSub direct peers at runtime, which is a great feature for operators. The implementation is well-structured, propagating the changes from the API layer down to the network core. My review focuses on improving API consistency and ensuring the new logic is well-tested. I've suggested using plural endpoint names for consistency and adding unit tests for the new peer management functions in Eth2Gossipsub.

Consistent with REST conventions - all operations on the direct_peers
resource now use the plural form.
- Reject addDirectPeer if no addresses provided (cannot connect without them)
- Add DirectPeerInput type alias to clarify that parameter accepts multiaddr or ENR
wemeetagain
wemeetagain previously approved these changes Feb 4, 2026
Copy link
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

straightforward PR, lgtm

Added DirectPeerInput type to @lodestar/api routes.lodestar for API consumers.
Type is still defined in beacon-node for internal use until api package exports
are updated to allow direct imports from routes modules.
@nflaig
Copy link
Member

nflaig commented Feb 5, 2026

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable feature for runtime management of direct peers. The implementation is well-structured, following the existing layered architecture of the network stack and reusing existing parsing logic. The new API endpoints are RESTful and their server-side implementations are clean. I have one suggestion to add a sanity check to prevent a node from adding itself as a direct peer.

@lodekeeper lodekeeper force-pushed the feat/add-direct-peer-api branch from eaa6791 to 47a5e4f Compare February 5, 2026 12:17
nflaig
nflaig previously approved these changes Feb 5, 2026
Copy link
Member

@nflaig nflaig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - we can simplify changes in Lodestar later on if we decide to upstream the direct peer management ChainSafe/js-libp2p-gossipsub#541

@nflaig nflaig added this to the v1.40.0 milestone Feb 5, 2026
@nflaig nflaig merged commit 13ab628 into ChainSafe:unstable Feb 5, 2026
27 of 29 checks passed
@codecov
Copy link

codecov bot commented Feb 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.36%. Comparing base (6cfc0a8) to head (5221189).
⚠️ Report is 1 commits behind head on unstable.

Additional details and impacted files
@@            Coverage Diff            @@
##           unstable    #8853   +/-   ##
=========================================
  Coverage     52.36%   52.36%           
=========================================
  Files           848      848           
  Lines         63532    63532           
  Branches       4710     4710           
=========================================
  Hits          33268    33268           
  Misses        30195    30195           
  Partials         69       69           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wemeetagain
Copy link
Member

🎉 This PR is included in v1.40.0 🎉

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.

New HTTP endpoint to add trusted peer

3 participants