Skip to content

chore(node/rpc): Refactor p2p rpc #1633

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bin/node/src/commands/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::flags::{GlobalArgs, MetricsArgs, P2PArgs, RpcArgs};
use clap::Parser;
use kona_p2p::{NetRpcRequest, NetworkBuilder, NetworkRpc};
use kona_rpc::{OpP2PApiServer, RpcConfig};
use kona_p2p::{NetworkBuilder, P2pRpcRequest};
use kona_rpc::{NetworkRpc, OpP2PApiServer, RpcConfig};
use tracing::{debug, info, warn};
use url::Url;

Expand Down Expand Up @@ -86,7 +86,7 @@ impl NetCommand {
}
_ = interval.tick() => {
let (otx, mut orx) = tokio::sync::oneshot::channel();
if let Err(e) = tx.send(NetRpcRequest::PeerCount(otx)).await {
if let Err(e) = tx.send(P2pRpcRequest::PeerCount(otx)).await {
warn!(target: "net", "Failed to send network rpc request: {:?}", e);
continue;
}
Expand Down
5 changes: 1 addition & 4 deletions crates/node/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ workspace = true
# Kona
kona-macros.workspace = true
kona-genesis.workspace = true
kona-rpc = { workspace = true, features = ["jsonrpsee", "reqwest", "std"] }

# Alloy
alloy-rlp.workspace = true
Expand Down Expand Up @@ -50,10 +49,8 @@ tokio.workspace = true
tracing.workspace = true
thiserror.workspace = true
lazy_static.workspace = true
async-trait.workspace = true
unsigned-varint.workspace = true
rand = { workspace = true, features = ["thread_rng"] }
jsonrpsee = { workspace = true, features = ["server"] }
serde_json = { workspace = true, features = ["alloc"] }
derive_more = { workspace = true, features = ["display", "deref", "debug"] }

Expand All @@ -80,4 +77,4 @@ op-alloy-consensus = { workspace = true, features = ["arbitrary", "k256"] }
[features]
default = []
metrics = ["dep:metrics", "libp2p/metrics"]
arbitrary = ["dep:arbitrary", "alloy-primitives/arbitrary"]
arbitrary = ["dep:arbitrary", "alloy-primitives/arbitrary"]
7 changes: 4 additions & 3 deletions crates/node/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
#[macro_use]
extern crate tracing;

#[cfg(feature = "metrics")]
mod metrics;
#[cfg(feature = "metrics")]
pub use metrics::Metrics;

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

mod rpc;
pub use rpc::{NetRpcRequest, NetworkRpc};
pub use rpc::{
Connectedness, Direction, GossipScores, P2pRpcRequest, PeerCount, PeerDump, PeerInfo,
PeerScores, PeerStats, ReqRespScores, TopicScores,
};

mod gossip;
pub use gossip::{
Expand Down
3 changes: 3 additions & 0 deletions crates/node/p2p/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ impl Metrics {
/// This does two things:
/// * Describes various metrics.
/// * Initializes metrics to 0 so they can be queried immediately.
#[cfg(feature = "metrics")]
pub fn init() {
Self::describe();
Self::zero();
}

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

/// Initializes metrics to `0` so they can be queried immediately by consumers of prometheus
/// metrics.
#[cfg(feature = "metrics")]
pub fn zero() {
// RPC Calls
kona_macros::set!(gauge, Self::RPC_CALLS, "method", "opp2p_self", 0);
Expand Down
8 changes: 4 additions & 4 deletions crates/node/p2p/src/net/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use tokio::sync::broadcast::Sender as BroadcastSender;

use crate::{
Broadcast, Config, Discv5Builder, GossipDriverBuilder, NetRpcRequest, Network,
NetworkBuilderError, PeerMonitoring, PeerScoreLevel, discv5::LocalNode,
Broadcast, Config, Discv5Builder, GossipDriverBuilder, Network, NetworkBuilderError,
P2pRpcRequest, PeerMonitoring, PeerScoreLevel, discv5::LocalNode,
};

/// Constructs a [`Network`] for the OP Stack Consensus Layer.
Expand All @@ -25,7 +25,7 @@
/// The [`RollupConfig`] only used to select which topic to publish blocks to.
cfg: Option<RollupConfig>,
/// A receiver for network RPC requests.
rpc_recv: Option<tokio::sync::mpsc::Receiver<NetRpcRequest>>,
rpc_recv: Option<tokio::sync::mpsc::Receiver<P2pRpcRequest>>,
/// A broadcast sender for the unsafe block payloads.
payload_tx: Option<BroadcastSender<OpNetworkPayloadEnvelope>>,
/// A receiver for unsafe blocks to publish.
Expand Down Expand Up @@ -132,7 +132,7 @@
}

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

Check warning on line 135 in crates/node/p2p/src/net/builder.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L135 was not covered by tests
Self { rpc_recv: Some(rpc_recv), ..self }
}

Expand Down
4 changes: 2 additions & 2 deletions crates/node/p2p/src/net/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::{
time::Duration,
};

use crate::{Broadcast, Discv5Driver, GossipDriver, HandlerRequest, NetRpcRequest, NetworkBuilder};
use crate::{Broadcast, Discv5Driver, GossipDriver, HandlerRequest, NetworkBuilder, P2pRpcRequest};

/// Network
///
Expand All @@ -28,7 +28,7 @@ pub struct Network {
///
/// This is allowed to be optional since it may not be desirable
/// run a networking stack with RPC access.
pub(crate) rpc: Option<tokio::sync::mpsc::Receiver<NetRpcRequest>>,
pub(crate) rpc: Option<tokio::sync::mpsc::Receiver<P2pRpcRequest>>,
/// A channel to publish an unsafe block.
pub(crate) publish_rx: Option<tokio::sync::mpsc::Receiver<OpNetworkPayloadEnvelope>>,
/// The swarm instance.
Expand Down
11 changes: 7 additions & 4 deletions crates/node/p2p/src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Contains RPC types specific to Kona's network stack.
//! Contains RPC types specific to Kona's p2p stack.

mod request;
pub use request::NetRpcRequest;
pub use request::P2pRpcRequest;

mod server;
pub use server::NetworkRpc;
mod types;
pub use types::{
Connectedness, Direction, GossipScores, PeerCount, PeerDump, PeerInfo, PeerScores, PeerStats,
ReqRespScores, TopicScores,
};
19 changes: 10 additions & 9 deletions crates/node/p2p/src/rpc/request.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Contains the network RPC request type.
//! Contains the p2p RPC request type.

use crate::{Discv5Handler, GossipDriver};
use discv5::multiaddr::Protocol;
use kona_rpc::PeerInfo;
use tokio::sync::oneshot::Sender;

/// A network RPC Request.
use super::types::{Connectedness, Direction, PeerInfo, PeerScores};

/// A p2p RPC Request.
#[derive(Debug)]
pub enum NetRpcRequest {
pub enum P2pRpcRequest {
/// Returns [`PeerInfo`] for the [`crate::Network`].
PeerInfo(Sender<PeerInfo>),
/// Dumps the node's discovery table from the [`crate::Discv5Driver`].
Expand All @@ -18,7 +19,7 @@
PeerCount(Sender<(Option<usize>, usize)>),
}

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

// We need to add the local multiaddr to the list of known addresses.
let peer_info = kona_rpc::PeerInfo {
let peer_info = PeerInfo {

Check warning on line 75 in crates/node/p2p/src/rpc/request.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L75 was not covered by tests
peer_id: peer_id.to_string(),
node_id,
user_agent: "kona".to_string(),
protocol_version: "1".to_string(),
enr: enr.to_string(),
addresses,
protocols: None,
connectedness: kona_rpc::Connectedness::Connected,
direction: kona_rpc::Direction::Inbound,
connectedness: Connectedness::Connected,
direction: Direction::Inbound,

Check warning on line 84 in crates/node/p2p/src/rpc/request.rs

View check run for this annotation

Codecov / codecov/patch

crates/node/p2p/src/rpc/request.rs#L83-L84

Added lines #L83 - L84 were not covered by tests
protected: false,
chain_id,
latency: 0,
gossip_blocks: true,
peer_scores: kona_rpc::PeerScores::default(),
peer_scores: PeerScores::default(),

Check warning on line 89 in crates/node/p2p/src/rpc/request.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L89 was not covered by tests
};
if let Err(e) = sender.send(peer_info) {
warn!("Failed to send peer info through response channel: {:?}", e);
Expand Down
Loading
Loading