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

feat: introduce OpNetworkPrimitives #13335

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions bin/reth/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use reth_cli_commands::{
use reth_cli_runner::CliRunner;
use reth_db::DatabaseEnv;
use reth_ethereum_cli::chainspec::EthereumChainSpecParser;
use reth_network::EthNetworkPrimitives;
use reth_node_builder::{NodeBuilder, WithLaunchContext};
use reth_node_ethereum::{EthExecutorProvider, EthereumNode};
use reth_node_metrics::recorder::install_prometheus_recorder;
Expand Down Expand Up @@ -169,9 +170,14 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>, Ext: clap::Args + fmt::Debug> Cl
runner.run_blocking_until_ctrl_c(command.execute::<EthereumNode>())
}
Commands::Stage(command) => runner.run_command_until_exit(|ctx| {
command.execute::<EthereumNode, _, _>(ctx, EthExecutorProvider::ethereum)
command.execute::<EthereumNode, _, _, EthNetworkPrimitives>(
ctx,
EthExecutorProvider::ethereum,
)
}),
Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()),
Commands::P2P(command) => {
runner.run_until_ctrl_c(command.execute::<EthNetworkPrimitives>())
}
#[cfg(feature = "dev")]
Commands::TestVectors(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/commands/src/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_util::{get_secret_key, hash_or_num_value_parser};
use reth_config::Config;
use reth_network::{BlockDownloaderProvider, NetworkConfigBuilder};
use reth_network::{BlockDownloaderProvider, NetworkConfigBuilder, NetworkPrimitives};
use reth_network_p2p::bodies::client::BodiesClient;
use reth_node_core::{
args::{DatabaseArgs, DatadirArgs, NetworkArgs},
Expand Down Expand Up @@ -75,7 +75,7 @@ pub enum Subcommands {

impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
/// Execute `p2p` command
pub async fn execute(self) -> eyre::Result<()> {
pub async fn execute<N: NetworkPrimitives>(self) -> eyre::Result<()> {
let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain());
let config_path = self.config.clone().unwrap_or_else(|| data_dir.config());

Expand All @@ -97,7 +97,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>
let rlpx_socket = (self.network.addr, self.network.port).into();
let boot_nodes = self.chain.bootnodes().unwrap_or_default();

let net = NetworkConfigBuilder::new(p2p_secret_key)
let net = NetworkConfigBuilder::<N>::new(p2p_secret_key)
.peer_config(config.peers_config_with_basic_nodes_from_file(None))
.external_ip_resolver(self.network.nat)
.disable_discv4_discovery_if(self.chain.chain().is_optimism())
Expand Down
6 changes: 4 additions & 2 deletions crates/cli/commands/src/stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use clap::{Parser, Subcommand};
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_runner::CliContext;
use reth_eth_wire::NetPrimitivesFor;
use reth_evm::execute::BlockExecutorProvider;

pub mod drop;
Expand Down Expand Up @@ -41,14 +42,15 @@ pub enum Subcommands<C: ChainSpecParser> {

impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
/// Execute `stage` command
pub async fn execute<N, E, F>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
pub async fn execute<N, E, F, P>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
where
N: CliNodeTypes<ChainSpec = C::ChainSpec>,
E: BlockExecutorProvider<Primitives = N::Primitives>,
F: FnOnce(Arc<C::ChainSpec>) -> E,
P: NetPrimitivesFor<N::Primitives>,
{
match self.command {
Subcommands::Run(command) => command.execute::<N, _, _>(ctx, executor).await,
Subcommands::Run(command) => command.execute::<N, _, _, P>(ctx, executor).await,
Subcommands::Drop(command) => command.execute::<N>().await,
Subcommands::Dump(command) => command.execute::<N, _, _>(executor).await,
Subcommands::Unwind(command) => command.execute::<N>().await,
Expand Down
11 changes: 6 additions & 5 deletions crates/cli/commands/src/stage/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use reth_downloaders::{
bodies::bodies::BodiesDownloaderBuilder,
headers::reverse_headers::ReverseHeadersDownloaderBuilder,
};
use reth_eth_wire::NetPrimitivesFor;
use reth_evm::execute::BlockExecutorProvider;
use reth_exex::ExExManagerHandle;
use reth_network::BlockDownloaderProvider;
Expand Down Expand Up @@ -104,11 +105,12 @@ pub struct Command<C: ChainSpecParser> {

impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
/// Execute `stage` command
pub async fn execute<N, E, F>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
pub async fn execute<N, E, F, P>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
where
N: CliNodeTypes<ChainSpec = C::ChainSpec>,
E: BlockExecutorProvider<Primitives = N::Primitives>,
F: FnOnce(Arc<C::ChainSpec>) -> E,
P: NetPrimitivesFor<N::Primitives>,
{
// Raise the fd limit of the process.
// Does not do anything on windows.
Expand Down Expand Up @@ -174,7 +176,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>

let network = self
.network
.network_config(
.network_config::<P>(
&config,
provider_factory.chain_spec(),
p2p_secret_key,
Expand All @@ -186,13 +188,12 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>
let fetch_client = Arc::new(network.fetch_client().await?);

// Use `to` as the tip for the stage
let tip = fetch_client
let tip: P::BlockHeader = fetch_client
.get_header(BlockHashOrNumber::Number(self.to))
.await?
.into_data()
.ok_or(StageError::MissingSyncGap)?;
let (_, rx) = watch::channel(tip.hash_slow());

(
Box::new(HeaderStage::new(
provider_factory.clone(),
Expand Down Expand Up @@ -224,7 +225,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>

let network = self
.network
.network_config(
.network_config::<P>(
&config,
provider_factory.chain_spec(),
p2p_secret_key,
Expand Down
27 changes: 26 additions & 1 deletion crates/net/eth-wire-types/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use alloy_consensus::{RlpDecodableReceipt, RlpEncodableReceipt, TxReceipt};
use alloy_rlp::{Decodable, Encodable};
use reth_primitives::NodePrimitives;
use reth_primitives_traits::{Block, BlockBody, BlockHeader, SignedTransaction};
use std::fmt::Debug;

Expand Down Expand Up @@ -40,7 +41,31 @@ pub trait NetworkPrimitives:
+ 'static;
}

/// Primitive types used by Ethereum network.
/// This is a helper trait for use in bounds, where some of the [`NetworkPrimitives`] associated
/// types must be the same as the [`NodePrimitives`] associated types.
pub trait NetPrimitivesFor<N: NodePrimitives>:
NetworkPrimitives<
BlockHeader = N::BlockHeader,
BlockBody = N::BlockBody,
Block = N::Block,
Receipt = N::Receipt,
>
{
}

impl<N, T> NetPrimitivesFor<N> for T
where
N: NodePrimitives,
T: NetworkPrimitives<
BlockHeader = N::BlockHeader,
BlockBody = N::BlockBody,
Block = N::Block,
Receipt = N::Receipt,
>,
{
}

/// Network primitive types used by Ethereum networks.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct EthNetworkPrimitives;
Expand Down
22 changes: 16 additions & 6 deletions crates/node/builder/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,12 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
}

/// Builds the [`NetworkConfig`].
pub fn build_network_config(
pub fn build_network_config<N>(
&self,
network_builder: NetworkConfigBuilder,
) -> NetworkConfig<Node::Provider>
network_builder: NetworkConfigBuilder<N>,
) -> NetworkConfig<Node::Provider, N>
where
N: NetworkPrimitives,
Node::Types: NodeTypes<ChainSpec: Hardforks>,
{
network_builder.build(self.provider.clone())
Expand All @@ -747,20 +748,29 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {

impl<Node: FullNodeTypes<Types: NodeTypes<ChainSpec: Hardforks>>> BuilderContext<Node> {
/// Creates the [`NetworkBuilder`] for the node.
pub async fn network_builder(&self) -> eyre::Result<NetworkBuilder<(), ()>> {
pub async fn network_builder<N>(&self) -> eyre::Result<NetworkBuilder<(), (), N>>
where
N: NetworkPrimitives,
{
let network_config = self.network_config()?;
let builder = NetworkManager::builder(network_config).await?;
Ok(builder)
}

/// Returns the default network config for the node.
pub fn network_config(&self) -> eyre::Result<NetworkConfig<Node::Provider>> {
pub fn network_config<N>(&self) -> eyre::Result<NetworkConfig<Node::Provider, N>>
where
N: NetworkPrimitives,
{
let network_builder = self.network_config_builder();
Ok(self.build_network_config(network_builder?))
}

/// Get the [`NetworkConfigBuilder`].
pub fn network_config_builder(&self) -> eyre::Result<NetworkConfigBuilder> {
pub fn network_config_builder<N>(&self) -> eyre::Result<NetworkConfigBuilder<N>>
where
N: NetworkPrimitives,
{
let secret_key = self.network_secret(&self.config().datadir())?;
let default_peers_path = self.config().datadir().known_peers();
let builder = self
Expand Down
17 changes: 10 additions & 7 deletions crates/node/core/src/args/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use reth_network::{
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
},
HelloMessageWithProtocols, NetworkConfigBuilder, SessionsConfig,
HelloMessageWithProtocols, NetworkConfigBuilder, NetworkPrimitives, SessionsConfig,
};
use reth_network_peers::{mainnet_nodes, TrustedPeer};
use secp256k1::SecretKey;
Expand Down Expand Up @@ -196,13 +196,13 @@ impl NetworkArgs {
/// 1. --bootnodes flag
/// 2. Network preset flags (e.g. --holesky)
/// 3. default to mainnet nodes
pub fn network_config(
pub fn network_config<N: NetworkPrimitives>(
&self,
config: &Config,
chain_spec: impl EthChainSpec,
secret_key: SecretKey,
default_peers_file: PathBuf,
) -> NetworkConfigBuilder {
) -> NetworkConfigBuilder<N> {
let addr = self.resolved_addr();
let chain_bootnodes = self
.resolved_bootnodes()
Expand Down Expand Up @@ -230,7 +230,7 @@ impl NetworkArgs {
};

// Configure basic network stack
NetworkConfigBuilder::new(secret_key)
NetworkConfigBuilder::<N>::new(secret_key)
.peer_config(config.peers_config_with_basic_nodes_from_file(
self.persistent_peers_file(peers_file).as_deref(),
))
Expand Down Expand Up @@ -408,12 +408,15 @@ pub struct DiscoveryArgs {

impl DiscoveryArgs {
/// Apply the discovery settings to the given [`NetworkConfigBuilder`]
pub fn apply_to_builder(
pub fn apply_to_builder<N>(
&self,
mut network_config_builder: NetworkConfigBuilder,
mut network_config_builder: NetworkConfigBuilder<N>,
rlpx_tcp_socket: SocketAddr,
boot_nodes: impl IntoIterator<Item = NodeRecord>,
) -> NetworkConfigBuilder {
) -> NetworkConfigBuilder<N>
where
N: NetworkPrimitives,
{
if self.disable_discovery || self.disable_dns_discovery {
network_config_builder = network_config_builder.disable_dns_discovery();
}
Expand Down
9 changes: 6 additions & 3 deletions crates/optimism/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use reth_node_core::{
version::{LONG_VERSION, SHORT_VERSION},
};
use reth_optimism_evm::OpExecutorProvider;
use reth_optimism_node::OpNode;
use reth_optimism_node::{OpNetworkPrimitives, OpNode};
use reth_tracing::FileWorkerGuard;
use tracing::info;

Expand Down Expand Up @@ -164,9 +164,12 @@ where
Commands::DumpGenesis(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Db(command) => runner.run_blocking_until_ctrl_c(command.execute::<OpNode>()),
Commands::Stage(command) => runner.run_command_until_exit(|ctx| {
command.execute::<OpNode, _, _>(ctx, OpExecutorProvider::optimism)
command
.execute::<OpNode, _, _, OpNetworkPrimitives>(ctx, OpExecutorProvider::optimism)
}),
Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()),
Commands::P2P(command) => {
runner.run_until_ctrl_c(command.execute::<OpNetworkPrimitives>())
}
Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Recover(command) => {
runner.run_command_until_exit(|ctx| command.execute::<OpNode>(ctx))
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod engine;
pub use engine::OpEngineTypes;

pub mod node;
pub use node::OpNode;
pub use node::{OpNetworkPrimitives, OpNode};

pub mod txpool;

Expand Down
22 changes: 18 additions & 4 deletions crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGenera
use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks};
use reth_db::transaction::{DbTx, DbTxMut};
use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm};
use reth_network::{EthNetworkPrimitives, NetworkConfig, NetworkHandle, NetworkManager, PeersInfo};
use reth_network::{NetworkConfig, NetworkHandle, NetworkManager, NetworkPrimitives, PeersInfo};
use reth_node_api::{AddOnsContext, EngineValidator, FullNodeComponents, NodeAddOns, TxTy};
use reth_node_builder::{
components::{
Expand Down Expand Up @@ -621,7 +621,7 @@ impl OpNetworkBuilder {
pub fn network_config<Node>(
&self,
ctx: &BuilderContext<Node>,
) -> eyre::Result<NetworkConfig<<Node as FullNodeTypes>::Provider>>
) -> eyre::Result<NetworkConfig<<Node as FullNodeTypes>::Provider, OpNetworkPrimitives>>
where
Node: FullNodeTypes<Types: NodeTypes<ChainSpec: Hardforks>>,
{
Expand Down Expand Up @@ -670,13 +670,13 @@ where
> + Unpin
+ 'static,
{
type Primitives = EthNetworkPrimitives;
type Primitives = OpNetworkPrimitives;

async fn build_network(
self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> eyre::Result<NetworkHandle> {
) -> eyre::Result<NetworkHandle<Self::Primitives>> {
let network_config = self.network_config(ctx)?;
let network = NetworkManager::builder(network_config).await?;
let handle = ctx.start_network(network, pool);
Expand Down Expand Up @@ -722,3 +722,17 @@ where
Ok(OpEngineValidator::new(ctx.config.chain.clone()))
}
}

/// Network primitive types used by Optimism networks.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct OpNetworkPrimitives;

impl NetworkPrimitives for OpNetworkPrimitives {
type BlockHeader = alloy_consensus::Header;
type BlockBody = reth_primitives::BlockBody;
type Block = reth_primitives::Block;
type BroadcastedTransaction = reth_primitives::TransactionSigned;
type PooledTransaction = reth_primitives::PooledTransaction;
type Receipt = reth_primitives::Receipt;
}
Loading