From edd43cd5c37cda1f0f1cc667f97e71319d952f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 20 Jun 2016 11:32:29 +0200 Subject: [PATCH] Fixing local transactions prioritization --- ethcore/src/miner/transaction_queue.rs | 29 +++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ethcore/src/miner/transaction_queue.rs b/ethcore/src/miner/transaction_queue.rs index 385b2e473..74f996a98 100644 --- a/ethcore/src/miner/transaction_queue.rs +++ b/ethcore/src/miner/transaction_queue.rs @@ -172,6 +172,12 @@ impl Ord for TransactionOrder { return self.nonce_height.cmp(&b.nonce_height); } + // Local transactions should always have priority + // NOTE nonce has to be checked first, cause otherwise the order might be wrong. + if self.origin != b.origin { + return self.origin.cmp(&b.origin); + } + // Then compare gas_prices let a_gas = self.gas_price; let b_gas = b.gas_price; @@ -1082,7 +1088,28 @@ mod test { } #[test] - fn should_not_prioritize_local_transactions() { + fn should_prioritize_local_transactions_within_same_nonce_height() { + // given + let mut txq = TransactionQueue::new(); + let tx = new_tx(); + // the second one has same nonce but higher `gas_price` + let (_, tx2) = new_similar_txs(); + + // when + // first insert the one with higher gas price + txq.add(tx2.clone(), &default_nonce, TransactionOrigin::External).unwrap(); + // then the one with lower gas price, but local + txq.add(tx.clone(), &default_nonce, TransactionOrigin::Local).unwrap(); + + // then + let top = txq.top_transactions(); + assert_eq!(top[0], tx); // local should be first + assert_eq!(top[1], tx2); + assert_eq!(top.len(), 2); + } + + #[test] + fn should_not_prioritize_local_transactions_with_different_nonce_height() { // given let mut txq = TransactionQueue::new(); let (tx, tx2) = new_txs(U256::from(1));