Prepare for txpool release. (#7741)

This commit is contained in:
Tomasz Drwięga 2018-01-31 07:28:28 +01:00 committed by Svyatoslav Nikolsky
parent afea11ede6
commit dae99cc53e
7 changed files with 26 additions and 8 deletions

View File

@ -9,4 +9,4 @@ authors = ["Parity Technologies <admin@parity.io>"]
error-chain = "0.11" error-chain = "0.11"
log = "0.3" log = "0.3"
smallvec = "0.4" smallvec = "0.4"
ethereum-types = { version = "0.1", features = ["heapsizeof"] } ethereum-types = { version = "0.2", features = ["heapsizeof"] }

View File

@ -18,16 +18,19 @@ use ethereum_types::H256;
error_chain! { error_chain! {
errors { errors {
/// Transaction is already imported
AlreadyImported(hash: H256) { AlreadyImported(hash: H256) {
description("transaction is already in the queue"), description("transaction is already in the pool"),
display("[{:?}] transaction already imported", hash) display("[{:?}] transaction already imported", hash)
} }
/// Transaction is too cheap to enter the queue
TooCheapToEnter(hash: H256) { TooCheapToEnter(hash: H256) {
description("the pool is full and transaction is too cheap to replace any transaction"), description("the pool is full and transaction is too cheap to replace any transaction"),
display("[{:?}] transaction too cheap to enter the pool", hash) display("[{:?}] transaction too cheap to enter the pool", hash)
} }
/// Transaction is too cheap to replace existing transaction that occupies the same slot.
TooCheapToReplace(old_hash: H256, hash: H256) { TooCheapToReplace(old_hash: H256, hash: H256) {
description("transaction is too cheap to replace existing transaction in the queue"), description("transaction is too cheap to replace existing transaction in the pool"),
display("[{:?}] transaction too cheap to replace: {:?}", hash, old_hash) display("[{:?}] transaction too cheap to replace: {:?}", hash, old_hash)
} }
} }

View File

@ -92,7 +92,7 @@ pub mod scoring;
pub use self::listener::{Listener, NoopListener}; pub use self::listener::{Listener, NoopListener};
pub use self::options::Options; pub use self::options::Options;
pub use self::pool::Pool; pub use self::pool::{Pool, PendingIterator};
pub use self::ready::{Ready, Readiness}; pub use self::ready::{Ready, Readiness};
pub use self::scoring::Scoring; pub use self::scoring::Scoring;
pub use self::status::{LightStatus, Status}; pub use self::status::{LightStatus, Status};

View File

@ -44,5 +44,6 @@ pub trait Listener<T> {
} }
/// A no-op implementation of `Listener`. /// A no-op implementation of `Listener`.
#[derive(Debug)]
pub struct NoopListener; pub struct NoopListener;
impl<T> Listener<T> for NoopListener {} impl<T> Listener<T> for NoopListener {}

View File

@ -109,6 +109,8 @@ impl<T, S, L> Pool<T, S, L> where
ensure!(!self.by_hash.contains_key(transaction.hash()), error::ErrorKind::AlreadyImported(*transaction.hash())); ensure!(!self.by_hash.contains_key(transaction.hash()), error::ErrorKind::AlreadyImported(*transaction.hash()));
// TODO [ToDr] Most likely move this after the transsaction is inserted.
// Avoid using should_replace, but rather use scoring for that.
{ {
let remove_worst = |s: &mut Self, transaction| { let remove_worst = |s: &mut Self, transaction| {
match s.remove_worst(&transaction) { match s.remove_worst(&transaction) {
@ -288,7 +290,7 @@ impl<T, S, L> Pool<T, S, L> where
/// Removes single transaction from the pool. /// Removes single transaction from the pool.
/// Depending on the `is_invalid` flag the listener /// Depending on the `is_invalid` flag the listener
/// will either get a `cancelled` or `invalid` notification. /// will either get a `cancelled` or `invalid` notification.
pub fn remove(&mut self, hash: &H256, is_invalid: bool) -> bool { pub fn remove(&mut self, hash: &H256, is_invalid: bool) -> Option<Arc<T>> {
if let Some(tx) = self.finalize_remove(hash) { if let Some(tx) = self.finalize_remove(hash) {
self.remove_from_set(tx.sender(), |set, scoring| { self.remove_from_set(tx.sender(), |set, scoring| {
set.remove(&tx, scoring) set.remove(&tx, scoring)
@ -298,9 +300,9 @@ impl<T, S, L> Pool<T, S, L> where
} else { } else {
self.listener.cancelled(&tx); self.listener.cancelled(&tx);
} }
true Some(tx)
} else { } else {
false None
} }
} }

View File

@ -40,3 +40,15 @@ impl<T, F> Ready<T> for F where F: FnMut(&T) -> Readiness {
(*self)(tx) (*self)(tx)
} }
} }
impl<T, A, B> Ready<T> for (A, B) where
A: Ready<T>,
B: Ready<T>,
{
fn is_ready(&mut self, tx: &T) -> Readiness {
match self.0.is_ready(tx) {
Readiness::Ready => self.1.is_ready(tx),
r => r,
}
}
}

View File

@ -261,7 +261,7 @@ fn should_remove_transaction() {
assert_eq!(txq.light_status().transaction_count, 3); assert_eq!(txq.light_status().transaction_count, 3);
// when // when
assert!(txq.remove(&tx2.hash(), false)); assert!(txq.remove(&tx2.hash(), false).is_some());
// then // then
assert_eq!(txq.light_status().transaction_count, 2); assert_eq!(txq.light_status().transaction_count, 2);