Faster tx known check (#1468)

* fast check for known tx when adding to pool

* rustfmt
This commit is contained in:
Antioch Peverell 2018-09-03 12:35:37 +01:00 committed by GitHub
parent 544a296f77
commit 0cbfeaab94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View file

@ -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 /// Query the tx pool for all known txs based on kernel short_ids
/// from the provided compact_block. /// from the provided compact_block.
/// Note: does not validate that we return the full set of required txs. /// Note: does not validate that we return the full set of required txs.

View file

@ -101,6 +101,12 @@ impl TransactionPool {
stem: bool, stem: bool,
block_hash: &Hash, block_hash: &Hash,
) -> Result<(), PoolError> { ) -> 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? // Do we have the capacity to accept this transaction?
self.is_acceptable(&tx)?; self.is_acceptable(&tx)?;