Merge pull request #995 from ethcore/tx-queue-fix

Fixing transaction queue last_nonces update
This commit is contained in:
Nikolay Volf 2016-04-25 02:37:00 +03:00
commit 22e6e3f995

View File

@ -577,6 +577,7 @@ impl TransactionQueue {
/// Checks if there are any transactions in `future` that should actually be promoted to `current` /// Checks if there are any transactions in `future` that should actually be promoted to `current`
/// (because nonce matches). /// (because nonce matches).
fn move_matching_future_to_current(&mut self, address: Address, mut current_nonce: U256, first_nonce: U256) { 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); let by_nonce = self.future.by_address.row_mut(&address);
if let None = by_nonce { if let None = by_nonce {
@ -589,12 +590,15 @@ impl TransactionQueue {
// Put to current // Put to current
let order = order.update_height(current_nonce, first_nonce); let order = order.update_height(current_nonce, first_nonce);
self.current.insert(address, current_nonce, order); self.current.insert(address, current_nonce, order);
update_last_nonce_to = Some(current_nonce);
current_nonce = current_nonce + U256::one(); current_nonce = current_nonce + U256::one();
} }
} }
self.future.by_address.clear_if_empty(&address); self.future.by_address.clear_if_empty(&address);
if let Some(x) = update_last_nonce_to {
// Update last inserted nonce // Update last inserted nonce
self.last_nonces.insert(address, current_nonce - U256::one()); self.last_nonces.insert(address, x);
}
} }
/// Adds VerifiedTransaction to this queue. /// Adds VerifiedTransaction to this queue.
@ -1457,4 +1461,29 @@ mod test {
// then // then
assert!(txq.top_transactions().is_empty()); 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);
}
} }