mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add NetworkManager::eth (#13936)
This commit is contained in:
@ -142,7 +142,25 @@ pub struct NetworkManager<N: NetworkPrimitives = EthNetworkPrimitives> {
|
|||||||
disconnect_metrics: DisconnectMetrics,
|
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> {
|
impl<N: NetworkPrimitives> NetworkManager<N> {
|
||||||
/// Sets the dedicated channel for events indented for the
|
/// Sets the dedicated channel for events indented for the
|
||||||
/// [`TransactionsManager`](crate::transactions::TransactionsManager).
|
/// [`TransactionsManager`](crate::transactions::TransactionsManager).
|
||||||
|
|||||||
@ -14,9 +14,7 @@
|
|||||||
|
|
||||||
use chainspec::{boot_nodes, bsc_chain_spec};
|
use chainspec::{boot_nodes, bsc_chain_spec};
|
||||||
use reth_discv4::Discv4ConfigBuilder;
|
use reth_discv4::Discv4ConfigBuilder;
|
||||||
use reth_network::{
|
use reth_network::{NetworkConfig, NetworkEvent, NetworkEventListenerProvider, NetworkManager};
|
||||||
EthNetworkPrimitives, NetworkConfig, NetworkEvent, NetworkEventListenerProvider, NetworkManager,
|
|
||||||
};
|
|
||||||
use reth_network_api::{
|
use reth_network_api::{
|
||||||
events::{PeerEvent, SessionInfo},
|
events::{PeerEvent, SessionInfo},
|
||||||
PeersInfo,
|
PeersInfo,
|
||||||
@ -69,7 +67,7 @@ async fn main() {
|
|||||||
// latest BSC forkId, we need to override this to allow connections from BSC nodes
|
// 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 };
|
let fork_id = ForkId { hash: ForkHash([0x07, 0xb5, 0x43, 0x28]), next: 0 };
|
||||||
net_cfg.fork_filter.set_current_fork_id(fork_id);
|
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.
|
// The network handle is our entrypoint into the network.
|
||||||
let net_handle = net_manager.handle().clone();
|
let net_handle = net_manager.handle().clone();
|
||||||
|
|||||||
@ -14,8 +14,8 @@ use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
|
|||||||
|
|
||||||
use reth::builder::NodeHandle;
|
use reth::builder::NodeHandle;
|
||||||
use reth_network::{
|
use reth_network::{
|
||||||
config::SecretKey, protocol::IntoRlpxSubProtocol, EthNetworkPrimitives, NetworkConfig,
|
config::SecretKey, protocol::IntoRlpxSubProtocol, NetworkConfig, NetworkManager,
|
||||||
NetworkManager, NetworkProtocols,
|
NetworkProtocols,
|
||||||
};
|
};
|
||||||
use reth_network_api::{test_utils::PeersHandleProvider, NetworkInfo};
|
use reth_network_api::{test_utils::PeersHandleProvider, NetworkInfo};
|
||||||
use reth_node_ethereum::EthereumNode;
|
use reth_node_ethereum::EthereumNode;
|
||||||
@ -53,7 +53,7 @@ fn main() -> eyre::Result<()> {
|
|||||||
.build_with_noop_provider(node.chain_spec());
|
.build_with_noop_provider(node.chain_spec());
|
||||||
|
|
||||||
// spawn the second network instance
|
// 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_id = *subnetwork.peer_id();
|
||||||
let subnetwork_peer_addr = subnetwork.local_addr();
|
let subnetwork_peer_addr = subnetwork.local_addr();
|
||||||
let subnetwork_handle = subnetwork.peers_handle();
|
let subnetwork_handle = subnetwork.peers_handle();
|
||||||
|
|||||||
@ -8,8 +8,7 @@
|
|||||||
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use reth_network::{
|
use reth_network::{
|
||||||
config::rng_secret_key, EthNetworkPrimitives, NetworkConfig, NetworkEventListenerProvider,
|
config::rng_secret_key, NetworkConfig, NetworkEventListenerProvider, NetworkManager,
|
||||||
NetworkManager,
|
|
||||||
};
|
};
|
||||||
use reth_provider::test_utils::NoopProvider;
|
use reth_provider::test_utils::NoopProvider;
|
||||||
|
|
||||||
@ -25,7 +24,7 @@ async fn main() -> eyre::Result<()> {
|
|||||||
let config = NetworkConfig::builder(local_key).mainnet_boot_nodes().build(client);
|
let config = NetworkConfig::builder(local_key).mainnet_boot_nodes().build(client);
|
||||||
|
|
||||||
// create the network instance
|
// 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
|
// get a handle to the network to interact with it
|
||||||
let handle = network.handle().clone();
|
let handle = network.handle().clone();
|
||||||
|
|||||||
@ -12,8 +12,7 @@
|
|||||||
use chain_cfg::{boot_nodes, head, polygon_chain_spec};
|
use chain_cfg::{boot_nodes, head, polygon_chain_spec};
|
||||||
use reth_discv4::Discv4ConfigBuilder;
|
use reth_discv4::Discv4ConfigBuilder;
|
||||||
use reth_network::{
|
use reth_network::{
|
||||||
config::NetworkMode, EthNetworkPrimitives, NetworkConfig, NetworkEvent,
|
config::NetworkMode, NetworkConfig, NetworkEvent, NetworkEventListenerProvider, NetworkManager,
|
||||||
NetworkEventListenerProvider, NetworkManager,
|
|
||||||
};
|
};
|
||||||
use reth_network_api::events::SessionInfo;
|
use reth_network_api::events::SessionInfo;
|
||||||
use reth_tracing::{
|
use reth_tracing::{
|
||||||
@ -59,7 +58,7 @@ async fn main() {
|
|||||||
discv4_cfg.add_boot_nodes(boot_nodes()).lookup_interval(interval);
|
discv4_cfg.add_boot_nodes(boot_nodes()).lookup_interval(interval);
|
||||||
let net_cfg = net_cfg.set_discovery_v4(discv4_cfg.build());
|
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.
|
// The network handle is our entrypoint into the network.
|
||||||
let net_handle = net_manager.handle();
|
let net_handle = net_manager.handle();
|
||||||
|
|||||||
Reference in New Issue
Block a user