Skip to content

Commit

Permalink
feat: add NetworkManager::eth (#13936)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 22, 2025
1 parent 24c5234 commit 4dcc135
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
20 changes: 19 additions & 1 deletion crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,25 @@ pub struct NetworkManager<N: NetworkPrimitives = EthNetworkPrimitives> {
disconnect_metrics: DisconnectMetrics,
}

// === impl NetworkManager ===
impl NetworkManager {
/// Creates the manager of a new network with [`EthNetworkPrimitives`] types.
///
/// ```no_run
/// # async fn f() {
/// use reth_chainspec::MAINNET;
/// use reth_network::{NetworkConfig, NetworkManager};
/// let config =
/// NetworkConfig::builder_with_rng_secret_key().build_with_noop_provider(MAINNET.clone());
/// let manager = NetworkManager::eth(config).await;
/// # }
/// ```
pub async fn eth<C: BlockNumReader + 'static>(
config: NetworkConfig<C, EthNetworkPrimitives>,
) -> Result<Self, NetworkError> {
Self::new(config).await
}
}

impl<N: NetworkPrimitives> NetworkManager<N> {
/// Sets the dedicated channel for events indented for the
/// [`TransactionsManager`](crate::transactions::TransactionsManager).
Expand Down
6 changes: 2 additions & 4 deletions examples/bsc-p2p/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

use chainspec::{boot_nodes, bsc_chain_spec};
use reth_discv4::Discv4ConfigBuilder;
use reth_network::{
EthNetworkPrimitives, NetworkConfig, NetworkEvent, NetworkEventListenerProvider, NetworkManager,
};
use reth_network::{NetworkConfig, NetworkEvent, NetworkEventListenerProvider, NetworkManager};
use reth_network_api::{
events::{PeerEvent, SessionInfo},
PeersInfo,
Expand Down Expand Up @@ -69,7 +67,7 @@ async fn main() {
// latest BSC forkId, we need to override this to allow connections from BSC nodes
let fork_id = ForkId { hash: ForkHash([0x07, 0xb5, 0x43, 0x28]), next: 0 };
net_cfg.fork_filter.set_current_fork_id(fork_id);
let net_manager = NetworkManager::<EthNetworkPrimitives>::new(net_cfg).await.unwrap();
let net_manager = NetworkManager::eth(net_cfg).await.unwrap();

// The network handle is our entrypoint into the network.
let net_handle = net_manager.handle().clone();
Expand Down
6 changes: 3 additions & 3 deletions examples/custom-rlpx-subprotocol/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};

use reth::builder::NodeHandle;
use reth_network::{
config::SecretKey, protocol::IntoRlpxSubProtocol, EthNetworkPrimitives, NetworkConfig,
NetworkManager, NetworkProtocols,
config::SecretKey, protocol::IntoRlpxSubProtocol, NetworkConfig, NetworkManager,
NetworkProtocols,
};
use reth_network_api::{test_utils::PeersHandleProvider, NetworkInfo};
use reth_node_ethereum::EthereumNode;
Expand Down Expand Up @@ -53,7 +53,7 @@ fn main() -> eyre::Result<()> {
.build_with_noop_provider(node.chain_spec());

// spawn the second network instance
let subnetwork = NetworkManager::<EthNetworkPrimitives>::new(net_cfg).await?;
let subnetwork = NetworkManager::eth(net_cfg).await?;
let subnetwork_peer_id = *subnetwork.peer_id();
let subnetwork_peer_addr = subnetwork.local_addr();
let subnetwork_handle = subnetwork.peers_handle();
Expand Down
5 changes: 2 additions & 3 deletions examples/network/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use futures::StreamExt;
use reth_network::{
config::rng_secret_key, EthNetworkPrimitives, NetworkConfig, NetworkEventListenerProvider,
NetworkManager,
config::rng_secret_key, NetworkConfig, NetworkEventListenerProvider, NetworkManager,
};
use reth_provider::test_utils::NoopProvider;

Expand All @@ -25,7 +24,7 @@ async fn main() -> eyre::Result<()> {
let config = NetworkConfig::builder(local_key).mainnet_boot_nodes().build(client);

// create the network instance
let network = NetworkManager::<EthNetworkPrimitives>::new(config).await?;
let network = NetworkManager::eth(config).await?;

// get a handle to the network to interact with it
let handle = network.handle().clone();
Expand Down
5 changes: 2 additions & 3 deletions examples/polygon-p2p/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
use chain_cfg::{boot_nodes, head, polygon_chain_spec};
use reth_discv4::Discv4ConfigBuilder;
use reth_network::{
config::NetworkMode, EthNetworkPrimitives, NetworkConfig, NetworkEvent,
NetworkEventListenerProvider, NetworkManager,
config::NetworkMode, NetworkConfig, NetworkEvent, NetworkEventListenerProvider, NetworkManager,
};
use reth_network_api::events::SessionInfo;
use reth_tracing::{
Expand Down Expand Up @@ -59,7 +58,7 @@ async fn main() {
discv4_cfg.add_boot_nodes(boot_nodes()).lookup_interval(interval);
let net_cfg = net_cfg.set_discovery_v4(discv4_cfg.build());

let net_manager = NetworkManager::<EthNetworkPrimitives>::new(net_cfg).await.unwrap();
let net_manager = NetworkManager::eth(net_cfg).await.unwrap();

// The network handle is our entrypoint into the network.
let net_handle = net_manager.handle();
Expand Down

0 comments on commit 4dcc135

Please sign in to comment.