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

dev(pool): trait object safe BestTransactions #10478

Merged
merged 3 commits into from
Aug 26, 2024
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
30 changes: 30 additions & 0 deletions crates/transaction-pool/src/pool/best.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,34 @@ mod tests {
assert_eq!(best.independent.len(), 2);
assert!(!best.independent.contains(&pending_tx2));
}

#[test]
fn test_best_transactions_filter_trait_object() {
// Initialize a new PendingPool with default MockOrdering and MockTransactionFactory
let mut pool = PendingPool::new(MockOrdering::default());
let mut f = MockTransactionFactory::default();

// Add 5 transactions with increasing nonces to the pool
let num_tx = 5;
let tx = MockTransaction::eip1559();
for nonce in 0..num_tx {
let tx = tx.clone().rng_hash().with_nonce(nonce);
let valid_tx = f.validated(tx);
pool.add_transaction(Arc::new(valid_tx), 0);
}

// Create a trait object of BestTransactions iterator from the pool
let best: Box<dyn crate::traits::BestTransactions<Item = _>> = Box::new(pool.best());

// Create a filter that only returns transactions with even nonces
let filter =
BestTransactionFilter::new(best, |tx: &Arc<ValidPoolTransaction<MockTransaction>>| {
tx.nonce() % 2 == 0
});
Comment on lines +616 to +620
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should use the trait function on best

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean the trait function filter? I can't use it because of the Sized requirement

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah I see, then this won't be very useful because we usually operate on Box<dyn and won't be much different from the previous impl right? so what does this change actually achieve?


// Verify that the filter only returns transactions with even nonces
for tx in filter {
assert_eq!(tx.nonce() % 2, 0);
}
}
}
28 changes: 27 additions & 1 deletion crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,31 @@ pub trait BestTransactions: Iterator + Send {
///
/// If set to true, no blob transactions will be returned.
fn set_skip_blobs(&mut self, skip_blobs: bool);
}

impl<T> BestTransactions for Box<T>
where
T: BestTransactions + ?Sized,
{
fn mark_invalid(&mut self, transaction: &Self::Item) {
(**self).mark_invalid(transaction);
}

fn no_updates(&mut self) {
(**self).no_updates();
}

fn skip_blobs(&mut self) {
(**self).skip_blobs();
}

fn set_skip_blobs(&mut self, skip_blobs: bool) {
(**self).set_skip_blobs(skip_blobs);
}
}

/// A subtrait on the [`BestTransactions`] trait that allows to filter transactions.
pub trait BestTransactionsFilter: BestTransactions {
/// Creates an iterator which uses a closure to determine if a transaction should be yielded.
///
/// Given an element the closure must return true or false. The returned iterator will yield
Expand All @@ -732,6 +756,8 @@ pub trait BestTransactions: Iterator + Send {
}
}

impl<T> BestTransactionsFilter for T where T: BestTransactions {}

/// A no-op implementation that yields no transactions.
impl<T> BestTransactions for std::iter::Empty<T> {
fn mark_invalid(&mut self, _tx: &T) {}
Expand All @@ -743,7 +769,7 @@ impl<T> BestTransactions for std::iter::Empty<T> {
fn set_skip_blobs(&mut self, _skip_blobs: bool) {}
}

/// A Helper type that bundles best transactions attributes together.
/// A Helper type that bundles the best transactions attributes together.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct BestTransactionsAttributes {
/// The base fee attribute for best transactions.
Expand Down
Loading