Skip to content

Commit

Permalink
Improve docstrings and code of TransactionPool (#8944)
Browse files Browse the repository at this point in the history
This commit makes docstrings of `TransactionPool` more consistent and simplifies `remove_transaction` implementation.

This is a part of refactoring for #8878.
  • Loading branch information
akashin committed Apr 24, 2023
1 parent fdad133 commit 12e9729
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions chain/pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl TransactionPool {
hash(&v)
}

/// Insert a signed transaction into the pool that passed validation.
/// Inserts a signed transaction that passed validation into the pool.
pub fn insert_transaction(&mut self, signed_transaction: SignedTransaction) -> bool {
if !self.unique_transactions.insert(signed_transaction.get_hash()) {
// The hash of this transaction was already seen, skip it.
Expand All @@ -65,26 +65,31 @@ impl TransactionPool {
true
}

/// Returns a pool iterator wrapper that implements an iterator like trait to iterate over
/// Returns a pool iterator wrapper that implements an iterator-like trait to iterate over
/// transaction groups in the proper order defined by the protocol.
/// When the iterator is dropped, all remaining groups are inserted back into the pool.
pub fn pool_iterator(&mut self) -> PoolIteratorWrapper<'_> {
PoolIteratorWrapper::new(self)
}

/// Quick reconciliation step - evict all transactions that already in the block
/// or became invalid after it.
/// Removes given transactions from the pool.
///
/// In practice, used to evict transactions that have already been included into the block or
/// became invalid.
pub fn remove_transactions(&mut self, transactions: &[SignedTransaction]) {
let mut grouped_transactions = HashMap::new();
for tx in transactions {
if self.unique_transactions.contains(&tx.get_hash()) {
let signer_id = &tx.transaction.signer_id;
let signer_public_key = &tx.transaction.public_key;
grouped_transactions
.entry(self.key(signer_id, signer_public_key))
.or_insert_with(HashSet::new)
.insert(tx.get_hash());
// Transaction is not present in the pool, skip it.
if !self.unique_transactions.contains(&tx.get_hash()) {
continue;
}

let signer_id = &tx.transaction.signer_id;
let signer_public_key = &tx.transaction.public_key;
grouped_transactions
.entry(self.key(signer_id, signer_public_key))
.or_insert_with(HashSet::new)
.insert(tx.get_hash());
}
for (key, hashes) in grouped_transactions {
let mut remove_entry = false;
Expand All @@ -103,13 +108,14 @@ impl TransactionPool {
}
}

/// Reintroduce transactions back during the chain reorg
/// Reintroduces transactions back during the chain reorg.
pub fn reintroduce_transactions(&mut self, transactions: Vec<SignedTransaction>) {
for tx in transactions {
self.insert_transaction(tx);
}
}

/// Returns the number of unique transactions in the pool.
pub fn len(&self) -> usize {
self.unique_transactions.len()
}
Expand Down

0 comments on commit 12e9729

Please sign in to comment.