chore: add network example (#3753)

This commit is contained in:
Matthias Seitz
2023-07-13 17:26:02 +02:00
committed by GitHub
parent f3e13db71e
commit 4c7f980c77
6 changed files with 84 additions and 14 deletions

View File

@ -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
View 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(())
}