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(txpool) feed new pending transactions to BestTxns iterator #4053

Merged
merged 5 commits into from
Aug 4, 2023

Conversation

0xprames
Copy link
Contributor

@0xprames 0xprames commented Aug 4, 2023

creating a draft pr for #4045

feel free to close this out in favor of @supernovahs solution, since they had asked to be assigned - mainly would love any feedback as to whether my understanding of the issue was correct, and if not, where I'm thinking about this solution wrong

@codecov
Copy link

codecov bot commented Aug 4, 2023

Codecov Report

Merging #4053 (968d517) into main (3f63a08) will decrease coverage by 0.03%.
Report is 7 commits behind head on main.
The diff coverage is 100.00%.

Impacted file tree graph

Files Changed Coverage Δ
crates/transaction-pool/src/traits.rs 7.54% <ø> (ø)
crates/transaction-pool/src/pool/best.rs 81.44% <100.00%> (+6.44%) ⬆️
crates/transaction-pool/src/pool/pending.rs 79.06% <100.00%> (+0.70%) ⬆️

... and 21 files with indirect coverage changes

Flag Coverage Δ
integration-tests 16.54% <100.00%> (+0.07%) ⬆️
unit-tests 64.10% <60.86%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
reth binary 25.64% <ø> (-0.21%) ⬇️
blockchain tree 83.04% <ø> (ø)
pipeline 90.07% <ø> (ø)
storage (db) 74.41% <ø> (ø)
trie 94.70% <ø> (ø)
txpool 48.94% <100.00%> (+0.60%) ⬆️
networking 77.40% <ø> (-0.04%) ⬇️
rpc 57.85% <ø> (+<0.01%) ⬆️
consensus 64.08% <ø> (+0.04%) ⬆️
revm 32.59% <ø> (ø)
payload builder 6.58% <ø> (ø)
primitives 88.01% <ø> (ø)

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

this looks great and is exactly what I had in mind.

maybe @supernovahs can take a look as well.
and sorry that issues keep getting sniped lol

/// Used to recieve any new pending transactions that have been added to the pool after this
/// iterator was snapshotted
///
/// We prioritize these new transactions over existing ones by first inserting these into the
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think prioritize is not correct here, since they get the same treatment as txs already included in this snapshot, it's just an extra data source.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yeah given what priority actually means in the pool data structures - prioritize is the wrong word here.
makes sense, i removed this comment completely.

Comment on lines 69 to 71
/// pool, and then yielding. This breaks the contract that the iterator yields the directly
/// "next" best transaction from the POV of the consumer, but we assume more often than not
/// newer transactions are better
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's move these docs to the trait docs, that there is no guarantee that the iterator will only ever return txs with decreasing priority, and instead always returns the best transaction that it currently knows

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah that makes sense and your wording is clearer - modified and moved to trait docs.

Comment on lines 116 to 125
// check for new txs that have come in after this iterator was snapshotted and add them
if let Some(pending_tx) = self.try_recv() {
let tx = pending_tx.transaction.clone();
// same logic as PendingPool::add_transaction/PendingPool::best_with_unlocked
let tx_id = *tx.id();
if self.ancestor(&tx_id).is_none() {
self.independent.insert(pending_tx.clone());
}
self.all.insert(tx_id, pending_tx);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to extract this into a standalone function

@@ -89,6 +113,16 @@ impl<T: TransactionOrdering> Iterator for BestTransactions<T> {

fn next(&mut self) -> Option<Self::Item> {
loop {
// check for new txs that have come in after this iterator was snapshotted and add them
if let Some(pending_tx) = self.try_recv() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this could be made into a while loop

}

// === impl PendingPool ===

impl<T: TransactionOrdering> PendingPool<T> {
/// Create a new pool instance.
pub(crate) fn new(ordering: T) -> Self {
let (new_transaction_notifier, _) = broadcast::channel(1000);
Copy link
Collaborator

Choose a reason for hiding this comment

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

1000 is a bit large, I think 200 would be appropriate

// send the new transaction to any existing pendingpool snapshot iterators
// drop the Result value returned as Err effectively means there are no subscribers
// and Ok just returns the # of subscribed recievers
let _ = self.new_transaction_notifier.send(tx);
Copy link
Collaborator

Choose a reason for hiding this comment

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

we assume the most common case is that there are no receivers in which case we don't need to send anything, so lets move this above the self.by_id.insert and check for receiver_count > 0 this prevents 1 clone

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, modified

@mattsse mattsse marked this pull request as ready for review August 4, 2023 11:50
@mattsse mattsse added the A-tx-pool Related to the transaction mempool label Aug 4, 2023
@supernovahs
Copy link
Contributor

looks good to me

@0xprames 0xprames requested a review from mattsse August 4, 2023 13:54
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

nice,

It would be great to have a test for this, basically

get iterator
insert pending
call iterator.next

@0xprames
Copy link
Contributor Author

0xprames commented Aug 4, 2023

It would be great to have a test for this basically
get iterator
insert pending
call iterator.next

yep makes sense, added in the transaction-pool/tests/it

running 1 test
test pending::txpool_new_pending_txs ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out; finished in 0.00s

@0xprames 0xprames requested a review from mattsse August 4, 2023 20:00
@mattsse mattsse added this pull request to the merge queue Aug 4, 2023
Merged via the queue into paradigmxyz:main with commit 544c51c Aug 4, 2023
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tx-pool Related to the transaction mempool
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants