Skip to content

Commit 4856e71

Browse files
committed
txpool POC
1 parent e296417 commit 4856e71

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-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

+10-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,13 @@ 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<Pool: TransactionPool, Client, EvmConfig, N: NodePrimitives, Txs = ()> {
227228
/// The type responsible for creating the evm.
228229
pub evm_config: EvmConfig,
229230
/// The builder's signer key to use for an end of block tx
230231
pub builder_signer: Option<Signer>,
231232
/// The transaction pool
232-
pub pool: Pool,
233+
pub pool: CustomPool<Pool>,
233234
/// Node client
234235
pub client: Client,
235236
/// Settings for the builder, e.g. DA settings.
@@ -247,7 +248,7 @@ pub struct OpPayloadBuilderVanilla<Pool, Client, EvmConfig, N: NodePrimitives, T
247248
pub supervisor_safety_level: SafetyLevel,
248249
}
249250

250-
impl<Pool, Client, EvmConfig, N: NodePrimitives>
251+
impl<Pool: TransactionPool, Client, EvmConfig, N: NodePrimitives>
251252
OpPayloadBuilderVanilla<Pool, Client, EvmConfig, N>
252253
{
253254
/// `OpPayloadBuilder` constructor.
@@ -298,6 +299,7 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives>
298299
serde_json::from_str(level.as_str()).expect("parsing supervisor_safety_level")
299300
})
300301
.unwrap_or(SafetyLevel::CrossUnsafe);
302+
let pool = CustomPool::new(pool.clone());
301303
Self {
302304
pool,
303305
client,
@@ -772,30 +774,30 @@ impl<Txs> OpBuilder<'_, Txs> {
772774
pub trait OpPayloadTransactions<Transaction>: Clone + Send + Sync + Unpin + 'static {
773775
/// Returns an iterator that yields the transaction in the order they should get included in the
774776
/// new payload.
775-
fn best_transactions<Pool: TransactionPool<Transaction = Transaction>>(
777+
fn best_transactions<Pool: CustomTransactionPool<Transaction = Transaction>>(
776778
&self,
777779
pool: Pool,
778780
attr: BestTransactionsAttributes,
779781
) -> impl PayloadTransactions<Transaction = Transaction>;
780782

781783
/// Removes invalid transactions from the tx pool
782-
fn remove_invalid<Pool: TransactionPool<Transaction = Transaction>>(
784+
fn remove_invalid<Pool: CustomTransactionPool<Transaction = Transaction>>(
783785
&self,
784786
pool: Pool,
785787
hashes: Vec<TxHash>,
786788
);
787789
}
788790

789791
impl<T: PoolTransaction> OpPayloadTransactions<T> for () {
790-
fn best_transactions<Pool: TransactionPool<Transaction = T>>(
792+
fn best_transactions<Pool: CustomTransactionPool<Transaction = T>>(
791793
&self,
792794
pool: Pool,
793795
attr: BestTransactionsAttributes,
794796
) -> impl PayloadTransactions<Transaction = T> {
795797
BestPayloadTransactions::new(pool.best_transactions_with_attributes(attr))
796798
}
797799

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

0 commit comments

Comments
 (0)