diff --git a/Cargo.lock b/Cargo.lock index 7813960ad..dc9d22480 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6510,7 +6510,6 @@ dependencies = [ "reth-provider", "reth-revm", "reth-tasks", - "reth-transaction-pool", "revm", "tokio", "tracing", diff --git a/bin/reth/src/commands/debug_cmd/build_block.rs b/bin/reth/src/commands/debug_cmd/build_block.rs index 84a800236..a4a428708 100644 --- a/bin/reth/src/commands/debug_cmd/build_block.rs +++ b/bin/reth/src/commands/debug_cmd/build_block.rs @@ -216,8 +216,6 @@ impl> Command { ); let args = BuildArguments::new( - blockchain_db.clone(), - transaction_pool, CachedReads::default(), payload_config, CancelOnDrop::default(), @@ -225,6 +223,8 @@ impl> Command { ); let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::new( + blockchain_db.clone(), + transaction_pool, EthEvmConfig::new(provider_factory.chain_spec()), EthereumBuilderConfig::new(Default::default()), ); diff --git a/crates/ethereum/node/src/payload.rs b/crates/ethereum/node/src/payload.rs index 7bfb62cdd..8c6d3e3e6 100644 --- a/crates/ethereum/node/src/payload.rs +++ b/crates/ethereum/node/src/payload.rs @@ -45,6 +45,8 @@ impl EthereumPayloadBuilder { { let conf = ctx.payload_builder_config(); let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::new( + ctx.provider().clone(), + pool, evm_config, EthereumBuilderConfig::new(conf.extra_data_bytes()).with_gas_limit(conf.gas_limit()), ); @@ -56,7 +58,6 @@ impl EthereumPayloadBuilder { let payload_generator = BasicPayloadJobGenerator::with_builder( ctx.provider().clone(), - pool, ctx.task_executor().clone(), payload_job_config, payload_builder, diff --git a/crates/ethereum/payload/src/lib.rs b/crates/ethereum/payload/src/lib.rs index b6c199ac1..7f0d770c1 100644 --- a/crates/ethereum/payload/src/lib.rs +++ b/crates/ethereum/payload/src/lib.rs @@ -37,8 +37,8 @@ use reth_primitives_traits::{ use reth_revm::database::StateProviderDatabase; use reth_storage_api::StateProviderFactory; use reth_transaction_pool::{ - error::InvalidPoolTransactionError, noop::NoopTransactionPool, BestTransactions, - BestTransactionsAttributes, PoolTransaction, TransactionPool, ValidPoolTransaction, + error::InvalidPoolTransactionError, BestTransactions, BestTransactionsAttributes, + PoolTransaction, TransactionPool, ValidPoolTransaction, }; use revm::{ db::{states::bundle_state::BundleRetention, State}, @@ -58,21 +58,30 @@ type BestTransactionsIter = Box< /// Ethereum payload builder #[derive(Debug, Clone, PartialEq, Eq)] -pub struct EthereumPayloadBuilder { +pub struct EthereumPayloadBuilder { + /// Client providing access to node state. + client: Client, + /// Transaction pool. + pool: Pool, /// The type responsible for creating the evm. evm_config: EvmConfig, /// Payload builder configuration. builder_config: EthereumBuilderConfig, } -impl EthereumPayloadBuilder { +impl EthereumPayloadBuilder { /// `EthereumPayloadBuilder` constructor. - pub const fn new(evm_config: EvmConfig, builder_config: EthereumBuilderConfig) -> Self { - Self { evm_config, builder_config } + pub const fn new( + client: Client, + pool: Pool, + evm_config: EvmConfig, + builder_config: EthereumBuilderConfig, + ) -> Self { + Self { client, pool, evm_config, builder_config } } } -impl EthereumPayloadBuilder +impl EthereumPayloadBuilder where EvmConfig: ConfigureEvm
, { @@ -94,10 +103,10 @@ where } // Default implementation of [PayloadBuilder] for unit type -impl PayloadBuilder for EthereumPayloadBuilder +impl PayloadBuilder for EthereumPayloadBuilder where EvmConfig: ConfigureEvm
, - Client: StateProviderFactory + ChainSpecProvider, + Client: StateProviderFactory + ChainSpecProvider + Clone, Pool: TransactionPool>, { type Attributes = EthPayloadBuilderAttributes; @@ -105,49 +114,41 @@ where fn try_build( &self, - args: BuildArguments, + args: BuildArguments, ) -> Result, PayloadBuilderError> { let evm_env = self .evm_env(&args.config, &args.config.parent_header) .map_err(PayloadBuilderError::other)?; - let pool = args.pool.clone(); default_ethereum_payload( self.evm_config.clone(), + self.client.clone(), + self.pool.clone(), self.builder_config.clone(), args, evm_env, - |attributes| pool.best_transactions_with_attributes(attributes), + |attributes| self.pool.best_transactions_with_attributes(attributes), ) } fn build_empty_payload( &self, - client: &Client, config: PayloadConfig, ) -> Result { - let args = BuildArguments::new( - client, - // we use defaults here because for the empty payload we don't need to execute anything - NoopTransactionPool::default(), - Default::default(), - config, - Default::default(), - None, - ); + let args = BuildArguments::new(Default::default(), config, Default::default(), None); let evm_env = self .evm_env(&args.config, &args.config.parent_header) .map_err(PayloadBuilderError::other)?; - let pool = args.pool.clone(); - default_ethereum_payload( self.evm_config.clone(), + self.client.clone(), + self.pool.clone(), self.builder_config.clone(), args, evm_env, - |attributes| pool.best_transactions_with_attributes(attributes), + |attributes| self.pool.best_transactions_with_attributes(attributes), )? .into_payload() .ok_or_else(|| PayloadBuilderError::MissingPayload) @@ -160,10 +161,12 @@ where /// and configuration, this function creates a transaction payload. Returns /// a result indicating success with the payload or an error in case of failure. #[inline] -pub fn default_ethereum_payload( +pub fn default_ethereum_payload( evm_config: EvmConfig, + client: Client, + pool: Pool, builder_config: EthereumBuilderConfig, - args: BuildArguments, + args: BuildArguments, evm_env: EvmEnv, best_txs: F, ) -> Result, PayloadBuilderError> @@ -173,7 +176,7 @@ where Pool: TransactionPool>, F: FnOnce(BestTransactionsAttributes) -> BestTransactionsIter, { - let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args; + let BuildArguments { mut cached_reads, config, cancel, best_payload } = args; let chain_spec = client.chain_spec(); let state_provider = client.state_by_block_hash(config.parent_header.hash())?; diff --git a/crates/optimism/node/src/node.rs b/crates/optimism/node/src/node.rs index 83073673a..53976cf24 100644 --- a/crates/optimism/node/src/node.rs +++ b/crates/optimism/node/src/node.rs @@ -210,6 +210,8 @@ where let Self { rpc_add_ons, da_config } = self; let builder = reth_optimism_payload_builder::OpPayloadBuilder::new( + ctx.node.pool().clone(), + ctx.node.provider().clone(), ctx.node.evm_config().clone(), BasicOpReceiptBuilder::default(), ); @@ -507,6 +509,8 @@ where Txs: OpPayloadTransactions>, { let payload_builder = reth_optimism_payload_builder::OpPayloadBuilder::with_builder_config( + pool, + ctx.provider().clone(), evm_config, BasicOpReceiptBuilder::default(), OpBuilderConfig { da_config: self.da_config }, @@ -522,7 +526,6 @@ where let payload_generator = BasicPayloadJobGenerator::with_builder( ctx.provider().clone(), - pool, ctx.task_executor().clone(), payload_job_config, payload_builder, diff --git a/crates/optimism/payload/src/builder.rs b/crates/optimism/payload/src/builder.rs index cbb97bf9e..eb3b71a41 100644 --- a/crates/optimism/payload/src/builder.rs +++ b/crates/optimism/payload/src/builder.rs @@ -59,12 +59,16 @@ use tracing::{debug, trace, warn}; /// Optimism's payload builder #[derive(Debug, Clone)] -pub struct OpPayloadBuilder { +pub struct OpPayloadBuilder { /// The rollup's compute pending block configuration option. // TODO(clabby): Implement this feature. pub compute_pending_block: bool, /// The type responsible for creating the evm. pub evm_config: EvmConfig, + /// Transaction pool. + pub pool: Pool, + /// Node client. + pub client: Client, /// Settings for the builder, e.g. DA settings. pub config: OpBuilderConfig, /// The type responsible for yielding the best transactions for the payload if mempool @@ -74,24 +78,30 @@ pub struct OpPayloadBuilder { pub receipt_builder: Arc>, } -impl OpPayloadBuilder { +impl OpPayloadBuilder { /// `OpPayloadBuilder` constructor. /// /// Configures the builder with the default settings. pub fn new( + pool: Pool, + client: Client, evm_config: EvmConfig, receipt_builder: impl OpReceiptBuilder, ) -> Self { - Self::with_builder_config(evm_config, receipt_builder, Default::default()) + Self::with_builder_config(pool, client, evm_config, receipt_builder, Default::default()) } /// Configures the builder with the given [`OpBuilderConfig`]. pub fn with_builder_config( + pool: Pool, + client: Client, evm_config: EvmConfig, receipt_builder: impl OpReceiptBuilder, config: OpBuilderConfig, ) -> Self { Self { + pool, + client, compute_pending_block: true, receipt_builder: Arc::new(receipt_builder), evm_config, @@ -101,7 +111,9 @@ impl OpPayloadBuilder { } } -impl OpPayloadBuilder { +impl + OpPayloadBuilder +{ /// Sets the rollup's compute pending block configuration option. pub const fn set_compute_pending_block(mut self, compute_pending_block: bool) -> Self { self.compute_pending_block = compute_pending_block; @@ -113,9 +125,13 @@ impl OpPayloadBuilder { pub fn with_transactions( self, best_transactions: T, - ) -> OpPayloadBuilder { - let Self { compute_pending_block, evm_config, config, receipt_builder, .. } = self; + ) -> OpPayloadBuilder { + let Self { + pool, client, compute_pending_block, evm_config, config, receipt_builder, .. + } = self; OpPayloadBuilder { + pool, + client, compute_pending_block, evm_config, best_transactions, @@ -134,8 +150,9 @@ impl OpPayloadBuilder { self.compute_pending_block } } -impl OpPayloadBuilder +impl OpPayloadBuilder where + Client: StateProviderFactory + ChainSpecProvider, N: OpPayloadPrimitives, EvmConfig: ConfigureEvmFor, { @@ -147,31 +164,24 @@ where /// Given build arguments including an Optimism client, transaction pool, /// and configuration, this function creates a transaction payload. Returns /// a result indicating success with the payload or an error in case of failure. - fn build_payload<'a, Client, Pool, Txs>( + fn build_payload<'a, Txs>( &self, - args: BuildArguments< - Pool, - Client, - OpPayloadBuilderAttributes, - OpBuiltPayload, - >, + args: BuildArguments, OpBuiltPayload>, best: impl FnOnce(BestTransactionsAttributes) -> Txs + Send + Sync + 'a, ) -> Result>, PayloadBuilderError> where - Client: StateProviderFactory + ChainSpecProvider, Txs: PayloadTransactions, { let evm_env = self .evm_env(&args.config.attributes, &args.config.parent_header) .map_err(PayloadBuilderError::other)?; - let BuildArguments { client, pool: _, mut cached_reads, config, cancel, best_payload } = - args; + let BuildArguments { mut cached_reads, config, cancel, best_payload } = args; let ctx = OpPayloadBuilderCtx { evm_config: self.evm_config.clone(), da_config: self.config.da_config.clone(), - chain_spec: client.chain_spec(), + chain_spec: self.client.chain_spec(), config, evm_env, cancel, @@ -181,7 +191,7 @@ where let builder = OpBuilder::new(best); - let state_provider = client.state_by_block_hash(ctx.parent().hash())?; + let state_provider = self.client.state_by_block_hash(ctx.parent().hash())?; let state = StateProviderDatabase::new(state_provider); if ctx.attributes().no_tx_pool { @@ -215,15 +225,11 @@ where } /// Computes the witness for the payload. - pub fn payload_witness( + pub fn payload_witness( &self, - client: &Client, parent: SealedHeader, attributes: OpPayloadAttributes, - ) -> Result - where - Client: StateProviderFactory + ChainSpecProvider, - { + ) -> Result { let attributes = OpPayloadBuilderAttributes::try_new(parent.hash(), attributes, 3) .map_err(PayloadBuilderError::other)?; @@ -233,7 +239,7 @@ where let ctx: OpPayloadBuilderCtx = OpPayloadBuilderCtx { evm_config: self.evm_config.clone(), da_config: self.config.da_config.clone(), - chain_spec: client.chain_spec(), + chain_spec: self.client.chain_spec(), config, evm_env, cancel: Default::default(), @@ -241,7 +247,7 @@ where receipt_builder: self.receipt_builder.clone(), }; - let state_provider = client.state_by_block_hash(ctx.parent().hash())?; + let state_provider = self.client.state_by_block_hash(ctx.parent().hash())?; let state = StateProviderDatabase::new(state_provider); let mut state = State::builder().with_database(state).with_bundle_update().build(); @@ -251,10 +257,10 @@ where } /// Implementation of the [`PayloadBuilder`] trait for [`OpPayloadBuilder`]. -impl PayloadBuilder - for OpPayloadBuilder +impl PayloadBuilder + for OpPayloadBuilder where - Client: StateProviderFactory + ChainSpecProvider, + Client: StateProviderFactory + ChainSpecProvider + Clone, N: OpPayloadPrimitives, Pool: TransactionPool>, EvmConfig: ConfigureEvmFor, @@ -265,15 +271,15 @@ where fn try_build( &self, - args: BuildArguments, + args: BuildArguments, ) -> Result, PayloadBuilderError> { - let pool = args.pool.clone(); + let pool = self.pool.clone(); self.build_payload(args, |attrs| self.best_transactions.best_transactions(pool, attrs)) } fn on_missing_payload( &self, - _args: BuildArguments, + _args: BuildArguments, ) -> MissingPayloadBehaviour { // we want to await the job that's already in progress because that should be returned as // is, there's no benefit in racing another job @@ -284,14 +290,10 @@ where // system txs, hence on_missing_payload we return [MissingPayloadBehaviour::AwaitInProgress]. fn build_empty_payload( &self, - client: &Client, config: PayloadConfig, ) -> Result { let args = BuildArguments { - client, config, - // we use defaults here because for the empty payload we don't need to execute anything - pool: (), cached_reads: Default::default(), cancel: Default::default(), best_payload: None, diff --git a/crates/optimism/rpc/src/witness.rs b/crates/optimism/rpc/src/witness.rs index 2acd4cd47..ea6cf602c 100644 --- a/crates/optimism/rpc/src/witness.rs +++ b/crates/optimism/rpc/src/witness.rs @@ -19,16 +19,18 @@ use std::{fmt::Debug, sync::Arc}; use tokio::sync::{oneshot, Semaphore}; /// An extension to the `debug_` namespace of the RPC API. -pub struct OpDebugWitnessApi { - inner: Arc>, +pub struct OpDebugWitnessApi { + inner: Arc>, } -impl OpDebugWitnessApi { +impl + OpDebugWitnessApi +{ /// Creates a new instance of the `OpDebugWitnessApi`. pub fn new( provider: Provider, task_spawner: Box, - builder: OpPayloadBuilder, + builder: OpPayloadBuilder, ) -> Self { let semaphore = Arc::new(Semaphore::new(3)); let inner = OpDebugWitnessApiInner { provider, builder, task_spawner, semaphore }; @@ -36,7 +38,7 @@ impl OpDebugWitnessApi OpDebugWitnessApi +impl OpDebugWitnessApi where Provider: NodePrimitivesProvider + BlockReaderIdExt
, { @@ -50,9 +52,10 @@ where } #[async_trait] -impl DebugExecutionWitnessApiServer - for OpDebugWitnessApi +impl DebugExecutionWitnessApiServer + for OpDebugWitnessApi where + Pool: Send + Sync + 'static, Provider: BlockReaderIdExt
+ NodePrimitivesProvider + StateProviderFactory @@ -73,8 +76,7 @@ where let (tx, rx) = oneshot::channel(); let this = self.clone(); self.inner.task_spawner.spawn_blocking(Box::pin(async move { - let res = - this.inner.builder.payload_witness(&this.inner.provider, parent_header, attributes); + let res = this.inner.builder.payload_witness(parent_header, attributes); let _ = tx.send(res); })); @@ -84,7 +86,7 @@ where } } -impl Clone for OpDebugWitnessApi +impl Clone for OpDebugWitnessApi where Provider: NodePrimitivesProvider, { @@ -92,7 +94,7 @@ where Self { inner: Arc::clone(&self.inner) } } } -impl Debug for OpDebugWitnessApi +impl Debug for OpDebugWitnessApi where Provider: NodePrimitivesProvider, { @@ -101,9 +103,9 @@ where } } -struct OpDebugWitnessApiInner { +struct OpDebugWitnessApiInner { provider: Provider, - builder: OpPayloadBuilder, + builder: OpPayloadBuilder, task_spawner: Box, semaphore: Arc, } diff --git a/crates/payload/basic/Cargo.toml b/crates/payload/basic/Cargo.toml index d8e3b0faf..7c975ef7d 100644 --- a/crates/payload/basic/Cargo.toml +++ b/crates/payload/basic/Cargo.toml @@ -16,7 +16,6 @@ workspace = true reth-chainspec.workspace = true reth-primitives.workspace = true reth-primitives-traits.workspace = true -reth-transaction-pool.workspace = true reth-provider.workspace = true reth-payload-builder.workspace = true reth-payload-builder-primitives.workspace = true diff --git a/crates/payload/basic/src/lib.rs b/crates/payload/basic/src/lib.rs index 15790ad52..70f65e24f 100644 --- a/crates/payload/basic/src/lib.rs +++ b/crates/payload/basic/src/lib.rs @@ -24,7 +24,6 @@ use reth_primitives_traits::proofs; use reth_provider::{BlockReaderIdExt, CanonStateNotification, StateProviderFactory}; use reth_revm::{cached::CachedReads, cancelled::CancelOnDrop}; use reth_tasks::TaskSpawner; -use reth_transaction_pool::TransactionPool; use revm::{Database, State}; use std::{ fmt, @@ -48,11 +47,9 @@ pub use stack::PayloadBuilderStack; /// The [`PayloadJobGenerator`] that creates [`BasicPayloadJob`]s. #[derive(Debug)] -pub struct BasicPayloadJobGenerator { +pub struct BasicPayloadJobGenerator { /// The client that can interact with the chain. client: Client, - /// The transaction pool to pull transactions from. - pool: Pool, /// The task executor to spawn payload building tasks on. executor: Tasks, /// The configuration for the job generator. @@ -69,19 +66,17 @@ pub struct BasicPayloadJobGenerator { // === impl BasicPayloadJobGenerator === -impl BasicPayloadJobGenerator { +impl BasicPayloadJobGenerator { /// Creates a new [`BasicPayloadJobGenerator`] with the given config and custom /// [`PayloadBuilder`] pub fn with_builder( client: Client, - pool: Pool, executor: Tasks, config: BasicPayloadJobGeneratorConfig, builder: Builder, ) -> Self { Self { client, - pool, executor, payload_task_guard: PayloadTaskGuard::new(config.max_payload_tasks), config, @@ -129,21 +124,20 @@ impl BasicPayloadJobGenerator PayloadJobGenerator - for BasicPayloadJobGenerator +impl PayloadJobGenerator + for BasicPayloadJobGenerator where Client: StateProviderFactory + BlockReaderIdExt
+ Clone + Unpin + 'static, - Pool: TransactionPool + Unpin + 'static, Tasks: TaskSpawner + Clone + Unpin + 'static, - Builder: PayloadBuilder + Unpin + 'static, - >::Attributes: Unpin + Clone, - >::BuiltPayload: Unpin + Clone, + Builder: PayloadBuilder + Unpin + 'static, + Builder::Attributes: Unpin + Clone, + Builder::BuiltPayload: Unpin + Clone, { - type Job = BasicPayloadJob; + type Job = BasicPayloadJob; fn new_payload_job( &self, @@ -172,8 +166,6 @@ where let mut job = BasicPayloadJob { config, - client: self.client.clone(), - pool: self.pool.clone(), executor: self.executor.clone(), deadline, // ticks immediately @@ -306,16 +298,12 @@ impl Default for BasicPayloadJobGeneratorConfig { /// built and this future will wait to be resolved: [`PayloadJob::resolve`] or terminated if the /// deadline is reached.. #[derive(Debug)] -pub struct BasicPayloadJob +pub struct BasicPayloadJob where - Builder: PayloadBuilder, + Builder: PayloadBuilder, { /// The configuration for how the payload will be created. config: PayloadConfig, - /// The client that can interact with the chain. - client: Client, - /// The transaction pool. - pool: Pool, /// How to spawn building tasks executor: Tasks, /// The deadline when this job should resolve. @@ -341,21 +329,17 @@ where builder: Builder, } -impl BasicPayloadJob +impl BasicPayloadJob where - Client: StateProviderFactory + Clone + Unpin + 'static, - Pool: TransactionPool + Unpin + 'static, Tasks: TaskSpawner + Clone + 'static, - Builder: PayloadBuilder + Unpin + 'static, - >::Attributes: Unpin + Clone, - >::BuiltPayload: Unpin + Clone, + Builder: PayloadBuilder + Unpin + 'static, + Builder::Attributes: Unpin + Clone, + Builder::BuiltPayload: Unpin + Clone, { /// Spawns a new payload build task. fn spawn_build_job(&mut self) { trace!(target: "payload_builder", id = %self.config.payload_id(), "spawn new payload build task"); let (tx, rx) = oneshot::channel(); - let client = self.client.clone(); - let pool = self.pool.clone(); let cancel = CancelOnDrop::default(); let _cancel = cancel.clone(); let guard = self.payload_task_guard.clone(); @@ -367,14 +351,8 @@ where self.executor.spawn_blocking(Box::pin(async move { // acquire the permit for executing the task let _permit = guard.acquire().await; - let args = BuildArguments { - client, - pool, - cached_reads, - config: payload_config, - cancel, - best_payload, - }; + let args = + BuildArguments { cached_reads, config: payload_config, cancel, best_payload }; let result = builder.try_build(args); let _ = tx.send(result); })); @@ -383,14 +361,12 @@ where } } -impl Future for BasicPayloadJob +impl Future for BasicPayloadJob where - Client: StateProviderFactory + Clone + Unpin + 'static, - Pool: TransactionPool + Unpin + 'static, Tasks: TaskSpawner + Clone + 'static, - Builder: PayloadBuilder + Unpin + 'static, - >::Attributes: Unpin + Clone, - >::BuiltPayload: Unpin + Clone, + Builder: PayloadBuilder + Unpin + 'static, + Builder::Attributes: Unpin + Clone, + Builder::BuiltPayload: Unpin + Clone, { type Output = Result<(), PayloadBuilderError>; @@ -448,14 +424,12 @@ where } } -impl PayloadJob for BasicPayloadJob +impl PayloadJob for BasicPayloadJob where - Client: StateProviderFactory + Clone + Unpin + 'static, - Pool: TransactionPool + Unpin + 'static, Tasks: TaskSpawner + Clone + 'static, - Builder: PayloadBuilder + Unpin + 'static, - >::Attributes: Unpin + Clone, - >::BuiltPayload: Unpin + Clone, + Builder: PayloadBuilder + Unpin + 'static, + Builder::Attributes: Unpin + Clone, + Builder::BuiltPayload: Unpin + Clone, { type PayloadAttributes = Builder::Attributes; type ResolvePayloadFuture = ResolveBestPayload; @@ -472,7 +446,7 @@ where // started right away and the first full block should have been // built by the time CL is requesting the payload. self.metrics.inc_requested_empty_payload(); - self.builder.build_empty_payload(&self.client, self.config.clone()) + self.builder.build_empty_payload(self.config.clone()) } } @@ -497,8 +471,6 @@ where debug!(target: "payload_builder", id=%self.config.payload_id(), "no best payload yet to resolve, building empty payload"); let args = BuildArguments { - client: self.client.clone(), - pool: self.pool.clone(), cached_reads: self.cached_reads.take().unwrap_or_default(), config: self.config.clone(), cancel: CancelOnDrop::default(), @@ -516,11 +488,10 @@ where self.metrics.inc_requested_empty_payload(); // no payload built yet, so we need to return an empty payload let (tx, rx) = oneshot::channel(); - let client = self.client.clone(); let config = self.config.clone(); let builder = self.builder.clone(); self.executor.spawn_blocking(Box::pin(async move { - let res = builder.build_empty_payload(&client, config); + let res = builder.build_empty_payload(config); let _ = tx.send(res); })); @@ -806,13 +777,7 @@ impl BuildOutcomeKind { /// building process. It holds references to the Ethereum client, transaction pool, cached reads, /// payload configuration, cancellation status, and the best payload achieved so far. #[derive(Debug)] -pub struct BuildArguments { - /// How to interact with the chain. - pub client: Client, - /// The transaction pool. - /// - /// Or the type that provides the transactions to build the payload. - pub pool: Pool, +pub struct BuildArguments { /// Previously cached disk reads pub cached_reads: CachedReads, /// How to configure the payload. @@ -823,44 +788,15 @@ pub struct BuildArguments { pub best_payload: Option, } -impl BuildArguments { +impl BuildArguments { /// Create new build arguments. pub const fn new( - client: Client, - pool: Pool, cached_reads: CachedReads, config: PayloadConfig, cancel: CancelOnDrop, best_payload: Option, ) -> Self { - Self { client, pool, cached_reads, config, cancel, best_payload } - } - - /// Maps the transaction pool to a new type. - pub fn with_pool

(self, pool: P) -> BuildArguments { - BuildArguments { - client: self.client, - pool, - cached_reads: self.cached_reads, - config: self.config, - cancel: self.cancel, - best_payload: self.best_payload, - } - } - - /// Maps the transaction pool to a new type using a closure with the current pool type as input. - pub fn map_pool(self, f: F) -> BuildArguments - where - F: FnOnce(Pool) -> P, - { - BuildArguments { - client: self.client, - pool: f(self.pool), - cached_reads: self.cached_reads, - config: self.config, - cancel: self.cancel, - best_payload: self.best_payload, - } + Self { cached_reads, config, cancel, best_payload } } } @@ -872,7 +808,7 @@ impl BuildArguments: Send + Sync + Clone { +pub trait PayloadBuilder: Send + Sync + Clone { /// The payload attributes type to accept for building. type Attributes: PayloadBuilderAttributes; /// The type of the built payload. @@ -892,7 +828,7 @@ pub trait PayloadBuilder: Send + Sync + Clone { /// A `Result` indicating the build outcome or an error. fn try_build( &self, - args: BuildArguments, + args: BuildArguments, ) -> Result, PayloadBuilderError>; /// Invoked when the payload job is being resolved and there is no payload yet. @@ -900,7 +836,7 @@ pub trait PayloadBuilder: Send + Sync + Clone { /// This can happen if the CL requests a payload before the first payload has been built. fn on_missing_payload( &self, - _args: BuildArguments, + _args: BuildArguments, ) -> MissingPayloadBehaviour { MissingPayloadBehaviour::RaceEmptyPayload } @@ -908,7 +844,6 @@ pub trait PayloadBuilder: Send + Sync + Clone { /// Builds an empty payload without any transaction. fn build_empty_payload( &self, - client: &Client, config: PayloadConfig, ) -> Result; } diff --git a/crates/payload/basic/src/stack.rs b/crates/payload/basic/src/stack.rs index 1dd57d155..6616e7e09 100644 --- a/crates/payload/basic/src/stack.rs +++ b/crates/payload/basic/src/stack.rs @@ -177,54 +177,45 @@ where } } -impl PayloadBuilder for PayloadBuilderStack +impl PayloadBuilder for PayloadBuilderStack where - L: PayloadBuilder + Unpin + 'static, - R: PayloadBuilder + Unpin + 'static, - Client: Clone, - Pool: Clone, + L: PayloadBuilder + Unpin + 'static, + R: PayloadBuilder + Unpin + 'static, L::Attributes: Unpin + Clone, R::Attributes: Unpin + Clone, L::BuiltPayload: Unpin + Clone, R::BuiltPayload: BuiltPayload::Primitives> + Unpin + Clone, - <>::Attributes as PayloadBuilderAttributes>::Error: 'static, - <>::Attributes as PayloadBuilderAttributes>::Error: 'static, { type Attributes = Either; type BuiltPayload = Either; fn try_build( &self, - args: BuildArguments, + args: BuildArguments, ) -> Result, PayloadBuilderError> { match args.config.attributes { Either::Left(ref left_attr) => { - let left_args: BuildArguments = - BuildArguments { - client: args.client.clone(), - pool: args.pool.clone(), - cached_reads: args.cached_reads.clone(), - config: PayloadConfig { - parent_header: args.config.parent_header.clone(), - attributes: left_attr.clone(), - }, - cancel: args.cancel.clone(), - best_payload: args.best_payload.clone().and_then(|payload| { - if let Either::Left(p) = payload { - Some(p) - } else { - None - } - }), - }; + let left_args: BuildArguments = BuildArguments { + cached_reads: args.cached_reads.clone(), + config: PayloadConfig { + parent_header: args.config.parent_header.clone(), + attributes: left_attr.clone(), + }, + cancel: args.cancel.clone(), + best_payload: args.best_payload.clone().and_then(|payload| { + if let Either::Left(p) = payload { + Some(p) + } else { + None + } + }), + }; self.left.try_build(left_args).map(|out| out.map_payload(Either::Left)) } Either::Right(ref right_attr) => { let right_args = BuildArguments { - client: args.client.clone(), - pool: args.pool.clone(), cached_reads: args.cached_reads.clone(), config: PayloadConfig { parent_header: args.config.parent_header.clone(), @@ -247,7 +238,6 @@ where fn build_empty_payload( &self, - client: &Client, config: PayloadConfig, ) -> Result { match config.attributes { @@ -256,14 +246,14 @@ where parent_header: config.parent_header.clone(), attributes: left_attr, }; - self.left.build_empty_payload(client, left_config).map(Either::Left) + self.left.build_empty_payload(left_config).map(Either::Left) } Either::Right(right_attr) => { let right_config = PayloadConfig { parent_header: config.parent_header.clone(), attributes: right_attr, }; - self.right.build_empty_payload(client, right_config).map(Either::Right) + self.right.build_empty_payload(right_config).map(Either::Right) } } } diff --git a/examples/custom-engine-types/src/main.rs b/examples/custom-engine-types/src/main.rs index 30b9dc0e6..7cb1ec4d0 100644 --- a/examples/custom-engine-types/src/main.rs +++ b/examples/custom-engine-types/src/main.rs @@ -372,7 +372,14 @@ where ctx: &BuilderContext, pool: Pool, ) -> eyre::Result::Engine>> { - let payload_builder = CustomPayloadBuilder::default(); + let payload_builder = CustomPayloadBuilder { + inner: reth_ethereum_payload_builder::EthereumPayloadBuilder::new( + ctx.provider().clone(), + pool, + EthEvmConfig::new(ctx.provider().chain_spec().clone()), + EthereumBuilderConfig::new(default_extra_data_bytes()), + ), + }; let conf = ctx.payload_builder_config(); let payload_job_config = BasicPayloadJobGeneratorConfig::default() @@ -382,7 +389,6 @@ where let payload_generator = BasicPayloadJobGenerator::with_builder( ctx.provider().clone(), - pool, ctx.task_executor().clone(), payload_job_config, payload_builder, @@ -397,13 +403,15 @@ where } /// The type responsible for building custom payloads -#[derive(Debug, Default, Clone)] +#[derive(Debug, Clone)] #[non_exhaustive] -pub struct CustomPayloadBuilder; +pub struct CustomPayloadBuilder { + inner: reth_ethereum_payload_builder::EthereumPayloadBuilder, +} -impl PayloadBuilder for CustomPayloadBuilder +impl PayloadBuilder for CustomPayloadBuilder where - Client: StateProviderFactory + ChainSpecProvider, + Client: StateProviderFactory + ChainSpecProvider + Clone, Pool: TransactionPool>, { type Attributes = CustomPayloadBuilderAttributes; @@ -411,22 +419,14 @@ where fn try_build( &self, - args: BuildArguments, + args: BuildArguments, ) -> Result, PayloadBuilderError> { - let BuildArguments { client, pool, cached_reads, config, cancel, best_payload } = args; + let BuildArguments { cached_reads, config, cancel, best_payload } = args; let PayloadConfig { parent_header, attributes } = config; - let chain_spec = client.chain_spec(); - // This reuses the default EthereumPayloadBuilder to build the payload // but any custom logic can be implemented here - reth_ethereum_payload_builder::EthereumPayloadBuilder::new( - EthEvmConfig::new(chain_spec.clone()), - EthereumBuilderConfig::new(default_extra_data_bytes()), - ) - .try_build(BuildArguments { - client, - pool, + self.inner.try_build(BuildArguments { cached_reads, config: PayloadConfig { parent_header, attributes: attributes.0 }, cancel, @@ -436,19 +436,10 @@ where fn build_empty_payload( &self, - client: &Client, config: PayloadConfig, ) -> Result { let PayloadConfig { parent_header, attributes } = config; - let chain_spec = client.chain_spec(); - >::build_empty_payload( - &reth_ethereum_payload_builder::EthereumPayloadBuilder::new( - EthEvmConfig::new(chain_spec.clone()), - EthereumBuilderConfig::new(default_extra_data_bytes()) - ), - client, - PayloadConfig { parent_header, attributes: attributes.0} - ) + self.inner.build_empty_payload(PayloadConfig { parent_header, attributes: attributes.0 }) } } diff --git a/examples/custom-payload-builder/src/generator.rs b/examples/custom-payload-builder/src/generator.rs index dfb973a96..8de625040 100644 --- a/examples/custom-payload-builder/src/generator.rs +++ b/examples/custom-payload-builder/src/generator.rs @@ -4,7 +4,6 @@ use reth::{ api::Block, providers::{BlockReaderIdExt, BlockSource, StateProviderFactory}, tasks::TaskSpawner, - transaction_pool::TransactionPool, }; use reth_basic_payload_builder::{BasicPayloadJobGeneratorConfig, PayloadBuilder, PayloadConfig}; use reth_node_api::PayloadBuilderAttributes; @@ -14,11 +13,9 @@ use std::sync::Arc; /// The generator type that creates new jobs that builds empty blocks. #[derive(Debug)] -pub struct EmptyBlockPayloadJobGenerator { +pub struct EmptyBlockPayloadJobGenerator { /// The client that can interact with the chain. client: Client, - /// txpool - pool: Pool, /// How to spawn building tasks executor: Tasks, /// The configuration for the job generator. @@ -31,41 +28,39 @@ pub struct EmptyBlockPayloadJobGenerator { // === impl EmptyBlockPayloadJobGenerator === -impl EmptyBlockPayloadJobGenerator { +impl EmptyBlockPayloadJobGenerator { /// Creates a new [EmptyBlockPayloadJobGenerator] with the given config and custom /// [PayloadBuilder] pub fn with_builder( client: Client, - pool: Pool, executor: Tasks, config: BasicPayloadJobGeneratorConfig, builder: Builder, ) -> Self { - Self { client, pool, executor, _config: config, builder } + Self { client, executor, _config: config, builder } } } -impl PayloadJobGenerator - for EmptyBlockPayloadJobGenerator +impl PayloadJobGenerator + for EmptyBlockPayloadJobGenerator where Client: StateProviderFactory + BlockReaderIdExt + Clone + Unpin + 'static, - Pool: TransactionPool + Unpin + 'static, Tasks: TaskSpawner + Clone + Unpin + 'static, - Builder: PayloadBuilder + Unpin + 'static, - >::Attributes: Unpin + Clone, - >::BuiltPayload: Unpin + Clone, + Builder: PayloadBuilder + Unpin + 'static, + Builder::Attributes: Unpin + Clone, + Builder::BuiltPayload: Unpin + Clone, { - type Job = EmptyBlockPayloadJob; + type Job = EmptyBlockPayloadJob; /// This is invoked when the node receives payload attributes from the beacon node via /// `engine_forkchoiceUpdatedV1` fn new_payload_job( &self, - attributes: >::Attributes, + attributes: Builder::Attributes, ) -> Result { let parent_block = if attributes.parent().is_zero() { // use latest block if parent is zero: genesis block @@ -87,8 +82,6 @@ where let config = PayloadConfig::new(Arc::new(header), attributes); Ok(EmptyBlockPayloadJob { - client: self.client.clone(), - _pool: self.pool.clone(), _executor: self.executor.clone(), builder: self.builder.clone(), config, diff --git a/examples/custom-payload-builder/src/job.rs b/examples/custom-payload-builder/src/job.rs index 014198259..73bb21238 100644 --- a/examples/custom-payload-builder/src/job.rs +++ b/examples/custom-payload-builder/src/job.rs @@ -1,7 +1,5 @@ use futures_util::Future; -use reth::{ - providers::StateProviderFactory, tasks::TaskSpawner, transaction_pool::TransactionPool, -}; +use reth::tasks::TaskSpawner; use reth_basic_payload_builder::{PayloadBuilder, PayloadConfig}; use reth_node_api::PayloadKind; use reth_payload_builder::{KeepPayloadJobAlive, PayloadBuilderError, PayloadJob}; @@ -12,16 +10,12 @@ use std::{ }; /// A [PayloadJob] that builds empty blocks. -pub struct EmptyBlockPayloadJob +pub struct EmptyBlockPayloadJob where - Builder: PayloadBuilder, + Builder: PayloadBuilder, { /// The configuration for how the payload will be created. pub(crate) config: PayloadConfig, - /// The client that can interact with the chain. - pub(crate) client: Client, - /// The transaction pool. - pub(crate) _pool: Pool, /// How to spawn building tasks pub(crate) _executor: Tasks, /// The type responsible for building payloads. @@ -30,14 +24,12 @@ where pub(crate) builder: Builder, } -impl PayloadJob for EmptyBlockPayloadJob +impl PayloadJob for EmptyBlockPayloadJob where - Client: StateProviderFactory + Clone + Unpin + 'static, - Pool: TransactionPool + Unpin + 'static, Tasks: TaskSpawner + Clone + 'static, - Builder: PayloadBuilder + Unpin + 'static, - >::Attributes: Unpin + Clone, - >::BuiltPayload: Unpin + Clone, + Builder: PayloadBuilder + Unpin + 'static, + Builder::Attributes: Unpin + Clone, + Builder::BuiltPayload: Unpin + Clone, { type PayloadAttributes = Builder::Attributes; type ResolvePayloadFuture = @@ -45,7 +37,7 @@ where type BuiltPayload = Builder::BuiltPayload; fn best_payload(&self) -> Result { - let payload = self.builder.build_empty_payload(&self.client, self.config.clone())?; + let payload = self.builder.build_empty_payload(self.config.clone())?; Ok(payload) } @@ -63,14 +55,12 @@ where } /// A [PayloadJob] is a a future that's being polled by the `PayloadBuilderService` -impl Future for EmptyBlockPayloadJob +impl Future for EmptyBlockPayloadJob where - Client: StateProviderFactory + Clone + Unpin + 'static, - Pool: TransactionPool + Unpin + 'static, Tasks: TaskSpawner + Clone + 'static, - Builder: PayloadBuilder + Unpin + 'static, - >::Attributes: Unpin + Clone, - >::BuiltPayload: Unpin + Clone, + Builder: PayloadBuilder + Unpin + 'static, + Builder::Attributes: Unpin + Clone, + Builder::BuiltPayload: Unpin + Clone, { type Output = Result<(), PayloadBuilderError>; diff --git a/examples/custom-payload-builder/src/main.rs b/examples/custom-payload-builder/src/main.rs index bdbfe1510..1ea7f340c 100644 --- a/examples/custom-payload-builder/src/main.rs +++ b/examples/custom-payload-builder/src/main.rs @@ -62,10 +62,11 @@ where let payload_generator = EmptyBlockPayloadJobGenerator::with_builder( ctx.provider().clone(), - pool, ctx.task_executor().clone(), payload_job_config, reth_ethereum_payload_builder::EthereumPayloadBuilder::new( + ctx.provider().clone(), + pool, EthEvmConfig::new(ctx.chain_spec()), EthereumBuilderConfig::new(conf.extra_data_bytes()), ),