Skip to content

Commit bc7d8c6

Browse files
authored
feat(perp): optimize OpTxpool 2718 bytes encoding (#16336)
1 parent fcee481 commit bc7d8c6

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

crates/optimism/node/src/node.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ use reth_optimism_rpc::{
4141
OpEthApi, OpEthApiError, SequencerClient,
4242
};
4343
use reth_optimism_txpool::{
44-
conditional::MaybeConditionalTransaction,
45-
interop::MaybeInteropTransaction,
4644
supervisor::{SupervisorClient, DEFAULT_SUPERVISOR_URL},
4745
OpPooledTx,
4846
};
@@ -536,9 +534,7 @@ impl<T> OpPoolBuilder<T> {
536534
impl<Node, T> PoolBuilder<Node> for OpPoolBuilder<T>
537535
where
538536
Node: FullNodeTypes<Types: NodeTypes<ChainSpec: OpHardforks>>,
539-
T: EthPoolTransaction<Consensus = TxTy<Node::Types>>
540-
+ MaybeConditionalTransaction
541-
+ MaybeInteropTransaction,
537+
T: EthPoolTransaction<Consensus = TxTy<Node::Types>> + OpPooledTx,
542538
{
543539
type Pool = OpTransactionPool<Node::Provider, DiskFileBlobStore, T>;
544540

crates/optimism/txpool/src/transaction.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use crate::{
55
use alloy_consensus::{
66
transaction::Recovered, BlobTransactionSidecar, BlobTransactionValidationError, Typed2718,
77
};
8-
use alloy_eips::{eip2718::WithEncoded, eip2930::AccessList, eip7702::SignedAuthorization};
8+
use alloy_eips::{
9+
eip2718::{Encodable2718, WithEncoded},
10+
eip2930::AccessList,
11+
eip7702::SignedAuthorization,
12+
};
913
use alloy_primitives::{Address, Bytes, TxHash, TxKind, B256, U256};
1014
use alloy_rpc_types_eth::erc4337::TransactionConditional;
1115
use c_kzg::KzgSettings;
@@ -15,9 +19,12 @@ use reth_primitives_traits::{InMemorySize, SignedTransaction};
1519
use reth_transaction_pool::{
1620
EthBlobTransactionSidecar, EthPoolTransaction, EthPooledTransaction, PoolTransaction,
1721
};
18-
use std::sync::{
19-
atomic::{AtomicU64, Ordering},
20-
Arc, OnceLock,
22+
use std::{
23+
borrow::Cow,
24+
sync::{
25+
atomic::{AtomicU64, Ordering},
26+
Arc, OnceLock,
27+
},
2128
};
2229

2330
/// Marker for no-interop transactions
@@ -287,13 +294,19 @@ where
287294
pub trait OpPooledTx:
288295
MaybeConditionalTransaction + MaybeInteropTransaction + PoolTransaction + DataAvailabilitySized
289296
{
297+
/// Returns the EIP-2718 encoded bytes of the transaction.
298+
fn encoded_2718(&self) -> Cow<'_, Bytes>;
290299
}
291-
impl<T> OpPooledTx for T where
292-
T: MaybeConditionalTransaction
293-
+ MaybeInteropTransaction
294-
+ PoolTransaction
295-
+ DataAvailabilitySized
300+
301+
impl<Cons, Pooled> OpPooledTx for OpPooledTransaction<Cons, Pooled>
302+
where
303+
Cons: SignedTransaction + From<Pooled>,
304+
Pooled: SignedTransaction + TryFrom<Cons>,
305+
<Pooled as TryFrom<Cons>>::Error: core::error::Error,
296306
{
307+
fn encoded_2718(&self) -> Cow<'_, Bytes> {
308+
Cow::Borrowed(self.encoded_2718())
309+
}
297310
}
298311

299312
#[cfg(test)]

crates/optimism/txpool/src/validator.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::{interop::MaybeInteropTransaction, supervisor::SupervisorClient, InvalidCrossTx};
1+
use crate::{supervisor::SupervisorClient, InvalidCrossTx, OpPooledTx};
22
use alloy_consensus::{BlockHeader, Transaction};
3-
use alloy_eips::Encodable2718;
43
use op_revm::L1BlockInfo;
54
use parking_lot::RwLock;
65
use reth_chainspec::ChainSpecProvider;
@@ -92,7 +91,7 @@ impl<Client, Tx> OpTransactionValidator<Client, Tx> {
9291
impl<Client, Tx> OpTransactionValidator<Client, Tx>
9392
where
9493
Client: ChainSpecProvider<ChainSpec: OpHardforks> + StateProviderFactory + BlockReaderIdExt,
95-
Tx: EthPoolTransaction + MaybeInteropTransaction,
94+
Tx: EthPoolTransaction + OpPooledTx,
9695
{
9796
/// Create a new [`OpTransactionValidator`].
9897
pub fn new(inner: EthTransactionValidator<Client, Tx>) -> Self {
@@ -268,9 +267,7 @@ where
268267
{
269268
let mut l1_block_info = self.block_info.l1_block_info.read().clone();
270269

271-
let mut encoded = Vec::with_capacity(valid_tx.transaction().encoded_length());
272-
let tx = valid_tx.transaction().clone_into_consensus();
273-
tx.encode_2718(&mut encoded);
270+
let encoded = valid_tx.transaction().encoded_2718();
274271

275272
let cost_addition = match l1_block_info.l1_tx_data_fee(
276273
self.chain_spec(),
@@ -328,7 +325,7 @@ where
328325
impl<Client, Tx> TransactionValidator for OpTransactionValidator<Client, Tx>
329326
where
330327
Client: ChainSpecProvider<ChainSpec: OpHardforks> + StateProviderFactory + BlockReaderIdExt,
331-
Tx: EthPoolTransaction + MaybeInteropTransaction,
328+
Tx: EthPoolTransaction + OpPooledTx,
332329
{
333330
type Transaction = Tx;
334331

examples/custom-node/src/pool.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ use reth_node_builder::{
99
};
1010
use reth_op::{
1111
node::txpool::{
12-
conditional::MaybeConditionalTransaction,
13-
interop::MaybeInteropTransaction,
1412
supervisor::{SupervisorClient, DEFAULT_SUPERVISOR_URL},
15-
OpPooledTransaction, OpTransactionPool, OpTransactionValidator,
13+
OpPooledTransaction, OpPooledTx, OpTransactionPool, OpTransactionValidator,
1614
},
1715
pool::{
1816
blobstore::DiskFileBlobStore, CoinbaseTipOrdering, EthPoolTransaction,
@@ -84,8 +82,7 @@ where
8482
<Node::Types as NodeTypes>::Primitives:
8583
NodePrimitives<SignedTx = Extended<OpTxEnvelope, CustomTransactionEnvelope>>,
8684
T: EthPoolTransaction<Consensus = Extended<OpTxEnvelope, CustomTransactionEnvelope>>
87-
+ MaybeConditionalTransaction
88-
+ MaybeInteropTransaction,
85+
+ OpPooledTx,
8986
{
9087
type Pool = OpTransactionPool<Node::Provider, DiskFileBlobStore, T>;
9188

0 commit comments

Comments
 (0)