-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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): function to return the next free nonce #11744
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also need a simple test for this
1244355
to
6334854
Compare
@@ -356,6 +356,9 @@ pub trait TransactionPool: Send + Sync + Clone { | |||
sender: Address, | |||
) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>; | |||
|
|||
/// Returns the next free nonce for the given sender. | |||
fn get_next_valid_transaction_by_sender(&self, sender: Address, on_chain_nonce: u64) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should call this get_highest_consecutive_transaction_by_sender
@@ -356,6 +356,9 @@ pub trait TransactionPool: Send + Sync + Clone { | |||
sender: Address, | |||
) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>; | |||
|
|||
/// Returns the next free nonce for the given sender. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns the next free nonce for the given sender. | |
/// Returns the transaction with the highest nonce that is a direct ancestor of the on chain nonce without a nonce gap. |
|
||
if tx_nonce == current_nonce + 1 { | ||
// This transaction is connected to the previous one | ||
last_connected_tx = Some(Arc::clone(&tx.transaction)); | ||
current_nonce = tx_nonce; | ||
} else if tx_nonce > current_nonce + 1 { | ||
// We've found a gap, so we stop here | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can make this slightly nicer by incrementing the next consecutive nonce early
sender: SenderId, | ||
on_chain_nonce: u64, | ||
) -> Option<Arc<ValidPoolTransaction<T::Transaction>>> { | ||
let mut next_expected_nonce = on_chain_nonce + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not sure this is entirely correct. if the on chain nonce for an account is 1, then the next transaction sent should have nonce 1, not nonce 2.
@@ -108,6 +108,32 @@ impl<T: TransactionOrdering> TxPool<T> { | |||
self.all().txs_iter(sender).last().map(|(_, tx)| Arc::clone(&tx.transaction)) | |||
} | |||
|
|||
/// Returns the next valid transaction for the given sender. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
going by the test, this returns the highest consecutive nonce in the pool, right? so the consumer would need to +1 on the value returned by this fn. this is slightly different behavior than e.g. the nonce RPC API, as this actually returns the exact value you need (i.e. no need to +1)
@onbjerg tks, that makes sense. I updated the code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
I made one simplification by leveraging one special API function
Closes #11739