feat(examples): add custom NetworkPrimitives and Networkbuilder to custom-node example (#15468)

This commit is contained in:
Federico Gimenez
2025-04-02 20:37:55 +02:00
committed by GitHub
parent 98692cf1f1
commit 60867680fd
4 changed files with 102 additions and 0 deletions

2
Cargo.lock generated
View File

@@ -3477,6 +3477,7 @@ dependencies = [
"reth-chain-state",
"reth-chainspec",
"reth-codecs",
"reth-network",
"reth-network-peers",
"reth-node-api",
"reth-node-builder",
@@ -3488,6 +3489,7 @@ dependencies = [
"reth-primitives-traits",
"reth-rpc-api",
"reth-rpc-engine-api",
"reth-transaction-pool",
"revm-primitives",
"serde",
]

View File

@@ -10,6 +10,7 @@ license.workspace = true
reth-chain-state.workspace = true
reth-chainspec.workspace = true
reth-codecs.workspace = true
reth-network.workspace = true
reth-network-peers.workspace = true
reth-node-api.workspace = true
reth-node-builder.workspace = true
@@ -21,6 +22,7 @@ reth-payload-builder.workspace = true
reth-primitives-traits.workspace = true
reth-rpc-api.workspace = true
reth-rpc-engine-api.workspace = true
reth-transaction-pool.workspace = true
# revm
revm-primitives.workspace = true
@@ -55,4 +57,5 @@ arbitrary = [
"reth-optimism-primitives/arbitrary",
"reth-primitives-traits/arbitrary",
"revm-primitives/arbitrary",
"reth-transaction-pool/arbitrary",
]

View File

@@ -20,6 +20,7 @@ use reth_optimism_node::{
pub mod chainspec;
pub mod engine;
pub mod engine_api;
pub mod network;
pub mod primitives;
#[derive(Debug, Clone)]

View File

@@ -0,0 +1,96 @@
use crate::{
chainspec::CustomChainSpec,
primitives::{CustomHeader, CustomNodePrimitives},
};
use alloy_consensus::{Block, BlockBody};
use eyre::Result;
use op_alloy_consensus::OpPooledTransaction;
use reth_chainspec::{EthChainSpec, Hardforks};
use reth_network::{NetworkConfig, NetworkHandle, NetworkManager, NetworkPrimitives};
use reth_node_api::{FullNodeTypes, NodeTypes, TxTy};
use reth_node_builder::{components::NetworkBuilder, BuilderContext};
use reth_optimism_primitives::{OpReceipt, OpTransactionSigned};
use reth_transaction_pool::{PoolTransaction, TransactionPool};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct CustomNetworkPrimitives;
impl NetworkPrimitives for CustomNetworkPrimitives {
type BlockHeader = CustomHeader;
type BlockBody = BlockBody<OpTransactionSigned, CustomHeader>;
type Block = Block<OpTransactionSigned, CustomHeader>;
type BroadcastedTransaction = OpTransactionSigned;
type PooledTransaction = OpPooledTransaction;
type Receipt = OpReceipt;
}
#[derive(Default)]
pub struct CustomNetworkBuilder {}
impl CustomNetworkBuilder {
fn network_config<Node>(
&self,
ctx: &BuilderContext<Node>,
) -> eyre::Result<NetworkConfig<<Node as FullNodeTypes>::Provider, CustomNetworkPrimitives>>
where
Node: FullNodeTypes<Types: NodeTypes<ChainSpec: Hardforks>>,
{
let args = &ctx.config().network;
let network_builder = ctx
.network_config_builder()?
// apply discovery settings
.apply(|mut builder| {
let rlpx_socket = (args.addr, args.port).into();
if args.discovery.disable_discovery {
builder = builder.disable_discv4_discovery();
}
if !args.discovery.disable_discovery {
builder = builder.discovery_v5(
args.discovery.discovery_v5_builder(
rlpx_socket,
ctx.config()
.network
.resolved_bootnodes()
.or_else(|| ctx.chain_spec().bootnodes())
.unwrap_or_default(),
),
);
}
builder
});
let network_config = ctx.build_network_config(network_builder);
Ok(network_config)
}
}
impl<Node, Pool> NetworkBuilder<Node, Pool> for CustomNetworkBuilder
where
Node: FullNodeTypes<
Types: NodeTypes<ChainSpec = CustomChainSpec, Primitives = CustomNodePrimitives>,
>,
Pool: TransactionPool<
Transaction: PoolTransaction<
Consensus = TxTy<Node::Types>,
Pooled = OpPooledTransaction,
>,
> + Unpin
+ 'static,
{
type Primitives = CustomNetworkPrimitives;
async fn build_network(
self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> 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);
Ok(handle)
}
}