Skip to content

Commit 6b82800

Browse files
committed
txpool POC
1 parent e296417 commit 6b82800

File tree

5 files changed

+99
-8
lines changed

5 files changed

+99
-8
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/op-rbuilder/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ derive_more.workspace = true
7979
metrics.workspace = true
8080
serde_json.workspace = true
8181
tokio-util.workspace = true
82+
auto_impl.workspace = true
8283

8384
time = { version = "0.3.36", features = ["macros", "formatting", "parsing"] }
8485
chrono = "0.4"

crates/op-rbuilder/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod primitives;
2828
mod tester;
2929
mod tx_signer;
3030
use monitor_tx_pool::monitor_tx_pool;
31+
mod txpool_trait;
3132

3233
fn main() {
3334
Cli::<OpChainSpecParser, args::OpRbuilderArgs>::parse()

crates/op-rbuilder/src/payload_builder_vanilla.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::txpool_trait::{CustomPool, CustomTransactionPool};
12
use crate::{
23
generator::{BlockCell, BlockPayloadJobGenerator, BuildArguments, PayloadBuilder},
34
metrics::OpRBuilderMetrics,
@@ -194,7 +195,7 @@ where
194195
impl<Pool, Client, EvmConfig, N, Txs> reth_basic_payload_builder::PayloadBuilder
195196
for OpPayloadBuilderVanilla<Pool, Client, EvmConfig, N, Txs>
196197
where
197-
Pool: Clone + Send + Sync,
198+
Pool: TransactionPool + Clone + Send + Sync,
198199
Client: Clone + Send + Sync,
199200
EvmConfig: Clone + Send + Sync,
200201
N: NodePrimitives,
@@ -223,13 +224,19 @@ where
223224

224225
/// Optimism's payload builder
225226
#[derive(Debug, Clone)]
226-
pub struct OpPayloadBuilderVanilla<Pool, Client, EvmConfig, N: NodePrimitives, Txs = ()> {
227+
pub struct OpPayloadBuilderVanilla<
228+
Pool: TransactionPool,
229+
Client,
230+
EvmConfig,
231+
N: NodePrimitives,
232+
Txs = (),
233+
> {
227234
/// The type responsible for creating the evm.
228235
pub evm_config: EvmConfig,
229236
/// The builder's signer key to use for an end of block tx
230237
pub builder_signer: Option<Signer>,
231238
/// The transaction pool
232-
pub pool: Pool,
239+
pub pool: CustomPool<Pool>,
233240
/// Node client
234241
pub client: Client,
235242
/// Settings for the builder, e.g. DA settings.
@@ -247,7 +254,7 @@ pub struct OpPayloadBuilderVanilla<Pool, Client, EvmConfig, N: NodePrimitives, T
247254
pub supervisor_safety_level: SafetyLevel,
248255
}
249256

250-
impl<Pool, Client, EvmConfig, N: NodePrimitives>
257+
impl<Pool: TransactionPool, Client, EvmConfig, N: NodePrimitives>
251258
OpPayloadBuilderVanilla<Pool, Client, EvmConfig, N>
252259
{
253260
/// `OpPayloadBuilder` constructor.
@@ -298,6 +305,7 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives>
298305
serde_json::from_str(level.as_str()).expect("parsing supervisor_safety_level")
299306
})
300307
.unwrap_or(SafetyLevel::CrossUnsafe);
308+
let pool = CustomPool::new(pool.clone());
301309
Self {
302310
pool,
303311
client,
@@ -772,30 +780,30 @@ impl<Txs> OpBuilder<'_, Txs> {
772780
pub trait OpPayloadTransactions<Transaction>: Clone + Send + Sync + Unpin + 'static {
773781
/// Returns an iterator that yields the transaction in the order they should get included in the
774782
/// new payload.
775-
fn best_transactions<Pool: TransactionPool<Transaction = Transaction>>(
783+
fn best_transactions<Pool: CustomTransactionPool<Transaction = Transaction>>(
776784
&self,
777785
pool: Pool,
778786
attr: BestTransactionsAttributes,
779787
) -> impl PayloadTransactions<Transaction = Transaction>;
780788

781789
/// Removes invalid transactions from the tx pool
782-
fn remove_invalid<Pool: TransactionPool<Transaction = Transaction>>(
790+
fn remove_invalid<Pool: CustomTransactionPool<Transaction = Transaction>>(
783791
&self,
784792
pool: Pool,
785793
hashes: Vec<TxHash>,
786794
);
787795
}
788796

789797
impl<T: PoolTransaction> OpPayloadTransactions<T> for () {
790-
fn best_transactions<Pool: TransactionPool<Transaction = T>>(
798+
fn best_transactions<Pool: CustomTransactionPool<Transaction = T>>(
791799
&self,
792800
pool: Pool,
793801
attr: BestTransactionsAttributes,
794802
) -> impl PayloadTransactions<Transaction = T> {
795803
BestPayloadTransactions::new(pool.best_transactions_with_attributes(attr))
796804
}
797805

798-
fn remove_invalid<Pool: TransactionPool<Transaction = T>>(
806+
fn remove_invalid<Pool: CustomTransactionPool<Transaction = T>>(
799807
&self,
800808
pool: Pool,
801809
hashes: Vec<TxHash>,
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use alloy_primitives::TxHash;
2+
use reth_transaction_pool::TransactionPool;
3+
use reth_transaction_pool::{
4+
BestTransactions, BestTransactionsAttributes, BlobStore, EthPoolTransaction, Pool,
5+
PoolTransaction, TransactionOrdering, TransactionValidator, ValidPoolTransaction,
6+
};
7+
use std::sync::Arc;
8+
#[auto_impl::auto_impl(&, Arc)]
9+
pub trait CustomTransactionPool: Send + Sync + Clone {
10+
/// The transaction type of the pool
11+
type Transaction: EthPoolTransaction;
12+
13+
/// Returns an iterator that yields transactions that are ready for block production with the
14+
/// given base fee and optional blob fee attributes.
15+
///
16+
/// Consumer: Block production
17+
fn best_transactions_with_attributes(
18+
&self,
19+
best_transactions_attributes: BestTransactionsAttributes,
20+
) -> Box<dyn BestTransactions<Item = Arc<ValidPoolTransaction<Self::Transaction>>>>;
21+
22+
/// Removes all transactions corresponding to the given hashes.
23+
///
24+
/// Consumer: Utility
25+
fn remove_transactions(
26+
&self,
27+
hashes: Vec<TxHash>,
28+
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
29+
}
30+
31+
#[derive(Debug)]
32+
pub struct CustomPool<Pool>
33+
where
34+
Pool: TransactionPool,
35+
Pool::Transaction: PoolTransaction,
36+
{
37+
pool: Arc<Pool>,
38+
}
39+
40+
impl<Pool> CustomPool<Pool>
41+
where
42+
Pool: TransactionPool,
43+
Pool::Transaction: PoolTransaction,
44+
{
45+
pub fn new(pool: Pool) -> Self {
46+
Self {
47+
pool: Arc::new(pool),
48+
}
49+
}
50+
}
51+
52+
impl<Pool> CustomTransactionPool for CustomPool<Pool>
53+
where
54+
Pool: TransactionPool,
55+
Pool::Transaction: PoolTransaction,
56+
{
57+
type Transaction = Pool::Transaction;
58+
59+
fn best_transactions_with_attributes(
60+
&self,
61+
best_transactions_attributes: BestTransactionsAttributes,
62+
) -> Box<dyn BestTransactions<Item = Arc<ValidPoolTransaction<Self::Transaction>>>> {
63+
TransactionPool::best_transactions_with_attributes(&self.pool, best_transactions_attributes)
64+
}
65+
66+
fn remove_transactions(
67+
&self,
68+
hashes: Vec<TxHash>,
69+
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
70+
TransactionPool::remove_transactions(&self.pool, hashes)
71+
}
72+
}
73+
74+
impl<Pool: TransactionPool> Clone for CustomPool<Pool> {
75+
fn clone(&self) -> Self {
76+
Self {
77+
pool: Arc::clone(&self.pool),
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)