From 0cbfeaab94945a37018f0fb1cc69ac161aa60c5d Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Mon, 3 Sep 2018 12:35:37 +0100 Subject: [PATCH] Faster tx known check (#1468) * fast check for known tx when adding to pool * rustfmt --- pool/src/pool.rs | 5 +++++ pool/src/transaction_pool.rs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/pool/src/pool.rs b/pool/src/pool.rs index b4bcb9b6f..03565853e 100644 --- a/pool/src/pool.rs +++ b/pool/src/pool.rs @@ -57,6 +57,11 @@ impl Pool { } } + /// Does the transaction pool contain an entry for the given transaction? + pub fn contains_tx(&self, tx: &Transaction) -> bool { + self.entries.iter().any(|x| x.tx.hash() == tx.hash()) + } + /// Query the tx pool for all known txs based on kernel short_ids /// from the provided compact_block. /// Note: does not validate that we return the full set of required txs. diff --git a/pool/src/transaction_pool.rs b/pool/src/transaction_pool.rs index 4ff236200..ce9b3ab5f 100644 --- a/pool/src/transaction_pool.rs +++ b/pool/src/transaction_pool.rs @@ -101,6 +101,12 @@ impl TransactionPool { stem: bool, block_hash: &Hash, ) -> Result<(), PoolError> { + // Quick check to deal with common case of seeing the *same* tx + // broadcast from multiple peers simultaneously. + if !stem && self.txpool.contains_tx(&tx) { + return Err(PoolError::DuplicateTx); + } + // Do we have the capacity to accept this transaction? self.is_acceptable(&tx)?;