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

Commit

Permalink
Fix local transactions policy. (#8691)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw authored and andresilva committed Jun 4, 2018
1 parent 53b56ff commit 2826238
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
33 changes: 33 additions & 0 deletions miner/src/pool/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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);
}

0 comments on commit 2826238

Please sign in to comment.