Skip to content

Commit

Permalink
feat: expose pool transaction in PayloadTransactions (#14249)
Browse files Browse the repository at this point in the history
Co-authored-by: Hamdi Allam <[email protected]>
  • Loading branch information
klkvr and hamdiallam authored Feb 6, 2025
1 parent c1a305c commit 14a51b5
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 202 deletions.
1 change: 1 addition & 0 deletions .github/assets/check_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ exclude_crates=(
reth-stages-api # reth-provider, reth-prune
reth-static-file # tokio
reth-transaction-pool # c-kzg
reth-payload-util # reth-transaction-pool
reth-trie-parallel # tokio
reth-testing-utils
)
Expand Down
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,16 +473,10 @@ impl OpPayloadBuilder {
}
}

impl<Txs> OpPayloadBuilder<Txs>
where
Txs: OpPayloadTransactions,
{
impl<Txs> OpPayloadBuilder<Txs> {
/// Configures the type responsible for yielding the transactions that should be included in the
/// payload.
pub fn with_transactions<T: OpPayloadTransactions>(
self,
best_transactions: T,
) -> OpPayloadBuilder<T> {
pub fn with_transactions<T>(self, best_transactions: T) -> OpPayloadBuilder<T> {
let Self { compute_pending_block, da_config, .. } = self;
OpPayloadBuilder { compute_pending_block, best_transactions, da_config }
}
Expand All @@ -506,7 +500,7 @@ where
+ Unpin
+ 'static,
Evm: ConfigureEvmFor<PrimitivesTy<Node::Types>>,
Txs: OpPayloadTransactions<TxTy<Node::Types>>,
Txs: OpPayloadTransactions<Pool::Transaction>,
{
let payload_builder = reth_optimism_payload_builder::OpPayloadBuilder::with_builder_config(
pool,
Expand Down Expand Up @@ -551,7 +545,7 @@ where
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>>
+ Unpin
+ 'static,
Txs: OpPayloadTransactions<TxTy<Node::Types>>,
Txs: OpPayloadTransactions<Pool::Transaction>,
{
async fn spawn_payload_service(
self,
Expand Down
27 changes: 14 additions & 13 deletions crates/optimism/node/tests/it/priority.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Node builder test that customizes priority of transactions in the block.
use alloy_consensus::TxEip1559;
use alloy_consensus::{SignableTransaction, TxEip1559};
use alloy_genesis::Genesis;
use alloy_network::TxSignerSync;
use alloy_primitives::{Address, ChainId, TxKind};
Expand All @@ -22,16 +22,19 @@ use reth_optimism_node::{
OpAddOns, OpConsensusBuilder, OpExecutorBuilder, OpNetworkBuilder, OpPayloadBuilder,
OpPoolBuilder,
},
txpool::OpPooledTransaction,
utils::optimism_payload_attributes,
OpEngineTypes, OpNode,
};
use reth_optimism_payload_builder::builder::OpPayloadTransactions;
use reth_optimism_primitives::{OpPrimitives, OpTransactionSigned};
use reth_payload_util::{PayloadTransactions, PayloadTransactionsChain, PayloadTransactionsFixed};
use reth_optimism_primitives::OpPrimitives;
use reth_payload_util::{
BestPayloadTransactions, PayloadTransactions, PayloadTransactionsChain,
PayloadTransactionsFixed,
};
use reth_primitives::Recovered;
use reth_provider::providers::BlockchainProvider;
use reth_tasks::TaskManager;
use reth_transaction_pool::{pool::BestPayloadTransactions, PoolTransaction};
use std::sync::Arc;
use tokio::sync::Mutex;

Expand All @@ -40,16 +43,14 @@ struct CustomTxPriority {
chain_id: ChainId,
}

impl OpPayloadTransactions for CustomTxPriority {
impl OpPayloadTransactions<OpPooledTransaction> for CustomTxPriority {
fn best_transactions<Pool>(
&self,
pool: Pool,
attr: reth_transaction_pool::BestTransactionsAttributes,
) -> impl PayloadTransactions<Transaction = OpTransactionSigned>
) -> impl PayloadTransactions<Transaction = OpPooledTransaction>
where
Pool: reth_transaction_pool::TransactionPool<
Transaction: PoolTransaction<Consensus = OpTransactionSigned>,
>,
Pool: reth_transaction_pool::TransactionPool<Transaction = OpPooledTransaction>,
{
// Block composition:
// 1. Best transactions from the pool (up to 250k gas)
Expand All @@ -68,12 +69,12 @@ impl OpPayloadTransactions for CustomTxPriority {
};
let signature = sender.sign_transaction_sync(&mut end_of_block_tx).unwrap();
let end_of_block_tx = Recovered::new_unchecked(
OpTransactionSigned::new_unhashed(
OpTypedTransaction::Eip1559(end_of_block_tx),
signature,
op_alloy_consensus::OpPooledTransaction::Eip1559(
end_of_block_tx.into_signed(signature),
),
sender.address(),
);
)
.into();

PayloadTransactionsChain::new(
BestPayloadTransactions::new(pool.best_transactions_with_attributes(attr)),
Expand Down
48 changes: 22 additions & 26 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ use reth_optimism_consensus::calculate_receipt_root_no_memo_optimism;
use reth_optimism_evm::{OpReceiptBuilder, ReceiptBuilderCtx};
use reth_optimism_forks::OpHardforks;
use reth_optimism_primitives::{
transaction::signed::OpTransaction, OpTransactionSigned, ADDRESS_L2_TO_L1_MESSAGE_PASSER,
transaction::signed::OpTransaction, ADDRESS_L2_TO_L1_MESSAGE_PASSER,
};
use reth_payload_builder_primitives::PayloadBuilderError;
use reth_payload_primitives::PayloadBuilderAttributes;
use reth_payload_util::{NoopPayloadTransactions, PayloadTransactions};
use reth_payload_util::{BestPayloadTransactions, NoopPayloadTransactions, PayloadTransactions};
use reth_primitives::{
transaction::SignedTransactionIntoRecoveredExt, BlockBody, NodePrimitives, SealedHeader,
};
Expand All @@ -46,9 +46,7 @@ use reth_provider::{
use reth_revm::{
cancelled::CancelOnDrop, database::StateProviderDatabase, witness::ExecutionWitnessRecord,
};
use reth_transaction_pool::{
pool::BestPayloadTransactions, BestTransactionsAttributes, PoolTransaction, TransactionPool,
};
use reth_transaction_pool::{BestTransactionsAttributes, PoolTransaction, TransactionPool};
use revm::{
db::{states::bundle_state::BundleRetention, State},
primitives::{ExecutionResult, ResultAndState},
Expand Down Expand Up @@ -122,7 +120,7 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives, Txs>

/// Configures the type responsible for yielding the transactions that should be included in the
/// payload.
pub fn with_transactions<T: OpPayloadTransactions>(
pub fn with_transactions<T>(
self,
best_transactions: T,
) -> OpPayloadBuilder<Pool, Client, EvmConfig, N, T> {
Expand Down Expand Up @@ -150,8 +148,10 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives, Txs>
self.compute_pending_block
}
}

impl<Pool, Client, EvmConfig, N, T> OpPayloadBuilder<Pool, Client, EvmConfig, N, T>
where
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = N::SignedTx>>,
Client: StateProviderFactory + ChainSpecProvider<ChainSpec = OpChainSpec>,
N: OpPayloadPrimitives,
EvmConfig: ConfigureEvmFor<N>,
Expand All @@ -170,7 +170,7 @@ where
best: impl FnOnce(BestTransactionsAttributes) -> Txs + Send + Sync + 'a,
) -> Result<BuildOutcome<OpBuiltPayload<N>>, PayloadBuilderError>
where
Txs: PayloadTransactions<Transaction = N::SignedTx>,
Txs: PayloadTransactions<Transaction: PoolTransaction<Consensus = N::SignedTx>>,
{
let evm_env = self
.evm_env(&args.config.attributes, &args.config.parent_header)
Expand Down Expand Up @@ -251,7 +251,7 @@ where
let state = StateProviderDatabase::new(state_provider);
let mut state = State::builder().with_database(state).with_bundle_update().build();

let builder = OpBuilder::new(|_| NoopPayloadTransactions::default());
let builder = OpBuilder::new(|_| NoopPayloadTransactions::<Pool::Transaction>::default());
builder.witness(&mut state, &ctx)
}
}
Expand All @@ -264,7 +264,7 @@ where
N: OpPayloadPrimitives,
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = N::SignedTx>>,
EvmConfig: ConfigureEvmFor<N>,
Txs: OpPayloadTransactions<N::SignedTx>,
Txs: OpPayloadTransactions<Pool::Transaction>,
{
type Attributes = OpPayloadBuilderAttributes<N::SignedTx>;
type BuiltPayload = OpBuiltPayload<N>;
Expand Down Expand Up @@ -298,7 +298,7 @@ where
cancel: Default::default(),
best_payload: None,
};
self.build_payload(args, |_| NoopPayloadTransactions::default())?
self.build_payload(args, |_| NoopPayloadTransactions::<Pool::Transaction>::default())?
.into_payload()
.ok_or_else(|| PayloadBuilderError::MissingPayload)
}
Expand Down Expand Up @@ -332,10 +332,7 @@ impl<'a, Txs> OpBuilder<'a, Txs> {
}
}

impl<Txs> OpBuilder<'_, Txs>
where
Txs: PayloadTransactions,
{
impl<Txs> OpBuilder<'_, Txs> {
/// Executes the payload and returns the outcome.
pub fn execute<EvmConfig, N, DB, P>(
self,
Expand All @@ -344,7 +341,7 @@ where
) -> Result<BuildOutcomeKind<ExecutedPayload<N>>, PayloadBuilderError>
where
N: OpPayloadPrimitives,
Txs: PayloadTransactions<Transaction = N::SignedTx>,
Txs: PayloadTransactions<Transaction: PoolTransaction<Consensus = N::SignedTx>>,
EvmConfig: ConfigureEvmFor<N>,
DB: Database<Error = ProviderError> + AsRef<P>,
P: StorageRootProvider,
Expand Down Expand Up @@ -408,7 +405,7 @@ where
where
EvmConfig: ConfigureEvmFor<N>,
N: OpPayloadPrimitives,
Txs: PayloadTransactions<Transaction = N::SignedTx>,
Txs: PayloadTransactions<Transaction: PoolTransaction<Consensus = N::SignedTx>>,
DB: Database<Error = ProviderError> + AsRef<P>,
P: StateRootProvider + HashedPostStateProvider + StorageRootProvider,
{
Expand Down Expand Up @@ -533,7 +530,7 @@ where
where
EvmConfig: ConfigureEvmFor<N>,
N: OpPayloadPrimitives,
Txs: PayloadTransactions<Transaction = N::SignedTx>,
Txs: PayloadTransactions<Transaction: PoolTransaction<Consensus = N::SignedTx>>,
DB: Database<Error = ProviderError> + AsRef<P>,
P: StateProofProvider + StorageRootProvider,
{
Expand All @@ -546,22 +543,18 @@ where
}

/// A type that returns a the [`PayloadTransactions`] that should be included in the pool.
pub trait OpPayloadTransactions<Transaction = OpTransactionSigned>:
Clone + Send + Sync + Unpin + 'static
{
pub trait OpPayloadTransactions<Transaction>: Clone + Send + Sync + Unpin + 'static {
/// Returns an iterator that yields the transaction in the order they should get included in the
/// new payload.
fn best_transactions<
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = Transaction>>,
>(
fn best_transactions<Pool: TransactionPool<Transaction = Transaction>>(
&self,
pool: Pool,
attr: BestTransactionsAttributes,
) -> impl PayloadTransactions<Transaction = Transaction>;
}

impl<T> OpPayloadTransactions<T> for () {
fn best_transactions<Pool: TransactionPool<Transaction: PoolTransaction<Consensus = T>>>(
impl<T: PoolTransaction> OpPayloadTransactions<T> for () {
fn best_transactions<Pool: TransactionPool<Transaction = T>>(
&self,
pool: Pool,
attr: BestTransactionsAttributes,
Expand Down Expand Up @@ -948,7 +941,9 @@ where
&self,
info: &mut ExecutionInfo<N>,
db: &mut State<DB>,
mut best_txs: impl PayloadTransactions<Transaction = EvmConfig::Transaction>,
mut best_txs: impl PayloadTransactions<
Transaction: PoolTransaction<Consensus = EvmConfig::Transaction>,
>,
) -> Result<Option<()>, PayloadBuilderError>
where
DB: Database<Error = ProviderError>,
Expand All @@ -961,6 +956,7 @@ where
let mut evm = self.evm_config.evm_with_env(&mut *db, self.evm_env.clone());

while let Some(tx) = best_txs.next(()) {
let tx = tx.into_consensus();
if info.is_tx_over_limits(tx.tx(), block_gas_limit, tx_da_limit, block_da_limit) {
// we can't fit this transaction into the block, so we need to mark it as
// invalid which also removes all dependent transaction from
Expand Down
8 changes: 7 additions & 1 deletion crates/optimism/rpc/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use jsonrpsee_core::{async_trait, RpcResult};
use op_alloy_rpc_types_engine::OpPayloadAttributes;
use reth_chainspec::ChainSpecProvider;
use reth_evm::ConfigureEvmFor;
use reth_node_api::NodePrimitives;
use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_payload_builder::{OpPayloadBuilder, OpPayloadPrimitives};
use reth_primitives::SealedHeader;
Expand All @@ -15,6 +16,7 @@ use reth_provider::{
pub use reth_rpc_api::DebugExecutionWitnessApiServer;
use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult};
use reth_tasks::TaskSpawner;
use reth_transaction_pool::{PoolTransaction, TransactionPool};
use std::{fmt::Debug, sync::Arc};
use tokio::sync::{oneshot, Semaphore};

Expand Down Expand Up @@ -55,7 +57,11 @@ where
impl<Pool, Provider, EvmConfig> DebugExecutionWitnessApiServer<OpPayloadAttributes>
for OpDebugWitnessApi<Pool, Provider, EvmConfig>
where
Pool: Send + Sync + 'static,
Pool: TransactionPool<
Transaction: PoolTransaction<
Consensus = <Provider::Primitives as NodePrimitives>::SignedTx,
>,
> + 'static,
Provider: BlockReaderIdExt<Header = reth_primitives::Header>
+ NodePrimitivesProvider<Primitives: OpPayloadPrimitives>
+ StateProviderFactory
Expand Down
2 changes: 1 addition & 1 deletion crates/payload/util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ workspace = true

[dependencies]
# reth
reth-primitives.workspace = true
reth-transaction-pool.workspace = true

# alloy
alloy-primitives.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/payload/util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
mod traits;
mod transaction;

pub use traits::{NoopPayloadTransactions, PayloadTransactions};
pub use traits::{BestPayloadTransactions, NoopPayloadTransactions, PayloadTransactions};
pub use transaction::{PayloadTransactionsChain, PayloadTransactionsFixed};
Loading

0 comments on commit 14a51b5

Please sign in to comment.