Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(net): extract NetworkHandle methods for launching node to traits #9966

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 98 additions & 70 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions bin/reth/src/commands/debug_cmd/execution.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Command for debugging execution.

use crate::{args::NetworkArgs, macros::block_executor, utils::get_single_header};
use std::{path::PathBuf, sync::Arc};

use clap::Parser;
use futures::{stream::select as stream_select, StreamExt};
use reth_beacon_consensus::EthBeaconConsensus;
Expand All @@ -16,7 +17,7 @@ use reth_downloaders::{
headers::reverse_headers::ReverseHeadersDownloaderBuilder,
};
use reth_exex::ExExManagerHandle;
use reth_network::{NetworkEvents, NetworkHandle};
use reth_network::{BlockDownloaderProvider, NetworkEvents, NetworkHandle};
use reth_network_api::NetworkInfo;
use reth_network_p2p::{bodies::client::BodiesClient, headers::client::HeadersClient};
use reth_primitives::{BlockHashOrNumber, BlockNumber, B256};
Expand All @@ -30,10 +31,11 @@ use reth_stages::{
};
use reth_static_file::StaticFileProducer;
use reth_tasks::TaskExecutor;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::watch;
use tracing::*;

use crate::{args::NetworkArgs, macros::block_executor, utils::get_single_header};

/// `reth debug execution` command
#[derive(Debug, Parser)]
pub struct Command {
Expand Down
16 changes: 9 additions & 7 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Command for debugging in-memory merkle trie calculation.

use crate::{
args::NetworkArgs,
macros::block_executor,
utils::{get_single_body, get_single_header},
};
use std::{path::PathBuf, sync::Arc};

use backon::{ConstantBuilder, Retryable};
use clap::Parser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
Expand All @@ -15,7 +12,7 @@ use reth_db::DatabaseEnv;
use reth_errors::BlockValidationError;
use reth_evm::execute::{BlockExecutionOutput, BlockExecutorProvider, Executor};
use reth_execution_types::ExecutionOutcome;
use reth_network::NetworkHandle;
use reth_network::{BlockDownloaderProvider, NetworkHandle};
use reth_network_api::NetworkInfo;
use reth_primitives::BlockHashOrNumber;
use reth_provider::{
Expand All @@ -28,9 +25,14 @@ use reth_stages::StageId;
use reth_tasks::TaskExecutor;
use reth_trie::StateRoot;
use reth_trie_db::DatabaseStateRoot;
use std::{path::PathBuf, sync::Arc};
use tracing::*;

use crate::{
args::NetworkArgs,
macros::block_executor,
utils::{get_single_body, get_single_header},
};

/// `reth debug in-memory-merkle` command
/// This debug routine requires that the node is positioned at the block before the target.
/// The script will then download the block from p2p network and attempt to calculate and verify
Expand Down
9 changes: 6 additions & 3 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Command for debugging merkle trie calculation.
use crate::{args::NetworkArgs, macros::block_executor, utils::get_single_header};

use std::{path::PathBuf, sync::Arc};

use backon::{ConstantBuilder, Retryable};
use clap::Parser;
use reth_beacon_consensus::EthBeaconConsensus;
Expand All @@ -11,7 +13,7 @@ use reth_consensus::Consensus;
use reth_db::{tables, DatabaseEnv};
use reth_db_api::{cursor::DbCursorRO, transaction::DbTx};
use reth_evm::execute::{BatchExecutor, BlockExecutorProvider};
use reth_network::NetworkHandle;
use reth_network::{BlockDownloaderProvider, NetworkHandle};
use reth_network_api::NetworkInfo;
use reth_network_p2p::full_block::FullBlockClient;
use reth_primitives::BlockHashOrNumber;
Expand All @@ -25,9 +27,10 @@ use reth_stages::{
ExecInput, Stage, StageCheckpoint,
};
use reth_tasks::TaskExecutor;
use std::{path::PathBuf, sync::Arc};
use tracing::*;

use crate::{args::NetworkArgs, macros::block_executor, utils::get_single_header};

/// `reth debug merkle` command
#[derive(Debug, Parser)]
pub struct Command {
Expand Down
8 changes: 5 additions & 3 deletions bin/reth/src/commands/debug_cmd/replay_engine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{args::NetworkArgs, macros::block_executor};
use std::{path::PathBuf, sync::Arc, time::Duration};

use clap::Parser;
use eyre::Context;
use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
Expand All @@ -14,7 +15,7 @@ use reth_consensus::Consensus;
use reth_db::DatabaseEnv;
use reth_engine_util::engine_store::{EngineMessageStore, StoredEngineApiMessage};
use reth_fs_util as fs;
use reth_network::NetworkHandle;
use reth_network::{BlockDownloaderProvider, NetworkHandle};
use reth_network_api::NetworkInfo;
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_provider::{
Expand All @@ -25,10 +26,11 @@ use reth_stages::Pipeline;
use reth_static_file::StaticFileProducer;
use reth_tasks::TaskExecutor;
use reth_transaction_pool::noop::NoopTransactionPool;
use std::{path::PathBuf, sync::Arc, time::Duration};
use tokio::sync::oneshot;
use tracing::*;

use crate::{args::NetworkArgs, macros::block_executor};

/// `reth debug replay-engine` command
/// This script will read stored engine API messages and replay them by the timestamp.
/// It does not require
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/commands/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::{Parser, Subcommand};
use reth_chainspec::ChainSpec;
use reth_cli_util::{get_secret_key, hash_or_num_value_parser};
use reth_config::Config;
use reth_network::NetworkConfigBuilder;
use reth_network::{BlockDownloaderProvider, NetworkConfigBuilder};
use reth_network_p2p::bodies::client::BodiesClient;
use reth_node_core::{
args::{
Expand Down
8 changes: 6 additions & 2 deletions crates/cli/commands/src/stage/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Main `stage` command
//!
//! Stage debugging tool
use crate::common::{AccessRights, Environment, EnvironmentArgs};

use std::{any::Any, net::SocketAddr, sync::Arc, time::Instant};

use clap::Parser;
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::ChainSpec;
Expand All @@ -11,6 +13,7 @@ use reth_config::config::{HashingConfig, SenderRecoveryConfig, TransactionLookup
use reth_downloaders::bodies::bodies::BodiesDownloaderBuilder;
use reth_evm::execute::BlockExecutorProvider;
use reth_exex::ExExManagerHandle;
use reth_network::BlockDownloaderProvider;
use reth_node_core::{
args::{NetworkArgs, StageEnum},
version::{
Expand All @@ -35,9 +38,10 @@ use reth_stages::{
},
ExecInput, ExecOutput, ExecutionStageThresholds, Stage, StageExt, UnwindInput, UnwindOutput,
};
use std::{any::Any, net::SocketAddr, sync::Arc, time::Instant};
use tracing::*;

use crate::common::{AccessRights, Environment, EnvironmentArgs};

/// `reth stage` command
#[derive(Debug, Parser)]
pub struct Command {
Expand Down
2 changes: 1 addition & 1 deletion crates/e2e-test-utils/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures_util::StreamExt;
use reth::{
network::{NetworkEvent, NetworkEvents, NetworkHandle, PeersInfo},
network::{NetworkEvent, NetworkEvents, NetworkHandle, PeersHandleProvider, PeersInfo},
rpc::types::PeerId,
};
use reth_network_peers::NodeRecord;
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/node/src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reth_engine_tree::tree::TreeConfig;
use reth_ethereum_engine::service::{ChainEvent, EthService};
use reth_ethereum_engine_primitives::EthEngineTypes;
use reth_exex::ExExManagerHandle;
use reth_network::{NetworkEvents, NetworkSyncUpdater, SyncState};
use reth_network::{BlockDownloaderProvider, NetworkEvents, NetworkSyncUpdater, SyncState};
use reth_node_api::{FullNodeTypes, NodeAddOns};
use reth_node_builder::{
hooks::NodeHooks,
Expand Down
51 changes: 16 additions & 35 deletions crates/net/network-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
/// Network Error
pub mod error;
/// Implementation of network traits for that does nothing.
pub mod noop;

pub use alloy_rpc_types_admin::EthProtocolInfo;
pub use error::NetworkError;
pub use reth_network_types::{PeerKind, PeersHandle, Reputation, ReputationChangeKind};

use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};

use reth_eth_wire::{capability::Capabilities, DisconnectReason, EthVersion, Status};
use reth_network_peers::NodeRecord;
pub use reth_network_types::{Reputation, ReputationChangeKind};

/// The `PeerId` type.
pub type PeerId = alloy_primitives::B512;

/// Network Error
pub mod error;

/// Implementation of network traits for that does nothing.
pub mod noop;

/// Provides general purpose information about the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait NetworkInfo: Send + Sync {
Expand All @@ -51,6 +50,7 @@ pub trait NetworkInfo: Send + Sync {
}

/// Provides general purpose information about Peers in the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait PeersInfo: Send + Sync {
/// Returns how many peers the network is currently connected to.
///
Expand All @@ -65,6 +65,7 @@ pub trait PeersInfo: Send + Sync {
}

/// Provides an API for managing the peers of the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait Peers: PeersInfo {
/// Adds a peer to the peer set with UDP `SocketAddr`.
fn add_peer(&self, peer: PeerId, tcp_addr: SocketAddr) {
Expand Down Expand Up @@ -157,33 +158,13 @@ pub trait Peers: PeersInfo {
) -> impl Future<Output = Result<Option<Reputation>, NetworkError>> + Send;
}

/// Represents the kind of peer
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub enum PeerKind {
/// Basic peer kind.
#[default]
Basic,
/// Static peer, added via JSON-RPC.
Static,
/// Trusted peer.
Trusted,
}

impl PeerKind {
/// Returns `true` if the peer is trusted.
pub const fn is_trusted(&self) -> bool {
matches!(self, Self::Trusted)
}

/// Returns `true` if the peer is static.
pub const fn is_static(&self) -> bool {
matches!(self, Self::Static)
}

/// Returns `true` if the peer is basic.
pub const fn is_basic(&self) -> bool {
matches!(self, Self::Basic)
}
/// Provides an API for managing the peers of the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait PeersHandleProvider {
/// Returns the [`PeersHandle`] that can be cloned and shared.
///
/// The [`PeersHandle`] can be used to interact with the network's peer set.
fn peers_handle(&self) -> &PeersHandle;
}

/// Info about an active peer session.
Expand Down
10 changes: 5 additions & 5 deletions crates/net/network-api/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
//! This is useful for wiring components together that don't require network but still need to be
//! generic over it.

use crate::{
NetworkError, NetworkInfo, NetworkStatus, PeerId, PeerInfo, PeerKind, Peers, PeersInfo,
Reputation, ReputationChangeKind,
};
use std::net::{IpAddr, SocketAddr};

use alloy_rpc_types_admin::EthProtocolInfo;
use enr::{secp256k1::SecretKey, Enr};
use reth_eth_wire::{DisconnectReason, ProtocolVersion};
use reth_network_peers::NodeRecord;
use std::net::{IpAddr, SocketAddr};
use reth_network_types::{PeerKind, Reputation, ReputationChangeKind};

use crate::{NetworkError, NetworkInfo, NetworkStatus, PeerId, PeerInfo, Peers, PeersInfo};

/// A type that implements all network trait that does nothing.
///
Expand Down
5 changes: 5 additions & 0 deletions crates/net/network-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ workspace = true
# reth
reth-network-peers.workspace = true
reth-net-banlist.workspace = true
reth-ethereum-forks.workspace = true

# async
tokio = { workspace = true, features = ["sync"] }

# io
serde = { workspace = true, optional = true }
Expand All @@ -23,6 +27,7 @@ serde_json = { workspace = true }

# misc
tracing.workspace = true
derive_more.workspace = true

[features]
serde = ["dep:serde", "dep:humantime-serde"]
Expand Down
19 changes: 13 additions & 6 deletions crates/net/network-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@

/// Types related to peering.
pub mod peers;
pub use peers::{
reputation::{Reputation, ReputationChangeKind, ReputationChangeWeights},
ConnectionsConfig, PeersConfig,
};

pub mod session;
pub use session::{SessionLimits, SessionsConfig};

/// [`BackoffKind`] definition.
mod backoff;

pub use backoff::BackoffKind;
pub use peers::{
addr::PeerAddr,
handle::PeersHandle,
kind::PeerKind,
reputation::{
is_banned_reputation, Reputation, ReputationChange, ReputationChangeKind,
ReputationChangeOutcome, ReputationChangeWeights, DEFAULT_REPUTATION,
},
state::PeerConnectionState,
ConnectionsConfig, Peer, PeerCommand, PeersConfig,
};
pub use session::{SessionLimits, SessionsConfig};
45 changes: 45 additions & 0 deletions crates/net/network-types/src/peers/addr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! `RLPx` (TCP) and `Discovery` (UDP) sockets of a peer.

use std::net::{IpAddr, SocketAddr};

/// Represents a peer's address information.
///
/// # Fields
///
/// - `tcp`: A `SocketAddr` representing the peer's data transfer address.
/// - `udp`: An optional `SocketAddr` representing the peer's discover address. `None` if the peer
/// is directly connecting to us or the port is the same to `tcp`'s
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct PeerAddr {
tcp: SocketAddr,
udp: Option<SocketAddr>,
}

impl PeerAddr {
/// Returns the peer's TCP address.
pub const fn tcp(&self) -> SocketAddr {
self.tcp
}

/// Returns the peer's UDP address.
pub const fn udp(&self) -> Option<SocketAddr> {
self.udp
}

/// Returns a new `PeerAddr` with the given `tcp` and `udp` addresses.
pub const fn new(tcp: SocketAddr, udp: Option<SocketAddr>) -> Self {
Self { tcp, udp }
}

/// Returns a new `PeerAddr` with a `tcp` address only.
pub const fn from_tcp(tcp: SocketAddr) -> Self {
Self { tcp, udp: None }
}

/// Returns a new `PeerAddr` with the given `tcp` and `udp` ports.
pub fn new_with_ports(ip: IpAddr, tcp_port: u16, udp_port: Option<u16>) -> Self {
let tcp = SocketAddr::new(ip, tcp_port);
let udp = udp_port.map(|port| SocketAddr::new(ip, port));
Self::new(tcp, udp)
}
}
Loading
Loading