Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

12 changes: 11 additions & 1 deletion crates/node/p2p/src/rpc/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use libp2p::PeerId;
use tokio::sync::oneshot::Sender;

use libp2p::gossipsub::TopicHash;
use libp2p::{Multiaddr, gossipsub::TopicHash};

use super::{
PeerDump, PeerStats,
Expand All @@ -40,6 +40,11 @@
/// Whether to only return connected peers.
connected: bool,
},
/// Request to connect to a given peer.
ConnectPeer {
/// The [`Multiaddr`] of the peer to connect to.
address: Multiaddr,
},
/// Request to disconnect the specified peer.
DisconnectPeer {
/// The peer id to disconnect.
Expand All @@ -64,9 +69,14 @@
Self::Peers { out, connected } => Self::handle_peers(out, connected, gossip, disc),
Self::DisconnectPeer { peer_id } => Self::disconnect_peer(peer_id, gossip),
Self::PeerStats(s) => Self::handle_peer_stats(s, gossip, disc),
Self::ConnectPeer { address } => Self::connect_peer(address, gossip),

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L72 was not covered by tests
}
}

fn connect_peer(address: Multiaddr, gossip: &mut GossipDriver) {
gossip.dial_multiaddr(address)
}

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

View check run for this annotation

Codecov / codecov/patch

crates/node/p2p/src/rpc/request.rs#L76-L78

Added lines #L76 - L78 were not covered by tests

fn disconnect_peer(peer_id: PeerId, gossip: &mut GossipDriver) {
if let Err(e) = gossip.swarm.disconnect_peer_id(peer_id) {
warn!(target: "p2p::rpc", "Failed to disconnect peer {}: {:?}", peer_id, e);
Expand Down
1 change: 1 addition & 0 deletions crates/node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ alloy-eips = { workspace = true, features = ["serde", "std"] }
alloy-primitives = { workspace = true, features = ["map", "rlp", "serde", "std"] }

# Misc
libp2p.workspace = true
tracing.workspace = true
thiserror.workspace = true
derive_more = { workspace = true, default-features = false, features = [
Expand Down
25 changes: 23 additions & 2 deletions crates/node/rpc/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,14 @@
}

async fn opp2p_connect_peer(&self, _peer: String) -> RpcResult<()> {
use std::str::FromStr;
kona_macros::inc!(gauge, kona_p2p::Metrics::RPC_CALLS, "method" => "opp2p_connectPeer");
// Method not supported yet.
Err(ErrorObject::from(ErrorCode::MethodNotFound))
let ma = libp2p::Multiaddr::from_str(&_peer)
.map_err(|_| ErrorObject::from(ErrorCode::InvalidParams))?;
self.sender
.send(P2pRpcRequest::ConnectPeer { address: ma })
.await
.map_err(|_| ErrorObject::from(ErrorCode::InternalError))

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

View check run for this annotation

Codecov / codecov/patch

crates/node/rpc/src/p2p.rs#L148-L153

Added lines #L148 - L153 were not covered by tests
}

async fn opp2p_disconnect_peer(&self, peer_id: String) -> RpcResult<()> {
Expand All @@ -163,3 +168,19 @@
.map_err(|_| ErrorObject::from(ErrorCode::InternalError))
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_parse_multiaddr_string() {
use std::str::FromStr;
let ma = "/ip4/127.0.0.1/udt";
let multiaddr = libp2p::Multiaddr::from_str(ma).unwrap();
let components = multiaddr.iter().collect::<Vec<_>>();
assert_eq!(
components[0],
libp2p::multiaddr::Protocol::Ip4(std::net::Ipv4Addr::new(127, 0, 0, 1))
);
assert_eq!(components[1], libp2p::multiaddr::Protocol::Udt);
}
}
Loading