mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
Move network.rs example to its own folder (#7936)
Co-authored-by: Elijah Hampton <elijahhampton@MBP-de-Elijah-2.lan>
This commit is contained in:
13
examples/network/Cargo.toml
Normal file
13
examples/network/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "network"
|
||||
version = "0.0.0"
|
||||
publish = false
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
reth-network.workspace = true
|
||||
reth-provider = { workspace = true, features = ["test-utils"] }
|
||||
futures.workspace = true
|
||||
tokio.workspace = true
|
||||
eyre.workspace = true
|
||||
40
examples/network/src/main.rs
Normal file
40
examples/network/src/main.rs
Normal file
@ -0,0 +1,40 @@
|
||||
//! Example of how to use the network as a standalone component
|
||||
//!
|
||||
//! Run with
|
||||
//!
|
||||
//! ```not_rust
|
||||
//! cargo run --release -p network
|
||||
//! ```
|
||||
|
||||
use futures::StreamExt;
|
||||
use reth_network::{config::rng_secret_key, NetworkConfig, NetworkEvents, 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::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