Skip to content

Commit 2dfce7f

Browse files
authored
fix: Handle size update on partial pool iterator update (#9186)
The previous version of the code had a leak in the `total_transaction_size` because we didn't update it when we were dropping the empty group from `sorted_groups`. Overall, I find the current implementation of transaction pool really tricky to work with and I'm more and more tempted to do #9060 sooner.
1 parent 8fd8feb commit 2dfce7f

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

chain/pool/src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,20 @@ impl<'a> PoolIterator for PoolIteratorWrapper<'a> {
242242
while let Some(sorted_group) = self.sorted_groups.pop_front() {
243243
if sorted_group.transactions.is_empty() {
244244
for hash in sorted_group.removed_transaction_hashes {
245-
if self.pool.unique_transactions.remove(&hash) {
246-
self.pool.transaction_pool_count_metric.dec();
247-
}
245+
self.pool.unique_transactions.remove(&hash);
248246
}
247+
// See the comment in `insert_transaction` where we increase the size for reasoning
248+
// why panicing here catches a logic error.
249+
self.pool.total_transaction_size = self
250+
.pool
251+
.total_transaction_size
252+
.checked_sub(sorted_group.removed_transaction_size)
253+
.expect("Total transaction size dropped below zero");
254+
255+
self.pool
256+
.transaction_pool_count_metric
257+
.set(self.pool.unique_transactions.len() as i64);
258+
self.pool.transaction_pool_size_metric.set(self.pool.transaction_size() as i64);
249259
} else {
250260
self.sorted_groups.push_back(sorted_group);
251261
return Some(self.sorted_groups.back_mut().expect("just pushed"));
@@ -478,6 +488,9 @@ mod tests {
478488
}
479489
}
480490
}
491+
drop(pool_iter);
492+
assert_eq!(pool.len(), 0);
493+
assert_eq!(pool.transaction_size(), 0);
481494
let mut nonces: Vec<_> = res.into_iter().map(|tx| tx.transaction.nonce).collect();
482495
sort_pairs(&mut nonces[..4]);
483496
assert_eq!(nonces, vec![1, 21, 3, 23, 25, 27, 29, 31]);

0 commit comments

Comments
 (0)