mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore(txpool): add AlreadyImported variant (#2009)
This commit is contained in:
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user