* parity-version: bump beta to 1.11.6 * scripts: remove md5 checksums (#8884) * Add support for --chain tobalaba * Convert indents to tabs :) * Fixes for misbehavior reporting in AuthorityRound (#8998) * aura: only report after checking for repeated skipped primaries * aura: refactor duplicate code for getting epoch validator set * aura: verify_external: report on validator set contract instance * aura: use correct validator set epoch number when reporting * aura: use epoch set when verifying blocks * aura: report skipped primaries when generating seal * aura: handle immediate transitions * aura: don't report skipped steps from genesis to first block * aura: fix reporting test * aura: refactor duplicate code to handle immediate_transitions * aura: let reporting fail on verify_block_basic * aura: add comment about possible failure of reporting * Only return error log for rustls (#9025) * Transaction Pool improvements (#8470) * Don't use ethereum_types in transaction pool. * Hide internal insertion_id. * Fix tests. * Review grumbles. * Improve should_replace on NonceAndGasPrice (#8980) * Additional tests for NonceAndGasPrice::should_replace. * Fix should_replace in the distinct sender case. * Use natural priority ordering to simplify should_replace. * Minimal effective gas price in the queue (#8934) * Minimal effective gas price. * Fix naming, add test * Fix minimal entry score and add test. * Fix worst_transaction. * Remove effective gas price threshold. * Don't leak gas_price decisions out of Scoring. * Never drop local transactions from different senders. (#9002) * Recently rejected cache for transaction queue (#9005) * Store recently rejected transactions. * Don't cache AlreadyImported rejections. * Make the size of transaction verification queue dependent on pool size. * Add a test for recently rejected. * Fix logging for recently rejected. * Make rejection cache smaller. * obsolete test removed * obsolete test removed * Construct cache with_capacity. * Optimize pending transactions filter (#9026) * rpc: return unordered transactions in pending transactions filter * ethcore: use LruCache for nonce cache Only clear the nonce cache when a block is retracted * Revert "ethcore: use LruCache for nonce cache" This reverts commit b382c19abdb9985be1724c3b8cde83906da07d68. * Use only cached nonces when computing pending hashes. * Give filters their own locks, so that they don't block one another. * Fix pending transaction count if not sealing. * Clear cache only when block is enacted. * Fix RPC tests. * Address review comments. * A last bunch of txqueue performance optimizations (#9024) * Clear cache only when block is enacted. * Add tracing for cull. * Cull split. * Cull after creating pending block. * Add constant, remove sync::read tracing. * Reset debug. * Remove excessive tracing. * Use struct for NonceCache. * Fix build * Remove warnings. * Fix build again. * miner: add missing macro use for trace_time * ci: remove md5 merge leftovers
222 lines
6.3 KiB
Rust
222 lines
6.3 KiB
Rust
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
|
// This file is part of Parity.
|
|
|
|
// Parity 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 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. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
use std::{fmt, mem};
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
use ready::{Ready, Readiness};
|
|
use scoring::{self, Scoring};
|
|
use pool::Transaction;
|
|
|
|
#[derive(Debug)]
|
|
pub enum AddResult<T, S> {
|
|
Ok(T),
|
|
TooCheapToEnter(T, S),
|
|
TooCheap {
|
|
old: T,
|
|
new: T,
|
|
},
|
|
Replaced {
|
|
old: T,
|
|
new: T,
|
|
},
|
|
PushedOut {
|
|
old: T,
|
|
new: T,
|
|
},
|
|
}
|
|
|
|
/// Represents all transactions from a particular sender ordered by nonce.
|
|
const PER_SENDER: usize = 8;
|
|
#[derive(Debug)]
|
|
pub struct Transactions<T, S: Scoring<T>> {
|
|
// TODO [ToDr] Consider using something that doesn't require shifting all records.
|
|
transactions: SmallVec<[Transaction<T>; PER_SENDER]>,
|
|
scores: SmallVec<[S::Score; PER_SENDER]>,
|
|
}
|
|
|
|
impl<T, S: Scoring<T>> Default for Transactions<T, S> {
|
|
fn default() -> Self {
|
|
Transactions {
|
|
transactions: Default::default(),
|
|
scores: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: fmt::Debug, S: Scoring<T>> Transactions<T, S> {
|
|
pub fn is_empty(&self) -> bool {
|
|
self.transactions.is_empty()
|
|
}
|
|
|
|
pub fn len(&self) -> usize {
|
|
self.transactions.len()
|
|
}
|
|
|
|
pub fn iter(&self) -> ::std::slice::Iter<Transaction<T>> {
|
|
self.transactions.iter()
|
|
}
|
|
|
|
pub fn worst_and_best(&self) -> Option<((S::Score, Transaction<T>), (S::Score, Transaction<T>))> {
|
|
let len = self.scores.len();
|
|
self.scores.get(0).cloned().map(|best| {
|
|
let worst = self.scores[len - 1].clone();
|
|
let best_tx = self.transactions[0].clone();
|
|
let worst_tx = self.transactions[len - 1].clone();
|
|
|
|
((worst, worst_tx), (best, best_tx))
|
|
})
|
|
}
|
|
|
|
pub fn find_next(&self, tx: &T, scoring: &S) -> Option<(S::Score, Transaction<T>)> {
|
|
self.transactions.binary_search_by(|old| scoring.compare(old, &tx)).ok().and_then(|index| {
|
|
let index = index + 1;
|
|
if index < self.scores.len() {
|
|
Some((self.scores[index].clone(), self.transactions[index].clone()))
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
}
|
|
|
|
fn push_cheapest_transaction(&mut self, tx: Transaction<T>, scoring: &S, max_count: usize) -> AddResult<Transaction<T>, S::Score> {
|
|
let index = self.transactions.len();
|
|
if index == max_count {
|
|
let min_score = self.scores[index - 1].clone();
|
|
AddResult::TooCheapToEnter(tx, min_score)
|
|
} else {
|
|
self.transactions.push(tx.clone());
|
|
self.scores.push(Default::default());
|
|
scoring.update_scores(&self.transactions, &mut self.scores, scoring::Change::InsertedAt(index));
|
|
|
|
AddResult::Ok(tx)
|
|
}
|
|
}
|
|
|
|
pub fn update_scores(&mut self, scoring: &S, event: S::Event) {
|
|
scoring.update_scores(&self.transactions, &mut self.scores, scoring::Change::Event(event));
|
|
}
|
|
|
|
pub fn add(&mut self, new: Transaction<T>, scoring: &S, max_count: usize) -> AddResult<Transaction<T>, S::Score> {
|
|
let index = match self.transactions.binary_search_by(|old| scoring.compare(old, &new)) {
|
|
Ok(index) => index,
|
|
Err(index) => index,
|
|
};
|
|
|
|
// Insert at the end.
|
|
if index == self.transactions.len() {
|
|
return self.push_cheapest_transaction(new, scoring, max_count)
|
|
}
|
|
|
|
// Decide if the transaction should replace some other.
|
|
match scoring.choose(&self.transactions[index], &new) {
|
|
// New transaction should be rejected
|
|
scoring::Choice::RejectNew => AddResult::TooCheap {
|
|
old: self.transactions[index].clone(),
|
|
new,
|
|
},
|
|
// New transaction should be kept along with old ones.
|
|
scoring::Choice::InsertNew => {
|
|
self.transactions.insert(index, new.clone());
|
|
self.scores.insert(index, Default::default());
|
|
scoring.update_scores(&self.transactions, &mut self.scores, scoring::Change::InsertedAt(index));
|
|
|
|
if self.transactions.len() > max_count {
|
|
let old = self.transactions.pop().expect("len is non-zero");
|
|
self.scores.pop();
|
|
scoring.update_scores(&self.transactions, &mut self.scores, scoring::Change::RemovedAt(self.transactions.len()));
|
|
|
|
AddResult::PushedOut {
|
|
old,
|
|
new,
|
|
}
|
|
} else {
|
|
AddResult::Ok(new)
|
|
}
|
|
},
|
|
// New transaction is replacing some other transaction already in the queue.
|
|
scoring::Choice::ReplaceOld => {
|
|
let old = mem::replace(&mut self.transactions[index], new.clone());
|
|
scoring.update_scores(&self.transactions, &mut self.scores, scoring::Change::ReplacedAt(index));
|
|
|
|
AddResult::Replaced {
|
|
old,
|
|
new,
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn remove(&mut self, tx: &T, scoring: &S) -> bool {
|
|
let index = match self.transactions.binary_search_by(|old| scoring.compare(old, tx)) {
|
|
Ok(index) => index,
|
|
Err(_) => {
|
|
warn!("Attempting to remove non-existent transaction {:?}", tx);
|
|
return false;
|
|
},
|
|
};
|
|
|
|
self.transactions.remove(index);
|
|
self.scores.remove(index);
|
|
// Update scoring
|
|
scoring.update_scores(&self.transactions, &mut self.scores, scoring::Change::RemovedAt(index));
|
|
return true;
|
|
}
|
|
|
|
pub fn cull<R: Ready<T>>(&mut self, ready: &mut R, scoring: &S) -> SmallVec<[Transaction<T>; PER_SENDER]> {
|
|
let mut result = SmallVec::new();
|
|
if self.is_empty() {
|
|
return result;
|
|
}
|
|
|
|
let mut first_non_stalled = 0;
|
|
for tx in &self.transactions {
|
|
match ready.is_ready(tx) {
|
|
Readiness::Stale => {
|
|
first_non_stalled += 1;
|
|
},
|
|
Readiness::Ready | Readiness::Future => break,
|
|
}
|
|
}
|
|
|
|
if first_non_stalled == 0 {
|
|
return result;
|
|
}
|
|
|
|
// reverse the vectors to easily remove first elements.
|
|
self.transactions.reverse();
|
|
self.scores.reverse();
|
|
|
|
for _ in 0..first_non_stalled {
|
|
self.scores.pop();
|
|
result.push(
|
|
self.transactions.pop().expect("first_non_stalled is never greater than transactions.len(); qed")
|
|
);
|
|
}
|
|
|
|
self.transactions.reverse();
|
|
self.scores.reverse();
|
|
|
|
// update scoring
|
|
scoring.update_scores(&self.transactions, &mut self.scores, scoring::Change::Culled(result.len()));
|
|
|
|
// reverse the result to maintain correct order.
|
|
result.reverse();
|
|
result
|
|
}
|
|
}
|