Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #995 from ethcore/tx-queue-fix
Browse files Browse the repository at this point in the history
Fixing transaction queue last_nonces update
  • Loading branch information
NikVolf committed Apr 24, 2016
2 parents a73323e + b4fc758 commit 22e6e3f
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions miner/src/transaction_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ impl TransactionQueue {
/// Checks if there are any transactions in `future` that should actually be promoted to `current`
/// (because nonce matches).
fn move_matching_future_to_current(&mut self, address: Address, mut current_nonce: U256, first_nonce: U256) {
let mut update_last_nonce_to = None;
{
let by_nonce = self.future.by_address.row_mut(&address);
if let None = by_nonce {
Expand All @@ -589,12 +590,15 @@ impl TransactionQueue {
// Put to current
let order = order.update_height(current_nonce, first_nonce);
self.current.insert(address, current_nonce, order);
update_last_nonce_to = Some(current_nonce);
current_nonce = current_nonce + U256::one();
}
}
self.future.by_address.clear_if_empty(&address);
// Update last inserted nonce
self.last_nonces.insert(address, current_nonce - U256::one());
if let Some(x) = update_last_nonce_to {
// Update last inserted nonce
self.last_nonces.insert(address, x);
}
}

/// Adds VerifiedTransaction to this queue.
Expand Down Expand Up @@ -1457,4 +1461,29 @@ mod test {
// then
assert!(txq.top_transactions().is_empty());
}

#[test]
fn should_return_valid_last_nonce_after_remove_all() {
// given
let mut txq = TransactionQueue::new();
let (tx1, tx2) = new_txs(U256::from(4));
let sender = tx1.sender().unwrap();
let (nonce1, nonce2) = (tx1.nonce, tx2.nonce);
let details1 = |_a: &Address| AccountDetails { nonce: nonce1, balance: !U256::zero() };

// when
// Insert first transaction
assert_eq!(txq.add(tx1, &details1).unwrap(), TransactionImportResult::Current);
// Second should go to future
assert_eq!(txq.add(tx2, &details1).unwrap(), TransactionImportResult::Future);
// Now block is imported
txq.remove_all(sender, nonce2 - U256::from(1));
// tx2 should be not be promoted to current
assert_eq!(txq.status().pending, 0);
assert_eq!(txq.status().future, 1);

// then
assert_eq!(txq.last_nonce(&sender), None);

}
}

0 comments on commit 22e6e3f

Please sign in to comment.