refactor: introduce BasicPayloadServiceBuilder (#14700)

This commit is contained in:
Arsenii Kulikov
2025-02-26 03:45:07 +04:00
committed by GitHub
parent 75ca54b790
commit 77aa17fb57
22 changed files with 142 additions and 162 deletions

View File

@@ -82,9 +82,6 @@ pub trait FullNodeComponents: FullNodeTypes + Clone + 'static {
/// Network API.
type Network: FullNetwork;
/// Builds new blocks.
type PayloadBuilder: PayloadBuilderFor<Self::Types>;
/// Returns the transaction pool of the node.
fn pool(&self) -> &Self::Pool;
@@ -100,9 +97,6 @@ pub trait FullNodeComponents: FullNodeTypes + Clone + 'static {
/// Returns the handle to the network
fn network(&self) -> &Self::Network;
/// Returns the configured payload builder.
fn payload_builder(&self) -> &Self::PayloadBuilder;
/// Returns the handle to the payload builder service handling payload building requests from
/// the engine.
fn payload_builder_handle(

View File

@@ -93,7 +93,6 @@ impl<T: FullNodeTypes, C: NodeComponents<T>> FullNodeComponents for NodeAdapter<
type Executor = C::Executor;
type Consensus = C::Consensus;
type Network = C::Network;
type PayloadBuilder = C::PayloadBuilder;
fn pool(&self) -> &Self::Pool {
self.components.pool()
@@ -115,10 +114,6 @@ impl<T: FullNodeTypes, C: NodeComponents<T>> FullNodeComponents for NodeAdapter<
self.components.network()
}
fn payload_builder(&self) -> &Self::PayloadBuilder {
self.components.payload_builder()
}
fn payload_builder_handle(
&self,
) -> &reth_payload_builder::PayloadBuilderHandle<

View File

@@ -14,8 +14,6 @@ use reth_node_api::{BlockTy, BodyTy, HeaderTy, PrimitivesTy, TxTy};
use reth_transaction_pool::{PoolTransaction, TransactionPool};
use std::{future::Future, marker::PhantomData};
use super::PayloadBuilderFor;
/// A generic, general purpose and customizable [`NodeComponentsBuilder`] implementation.
///
/// This type is stateful and captures the configuration of the node's components.
@@ -325,7 +323,6 @@ where
ExecB::EVM,
ExecB::Executor,
ConsB::Consensus,
PayloadB::PayloadBuilder,
>;
async fn build_components(
@@ -334,7 +331,7 @@ where
) -> eyre::Result<Self::Components> {
let Self {
pool_builder,
payload_builder: payload_builder_builder,
payload_builder,
network_builder,
executor_builder: evm_builder,
consensus_builder,
@@ -344,17 +341,14 @@ where
let (evm_config, executor) = evm_builder.build_evm(context).await?;
let pool = pool_builder.build_pool(context).await?;
let network = network_builder.build_network(context, pool.clone()).await?;
let payload_builder =
payload_builder_builder.build_payload_builder(context, pool.clone()).await?;
let payload_builder_handle = payload_builder_builder
.spawn_payload_builder_service(context, payload_builder.clone())?;
let payload_builder_handle =
payload_builder.spawn_payload_builder_service(context, pool.clone()).await?;
let consensus = consensus_builder.build_consensus(context).await?;
Ok(Components {
transaction_pool: pool,
evm_config,
network,
payload_builder,
payload_builder_handle,
executor,
consensus,
@@ -395,7 +389,7 @@ pub trait NodeComponentsBuilder<Node: FullNodeTypes>: Send {
) -> impl Future<Output = eyre::Result<Self::Components>> + Send;
}
impl<Node, N, F, Fut, Pool, EVM, Executor, Cons, Payload> NodeComponentsBuilder<Node> for F
impl<Node, N, F, Fut, Pool, EVM, Executor, Cons> NodeComponentsBuilder<Node> for F
where
N: NetworkPrimitives<
BlockHeader = HeaderTy<Node::Types>,
@@ -404,8 +398,7 @@ where
>,
Node: FullNodeTypes,
F: FnOnce(&BuilderContext<Node>) -> Fut + Send,
Fut: Future<Output = eyre::Result<Components<Node, N, Pool, EVM, Executor, Cons, Payload>>>
+ Send,
Fut: Future<Output = eyre::Result<Components<Node, N, Pool, EVM, Executor, Cons>>> + Send,
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>>
+ Unpin
+ 'static,
@@ -413,9 +406,8 @@ where
Executor: BlockExecutorProvider<Primitives = PrimitivesTy<Node::Types>>,
Cons:
FullConsensus<PrimitivesTy<Node::Types>, Error = ConsensusError> + Clone + Unpin + 'static,
Payload: PayloadBuilderFor<Node::Types> + Unpin + 'static,
{
type Components = Components<Node, N, Pool, EVM, Executor, Cons, Payload>;
type Components = Components<Node, N, Pool, EVM, Executor, Cons>;
fn build_components(
self,

View File

@@ -29,8 +29,7 @@ use reth_evm::execute::{BlockExecutionStrategyFactory, BlockExecutorProvider};
use reth_network::{NetworkHandle, NetworkPrimitives};
use reth_network_api::FullNetwork;
use reth_node_api::{
BlockTy, BodyTy, HeaderTy, NodeTypes, NodeTypesWithEngine, PayloadBuilderFor, PrimitivesTy,
TxTy,
BlockTy, BodyTy, HeaderTy, NodeTypes, NodeTypesWithEngine, PrimitivesTy, TxTy,
};
use reth_transaction_pool::{PoolTransaction, TransactionPool};
@@ -58,9 +57,6 @@ pub trait NodeComponents<T: FullNodeTypes>: Clone + Unpin + Send + Sync + 'stati
/// Network API.
type Network: FullNetwork<Client: BlockClient<Block = BlockTy<T::Types>>>;
/// Builds new blocks.
type PayloadBuilder: PayloadBuilderFor<T::Types> + Clone + Unpin + 'static;
/// Returns the transaction pool of the node.
fn pool(&self) -> &Self::Pool;
@@ -76,9 +72,6 @@ pub trait NodeComponents<T: FullNodeTypes>: Clone + Unpin + Send + Sync + 'stati
/// Returns the handle to the network
fn network(&self) -> &Self::Network;
/// Returns the payload builder that knows how to build blocks.
fn payload_builder(&self) -> &Self::PayloadBuilder;
/// Returns the handle to the payload builder service handling payload building requests from
/// the engine.
fn payload_builder_handle(
@@ -90,15 +83,7 @@ pub trait NodeComponents<T: FullNodeTypes>: Clone + Unpin + Send + Sync + 'stati
///
/// This provides access to all the components of the node.
#[derive(Debug)]
pub struct Components<
Node: FullNodeTypes,
N: NetworkPrimitives,
Pool,
EVM,
Executor,
Consensus,
Payload,
> {
pub struct Components<Node: FullNodeTypes, N: NetworkPrimitives, Pool, EVM, Executor, Consensus> {
/// The transaction pool of the node.
pub transaction_pool: Pool,
/// The node's EVM configuration, defining settings for the Ethereum Virtual Machine.
@@ -109,14 +94,12 @@ pub struct Components<
pub consensus: Consensus,
/// The network implementation of the node.
pub network: NetworkHandle<N>,
/// The payload builder.
pub payload_builder: Payload,
/// The handle to the payload builder service.
pub payload_builder_handle: PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine>,
}
impl<Node, Pool, EVM, Executor, Cons, N, Payload> NodeComponents<Node>
for Components<Node, N, Pool, EVM, Executor, Cons, Payload>
impl<Node, Pool, EVM, Executor, Cons, N> NodeComponents<Node>
for Components<Node, N, Pool, EVM, Executor, Cons>
where
Node: FullNodeTypes,
N: NetworkPrimitives<
@@ -131,14 +114,12 @@ where
Executor: BlockExecutorProvider<Primitives = PrimitivesTy<Node::Types>>,
Cons:
FullConsensus<PrimitivesTy<Node::Types>, Error = ConsensusError> + Clone + Unpin + 'static,
Payload: PayloadBuilderFor<Node::Types> + Clone + Unpin + 'static,
{
type Pool = Pool;
type Evm = EVM;
type Executor = Executor;
type Consensus = Cons;
type Network = NetworkHandle<N>;
type PayloadBuilder = Payload;
fn pool(&self) -> &Self::Pool {
&self.transaction_pool
@@ -160,10 +141,6 @@ where
&self.network
}
fn payload_builder(&self) -> &Self::PayloadBuilder {
&self.payload_builder
}
fn payload_builder_handle(
&self,
) -> &PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine> {
@@ -171,8 +148,7 @@ where
}
}
impl<Node, N, Pool, EVM, Executor, Cons, Payload> Clone
for Components<Node, N, Pool, EVM, Executor, Cons, Payload>
impl<Node, N, Pool, EVM, Executor, Cons> Clone for Components<Node, N, Pool, EVM, Executor, Cons>
where
N: NetworkPrimitives,
Node: FullNodeTypes,
@@ -180,7 +156,6 @@ where
EVM: ConfigureEvm,
Executor: BlockExecutorProvider,
Cons: Clone,
Payload: Clone,
{
fn clone(&self) -> Self {
Self {
@@ -189,7 +164,6 @@ where
executor: self.executor.clone(),
consensus: self.consensus.clone(),
network: self.network.clone(),
payload_builder: self.payload_builder.clone(),
payload_builder_handle: self.payload_builder_handle.clone(),
}
}

View File

@@ -10,18 +10,6 @@ use std::future::Future;
/// A type that knows how to spawn the payload service.
pub trait PayloadServiceBuilder<Node: FullNodeTypes, Pool: TransactionPool>: Send + Sized {
/// Payload builder implementation.
type PayloadBuilder: PayloadBuilderFor<Node::Types> + Unpin + 'static;
/// Spawns the payload service and returns the handle to it.
///
/// The [`BuilderContext`] is provided to allow access to the node's configuration.
fn build_payload_builder(
&self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> impl Future<Output = eyre::Result<Self::PayloadBuilder>> + Send;
/// Spawns the [`PayloadBuilderService`] and returns the handle to it for use by the engine.
///
/// We provide default implementation via [`BasicPayloadJobGenerator`] but it can be overridden
@@ -29,8 +17,73 @@ pub trait PayloadServiceBuilder<Node: FullNodeTypes, Pool: TransactionPool>: Sen
fn spawn_payload_builder_service(
self,
ctx: &BuilderContext<Node>,
payload_builder: Self::PayloadBuilder,
pool: Pool,
) -> impl Future<
Output = eyre::Result<PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine>>,
> + Send;
}
impl<Node, F, Fut, Pool> PayloadServiceBuilder<Node, Pool> for F
where
Node: FullNodeTypes,
Pool: TransactionPool,
F: Fn(&BuilderContext<Node>, Pool) -> Fut + Send,
Fut: Future<
Output = eyre::Result<
PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine>,
>,
> + Send,
{
fn spawn_payload_builder_service(
self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> impl Future<
Output = eyre::Result<PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine>>,
> {
self(ctx, pool)
}
}
/// A type that knows how to build a payload builder to plug into [`BasicPayloadServiceBuilder`].
pub trait PayloadBuilderBuilder<Node: FullNodeTypes, Pool: TransactionPool>: Send + Sized {
/// Payload builder implementation.
type PayloadBuilder: PayloadBuilderFor<Node::Types> + Unpin + 'static;
/// Spawns the payload service and returns the handle to it.
///
/// The [`BuilderContext`] is provided to allow access to the node's configuration.
fn build_payload_builder(
self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> impl Future<Output = eyre::Result<Self::PayloadBuilder>> + Send;
}
/// Basic payload service builder that spawns a [`BasicPayloadJobGenerator`]
#[derive(Debug, Default, Clone)]
pub struct BasicPayloadServiceBuilder<PB>(PB);
impl<PB> BasicPayloadServiceBuilder<PB> {
/// Create a new [`BasicPayloadServiceBuilder`].
pub fn new(payload_builder_builder: PB) -> Self {
Self(payload_builder_builder)
}
}
impl<Node, Pool, PB> PayloadServiceBuilder<Node, Pool> for BasicPayloadServiceBuilder<PB>
where
Node: FullNodeTypes,
Pool: TransactionPool,
PB: PayloadBuilderBuilder<Node, Pool>,
{
async fn spawn_payload_builder_service(
self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> eyre::Result<PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine>> {
let payload_builder = self.0.build_payload_builder(ctx, pool).await?;
let conf = ctx.config().builder.clone();
let payload_job_config = BasicPayloadJobGeneratorConfig::default()
@@ -52,22 +105,3 @@ pub trait PayloadServiceBuilder<Node: FullNodeTypes, Pool: TransactionPool>: Sen
Ok(payload_service_handle)
}
}
impl<Node, F, Fut, Pool, Builder> PayloadServiceBuilder<Node, Pool> for F
where
Node: FullNodeTypes,
Pool: TransactionPool,
F: Fn(&BuilderContext<Node>, Pool) -> Fut + Send,
Fut: Future<Output = eyre::Result<Builder>> + Send,
Builder: PayloadBuilderFor<Node::Types> + Unpin + 'static,
{
type PayloadBuilder = Builder;
fn build_payload_builder(
&self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> impl Future<Output = eyre::Result<Self::PayloadBuilder>> + Send {
self(ctx, pool)
}
}

View File

@@ -411,7 +411,6 @@ where
pool: ctx.components().pool().clone(),
network: ctx.components().network().clone(),
provider: ctx.node_adapter().provider.clone(),
payload_builder: ctx.components().payload_builder().clone(),
payload_builder_handle: ctx.components().payload_builder_handle().clone(),
task_executor: ctx.task_executor().clone(),
config: ctx.node_config().clone(),

View File

@@ -117,8 +117,6 @@ pub struct FullNode<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> {
pub network: Node::Network,
/// Provider to interact with the node's database
pub provider: Node::Provider,
/// Node's configured payload builder.
pub payload_builder: Node::PayloadBuilder,
/// Handle to the node's payload builder service.
pub payload_builder_handle: PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine>,
/// Task executor for the node.
@@ -139,7 +137,6 @@ impl<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> Clone for FullNode<Node
pool: self.pool.clone(),
network: self.network.clone(),
provider: self.provider.clone(),
payload_builder: self.payload_builder.clone(),
payload_builder_handle: self.payload_builder_handle.clone(),
task_executor: self.task_executor.clone(),
config: self.config.clone(),

View File

@@ -18,7 +18,7 @@ use reth_node_core::{
node_config::NodeConfig,
version::{CARGO_PKG_VERSION, CLIENT_CODE, NAME_CLIENT, VERGEN_GIT_SHA},
};
use reth_payload_builder::PayloadStore;
use reth_payload_builder::{PayloadBuilderHandle, PayloadStore};
use reth_provider::ChainSpecProvider;
use reth_rpc::{
eth::{EthApiTypes, FullEthApiServer},
@@ -293,8 +293,10 @@ where
}
/// Returns the handle to the payload builder service
pub fn payload_builder(&self) -> &Node::PayloadBuilder {
self.node.payload_builder()
pub fn payload_builder_handle(
&self,
) -> &PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine> {
self.node.payload_builder_handle()
}
}