From c71c8130329596990b9597958daef8b9773d1bdd Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Thu, 1 Aug 2024 19:50:20 +0200 Subject: [PATCH] chore(sdk): enable referencing network type via `FullNodeComponents::Network` (#9921) --- Cargo.lock | 3 +- bin/reth/src/lib.rs | 2 +- crates/cli/commands/src/p2p.rs | 3 +- crates/e2e-test-utils/src/lib.rs | 4 ++- crates/e2e-test-utils/src/network.rs | 13 ++++--- crates/e2e-test-utils/src/node.rs | 4 ++- crates/exex/exex/Cargo.toml | 1 - crates/exex/exex/src/context.rs | 8 +++-- crates/net/network-types/src/lib.rs | 4 +-- crates/node/api/src/node.rs | 9 +++-- crates/node/builder/src/builder/states.rs | 4 +-- crates/node/builder/src/components/mod.rs | 36 ++++++++++++-------- crates/node/builder/src/launch/mod.rs | 4 +-- crates/node/builder/src/node.rs | 3 +- crates/node/builder/src/rpc.rs | 7 ++-- crates/optimism/rpc/Cargo.toml | 1 - crates/optimism/rpc/src/eth/mod.rs | 5 ++- crates/rpc/rpc/Cargo.toml | 1 + crates/rpc/rpc/src/admin.rs | 3 +- examples/custom-rlpx-subprotocol/src/main.rs | 4 +-- 20 files changed, 66 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 67fd9c2a5..a52bf8e39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7338,7 +7338,6 @@ dependencies = [ "reth-evm-ethereum", "reth-exex-types", "reth-metrics", - "reth-network", "reth-node-api", "reth-node-core", "reth-payload-builder", @@ -7995,7 +7994,6 @@ dependencies = [ "parking_lot 0.12.3", "reth-evm", "reth-evm-optimism", - "reth-network", "reth-network-api", "reth-node-api", "reth-primitives", @@ -8265,6 +8263,7 @@ dependencies = [ "reth-evm-ethereum", "reth-network-api", "reth-network-peers", + "reth-network-types", "reth-node-api", "reth-primitives", "reth-provider", diff --git a/bin/reth/src/lib.rs b/bin/reth/src/lib.rs index 82690dca5..d207e2ed5 100644 --- a/bin/reth/src/lib.rs +++ b/bin/reth/src/lib.rs @@ -127,7 +127,7 @@ pub mod tasks { /// Re-exported from `reth_network`. pub mod network { pub use reth_network::*; - pub use reth_network_api::{noop, NetworkInfo, PeerKind, Peers, PeersInfo}; + pub use reth_network_api::{noop, NetworkInfo, Peers, PeersHandleProvider, PeersInfo}; } /// Re-exported from `reth_transaction_pool`. diff --git a/crates/cli/commands/src/p2p.rs b/crates/cli/commands/src/p2p.rs index 67fe7ad11..659e63387 100644 --- a/crates/cli/commands/src/p2p.rs +++ b/crates/cli/commands/src/p2p.rs @@ -1,5 +1,7 @@ //! P2P Debugging tool +use std::{path::PathBuf, sync::Arc}; + use backon::{ConstantBuilder, Retryable}; use clap::{Parser, Subcommand}; use reth_chainspec::ChainSpec; @@ -15,7 +17,6 @@ use reth_node_core::{ utils::get_single_header, }; use reth_primitives::BlockHashOrNumber; -use std::{path::PathBuf, sync::Arc}; /// `reth p2p` command #[derive(Debug, Parser)] diff --git a/crates/e2e-test-utils/src/lib.rs b/crates/e2e-test-utils/src/lib.rs index a32fded66..132bc36c1 100644 --- a/crates/e2e-test-utils/src/lib.rs +++ b/crates/e2e-test-utils/src/lib.rs @@ -6,6 +6,7 @@ use node::NodeTestContext; use reth::{ args::{DiscoveryArgs, NetworkArgs, RpcServerArgs}, builder::{NodeBuilder, NodeConfig, NodeHandle}, + network::PeersHandleProvider, rpc::api::eth::{helpers::AddDevSigners, FullEthApiServer}, tasks::TaskManager, }; @@ -13,7 +14,7 @@ use reth_chainspec::ChainSpec; use reth_db::{test_utils::TempDatabase, DatabaseEnv}; use reth_node_builder::{ components::NodeComponentsBuilder, rpc::EthApiBuilderProvider, FullNodeTypesAdapter, Node, - NodeAdapter, NodeAddOns, RethFullAdapter, + NodeAdapter, NodeAddOns, NodeComponents, RethFullAdapter, }; use reth_provider::providers::BlockchainProvider; use tracing::{span, Level}; @@ -50,6 +51,7 @@ pub async fn setup( ) -> eyre::Result<(Vec>, TaskManager, Wallet)> where N: Default + Node>, + <>>::Components as NodeComponents>>::Network: PeersHandleProvider, >>::EthApi: FullEthApiServer + AddDevSigners + EthApiBuilderProvider>, { diff --git a/crates/e2e-test-utils/src/network.rs b/crates/e2e-test-utils/src/network.rs index fd4258efa..0514c68e1 100644 --- a/crates/e2e-test-utils/src/network.rs +++ b/crates/e2e-test-utils/src/network.rs @@ -1,6 +1,6 @@ use futures_util::StreamExt; use reth::{ - network::{NetworkEvent, NetworkEvents, NetworkHandle, PeersHandleProvider, PeersInfo}, + network::{NetworkEvent, NetworkEvents, PeersHandleProvider, PeersInfo}, rpc::types::PeerId, }; use reth_network_peers::NodeRecord; @@ -9,14 +9,17 @@ use reth_tracing::tracing::info; /// Helper for network operations #[derive(Debug)] -pub struct NetworkTestContext { +pub struct NetworkTestContext { network_events: EventStream, - network: NetworkHandle, + network: Network, } -impl NetworkTestContext { +impl NetworkTestContext +where + Network: NetworkEvents + PeersInfo + PeersHandleProvider, +{ /// Creates a new network helper - pub fn new(network: NetworkHandle) -> Self { + pub fn new(network: Network) -> Self { let network_events = network.event_listener(); Self { network_events, network } } diff --git a/crates/e2e-test-utils/src/node.rs b/crates/e2e-test-utils/src/node.rs index e985255bb..19f387c7b 100644 --- a/crates/e2e-test-utils/src/node.rs +++ b/crates/e2e-test-utils/src/node.rs @@ -6,6 +6,7 @@ use futures_util::Future; use reth::{ api::{BuiltPayload, EngineTypes, FullNodeComponents, PayloadBuilderAttributes}, builder::FullNode, + network::PeersHandleProvider, payload::PayloadTypes, providers::{BlockReader, BlockReaderIdExt, CanonStateSubscriptions, StageCheckpointReader}, rpc::{ @@ -35,7 +36,7 @@ where /// Context for testing payload-related features. pub payload: PayloadTestContext, /// Context for testing network functionalities. - pub network: NetworkTestContext, + pub network: NetworkTestContext, /// Context for testing the Engine API. pub engine_api: EngineApiTestContext, /// Context for testing RPC features. @@ -45,6 +46,7 @@ where impl NodeTestContext where Node: FullNodeComponents, + Node::Network: PeersHandleProvider, AddOns: NodeAddOns, { /// Creates a new test node diff --git a/crates/exex/exex/Cargo.toml b/crates/exex/exex/Cargo.toml index 48e658c40..3ddeebcd6 100644 --- a/crates/exex/exex/Cargo.toml +++ b/crates/exex/exex/Cargo.toml @@ -17,7 +17,6 @@ reth-config.workspace = true reth-evm.workspace = true reth-exex-types.workspace = true reth-metrics.workspace = true -reth-network.workspace = true reth-node-api.workspace = true reth-node-core.workspace = true reth-payload-builder.workspace = true diff --git a/crates/exex/exex/src/context.rs b/crates/exex/exex/src/context.rs index 86939e3ba..c159b90bd 100644 --- a/crates/exex/exex/src/context.rs +++ b/crates/exex/exex/src/context.rs @@ -1,11 +1,13 @@ -use crate::{ExExEvent, ExExNotification}; +use std::fmt::Debug; + use reth_node_api::FullNodeComponents; use reth_node_core::node_config::NodeConfig; use reth_primitives::Head; use reth_tasks::TaskExecutor; -use std::fmt::Debug; use tokio::sync::mpsc::{Receiver, UnboundedSender}; +use crate::{ExExEvent, ExExNotification}; + /// Captures the context that an `ExEx` has access to. pub struct ExExContext { /// The current head of the blockchain at launch. @@ -69,7 +71,7 @@ impl ExExContext { } /// Returns the handle to the network - pub fn network(&self) -> &reth_network::NetworkHandle { + pub fn network(&self) -> &Node::Network { self.components.network() } diff --git a/crates/net/network-types/src/lib.rs b/crates/net/network-types/src/lib.rs index 2a220a58a..f45f63d1e 100644 --- a/crates/net/network-types/src/lib.rs +++ b/crates/net/network-types/src/lib.rs @@ -26,9 +26,7 @@ pub use peers::{ addr::PeerAddr, handle::PeersHandle, kind::PeerKind, - reputation::{ - is_banned_reputation, ReputationChange, ReputationChangeOutcome, DEFAULT_REPUTATION, - }, + reputation::{is_banned_reputation, ReputationChangeOutcome, DEFAULT_REPUTATION}, state::PeerConnectionState, ConnectionsConfig, Peer, PeerCommand, PeersConfig, }; diff --git a/crates/node/api/src/node.rs b/crates/node/api/src/node.rs index f0d9eef1b..da4115ec9 100644 --- a/crates/node/api/src/node.rs +++ b/crates/node/api/src/node.rs @@ -7,7 +7,7 @@ use reth_db_api::{ database_metrics::{DatabaseMetadata, DatabaseMetrics}, }; use reth_evm::execute::BlockExecutorProvider; -use reth_network::NetworkHandle; +use reth_network::FullNetwork; use reth_payload_builder::PayloadBuilderHandle; use reth_provider::FullProvider; use reth_tasks::TaskExecutor; @@ -126,6 +126,9 @@ pub trait FullNodeComponents: FullNodeTypes + Clone + 'static { /// The type that knows how to execute blocks. type Executor: BlockExecutorProvider; + /// Network API. + type Network: FullNetwork; + /// Returns the transaction pool of the node. fn pool(&self) -> &Self::Pool; @@ -139,12 +142,12 @@ pub trait FullNodeComponents: FullNodeTypes + Clone + 'static { fn provider(&self) -> &Self::Provider; /// Returns the handle to the network - fn network(&self) -> &NetworkHandle; + fn network(&self) -> &Self::Network; /// Returns the handle to the payload builder service. fn payload_builder(&self) -> &PayloadBuilderHandle; - /// Returns the task executor. + /// Returns handle to runtime. fn task_executor(&self) -> &TaskExecutor; } diff --git a/crates/node/builder/src/builder/states.rs b/crates/node/builder/src/builder/states.rs index 33ffd7901..c5d81a7dc 100644 --- a/crates/node/builder/src/builder/states.rs +++ b/crates/node/builder/src/builder/states.rs @@ -8,7 +8,6 @@ use std::{fmt, future::Future, marker::PhantomData}; use reth_exex::ExExContext; -use reth_network::NetworkHandle; use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeAddOns, NodeTypes}; use reth_node_core::{ node_config::NodeConfig, @@ -103,6 +102,7 @@ impl> FullNodeComponents for NodeAdapter< type Pool = C::Pool; type Evm = C::Evm; type Executor = C::Executor; + type Network = C::Network; fn pool(&self) -> &Self::Pool { self.components.pool() @@ -120,7 +120,7 @@ impl> FullNodeComponents for NodeAdapter< &self.provider } - fn network(&self) -> &NetworkHandle { + fn network(&self) -> &Self::Network { self.components.network() } diff --git a/crates/node/builder/src/components/mod.rs b/crates/node/builder/src/components/mod.rs index 0419f7a71..6f0e994d6 100644 --- a/crates/node/builder/src/components/mod.rs +++ b/crates/node/builder/src/components/mod.rs @@ -7,19 +7,6 @@ //! //! Components depend on a fully type configured node: [FullNodeTypes](crate::node::FullNodeTypes). -use crate::{ConfigureEvm, FullNodeTypes}; -pub use builder::*; -pub use consensus::*; -pub use execute::*; -pub use network::*; -pub use payload::*; -pub use pool::*; -use reth_consensus::Consensus; -use reth_evm::execute::BlockExecutorProvider; -use reth_network::NetworkHandle; -use reth_payload_builder::PayloadBuilderHandle; -use reth_transaction_pool::TransactionPool; - mod builder; mod consensus; mod execute; @@ -27,6 +14,21 @@ mod network; mod payload; mod pool; +pub use builder::*; +pub use consensus::*; +pub use execute::*; +pub use network::*; +pub use payload::*; +pub use pool::*; + +use reth_consensus::Consensus; +use reth_evm::execute::BlockExecutorProvider; +use reth_network::{FullNetwork, NetworkHandle}; +use reth_payload_builder::PayloadBuilderHandle; +use reth_transaction_pool::TransactionPool; + +use crate::{ConfigureEvm, FullNodeTypes}; + /// An abstraction over the components of a node, consisting of: /// - evm and executor /// - transaction pool @@ -45,6 +47,9 @@ pub trait NodeComponents: Clone + Unpin + Send + Sync /// The consensus type of the node. type Consensus: Consensus + Clone + Unpin + 'static; + /// Network API. + type Network: FullNetwork; + /// Returns the transaction pool of the node. fn pool(&self) -> &Self::Pool; @@ -58,7 +63,7 @@ pub trait NodeComponents: Clone + Unpin + Send + Sync fn consensus(&self) -> &Self::Consensus; /// Returns the handle to the network - fn network(&self) -> &NetworkHandle; + fn network(&self) -> &Self::Network; /// Returns the handle to the payload builder service. fn payload_builder(&self) -> &PayloadBuilderHandle; @@ -96,6 +101,7 @@ where type Evm = EVM; type Executor = Executor; type Consensus = Cons; + type Network = NetworkHandle; fn pool(&self) -> &Self::Pool { &self.transaction_pool @@ -113,7 +119,7 @@ where &self.consensus } - fn network(&self) -> &NetworkHandle { + fn network(&self) -> &Self::Network { &self.network } diff --git a/crates/node/builder/src/launch/mod.rs b/crates/node/builder/src/launch/mod.rs index 7791c7002..bd474b577 100644 --- a/crates/node/builder/src/launch/mod.rs +++ b/crates/node/builder/src/launch/mod.rs @@ -15,7 +15,7 @@ use reth_blockchain_tree::{noop::NoopBlockchainTree, BlockchainTreeConfig}; use reth_consensus_debug_client::{DebugConsensusClient, EtherscanBlockProvider, RpcBlockProvider}; use reth_engine_util::EngineMessageStreamExt; use reth_exex::ExExManagerHandle; -use reth_network::{BlockDownloaderProvider, NetworkEvents, NetworkHandle}; +use reth_network::{BlockDownloaderProvider, NetworkEvents}; use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeAddOns}; use reth_node_core::{ dirs::{ChainPath, DataDirPath}, @@ -49,7 +49,7 @@ pub type EthApiBuilderCtx = reth_rpc_eth_types::EthApiBuilderCtx< ::Provider, ::Pool, ::Evm, - NetworkHandle, + ::Network, TaskExecutor, ::Provider, >; diff --git a/crates/node/builder/src/node.rs b/crates/node/builder/src/node.rs index efc4ff447..8f5c641a0 100644 --- a/crates/node/builder/src/node.rs +++ b/crates/node/builder/src/node.rs @@ -4,7 +4,6 @@ pub use reth_node_api::{FullNodeTypes, NodeTypes}; use std::{marker::PhantomData, sync::Arc}; use reth_chainspec::ChainSpec; -use reth_network::NetworkHandle; use reth_node_api::FullNodeComponents; use reth_node_core::{ dirs::{ChainPath, DataDirPath}, @@ -91,7 +90,7 @@ pub struct FullNode> { /// The node's transaction pool. pub pool: Node::Pool, /// Handle to the node's network. - pub network: NetworkHandle, + pub network: Node::Network, /// Provider to interact with the node's database pub provider: Node::Provider, /// Handle to the node's payload builder service. diff --git a/crates/node/builder/src/rpc.rs b/crates/node/builder/src/rpc.rs index 31403d224..920882703 100644 --- a/crates/node/builder/src/rpc.rs +++ b/crates/node/builder/src/rpc.rs @@ -6,7 +6,6 @@ use std::{ }; use futures::TryFutureExt; -use reth_network::NetworkHandle; use reth_node_api::{BuilderProvider, FullNodeComponents}; use reth_node_core::{ node_config::NodeConfig, @@ -161,7 +160,7 @@ pub struct RpcRegistry { pub(crate) registry: RpcRegistryInner< Node::Provider, Node::Pool, - NetworkHandle, + Node::Network, TaskExecutor, Node::Provider, EthApi, @@ -172,7 +171,7 @@ impl Deref for RpcRegistry { type Target = RpcRegistryInner< Node::Provider, Node::Pool, - NetworkHandle, + Node::Network, TaskExecutor, Node::Provider, EthApi, @@ -241,7 +240,7 @@ impl<'a, Node: FullNodeComponents, EthApi> RpcContext<'a, Node, EthApi> { } /// Returns the handle to the network - pub fn network(&self) -> &NetworkHandle { + pub fn network(&self) -> &Node::Network { self.node.network() } diff --git a/crates/optimism/rpc/Cargo.toml b/crates/optimism/rpc/Cargo.toml index b8bfd6db6..e5c2bede4 100644 --- a/crates/optimism/rpc/Cargo.toml +++ b/crates/optimism/rpc/Cargo.toml @@ -26,7 +26,6 @@ reth-transaction-pool.workspace = true reth-rpc.workspace = true reth-node-api.workspace = true reth-network-api.workspace = true -reth-network.workspace = true # ethereum alloy-primitives.workspace = true diff --git a/crates/optimism/rpc/src/eth/mod.rs b/crates/optimism/rpc/src/eth/mod.rs index 08a90eda3..fcb2fb2d5 100644 --- a/crates/optimism/rpc/src/eth/mod.rs +++ b/crates/optimism/rpc/src/eth/mod.rs @@ -12,7 +12,6 @@ use std::{fmt, sync::Arc}; use alloy_primitives::U256; use derive_more::Deref; use reth_evm::ConfigureEvm; -use reth_network::NetworkHandle; use reth_network_api::NetworkInfo; use reth_node_api::{BuilderProvider, FullNodeComponents, FullNodeTypes}; use reth_provider::{ @@ -40,7 +39,7 @@ use crate::OpEthApiError; pub type EthApiNodeBackend = EthApiInner< ::Provider, ::Pool, - NetworkHandle, + ::Network, ::Evm, >; @@ -49,7 +48,7 @@ pub type EthApiBuilderCtx = reth_rpc_eth_types::EthApiBuilderCtx< ::Provider, ::Pool, ::Evm, - NetworkHandle, + ::Network, TaskExecutor, ::Provider, >; diff --git a/crates/rpc/rpc/Cargo.toml b/crates/rpc/rpc/Cargo.toml index 78db90e81..1baf1f9d5 100644 --- a/crates/rpc/rpc/Cargo.toml +++ b/crates/rpc/rpc/Cargo.toml @@ -33,6 +33,7 @@ reth-evm.workspace = true reth-rpc-eth-types.workspace = true reth-rpc-server-types.workspace = true reth-node-api.workspace = true +reth-network-types.workspace = true # eth alloy-dyn-abi.workspace = true diff --git a/crates/rpc/rpc/src/admin.rs b/crates/rpc/rpc/src/admin.rs index 6d4f4d163..e66c4ed1e 100644 --- a/crates/rpc/rpc/src/admin.rs +++ b/crates/rpc/rpc/src/admin.rs @@ -4,8 +4,9 @@ use alloy_genesis::ChainConfig; use async_trait::async_trait; use jsonrpsee::core::RpcResult; use reth_chainspec::ChainSpec; -use reth_network_api::{NetworkInfo, PeerKind, Peers}; +use reth_network_api::{NetworkInfo, Peers}; use reth_network_peers::{id2pk, AnyNode, NodeRecord}; +use reth_network_types::PeerKind; use reth_primitives::EthereumHardfork; use reth_rpc_api::AdminApiServer; use reth_rpc_server_types::ToRpcResult; diff --git a/examples/custom-rlpx-subprotocol/src/main.rs b/examples/custom-rlpx-subprotocol/src/main.rs index d528a5a92..18745d84d 100644 --- a/examples/custom-rlpx-subprotocol/src/main.rs +++ b/examples/custom-rlpx-subprotocol/src/main.rs @@ -8,6 +8,8 @@ //! //! This launch a regular reth node with a custom rlpx subprotocol. +mod subprotocol; + use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use reth::builder::NodeHandle; @@ -28,8 +30,6 @@ use subprotocol::{ use tokio::sync::{mpsc, oneshot}; use tracing::info; -mod subprotocol; - fn main() -> eyre::Result<()> { reth::cli::Cli::parse_args().run(|builder, _args| async move { // launch the node