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

Fix local transactions policy. #8691

Merged
merged 1 commit into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions miner/src/pool/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ impl txpool::Scoring<VerifiedTransaction> for NonceAndGasPrice {
}
}

// Always kick out non-local transactions in favour of local ones.
if new.priority().is_local() && !old.priority().is_local() {
return true;
}
// And never kick out local transactions in favour of external ones.
if !new.priority().is_local() && old.priority.is_local() {
return false;
}

self.choose(old, new) == txpool::scoring::Choice::ReplaceOld
}
}
Expand All @@ -119,6 +128,30 @@ mod tests {
use pool::tests::tx::{Tx, TxExt};
use txpool::Scoring;

#[test]
fn should_replace_non_local_transaction_with_local_one() {
// given
let scoring = NonceAndGasPrice(PrioritizationStrategy::GasPriceOnly);
let tx1 = {
let tx = Tx::default().signed().verified();
txpool::Transaction {
insertion_id: 0,
transaction: Arc::new(tx),
}
};
let tx2 = {
let mut tx = Tx::default().signed().verified();
tx.priority = ::pool::Priority::Local;
txpool::Transaction {
insertion_id: 0,
transaction: Arc::new(tx),
}
};

assert!(scoring.should_replace(&tx1, &tx2));
assert!(!scoring.should_replace(&tx2, &tx1));
}

#[test]
fn should_calculate_score_correctly() {
// given
Expand Down
33 changes: 32 additions & 1 deletion miner/src/pool/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,4 +766,35 @@ fn should_reject_big_transaction() {
verifier::Transaction::Local(PendingTransaction::new(big_tx, transaction::Condition::Timestamp(1000).into()))
]);
assert_eq!(res, vec![Err(transaction::Error::TooBig)]);
}
}

#[test]
fn should_include_local_transaction_to_a_full_pool() {
// given
let txq = TransactionQueue::new(
txpool::Options {
max_count: 1,
max_per_sender: 2,
max_mem_usage: 50
},
verifier::Options {
minimal_gas_price: 1.into(),
block_gas_limit: 1_000_000.into(),
tx_gas_limit: 1_000_000.into(),
},
PrioritizationStrategy::GasPriceOnly,
);
let tx1 = Tx::gas_price(10_000).signed().unverified();
let tx2 = Tx::gas_price(1).signed().local();

let res = txq.import(TestClient::new().with_balance(1_000_000_000), vec![tx1]);
assert_eq!(res, vec![Ok(())]);
assert_eq!(txq.status().status.transaction_count, 1);

// when
let res = txq.import(TestClient::new(), vec![tx2]);
assert_eq!(res, vec![Ok(())]);

// then
assert_eq!(txq.status().status.transaction_count, 1);
}