Skip to content

Commit

Permalink
feat: introduce OpNetworkPrimitives
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Dec 12, 2024
1 parent 328d493 commit 6aca2ce
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/net/eth-wire-types/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait NetworkPrimitives:
+ 'static;
}

/// Primitive types used by Ethereum network.
/// 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 @@ -740,11 +740,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 @@ -753,20 +754,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
18 changes: 10 additions & 8 deletions crates/node/core/src/args/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use reth_network::{
TransactionFetcherConfig, TransactionsManagerConfig,
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 +195,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 +229,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 +407,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
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::{NetworkPrimitives, NetworkConfig, NetworkHandle, NetworkManager, PeersInfo};
use reth_node_api::{AddOnsContext, EngineValidator, FullNodeComponents, NodeAddOns, TxTy};
use reth_node_builder::{
components::{
Expand Down Expand Up @@ -604,7 +604,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 @@ -656,13 +656,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 @@ -708,3 +708,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::PooledTransactionsElement;
type Receipt = reth_primitives::Receipt;
}

0 comments on commit 6aca2ce

Please sign in to comment.