Skip to content

Commit

Permalink
chore: add network example (paradigmxyz#3753)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jul 13, 2023
1 parent f3e13db commit 4c7f980
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion crates/net/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions crates/net/network/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 18 additions & 13 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,3 +34,7 @@ path = "rpc-db.rs"
[[example]]
name = "db-access"
path = "db-access.rs"

[[example]]
name = "network"
path = "network.rs"
41 changes: 41 additions & 0 deletions examples/network.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 4c7f980

Please sign in to comment.