// Copyright 2015-2018 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 .
//! Local Transactions List.
use std::{fmt, sync::Arc};
use ethereum_types::H256;
use linked_hash_map::LinkedHashMap;
use pool::{VerifiedTransaction as Transaction, ScoredTransaction};
use txpool::{self, VerifiedTransaction};
/// Status of local transaction.
/// Can indicate that the transaction is currently part of the queue (`Pending/Future`)
/// or gives a reason why the transaction was removed.
#[derive(Debug, PartialEq, Clone)]
pub enum Status {
/// The transaction is currently in the transaction queue.
Pending(Arc),
/// Transaction is already mined.
Mined(Arc),
/// Transaction didn't get into any block, but some other tx with the same nonce got.
Culled(Arc),
/// Transaction is dropped because of limit
Dropped(Arc),
/// Replaced because of higher gas price of another transaction.
Replaced {
/// Replaced transaction
old: Arc,
/// Transaction that replaced this one.
new: Arc,
},
/// Transaction was never accepted to the queue.
/// It means that it was too cheap to replace any transaction already in the pool.
Rejected(Arc, String),
/// Transaction is invalid.
Invalid(Arc),
/// Transaction was canceled.
Canceled(Arc),
}
impl Status {
fn is_pending(&self) -> bool {
match *self {
Status::Pending(_) => true,
_ => false,
}
}
}
/// Keeps track of local transactions that are in the queue or were mined/dropped recently.
pub struct LocalTransactionsList {
max_old: usize,
transactions: LinkedHashMap,
pending: usize,
in_chain: Option bool + Send + Sync>>,
}
impl fmt::Debug for LocalTransactionsList {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("LocalTransactionsList")
.field("max_old", &self.max_old)
.field("transactions", &self.transactions)
.field("pending", &self.pending)
.field("in_chain", &self.in_chain.is_some())
.finish()
}
}
impl Default for LocalTransactionsList {
fn default() -> Self {
Self::new(10)
}
}
impl LocalTransactionsList {
/// Create a new list of local transactions.
pub fn new(max_old: usize) -> Self {
LocalTransactionsList {
max_old,
transactions: Default::default(),
pending: 0,
in_chain: None,
}
}
/// Set blockchain checker.
///
/// The function should return true if transaction is included in chain.
pub fn set_in_chain_checker(&mut self, checker: T) where
T: Into