Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add tx_type() and encoded_length() on PoolTransaction trait #1500

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/transaction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ normal = [

[dependencies]

# eth
# reth
reth-primitives = { path = "../primitives" }
reth-rlp = { path = "../rlp" }

# async/futures
async-trait = "0.1"
Expand Down
13 changes: 12 additions & 1 deletion crates/transaction-pool/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rand::{
};
use reth_primitives::{
Address, FromRecoveredTransaction, IntoRecoveredTransaction, Transaction, TransactionKind,
TransactionSignedEcRecovered, TxEip1559, TxHash, TxLegacy, H256, U128, U256,
TransactionSignedEcRecovered, TxEip1559, TxHash, TxLegacy, TxType, H256, U128, U256,
};
use std::{ops::Range, sync::Arc, time::Instant};

Expand Down Expand Up @@ -345,6 +345,17 @@ impl PoolTransaction for MockTransaction {
fn size(&self) -> usize {
0
}

fn tx_type(&self) -> u8 {
match self {
MockTransaction::Legacy { .. } => TxType::Legacy.into(),
MockTransaction::Eip1559 { .. } => TxType::EIP1559.into(),
}
}

fn encoded_length(&self) -> usize {
0
}
}

impl FromRecoveredTransaction for MockTransaction {
Expand Down
17 changes: 17 additions & 0 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use reth_primitives::{
Address, FromRecoveredTransaction, IntoRecoveredTransaction, PeerId, Transaction,
TransactionKind, TransactionSignedEcRecovered, TxHash, H256, U256,
};
use reth_rlp::Encodable;
use std::{collections::HashMap, fmt, sync::Arc};
use tokio::sync::mpsc::Receiver;

Expand Down Expand Up @@ -287,6 +288,12 @@ pub trait PoolTransaction: fmt::Debug + Send + Sync + FromRecoveredTransaction {

/// Returns a measurement of the heap usage of this type and all its internals.
fn size(&self) -> usize;

/// Returns the transaction type
fn tx_type(&self) -> u8;

/// Returns the length of the rlp encoded object
fn encoded_length(&self) -> usize;
}

/// The default [PoolTransaction] for the [Pool](crate::Pool).
Expand Down Expand Up @@ -372,6 +379,16 @@ impl PoolTransaction for PooledTransaction {
fn size(&self) -> usize {
self.transaction.transaction.input().len()
}

/// Returns the transaction type
fn tx_type(&self) -> u8 {
self.transaction.tx_type().into()
}

/// Returns the length of the rlp encoded object
fn encoded_length(&self) -> usize {
self.transaction.length()
}
Comment on lines +388 to +391
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is sufficient for now,
perhaps we should think about adding this value to the ValidatedTransaction instead so we don't have to do this all the time when we send this transaction over p2p

Copy link
Contributor Author

@leruaa leruaa Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was just writing a comment about this, I wasn't able to access a ValidPoolTransaction while impl encoded_length() on PooledTransaction, so it must be something I don't understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I get it. I'll wait this get merged and will do another PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let's use the same pattern as in Transaction ECRECOVER where we don't want to recalculate the signer all the time because it's expensive so we put it in TransactionEcRecovered or whatever.

}

impl FromRecoveredTransaction for PooledTransaction {
Expand Down