Skip to content

Commit

Permalink
Find PendingIterator in Transaction Pool (#218)
Browse files Browse the repository at this point in the history
* Find next Ready tx in PendingIterator

* Add test for Staled Pending Transactions
  • Loading branch information
ngotchac authored and dvdplm committed Sep 10, 2019
1 parent 5bc6781 commit 67a9e7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
18 changes: 12 additions & 6 deletions transaction-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,20 +611,26 @@ impl<'a, T, R, S, L> Iterator for PendingIterator<'a, T, R, S, L> where
self.best_transactions.take(&best).expect("Just taken from iterator; qed")
};

match self.ready.is_ready(&best.transaction) {
Readiness::Ready => {
// retrieve next one from that sender.
let tx_state = self.ready.is_ready(&best.transaction);
// Add the next best sender's transaction when applicable
match tx_state {
Readiness::Ready | Readiness::Stale => {
// retrieve next one from the same sender.
let next = self.pool.transactions
.get(best.transaction.sender())
.and_then(|s| s.find_next(&best.transaction, &self.pool.scoring));
if let Some((score, tx)) = next {
self.best_transactions.insert(ScoreWithRef::new(score, tx));
}

return Some(best.transaction.transaction)
},
state => trace!("[{:?}] Ignoring {:?} transaction.", best.transaction.hash(), state),
_ => (),
}

if tx_state == Readiness::Ready {
return Some(best.transaction.transaction)
}

trace!("[{:?}] Ignoring {:?} transaction.", best.transaction.hash(), tx_state);
}

None
Expand Down
17 changes: 17 additions & 0 deletions transaction-pool/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@ fn should_construct_pending() {
assert_eq!(pending.next(), None);
}

#[test]
fn should_skip_staled_pending_transactions() {
let b = TransactionBuilder::default();
let mut txq = TestPool::default();

let tx0 = import(&mut txq, b.tx().nonce(0).gas_price(5).new()).unwrap();
let tx2 = import(&mut txq, b.tx().nonce(2).gas_price(5).new()).unwrap();
let tx1 = import(&mut txq, b.tx().nonce(1).gas_price(5).new()).unwrap();

// tx0 and tx1 are Stale, tx2 is Ready
let mut pending = txq.pending(NonceReady::new(2));

// tx0 and tx1 should be skipped, tx2 should be the next Ready
assert_eq!(pending.next(), Some(tx2));
assert_eq!(pending.next(), None);
}

#[test]
fn should_return_unordered_iterator() {
// given
Expand Down

0 comments on commit 67a9e7d

Please sign in to comment.