* fix(rpc-types): replace uint and hash with `ethereum_types v0.4` (#10217) * fix(rpc-types): remove uint and hash wrappers * fix(tests) * fix(cleanup) * grumbles(rpc-api): revert `verify_signature` * revert change of `U64` -> `u64` * fix(cleanup after bad merge) * chore(bump ethereum-types) * fix(bad merge) * feat(tests ethereum-types): add tests * chore(update `ethereum-types` to 0.4.2) * feat(tests for h256) * chore(rpc): remove `ethbloom` import Use re-export from `ethereum-types` instead * fix(bad merge): remove `DefaultAccount` type * doc(add TODO with issue link) * chore(bump ethereum-types) (#10396) Fixes a de-serialization bug in `ethereum-tyes` * fix(light eth_gasPrice): ask network if not in cache (#10535) * fix(light eth_gasPrice): ask N/W if not in cache * fix(bad rebase) * fix(light account response): update `tx_queue` (#10545) * fix(bump dependencies) (#10540) * cargo update -p log:0.4.5 * cargo update -p regex:1.0.5 * cargo update -p parking_lot * cargo update -p serde_derive * cargo update -p serde_json * cargo update -p serde * cargo update -p lazy_static * cargo update -p num_cpus * cargo update -p toml # Conflicts: # Cargo.lock * tx-pool: check transaction readiness before replacing (#10526) * Update to vanilla tx pool error * Prevent a non ready tx replacing a ready tx * Make tests compile * Test ready tx not replaced by future tx * Transaction indirection * Use StateReadiness to calculate Ready in `should_replace` * Test existing txs from same sender are used to compute Readiness * private-tx: Wire up ShouldReplace * Revert "Use StateReadiness to calculate Ready in `should_replace`" This reverts commit af9e69c8 * Make replace generic so it works with private-tx * Rename Replace and add missing docs * ShouldReplace no longer mutable * tx-pool: update to transaction-pool 2.0 from crates.io * tx-pool: generic error type alias * Exit early for first unmatching nonce * Fix private-tx test, use existing write lock * Use read lock for pool scoring * fix #10390 (#10391) * private-tx: replace error_chain (#10510) * Update to vanilla tx pool error * private-tx: remove error-chain, implement Error, derive Display * private-tx: replace ErrorKind and bail! * private-tx: add missing From impls and other compiler errors * private-tx: use original tx-pool error * Don't be silly cargo
416 lines
13 KiB
Rust
416 lines
13 KiB
Rust
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
// This file is part of Parity Ethereum.
|
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Replacing Transactions
|
|
//!
|
|
//! When queue limits are reached, a new transaction may replace one already
|
|
//! in the pool. The decision whether to reject, replace or retain both is
|
|
//! delegated to an implementation of `ShouldReplace`.
|
|
//!
|
|
//! Here we decide based on the sender, the nonce and gas price, and finally
|
|
//! on the `Readiness` of the transactions when comparing them
|
|
|
|
use std::cmp;
|
|
|
|
use ethereum_types::{U256, H160 as Address};
|
|
use txpool::{self, scoring::{Choice, Scoring}, ReplaceTransaction};
|
|
use txpool::VerifiedTransaction;
|
|
use super::{client, ScoredTransaction};
|
|
|
|
/// Choose whether to replace based on the sender, the score and finally the
|
|
/// `Readiness` of the transactions being compared.
|
|
#[derive(Debug)]
|
|
pub struct ReplaceByScoreAndReadiness<S, C> {
|
|
scoring: S,
|
|
client: C,
|
|
}
|
|
|
|
impl<S, C> ReplaceByScoreAndReadiness<S, C> {
|
|
/// Create a new `ReplaceByScoreAndReadiness`
|
|
pub fn new(scoring: S, client: C) -> Self {
|
|
ReplaceByScoreAndReadiness { scoring, client }
|
|
}
|
|
}
|
|
|
|
impl<T, S, C> txpool::ShouldReplace<T> for ReplaceByScoreAndReadiness<S, C>
|
|
where
|
|
T: VerifiedTransaction<Sender = Address> + ScoredTransaction + PartialEq,
|
|
S: Scoring<T>,
|
|
C: client::NonceClient,
|
|
{
|
|
fn should_replace(
|
|
&self,
|
|
old: &ReplaceTransaction<T>,
|
|
new: &ReplaceTransaction<T>,
|
|
) -> Choice {
|
|
let both_local = old.priority().is_local() && new.priority().is_local();
|
|
if old.sender() == new.sender() {
|
|
// prefer earliest transaction
|
|
match new.nonce().cmp(&old.nonce()) {
|
|
cmp::Ordering::Equal => self.scoring.choose(&old, &new),
|
|
_ if both_local => Choice::InsertNew,
|
|
cmp::Ordering::Less => Choice::ReplaceOld,
|
|
cmp::Ordering::Greater => Choice::RejectNew,
|
|
}
|
|
} else if both_local {
|
|
Choice::InsertNew
|
|
} else {
|
|
let old_score = (old.priority(), old.gas_price());
|
|
let new_score = (new.priority(), new.gas_price());
|
|
if new_score > old_score {
|
|
let state = &self.client;
|
|
// calculate readiness based on state nonce + pooled txs from same sender
|
|
let is_ready = |replace: &ReplaceTransaction<T>| {
|
|
let mut nonce = state.account_nonce(replace.sender());
|
|
if let Some(txs) = replace.pooled_by_sender {
|
|
for tx in txs.iter() {
|
|
if nonce == tx.nonce() && *tx.transaction != ***replace.transaction {
|
|
nonce = nonce.saturating_add(U256::from(1))
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
nonce == replace.nonce()
|
|
};
|
|
|
|
if !is_ready(new) && is_ready(old) {
|
|
// prevent a ready transaction being replace by a non-ready transaction
|
|
Choice::RejectNew
|
|
} else {
|
|
Choice::ReplaceOld
|
|
}
|
|
} else {
|
|
Choice::RejectNew
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
use std::sync::Arc;
|
|
use ethkey::{Random, Generator, KeyPair};
|
|
use pool::tests::tx::{Tx, TxExt};
|
|
use pool::tests::client::TestClient;
|
|
use pool::scoring::*;
|
|
use pool::{PrioritizationStrategy, VerifiedTransaction};
|
|
use txpool::scoring::Choice::*;
|
|
use txpool::ShouldReplace;
|
|
|
|
fn local_tx_verified(tx: Tx, keypair: &KeyPair) -> VerifiedTransaction {
|
|
let mut verified_tx = tx.unsigned().sign(keypair.secret(), None).verified();
|
|
verified_tx.priority = ::pool::Priority::Local;
|
|
verified_tx
|
|
}
|
|
|
|
fn should_replace(replace: &ShouldReplace<VerifiedTransaction>, old: VerifiedTransaction, new: VerifiedTransaction) -> Choice {
|
|
let old_tx = txpool::Transaction { insertion_id: 0, transaction: Arc::new(old) };
|
|
let new_tx = txpool::Transaction { insertion_id: 0, transaction: Arc::new(new) };
|
|
let old = ReplaceTransaction::new(&old_tx, Default::default());
|
|
let new = ReplaceTransaction::new(&new_tx, Default::default());
|
|
replace.should_replace(&old, &new)
|
|
}
|
|
|
|
#[test]
|
|
fn should_always_accept_local_transactions_unless_same_sender_and_nonce() {
|
|
let scoring = NonceAndGasPrice(PrioritizationStrategy::GasPriceOnly);
|
|
let client = TestClient::new().with_nonce(1);
|
|
let replace = ReplaceByScoreAndReadiness::new(scoring, client);
|
|
|
|
// same sender txs
|
|
let keypair = Random.generate().unwrap();
|
|
|
|
let same_sender_tx1 = local_tx_verified(Tx {
|
|
nonce: 1,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
}, &keypair);
|
|
|
|
let same_sender_tx2 = local_tx_verified(Tx {
|
|
nonce: 2,
|
|
gas_price: 100,
|
|
..Default::default()
|
|
}, &keypair);
|
|
|
|
let same_sender_tx3 = local_tx_verified(Tx {
|
|
nonce: 2,
|
|
gas_price: 200,
|
|
..Default::default()
|
|
}, &keypair);
|
|
|
|
// different sender txs
|
|
let sender1 = Random.generate().unwrap();
|
|
let different_sender_tx1 = local_tx_verified(Tx {
|
|
nonce: 2,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
}, &sender1);
|
|
|
|
let sender2 = Random.generate().unwrap();
|
|
let different_sender_tx2 = local_tx_verified(Tx {
|
|
nonce: 1,
|
|
gas_price: 10,
|
|
..Default::default()
|
|
}, &sender2);
|
|
|
|
assert_eq!(should_replace(&replace, same_sender_tx1.clone(), same_sender_tx2.clone()), InsertNew);
|
|
assert_eq!(should_replace(&replace, same_sender_tx2.clone(), same_sender_tx1.clone()), InsertNew);
|
|
|
|
assert_eq!(should_replace(&replace, different_sender_tx1.clone(), different_sender_tx2.clone()), InsertNew);
|
|
assert_eq!(should_replace(&replace, different_sender_tx2.clone(), different_sender_tx1.clone()), InsertNew);
|
|
|
|
// txs with same sender and nonce
|
|
assert_eq!(should_replace(&replace, same_sender_tx2.clone(), same_sender_tx3.clone()), ReplaceOld);
|
|
assert_eq!(should_replace(&replace, same_sender_tx3.clone(), same_sender_tx2.clone()), RejectNew);
|
|
}
|
|
|
|
#[test]
|
|
fn should_replace_same_sender_by_nonce() {
|
|
let scoring = NonceAndGasPrice(PrioritizationStrategy::GasPriceOnly);
|
|
let client = TestClient::new().with_nonce(1);
|
|
let replace = ReplaceByScoreAndReadiness::new(scoring, client);
|
|
|
|
let tx1 = Tx {
|
|
nonce: 1,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
let tx2 = Tx {
|
|
nonce: 2,
|
|
gas_price: 100,
|
|
..Default::default()
|
|
};
|
|
let tx3 = Tx {
|
|
nonce: 2,
|
|
gas_price: 110,
|
|
..Default::default()
|
|
};
|
|
let tx4 = Tx {
|
|
nonce: 2,
|
|
gas_price: 130,
|
|
..Default::default()
|
|
};
|
|
|
|
let keypair = Random.generate().unwrap();
|
|
let txs = vec![tx1, tx2, tx3, tx4].into_iter().map(|tx| {
|
|
tx.unsigned().sign(keypair.secret(), None).verified()
|
|
}).collect::<Vec<_>>();
|
|
|
|
assert_eq!(should_replace(&replace, txs[0].clone(), txs[1].clone()), RejectNew);
|
|
assert_eq!(should_replace(&replace, txs[1].clone(), txs[0].clone()), ReplaceOld);
|
|
|
|
assert_eq!(should_replace(&replace, txs[1].clone(), txs[2].clone()), RejectNew);
|
|
assert_eq!(should_replace(&replace, txs[2].clone(), txs[1].clone()), RejectNew);
|
|
|
|
assert_eq!(should_replace(&replace, txs[1].clone(), txs[3].clone()), ReplaceOld);
|
|
assert_eq!(should_replace(&replace, txs[3].clone(), txs[1].clone()), RejectNew);
|
|
}
|
|
|
|
#[test]
|
|
fn should_replace_different_sender_by_priority_and_gas_price() {
|
|
// given
|
|
let scoring = NonceAndGasPrice(PrioritizationStrategy::GasPriceOnly);
|
|
let client = TestClient::new().with_nonce(0);
|
|
let replace = ReplaceByScoreAndReadiness::new(scoring, client);
|
|
|
|
let tx_regular_low_gas = {
|
|
let tx = Tx {
|
|
nonce: 1,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
tx.signed().verified()
|
|
};
|
|
let tx_regular_high_gas = {
|
|
let tx = Tx {
|
|
nonce: 2,
|
|
gas_price: 10,
|
|
..Default::default()
|
|
};
|
|
tx.signed().verified()
|
|
};
|
|
let tx_local_low_gas = {
|
|
let tx = Tx {
|
|
nonce: 2,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
let mut verified_tx = tx.signed().verified();
|
|
verified_tx.priority = ::pool::Priority::Local;
|
|
verified_tx
|
|
};
|
|
let tx_local_high_gas = {
|
|
let tx = Tx {
|
|
nonce: 1,
|
|
gas_price: 10,
|
|
..Default::default()
|
|
};
|
|
let mut verified_tx = tx.signed().verified();
|
|
verified_tx.priority = ::pool::Priority::Local;
|
|
verified_tx
|
|
};
|
|
|
|
assert_eq!(should_replace(&replace, tx_regular_low_gas.clone(), tx_regular_high_gas.clone()), ReplaceOld);
|
|
assert_eq!(should_replace(&replace, tx_regular_high_gas.clone(), tx_regular_low_gas.clone()), RejectNew);
|
|
|
|
assert_eq!(should_replace(&replace, tx_regular_high_gas.clone(), tx_local_low_gas.clone()), ReplaceOld);
|
|
assert_eq!(should_replace(&replace, tx_local_low_gas.clone(), tx_regular_high_gas.clone()), RejectNew);
|
|
|
|
assert_eq!(should_replace(&replace, tx_local_low_gas.clone(), tx_local_high_gas.clone()), InsertNew);
|
|
assert_eq!(should_replace(&replace, tx_local_high_gas.clone(), tx_regular_low_gas.clone()), RejectNew);
|
|
}
|
|
|
|
#[test]
|
|
fn should_not_replace_ready_transaction_with_future_transaction() {
|
|
let scoring = NonceAndGasPrice(PrioritizationStrategy::GasPriceOnly);
|
|
let client = TestClient::new().with_nonce(1);
|
|
let replace = ReplaceByScoreAndReadiness::new(scoring, client);
|
|
|
|
let tx_ready_low_score = {
|
|
let tx = Tx {
|
|
nonce: 1,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
tx.signed().verified()
|
|
};
|
|
let tx_future_high_score = {
|
|
let tx = Tx {
|
|
nonce: 3, // future nonce
|
|
gas_price: 10,
|
|
..Default::default()
|
|
};
|
|
tx.signed().verified()
|
|
};
|
|
|
|
assert_eq!(should_replace(&replace, tx_ready_low_score, tx_future_high_score), RejectNew);
|
|
}
|
|
|
|
#[test]
|
|
fn should_compute_readiness_with_pooled_transactions_from_the_same_sender_as_the_existing_transaction() {
|
|
let scoring = NonceAndGasPrice(PrioritizationStrategy::GasPriceOnly);
|
|
let client = TestClient::new().with_nonce(1);
|
|
let replace = ReplaceByScoreAndReadiness::new(scoring, client);
|
|
|
|
let old_sender = Random.generate().unwrap();
|
|
let tx_old_ready_1 = {
|
|
let tx = Tx {
|
|
nonce: 1,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
tx.unsigned().sign(&old_sender.secret(), None).verified()
|
|
};
|
|
let tx_old_ready_2 = {
|
|
let tx = Tx {
|
|
nonce: 2,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
tx.unsigned().sign(&old_sender.secret(), None).verified()
|
|
};
|
|
let tx_old_ready_3 = {
|
|
let tx = Tx {
|
|
nonce: 3,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
tx.unsigned().sign(&old_sender.secret(), None).verified()
|
|
};
|
|
|
|
let new_tx = {
|
|
let tx = Tx {
|
|
nonce: 3, // future nonce
|
|
gas_price: 10,
|
|
..Default::default()
|
|
};
|
|
tx.signed().verified()
|
|
};
|
|
|
|
let old_tx = txpool::Transaction { insertion_id: 0, transaction: Arc::new(tx_old_ready_3) };
|
|
let pooled_txs = [
|
|
txpool::Transaction { insertion_id: 0, transaction: Arc::new(tx_old_ready_1) },
|
|
txpool::Transaction { insertion_id: 0, transaction: Arc::new(tx_old_ready_2) },
|
|
];
|
|
|
|
let new_tx = txpool::Transaction { insertion_id: 0, transaction: Arc::new(new_tx) };
|
|
|
|
let old = ReplaceTransaction::new(&old_tx, Some(&pooled_txs));
|
|
let new = ReplaceTransaction::new(&new_tx, Default::default());
|
|
|
|
assert_eq!(replace.should_replace(&old, &new), RejectNew);
|
|
}
|
|
|
|
#[test]
|
|
fn should_compute_readiness_with_pooled_transactions_from_the_same_sender_as_the_new_transaction() {
|
|
let scoring = NonceAndGasPrice(PrioritizationStrategy::GasPriceOnly);
|
|
let client = TestClient::new().with_nonce(1);
|
|
let replace = ReplaceByScoreAndReadiness::new(scoring, client);
|
|
|
|
// current transaction is ready but has a lower gas price than the new one
|
|
let old_tx = {
|
|
let tx = Tx {
|
|
nonce: 1,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
tx.signed().verified()
|
|
};
|
|
|
|
let new_sender = Random.generate().unwrap();
|
|
let tx_new_ready_1 = {
|
|
let tx = Tx {
|
|
nonce: 1,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
tx.unsigned().sign(&new_sender.secret(), None).verified()
|
|
};
|
|
let tx_new_ready_2 = {
|
|
let tx = Tx {
|
|
nonce: 2,
|
|
gas_price: 1,
|
|
..Default::default()
|
|
};
|
|
tx.unsigned().sign(&new_sender.secret(), None).verified()
|
|
};
|
|
let tx_new_ready_3 = {
|
|
let tx = Tx {
|
|
nonce: 3,
|
|
gas_price: 10, // hi
|
|
..Default::default()
|
|
};
|
|
tx.unsigned().sign(&new_sender.secret(), None).verified()
|
|
};
|
|
|
|
let old_tx = txpool::Transaction { insertion_id: 0, transaction: Arc::new(old_tx) };
|
|
|
|
let new_tx = txpool::Transaction { insertion_id: 0, transaction: Arc::new(tx_new_ready_3) };
|
|
let pooled_txs = [
|
|
txpool::Transaction { insertion_id: 0, transaction: Arc::new(tx_new_ready_1) },
|
|
txpool::Transaction { insertion_id: 0, transaction: Arc::new(tx_new_ready_2) },
|
|
];
|
|
|
|
let old = ReplaceTransaction::new(&old_tx, None);
|
|
let new = ReplaceTransaction::new(&new_tx, Some(&pooled_txs));
|
|
|
|
assert_eq!(replace.should_replace(&old, &new), ReplaceOld);
|
|
}
|
|
}
|