Prioritizing re-imported transactions (#2372)
* Prioritizing re-imported transactions * Fixing compilation on beta
This commit is contained in:
@@ -853,9 +853,12 @@ impl BlockChainClient for Client {
|
||||
|
||||
fn transaction_receipt(&self, id: TransactionID) -> Option<LocalizedReceipt> {
|
||||
let chain = self.chain.read();
|
||||
self.transaction_address(id).and_then(|address| chain.block_number(&address.block_hash).and_then(|block_number| {
|
||||
self.transaction_address(id)
|
||||
.and_then(|address| chain.block_number(&address.block_hash).and_then(|block_number| {
|
||||
let t = chain.block_body(&address.block_hash)
|
||||
.and_then(|block| BodyView::new(&block).localized_transaction_at(&address.block_hash, block_number, address.index));
|
||||
.and_then(|block| {
|
||||
BodyView::new(&block).localized_transaction_at(&address.block_hash, block_number, address.index)
|
||||
});
|
||||
|
||||
match (t, chain.transaction_receipt(&address)) {
|
||||
(Some(tx), Some(receipt)) => {
|
||||
|
||||
@@ -924,7 +924,7 @@ impl MinerService for Miner {
|
||||
out_of_chain.for_each(|txs| {
|
||||
let mut transaction_queue = self.transaction_queue.lock();
|
||||
let _ = self.add_transactions_to_queue(
|
||||
chain, txs, TransactionOrigin::External, &mut transaction_queue
|
||||
chain, txs, TransactionOrigin::RetractedBlock, &mut transaction_queue
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -98,6 +98,8 @@ pub enum TransactionOrigin {
|
||||
Local,
|
||||
/// External transaction received from network
|
||||
External,
|
||||
/// Transactions from retracted blocks
|
||||
RetractedBlock,
|
||||
}
|
||||
|
||||
impl PartialOrd for TransactionOrigin {
|
||||
@@ -112,10 +114,11 @@ impl Ord for TransactionOrigin {
|
||||
return Ordering::Equal;
|
||||
}
|
||||
|
||||
if *self == TransactionOrigin::Local {
|
||||
Ordering::Less
|
||||
} else {
|
||||
Ordering::Greater
|
||||
match (*self, *other) {
|
||||
(TransactionOrigin::RetractedBlock, _) => Ordering::Less,
|
||||
(_, TransactionOrigin::RetractedBlock) => Ordering::Greater,
|
||||
(TransactionOrigin::Local, _) => Ordering::Less,
|
||||
_ => Ordering::Greater,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1014,6 +1017,17 @@ mod test {
|
||||
new_tx_pair_default(0.into(), 1.into())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ordering() {
|
||||
assert_eq!(TransactionOrigin::Local.cmp(&TransactionOrigin::External), Ordering::Less);
|
||||
assert_eq!(TransactionOrigin::RetractedBlock.cmp(&TransactionOrigin::Local), Ordering::Less);
|
||||
assert_eq!(TransactionOrigin::RetractedBlock.cmp(&TransactionOrigin::External), Ordering::Less);
|
||||
|
||||
assert_eq!(TransactionOrigin::External.cmp(&TransactionOrigin::Local), Ordering::Greater);
|
||||
assert_eq!(TransactionOrigin::Local.cmp(&TransactionOrigin::RetractedBlock), Ordering::Greater);
|
||||
assert_eq!(TransactionOrigin::External.cmp(&TransactionOrigin::RetractedBlock), Ordering::Greater);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_correct_nonces_when_dropped_because_of_limit() {
|
||||
// given
|
||||
@@ -1375,6 +1389,27 @@ mod test {
|
||||
assert_eq!(top.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_prioritize_reimported_transactions_within_same_nonce_height() {
|
||||
// given
|
||||
let mut txq = TransactionQueue::new();
|
||||
let tx = new_tx_default();
|
||||
// the second one has same nonce but higher `gas_price`
|
||||
let (_, tx2) = new_similar_tx_pair();
|
||||
|
||||
// when
|
||||
// first insert local one with higher gas price
|
||||
txq.add(tx2.clone(), &default_account_details, TransactionOrigin::Local).unwrap();
|
||||
// then the one with lower gas price, but from retracted block
|
||||
txq.add(tx.clone(), &default_account_details, TransactionOrigin::RetractedBlock).unwrap();
|
||||
|
||||
// then
|
||||
let top = txq.top_transactions();
|
||||
assert_eq!(top[0], tx); // retracted 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
|
||||
|
||||
Reference in New Issue
Block a user