mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: add network example (#3753)
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2162,6 +2162,7 @@ dependencies = [
|
||||
"reth-beacon-consensus",
|
||||
"reth-blockchain-tree",
|
||||
"reth-db",
|
||||
"reth-network",
|
||||
"reth-network-api",
|
||||
"reth-primitives",
|
||||
"reth-provider",
|
||||
|
||||
@ -101,6 +101,7 @@ reth-revm = { path = "./crates/revm" }
|
||||
reth-payload-builder = { path = "./crates/payload/builder" }
|
||||
reth-transaction-pool = { path = "./crates/transaction-pool" }
|
||||
reth-tasks = { path = "./crates/tasks" }
|
||||
reth-network = { path = "./crates/net/network" }
|
||||
reth-network-api = { path = "./crates/net/network-api" }
|
||||
|
||||
## eth
|
||||
|
||||
@ -11,7 +11,9 @@ use reth_discv4::{Discv4Config, Discv4ConfigBuilder, DEFAULT_DISCOVERY_PORT};
|
||||
use reth_dns_discovery::DnsDiscoveryConfig;
|
||||
use reth_ecies::util::pk2id;
|
||||
use reth_eth_wire::{HelloMessage, Status};
|
||||
use reth_primitives::{ChainSpec, ForkFilter, Head, NodeRecord, PeerId, MAINNET};
|
||||
use reth_primitives::{
|
||||
mainnet_nodes, sepolia_nodes, ChainSpec, ForkFilter, Head, NodeRecord, PeerId, MAINNET,
|
||||
};
|
||||
use reth_provider::{BlockReader, HeaderProvider};
|
||||
use reth_tasks::{TaskSpawner, TokioTaskExecutor};
|
||||
use secp256k1::SECP256K1;
|
||||
@ -31,6 +33,9 @@ pub fn rng_secret_key() -> SecretKey {
|
||||
/// All network related initialization settings.
|
||||
pub struct NetworkConfig<C> {
|
||||
/// The client type that can interact with the chain.
|
||||
///
|
||||
/// This type is used to fetch the block number after we established a session and received the
|
||||
/// [Status] block hash.
|
||||
pub client: C,
|
||||
/// The node's secret key, from which the node's identity is derived.
|
||||
pub secret_key: SecretKey,
|
||||
@ -278,6 +283,16 @@ impl NetworkConfigBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Convenience function for setting [Self::boot_nodes] to the mainnet boot nodes.
|
||||
pub fn mainnet_boot_nodes(self) -> Self {
|
||||
self.boot_nodes(mainnet_nodes())
|
||||
}
|
||||
|
||||
/// Convenience function for setting [Self::boot_nodes] to the sepolia boot nodes.
|
||||
pub fn sepolia_boot_nodes(self) -> Self {
|
||||
self.boot_nodes(sepolia_nodes())
|
||||
}
|
||||
|
||||
/// Sets the boot nodes.
|
||||
pub fn boot_nodes(mut self, nodes: impl IntoIterator<Item = NodeRecord>) -> Self {
|
||||
self.boot_nodes = nodes.into_iter().collect();
|
||||
@ -330,6 +345,10 @@ impl NetworkConfigBuilder {
|
||||
|
||||
/// Consumes the type and creates the actual [`NetworkConfig`]
|
||||
/// for the given client type that can interact with the chain.
|
||||
///
|
||||
/// The given client is to be used for interacting with the chain, for example fetching the
|
||||
/// corresponding block for a given block hash we receive from a peer in the status message when
|
||||
/// establishing a connection.
|
||||
pub fn build<C>(self, client: C) -> NetworkConfig<C> {
|
||||
let peer_id = self.get_peer_id();
|
||||
let Self {
|
||||
|
||||
@ -51,6 +51,9 @@ pub struct NetworkState<C> {
|
||||
/// Buffered messages until polled.
|
||||
queued_messages: VecDeque<StateAction>,
|
||||
/// The client type that can interact with the chain.
|
||||
///
|
||||
/// This type is used to fetch the block number after we established a session and received the
|
||||
/// [Status] block hash.
|
||||
client: C,
|
||||
/// Network discovery.
|
||||
discovery: Discovery,
|
||||
|
||||
@ -6,25 +6,26 @@ edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
reth-primitives = { workspace = true }
|
||||
reth-primitives.workspace = true
|
||||
|
||||
reth-db = { workspace = true }
|
||||
reth-provider = { workspace = true }
|
||||
reth-db.workspace = true
|
||||
reth-provider.workspace = true
|
||||
|
||||
reth-rpc-builder = { workspace = true }
|
||||
reth-rpc-types = { workspace = true }
|
||||
reth-rpc-builder.workspace = true
|
||||
reth-rpc-types.workspace = true
|
||||
|
||||
reth-revm = { workspace = true }
|
||||
reth-blockchain-tree = { workspace = true }
|
||||
reth-beacon-consensus = { workspace = true }
|
||||
reth-network-api = { workspace = true }
|
||||
reth-transaction-pool = { workspace = true }
|
||||
reth-tasks = { workspace = true }
|
||||
reth-revm.workspace = true
|
||||
reth-blockchain-tree.workspace = true
|
||||
reth-beacon-consensus.workspace = true
|
||||
reth-network-api.workspace = true
|
||||
reth-network.workspace = true
|
||||
reth-transaction-pool.workspace = true
|
||||
reth-tasks.workspace = true
|
||||
|
||||
|
||||
eyre = "0.6.8"
|
||||
futures = "0.3.0"
|
||||
tokio = { workspace = true }
|
||||
futures.workspace = true
|
||||
tokio.workspace = true
|
||||
|
||||
[[example]]
|
||||
name = "rpc-db"
|
||||
@ -33,3 +34,7 @@ path = "rpc-db.rs"
|
||||
[[example]]
|
||||
name = "db-access"
|
||||
path = "db-access.rs"
|
||||
|
||||
[[example]]
|
||||
name = "network"
|
||||
path = "network.rs"
|
||||
|
||||
41
examples/network.rs
Normal file
41
examples/network.rs
Normal file
@ -0,0 +1,41 @@
|
||||
//! Example of how to use the network as a standalone component
|
||||
//!
|
||||
//! Run with
|
||||
//!
|
||||
//! ```not_rust
|
||||
//! cargo run --example network
|
||||
//! ```
|
||||
|
||||
use futures::StreamExt;
|
||||
use reth_network::{config::rng_secret_key, NetworkConfig, NetworkManager};
|
||||
use reth_provider::test_utils::NoopProvider;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> eyre::Result<()> {
|
||||
// This block provider implementation is used for testing purposes.
|
||||
let client = NoopProvider::default();
|
||||
|
||||
// The key that's used for encrypting sessions and to identify our node.
|
||||
let local_key = rng_secret_key();
|
||||
|
||||
// Configure the network
|
||||
let config =
|
||||
NetworkConfig::<NoopProvider>::builder(local_key).mainnet_boot_nodes().build(client);
|
||||
|
||||
// create the network instance
|
||||
let network = NetworkManager::new(config).await?;
|
||||
|
||||
// get a handle to the network to interact with it
|
||||
let handle = network.handle().clone();
|
||||
|
||||
// spawn the network
|
||||
tokio::task::spawn(network);
|
||||
|
||||
// interact with the network
|
||||
let mut events = handle.event_listener();
|
||||
while let Some(event) = events.next().await {
|
||||
println!("Received event: {:?}", event);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user