Skip to content

feat(l1): add rpc endpoint admin_peers #2732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

feat(l1): add rpc endpoint admin_peers #2732

wants to merge 16 commits into from

Conversation

fmoletta
Copy link
Contributor

@fmoletta fmoletta commented May 9, 2025

Motivation
Support rpc endpoint admin_peers

Description

  • Add rpc endpoint admin_peers
  • Track inbound connections
  • Store peer node version when starting a connection
  • Add peer_handler: PeerHandler field to RpcContext so we can access peers from the rpc
  • (Misc) Syncer & SyncManager now receive a PeerHandler upon creation instead of a KademliaTable
  • (Misc) Fix common typo across the project

Data missing compared to geth implementation:

  • The local address of each connection
  • Whether a connection is trusted, static (we have no notion of this yet)

Closes #2671

Copy link

github-actions bot commented May 9, 2025

Lines of code report

Total lines added: 151
Total lines removed: 4
Total lines changed: 155

Detailed view
+-------------------------------------------------+-------+------+
| File                                            | Lines | Diff |
+-------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/initializers.rs               | 373   | +3   |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/kademlia.rs        | 495   | +4   |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/peer_handler.rs    | 557   | +14  |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/rlpx/connection.rs | 543   | +6   |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/rlpx/p2p.rs        | 269   | +9   |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/sync.rs            | 570   | -4   |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/types.rs           | 408   | +2   |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/rpc/admin/mod.rs       | 53    | +2   |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/rpc/admin/peers.rs     | 103   | +103 |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/rpc/rpc.rs             | 629   | +5   |
+-------------------------------------------------+-------+------+
| ethrex/crates/networking/rpc/utils.rs           | 408   | +3   |
+-------------------------------------------------+-------+------+

@@ -170,6 +173,7 @@ pub async fn start_api(
active_filters: active_filters.clone(),
syncer: Arc::new(syncer),
client_version,
peer_handler,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we refactor this to group a bunch of p2p related concepts like local_p2p_node, local_node_record , peer_handler , maybe syncer into something like Node or similar? Maybe out of scope of this PR, but RpcApiContext is like a bag were we just throw random things, would be nice to be a bit more thoughtful in this regard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think syncer and peer handler should remain separate from the rest as they have their own api and are not used in conjunction. I think readable data such as jwt_secret and local node data could be grouped together

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ill do this in a separate PR to be cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR opened #2752

Copy link
Contributor

Choose a reason for hiding this comment

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

Since the PR has been merged, the peer handler should be in the Node now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NodeData contains static data about our node, while the PeerHandler actually has its own behaviour and has nothing to do with the data stored in NodeData, that's why Id rather keep them separate

github-merge-queue bot pushed a commit that referenced this pull request May 13, 2025
**Motivation**
`RpcContext` has become quite extensive lately, so we need to group some
of its fields into individual structures that hold data used for similar
purposes. This PR groups static data related to the node (such as p2p
address, version, etc) into a `NodeData` struct in order to simplify it.

**Description**
* Group static data about the node held in `RpcContext` into `NodeData`
* (Misc) Remove duplicated code between test functions
<!-- A clear and concise general description of the changes this PR
introduces -->

<!-- Link to issues: Resolves #111, Resolves #222 -->

Addresses review comment:
#2732 (comment)
fmoletta added a commit that referenced this pull request May 15, 2025
**Motivation**
`RpcContext` has become quite extensive lately, so we need to group some
of its fields into individual structures that hold data used for similar
purposes. This PR groups static data related to the node (such as p2p
address, version, etc) into a `NodeData` struct in order to simplify it.

**Description**
* Group static data about the node held in `RpcContext` into `NodeData`
* (Misc) Remove duplicated code between test functions
<!-- A clear and concise general description of the changes this PR
introduces -->

<!-- Link to issues: Resolves #111, Resolves #222 -->

Addresses review comment:
#2732 (comment)
@fmoletta fmoletta marked this pull request as ready for review May 15, 2025 20:17
@fmoletta fmoletta requested a review from a team as a code owner May 15, 2025 20:17
Copy link

Benchmark for fd726ec

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 36.3±0.77ms 36.5±0.71ms +0.55%
Trie/cita-trie insert 1k 3.6±0.05ms 3.8±0.04ms +5.56%
Trie/ethrex-trie insert 10k 127.9±1.66ms 129.9±3.30ms +1.56%
Trie/ethrex-trie insert 1k 11.0±0.06ms 11.3±0.21ms +2.73%

Copy link

Benchmark for 7d331aa

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 41.0±2.15ms 40.0±2.33ms -2.44%
Trie/cita-trie insert 1k 3.6±0.04ms 3.7±0.04ms +2.78%
Trie/ethrex-trie insert 10k 134.8±1.96ms 134.5±3.47ms -0.22%
Trie/ethrex-trie insert 1k 11.2±0.11ms 11.2±0.13ms 0.00%

Copy link

Benchmark for e70f8b2

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 39.6±1.88ms 37.3±1.73ms -5.81%
Trie/cita-trie insert 1k 3.8±0.05ms 3.6±0.04ms -5.26%
Trie/ethrex-trie insert 10k 133.3±2.35ms 137.9±3.53ms +3.45%
Trie/ethrex-trie insert 1k 11.1±0.07ms 11.2±0.12ms +0.90%

Copy link
Contributor

@fedacking fedacking left a comment

Choose a reason for hiding this comment

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

We should integrate the peer table into the Node. Rest looks good

@@ -170,6 +173,7 @@ pub async fn start_api(
active_filters: active_filters.clone(),
syncer: Arc::new(syncer),
client_version,
peer_handler,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the PR has been merged, the peer handler should be in the Node now

) {
let peer = self.get_by_node_id_mut(node_id);
if let Some(peer) = peer {
peer.channels = Some(channels);
peer.supported_capabilities = capabilities;
peer.is_connected = true;
peer.is_connection_inbound = inbound;
} else {
debug!(
"[PEERS] Peer with node_id {:?} not found in the kademlia table when trying to init backend communication",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the debug information also include if it's inbound?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks like a very edge case rather than something we want to actively look for


/// Creates a dummy PeerHandler for tests where interacting with peers is not needed
/// This should only be used in tests as it won't be able to interact with the node's connected peers
pub fn dummy() -> PeerHandler {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: keep this inside test flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to but the test feature doesn't behave well when importing across crates

Copy link
Collaborator

@mpaulucci mpaulucci left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@fedacking fedacking left a comment

Choose a reason for hiding this comment

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

LGTM

@fmoletta fmoletta enabled auto-merge May 16, 2025 22:11
Copy link

Benchmark for 700f068

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 38.1±1.40ms 38.4±1.92ms +0.79%
Trie/cita-trie insert 1k 3.6±0.04ms 3.7±0.03ms +2.78%
Trie/ethrex-trie insert 10k 131.4±1.52ms 133.0±3.89ms +1.22%
Trie/ethrex-trie insert 1k 11.2±0.06ms 11.2±0.08ms 0.00%

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.

Add admin_peers rpc endpoint
3 participants