Skip to content

Commit d0463f7

Browse files
committed
chore(node/rpc): Refactor p2p rpc
1 parent 1cb1ce2 commit d0463f7

File tree

17 files changed

+480
-478
lines changed

17 files changed

+480
-478
lines changed

Cargo.lock

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/src/commands/net.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::flags::{GlobalArgs, MetricsArgs, P2PArgs, RpcArgs};
44
use clap::Parser;
5-
use kona_p2p::{NetRpcRequest, NetworkBuilder, NetworkRpc};
6-
use kona_rpc::{OpP2PApiServer, RpcConfig};
5+
use kona_p2p::{NetworkBuilder, P2pRpcRequest};
6+
use kona_rpc::{NetworkRpc, OpP2PApiServer, RpcConfig};
77
use tracing::{debug, info, warn};
88
use url::Url;
99

@@ -86,7 +86,7 @@ impl NetCommand {
8686
}
8787
_ = interval.tick() => {
8888
let (otx, mut orx) = tokio::sync::oneshot::channel();
89-
if let Err(e) = tx.send(NetRpcRequest::PeerCount(otx)).await {
89+
if let Err(e) = tx.send(P2pRpcRequest::PeerCount(otx)).await {
9090
warn!(target: "net", "Failed to send network rpc request: {:?}", e);
9191
continue;
9292
}

crates/node/p2p/Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ workspace = true
1919
# Kona
2020
kona-macros.workspace = true
2121
kona-genesis.workspace = true
22-
kona-rpc = { workspace = true, features = ["jsonrpsee", "reqwest", "std"] }
2322

2423
# Alloy
2524
alloy-rlp.workspace = true
@@ -50,10 +49,8 @@ tokio.workspace = true
5049
tracing.workspace = true
5150
thiserror.workspace = true
5251
lazy_static.workspace = true
53-
async-trait.workspace = true
5452
unsigned-varint.workspace = true
5553
rand = { workspace = true, features = ["thread_rng"] }
56-
jsonrpsee = { workspace = true, features = ["server"] }
5754
serde_json = { workspace = true, features = ["alloc"] }
5855
derive_more = { workspace = true, features = ["display", "deref", "debug"] }
5956

@@ -80,4 +77,4 @@ op-alloy-consensus = { workspace = true, features = ["arbitrary", "k256"] }
8077
[features]
8178
default = []
8279
metrics = ["dep:metrics", "libp2p/metrics"]
83-
arbitrary = ["dep:arbitrary", "alloy-primitives/arbitrary"]
80+
arbitrary = ["dep:arbitrary", "alloy-primitives/arbitrary"]

crates/node/p2p/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
#[macro_use]
1111
extern crate tracing;
1212

13-
#[cfg(feature = "metrics")]
1413
mod metrics;
15-
#[cfg(feature = "metrics")]
1614
pub use metrics::Metrics;
1715

1816
mod net;
1917
pub use net::{Broadcast, Config, Network, NetworkBuilder, NetworkBuilderError};
2018

2119
mod rpc;
22-
pub use rpc::{NetRpcRequest, NetworkRpc};
20+
pub use rpc::{
21+
Connectedness, Direction, GossipScores, P2pRpcRequest, PeerCount, PeerDump, PeerInfo,
22+
PeerScores, PeerStats, ReqRespScores, TopicScores,
23+
};
2324

2425
mod gossip;
2526
pub use gossip::{

crates/node/p2p/src/metrics/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ impl Metrics {
4040
/// This does two things:
4141
/// * Describes various metrics.
4242
/// * Initializes metrics to 0 so they can be queried immediately.
43+
#[cfg(feature = "metrics")]
4344
pub fn init() {
4445
Self::describe();
4546
Self::zero();
4647
}
4748

4849
/// Describes metrics used in [`kona_p2p`][crate].
50+
#[cfg(feature = "metrics")]
4951
pub fn describe() {
5052
metrics::describe_gauge!(Self::RPC_CALLS, "Calls made to the P2P RPC module");
5153
metrics::describe_gauge!(Self::GOSSIP_EVENT, "Gossip events received by the libp2p Swarm");
@@ -79,6 +81,7 @@ impl Metrics {
7981

8082
/// Initializes metrics to `0` so they can be queried immediately by consumers of prometheus
8183
/// metrics.
84+
#[cfg(feature = "metrics")]
8285
pub fn zero() {
8386
// RPC Calls
8487
kona_macros::set!(gauge, Self::RPC_CALLS, "method", "opp2p_self", 0);

crates/node/p2p/src/net/builder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::{path::PathBuf, time::Duration};
99
use tokio::sync::broadcast::Sender as BroadcastSender;
1010

1111
use crate::{
12-
Broadcast, Config, Discv5Builder, GossipDriverBuilder, NetRpcRequest, Network,
13-
NetworkBuilderError, PeerMonitoring, PeerScoreLevel, discv5::LocalNode,
12+
Broadcast, Config, Discv5Builder, GossipDriverBuilder, Network, NetworkBuilderError,
13+
P2pRpcRequest, PeerMonitoring, PeerScoreLevel, discv5::LocalNode,
1414
};
1515

1616
/// Constructs a [`Network`] for the OP Stack Consensus Layer.
@@ -25,7 +25,7 @@ pub struct NetworkBuilder {
2525
/// The [`RollupConfig`] only used to select which topic to publish blocks to.
2626
cfg: Option<RollupConfig>,
2727
/// A receiver for network RPC requests.
28-
rpc_recv: Option<tokio::sync::mpsc::Receiver<NetRpcRequest>>,
28+
rpc_recv: Option<tokio::sync::mpsc::Receiver<P2pRpcRequest>>,
2929
/// A broadcast sender for the unsafe block payloads.
3030
payload_tx: Option<BroadcastSender<OpNetworkPayloadEnvelope>>,
3131
/// A receiver for unsafe blocks to publish.
@@ -132,7 +132,7 @@ impl NetworkBuilder {
132132
}
133133

134134
/// Sets the rpc receiver for the [`crate::Network`].
135-
pub fn with_rpc_receiver(self, rpc_recv: tokio::sync::mpsc::Receiver<NetRpcRequest>) -> Self {
135+
pub fn with_rpc_receiver(self, rpc_recv: tokio::sync::mpsc::Receiver<P2pRpcRequest>) -> Self {
136136
Self { rpc_recv: Some(rpc_recv), ..self }
137137
}
138138

crates/node/p2p/src/net/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use tokio::{
1010
time::Duration,
1111
};
1212

13-
use crate::{Broadcast, Discv5Driver, GossipDriver, HandlerRequest, NetRpcRequest, NetworkBuilder};
13+
use crate::{Broadcast, Discv5Driver, GossipDriver, HandlerRequest, NetworkBuilder, P2pRpcRequest};
1414

1515
/// Network
1616
///
@@ -28,7 +28,7 @@ pub struct Network {
2828
///
2929
/// This is allowed to be optional since it may not be desirable
3030
/// run a networking stack with RPC access.
31-
pub(crate) rpc: Option<tokio::sync::mpsc::Receiver<NetRpcRequest>>,
31+
pub(crate) rpc: Option<tokio::sync::mpsc::Receiver<P2pRpcRequest>>,
3232
/// A channel to publish an unsafe block.
3333
pub(crate) publish_rx: Option<tokio::sync::mpsc::Receiver<OpNetworkPayloadEnvelope>>,
3434
/// The swarm instance.

crates/node/p2p/src/rpc/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
//! Contains RPC types specific to Kona's network stack.
1+
//! Contains RPC types specific to Kona's p2p stack.
22
33
mod request;
4-
pub use request::NetRpcRequest;
4+
pub use request::P2pRpcRequest;
55

6-
mod server;
7-
pub use server::NetworkRpc;
6+
mod types;
7+
pub use types::{
8+
Connectedness, Direction, GossipScores, PeerCount, PeerDump, PeerInfo, PeerScores, PeerStats,
9+
ReqRespScores, TopicScores,
10+
};

crates/node/p2p/src/rpc/request.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
//! Contains the network RPC request type.
1+
//! Contains the p2p RPC request type.
22
33
use crate::{Discv5Handler, GossipDriver};
44
use discv5::multiaddr::Protocol;
5-
use kona_rpc::PeerInfo;
65
use tokio::sync::oneshot::Sender;
76

8-
/// A network RPC Request.
7+
use super::types::{Connectedness, Direction, PeerInfo, PeerScores};
8+
9+
/// A p2p RPC Request.
910
#[derive(Debug)]
10-
pub enum NetRpcRequest {
11+
pub enum P2pRpcRequest {
1112
/// Returns [`PeerInfo`] for the [`crate::Network`].
1213
PeerInfo(Sender<PeerInfo>),
1314
/// Dumps the node's discovery table from the [`crate::Discv5Driver`].
@@ -18,7 +19,7 @@ pub enum NetRpcRequest {
1819
PeerCount(Sender<(Option<usize>, usize)>),
1920
}
2021

21-
impl NetRpcRequest {
22+
impl P2pRpcRequest {
2223
/// Handles the peer count request.
2324
pub fn handle(self, gossip: &GossipDriver, disc: &Discv5Handler) {
2425
match self {
@@ -71,21 +72,21 @@ impl NetRpcRequest {
7172
let node_id = enr.node_id().to_string();
7273

7374
// We need to add the local multiaddr to the list of known addresses.
74-
let peer_info = kona_rpc::PeerInfo {
75+
let peer_info = PeerInfo {
7576
peer_id: peer_id.to_string(),
7677
node_id,
7778
user_agent: "kona".to_string(),
7879
protocol_version: "1".to_string(),
7980
enr: enr.to_string(),
8081
addresses,
8182
protocols: None,
82-
connectedness: kona_rpc::Connectedness::Connected,
83-
direction: kona_rpc::Direction::Inbound,
83+
connectedness: Connectedness::Connected,
84+
direction: Direction::Inbound,
8485
protected: false,
8586
chain_id,
8687
latency: 0,
8788
gossip_blocks: true,
88-
peer_scores: kona_rpc::PeerScores::default(),
89+
peer_scores: PeerScores::default(),
8990
};
9091
if let Err(e) = sender.send(peer_info) {
9192
warn!("Failed to send peer info through response channel: {:?}", e);

0 commit comments

Comments
 (0)