chore(txpool): add AlreadyImported variant (#2009)

This commit is contained in:
Matthias Seitz
2023-03-28 19:00:30 +02:00
committed by GitHub
parent cf2a2dbf97
commit 3810548bd7
4 changed files with 33 additions and 5 deletions

View File

@ -8,6 +8,9 @@ pub type PoolResult<T> = Result<T, PoolError>;
/// All errors the Transaction pool can throw.
#[derive(Debug, thiserror::Error)]
pub enum PoolError {
/// Same transaction already imported
#[error("[{0:?}] Already imported")]
AlreadyImported(TxHash),
/// Thrown if a replacement transaction's gas price is below the already imported transaction
#[error("[{0:?}]: insufficient gas price to replace existing transaction.")]
ReplacementUnderpriced(TxHash),
@ -36,6 +39,7 @@ impl PoolError {
/// Returns the hash of the transaction that resulted in this error.
pub fn hash(&self) -> &TxHash {
match self {
PoolError::AlreadyImported(hash) => hash,
PoolError::ReplacementUnderpriced(hash) => hash,
PoolError::FeeCapBelowMinimumProtocolFeeCap(hash, _) => hash,
PoolError::SpammerExceededCapacity(_, hash) => hash,
@ -60,6 +64,10 @@ impl PoolError {
#[inline]
pub fn is_bad_transaction(&self) -> bool {
match self {
PoolError::AlreadyImported(_) => {
// already imported but not bad
false
}
PoolError::ReplacementUnderpriced(_) => {
// already imported but not bad
false

View File

@ -279,14 +279,14 @@ where
self.pool.pooled_transactions_hashes()
}
fn pooled_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
self.pool.pooled_transactions()
}
fn pooled_transaction_hashes_max(&self, max: usize) -> Vec<TxHash> {
self.pooled_transaction_hashes().into_iter().take(max).collect()
}
fn pooled_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
self.pool.pooled_transactions()
}
fn pooled_transactions_max(
&self,
max: usize,

View File

@ -214,6 +214,10 @@ impl<T: TransactionOrdering> TxPool<T> {
on_chain_balance: U256,
on_chain_nonce: u64,
) -> PoolResult<AddedTransaction<T::Transaction>> {
if self.contains(tx.hash()) {
return Err(PoolError::AlreadyImported(*tx.hash()))
}
// Update sender info with balance and nonce
self.sender_info
.entry(tx.sender_id())
@ -1146,7 +1150,7 @@ impl SenderInfo {
mod tests {
use super::*;
use crate::{
test_utils::{MockTransaction, MockTransactionFactory},
test_utils::{MockOrdering, MockTransaction, MockTransactionFactory},
traits::TransactionOrigin,
};
@ -1213,6 +1217,21 @@ mod tests {
assert!(inserted.state.intersects(expected_state));
}
#[test]
fn insert_already_imported() {
let on_chain_balance = U256::ZERO;
let on_chain_nonce = 0;
let mut f = MockTransactionFactory::default();
let mut pool = TxPool::new(MockOrdering::default(), Default::default());
let tx = MockTransaction::eip1559().inc_price().inc_limit();
let tx = f.validated(tx);
pool.add_transaction(tx.clone(), on_chain_balance, on_chain_nonce).unwrap();
match pool.add_transaction(tx, on_chain_balance, on_chain_nonce).unwrap_err() {
PoolError::AlreadyImported(_) => {}
_ => unreachable!(),
}
}
#[test]
fn insert_replace() {
let on_chain_balance = U256::ZERO;