feat: add configure network function to cli ext (#5536)

This commit is contained in:
Matthias Seitz
2023-11-22 20:58:02 +01:00
committed by GitHub
parent 1aa4ae8c6d
commit 5e2affb15a
8 changed files with 133 additions and 37 deletions

View File

@ -28,6 +28,21 @@ impl<C, Tx, Eth> NetworkBuilder<C, Tx, Eth> {
(network, transactions, request_handler)
}
/// Returns the network manager.
pub fn network(&self) -> &NetworkManager<C> {
&self.network
}
/// Returns the mutable network manager.
pub fn network_mut(&mut self) -> &mut NetworkManager<C> {
&mut self.network
}
/// Returns the handle to the network.
pub fn handle(&self) -> NetworkHandle {
self.network.handle().clone()
}
/// Consumes the type and returns all fields and also return a [`NetworkHandle`].
pub fn split_with_handle(self) -> (NetworkHandle, NetworkManager<C>, Tx, Eth) {
let NetworkBuilder { network, transactions, request_handler } = self;

View File

@ -26,6 +26,7 @@ use crate::{
metrics::{DisconnectMetrics, NetworkMetrics, NETWORK_POOL_TRANSACTIONS_SCOPE},
network::{NetworkHandle, NetworkHandleMessage},
peers::{PeersHandle, PeersManager},
protocol::IntoRlpxSubProtocol,
session::SessionManager,
state::NetworkState,
swarm::{NetworkConnectionState, Swarm, SwarmEvent},
@ -142,6 +143,11 @@ impl<C> NetworkManager<C> {
self.to_eth_request_handler = Some(tx);
}
/// Adds an additional protocol handler to the RLPx sub-protocol list.
pub fn add_rlpx_sub_protocol(&mut self, protocol: impl IntoRlpxSubProtocol) {
self.swarm.add_rlpx_sub_protocol(protocol)
}
/// Returns the [`NetworkHandle`] that can be cloned and shared.
///
/// The [`NetworkHandle`] can be used to interact with this [`NetworkManager`]

View File

@ -116,6 +116,12 @@ where
}
}
impl IntoRlpxSubProtocol for RlpxSubProtocol {
fn into_rlpx_sub_protocol(self) -> RlpxSubProtocol {
self
}
}
/// Additional RLPx-based sub-protocols.
#[derive(Debug, Default)]
pub struct RlpxSubProtocols {

View File

@ -48,7 +48,7 @@ pub use handle::{
SessionCommand,
};
use crate::protocol::RlpxSubProtocols;
use crate::protocol::{IntoRlpxSubProtocol, RlpxSubProtocols};
pub use reth_network_api::{Direction, PeerInfo};
/// Internal identifier for active sessions.
@ -103,7 +103,6 @@ pub struct SessionManager {
/// Receiver half that listens for [`ActiveSessionMessage`] produced by pending sessions.
active_session_rx: ReceiverStream<ActiveSessionMessage>,
/// Additional RLPx sub-protocols to be used by the session manager.
#[allow(unused)]
extra_protocols: RlpxSubProtocols,
/// Used to measure inbound & outbound bandwidth across all managed streams
bandwidth_meter: BandwidthMeter,
@ -176,6 +175,11 @@ impl SessionManager {
self.hello_message.clone()
}
/// Adds an additional protocol handler to the RLPx sub-protocol list.
pub(crate) fn add_rlpx_sub_protocol(&mut self, protocol: impl IntoRlpxSubProtocol) {
self.extra_protocols.push(protocol)
}
/// Spawns the given future onto a new task that is tracked in the `spawned_tasks`
/// [`JoinSet`](tokio::task::JoinSet).
fn spawn<F>(&self, f: F)

View File

@ -2,6 +2,7 @@ use crate::{
listener::{ConnectionListener, ListenerEvent},
message::{PeerMessage, PeerRequestSender},
peers::InboundConnectionError,
protocol::IntoRlpxSubProtocol,
session::{Direction, PendingSessionHandshakeError, SessionEvent, SessionId, SessionManager},
state::{NetworkState, StateAction},
};
@ -76,10 +77,7 @@ pub(crate) struct Swarm<C> {
// === impl Swarm ===
impl<C> Swarm<C>
where
C: BlockNumReader,
{
impl<C> Swarm<C> {
/// Configures a new swarm instance.
pub(crate) fn new(
incoming: ConnectionListener,
@ -90,6 +88,11 @@ where
Self { incoming, sessions, state, net_connection_state }
}
/// Adds an additional protocol handler to the RLPx sub-protocol list.
pub(crate) fn add_rlpx_sub_protocol(&mut self, protocol: impl IntoRlpxSubProtocol) {
self.sessions_mut().add_rlpx_sub_protocol(protocol);
}
/// Access to the state.
pub(crate) fn state(&self) -> &NetworkState<C> {
&self.state
@ -114,7 +117,12 @@ where
pub(crate) fn sessions_mut(&mut self) -> &mut SessionManager {
&mut self.sessions
}
}
impl<C> Swarm<C>
where
C: BlockNumReader,
{
/// Triggers a new outgoing connection to the given node
pub(crate) fn dial_outbound(&mut self, remote_addr: SocketAddr, remote_id: PeerId) {
self.sessions.dial_outbound(remote_addr, remote_id)