use std::{
collections::HashSet,
fmt,
hash,
sync::Arc,
};
use log::{trace, debug, warn};
use serde::Serialize;
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::traits::Member;
use sp_runtime::transaction_validity::{
TransactionTag as Tag,
TransactionLongevity as Longevity,
TransactionPriority as Priority,
TransactionSource as Source,
};
use sp_transaction_pool::{error, PoolStatus, InPoolTransaction};
use crate::future::{FutureTransactions, WaitingTransaction};
use crate::ready::ReadyTransactions;
#[derive(Debug, PartialEq, Eq)]
pub enum Imported<Hash, Ex> {
Ready {
hash: Hash,
promoted: Vec<Hash>,
failed: Vec<Hash>,
removed: Vec<Arc<Transaction<Hash, Ex>>>,
},
Future {
hash: Hash,
}
}
impl<Hash, Ex> Imported<Hash, Ex> {
pub fn hash(&self) -> &Hash {
use self::Imported::*;
match *self {
Ready { ref hash, .. } => hash,
Future { ref hash, .. } => hash,
}
}
}
#[derive(Debug)]
pub struct PruneStatus<Hash, Ex> {
pub promoted: Vec<Imported<Hash, Ex>>,
pub failed: Vec<Hash>,
pub pruned: Vec<Arc<Transaction<Hash, Ex>>>,
}
#[cfg_attr(test, derive(Clone))]
#[derive(PartialEq, Eq, parity_util_mem::MallocSizeOf)]
pub struct Transaction<Hash, Extrinsic> {
pub data: Extrinsic,
pub bytes: usize,
pub hash: Hash,
pub priority: Priority,
pub valid_till: Longevity,
pub requires: Vec<Tag>,
pub provides: Vec<Tag>,
pub propagate: bool,
pub source: Source,
}
impl<Hash, Extrinsic> AsRef<Extrinsic> for Transaction<Hash, Extrinsic> {
fn as_ref(&self) -> &Extrinsic {
&self.data
}
}
impl<Hash, Extrinsic> InPoolTransaction for Transaction<Hash, Extrinsic> {
type Transaction = Extrinsic;
type Hash = Hash;
fn data(&self) -> &Extrinsic {
&self.data
}
fn hash(&self) -> &Hash {
&self.hash
}
fn priority(&self) -> &Priority {
&self.priority
}
fn longevity(&self) ->&Longevity {
&self.valid_till
}
fn requires(&self) -> &[Tag] {
&self.requires
}
fn provides(&self) -> &[Tag] {
&self.provides
}
fn is_propagable(&self) -> bool {
self.propagate
}
}
impl<Hash: Clone, Extrinsic: Clone> Transaction<Hash, Extrinsic> {
pub fn duplicate(&self) -> Self {
Transaction {
data: self.data.clone(),
bytes: self.bytes.clone(),
hash: self.hash.clone(),
priority: self.priority.clone(),
source: self.source,
valid_till: self.valid_till.clone(),
requires: self.requires.clone(),
provides: self.provides.clone(),
propagate: self.propagate,
}
}
}
impl<Hash, Extrinsic> fmt::Debug for Transaction<Hash, Extrinsic> where
Hash: fmt::Debug,
Extrinsic: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fn print_tags(fmt: &mut fmt::Formatter, tags: &[Tag]) -> fmt::Result {
let mut it = tags.iter();
if let Some(t) = it.next() {
write!(fmt, "{}", HexDisplay::from(t))?;
}
for t in it {
write!(fmt, ",{}", HexDisplay::from(t))?;
}
Ok(())
}
write!(fmt, "Transaction {{ ")?;
write!(fmt, "hash: {:?}, ", &self.hash)?;
write!(fmt, "priority: {:?}, ", &self.priority)?;
write!(fmt, "valid_till: {:?}, ", &self.valid_till)?;
write!(fmt, "bytes: {:?}, ", &self.bytes)?;
write!(fmt, "propagate: {:?}, ", &self.propagate)?;
write!(fmt, "source: {:?}, ", &self.source)?;
write!(fmt, "requires: [")?;
print_tags(fmt, &self.requires)?;
write!(fmt, "], provides: [")?;
print_tags(fmt, &self.provides)?;
write!(fmt, "], ")?;
write!(fmt, "data: {:?}", &self.data)?;
write!(fmt, "}}")?;
Ok(())
}
}
const RECENTLY_PRUNED_TAGS: usize = 2;
#[derive(Debug)]
#[cfg_attr(not(target_os = "unknown"), derive(parity_util_mem::MallocSizeOf))]
pub struct BasePool<Hash: hash::Hash + Eq, Ex> {
reject_future_transactions: bool,
future: FutureTransactions<Hash, Ex>,
ready: ReadyTransactions<Hash, Ex>,
recently_pruned: [HashSet<Tag>; RECENTLY_PRUNED_TAGS],
recently_pruned_index: usize,
}
impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> Default for BasePool<Hash, Ex> {
fn default() -> Self {
Self::new(false)
}
}
impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash, Ex> {
pub fn new(reject_future_transactions: bool) -> Self {
BasePool {
reject_future_transactions,
future: Default::default(),
ready: Default::default(),
recently_pruned: Default::default(),
recently_pruned_index: 0,
}
}
pub(crate) fn with_futures_enabled<T>(&mut self, closure: impl FnOnce(&mut Self, bool) -> T) -> T {
let previous = self.reject_future_transactions;
self.reject_future_transactions = false;
let return_value = closure(self, previous);
self.reject_future_transactions = previous;
return_value
}
pub fn is_imported(&self, tx_hash: &Hash) -> bool {
self.future.contains(tx_hash) || self.ready.contains(tx_hash)
}
pub fn import(
&mut self,
tx: Transaction<Hash, Ex>,
) -> error::Result<Imported<Hash, Ex>> {
if self.is_imported(&tx.hash) {
return Err(error::Error::AlreadyImported(Box::new(tx.hash)))
}
let tx = WaitingTransaction::new(
tx,
self.ready.provided_tags(),
&self.recently_pruned,
);
trace!(target: "txpool", "[{:?}] {:?}", tx.transaction.hash, tx);
debug!(
target: "txpool",
"[{:?}] Importing to {}",
tx.transaction.hash,
if tx.is_ready() { "ready" } else { "future" }
);
if !tx.is_ready() {
if self.reject_future_transactions {
return Err(error::Error::RejectedFutureTransaction);
}
let hash = tx.transaction.hash.clone();
self.future.import(tx);
return Ok(Imported::Future { hash });
}
self.import_to_ready(tx)
}
fn import_to_ready(&mut self, tx: WaitingTransaction<Hash, Ex>) -> error::Result<Imported<Hash, Ex>> {
let hash = tx.transaction.hash.clone();
let mut promoted = vec![];
let mut failed = vec![];
let mut removed = vec![];
let mut first = true;
let mut to_import = vec![tx];
loop {
let tx = match to_import.pop() {
Some(tx) => tx,
None => break,
};
to_import.append(&mut self.future.satisfy_tags(&tx.transaction.provides));
let current_hash = tx.transaction.hash.clone();
match self.ready.import(tx) {
Ok(mut replaced) => {
if !first {
promoted.push(current_hash);
}
removed.append(&mut replaced);
},
Err(e) => if first {
debug!(target: "txpool", "[{:?}] Error importing: {:?}", current_hash, e);
return Err(e)
} else {
failed.push(current_hash);
},
}
first = false;
}
if removed.iter().any(|tx| tx.hash == hash) {
self.ready.remove_subtree(&promoted);
debug!(target: "txpool", "[{:?}] Cycle detected, bailing.", hash);
return Err(error::Error::CycleDetected)
}
Ok(Imported::Ready {
hash,
promoted,
failed,
removed,
})
}
pub fn ready(&self) -> impl Iterator<Item=Arc<Transaction<Hash, Ex>>> {
self.ready.get()
}
pub fn futures(&self) -> impl Iterator<Item=&Transaction<Hash, Ex>> {
self.future.all()
}
pub fn by_hashes(&self, hashes: &[Hash]) -> Vec<Option<Arc<Transaction<Hash, Ex>>>> {
let ready = self.ready.by_hashes(hashes);
let future = self.future.by_hashes(hashes);
ready
.into_iter()
.zip(future)
.map(|(a, b)| a.or(b))
.collect()
}
pub fn ready_by_hash(&self, hash: &Hash) -> Option<Arc<Transaction<Hash, Ex>>> {
self.ready.by_hash(hash)
}
pub fn enforce_limits(&mut self, ready: &Limit, future: &Limit) -> Vec<Arc<Transaction<Hash, Ex>>> {
let mut removed = vec![];
while ready.is_exceeded(self.ready.len(), self.ready.bytes()) {
let minimal = self.ready
.fold(|minimal, current| {
let transaction = ¤t.transaction;
match minimal {
None => Some(transaction.clone()),
Some(ref tx) if tx.insertion_id > transaction.insertion_id => {
Some(transaction.clone())
},
other => other,
}
});
if let Some(minimal) = minimal {
removed.append(&mut self.remove_subtree(&[minimal.transaction.hash.clone()]))
} else {
break;
}
}
while future.is_exceeded(self.future.len(), self.future.bytes()) {
let minimal = self.future
.fold(|minimal, current| {
match minimal {
None => Some(current.clone()),
Some(ref tx) if tx.imported_at > current.imported_at => {
Some(current.clone())
},
other => other,
}
});
if let Some(minimal) = minimal {
removed.append(&mut self.remove_subtree(&[minimal.transaction.hash.clone()]))
} else {
break;
}
}
removed
}
pub fn remove_subtree(&mut self, hashes: &[Hash]) -> Vec<Arc<Transaction<Hash, Ex>>> {
let mut removed = self.ready.remove_subtree(hashes);
removed.extend(self.future.remove(hashes));
removed
}
pub fn clear_future(&mut self) -> Vec<Arc<Transaction<Hash, Ex>>> {
self.future.clear()
}
pub fn prune_tags(&mut self, tags: impl IntoIterator<Item=Tag>) -> PruneStatus<Hash, Ex> {
let mut to_import = vec![];
let mut pruned = vec![];
let recently_pruned = &mut self.recently_pruned[self.recently_pruned_index];
self.recently_pruned_index = (self.recently_pruned_index + 1) % RECENTLY_PRUNED_TAGS;
recently_pruned.clear();
for tag in tags {
to_import.append(&mut self.future.satisfy_tags(std::iter::once(&tag)));
pruned.append(&mut self.ready.prune_tags(tag.clone()));
recently_pruned.insert(tag);
}
let mut promoted = vec![];
let mut failed = vec![];
for tx in to_import {
let hash = tx.transaction.hash.clone();
match self.import_to_ready(tx) {
Ok(res) => promoted.push(res),
Err(e) => {
warn!(target: "txpool", "[{:?}] Failed to promote during pruning: {:?}", hash, e);
failed.push(hash)
},
}
}
PruneStatus {
pruned,
failed,
promoted,
}
}
pub fn status(&self) -> PoolStatus {
PoolStatus {
ready: self.ready.len(),
ready_bytes: self.ready.bytes(),
future: self.future.len(),
future_bytes: self.future.bytes(),
}
}
}
#[derive(Debug, Clone)]
pub struct Limit {
pub count: usize,
pub total_bytes: usize,
}
impl Limit {
pub fn is_exceeded(&self, count: usize, bytes: usize) -> bool {
self.count < count || self.total_bytes < bytes
}
}
#[cfg(test)]
mod tests {
use super::*;
type Hash = u64;
fn pool() -> BasePool<Hash, Vec<u8>> {
BasePool::default()
}
#[test]
fn should_import_transaction_to_ready() {
let mut pool = pool();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1u64,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.ready().count(), 1);
assert_eq!(pool.ready.len(), 1);
}
#[test]
fn should_not_import_same_transaction_twice() {
let mut pool = pool();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap_err();
assert_eq!(pool.ready().count(), 1);
assert_eq!(pool.ready.len(), 1);
}
#[test]
fn should_import_transaction_to_future_and_promote_it_later() {
let mut pool = pool();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.ready().count(), 0);
assert_eq!(pool.ready.len(), 0);
pool.import(Transaction {
data: vec![2u8],
bytes: 1,
hash: 2,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![0]],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.ready().count(), 2);
assert_eq!(pool.ready.len(), 2);
}
#[test]
fn should_promote_a_subgraph() {
let mut pool = pool();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![3u8],
bytes: 1,
hash: 3,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![2]],
provides: vec![],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![2u8],
bytes: 1,
hash: 2,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![1]],
provides: vec![vec![3], vec![2]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![4u8],
bytes: 1,
hash: 4,
priority: 1_000u64,
valid_till: 64u64,
requires: vec![vec![3], vec![4]],
provides: vec![],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.ready().count(), 0);
assert_eq!(pool.ready.len(), 0);
let res = pool.import(Transaction {
data: vec![5u8],
bytes: 1,
hash: 5,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![0], vec![4]],
propagate: true,
source: Source::External,
}).unwrap();
let mut it = pool.ready().into_iter().map(|tx| tx.data[0]);
assert_eq!(it.next(), Some(5));
assert_eq!(it.next(), Some(1));
assert_eq!(it.next(), Some(2));
assert_eq!(it.next(), Some(4));
assert_eq!(it.next(), Some(3));
assert_eq!(it.next(), None);
assert_eq!(res, Imported::Ready {
hash: 5,
promoted: vec![1, 2, 3, 4],
failed: vec![],
removed: vec![],
});
}
#[test]
fn should_handle_a_cycle() {
let mut pool = pool();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![3u8],
bytes: 1,
hash: 3,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![1]],
provides: vec![vec![2]],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.ready().count(), 0);
assert_eq!(pool.ready.len(), 0);
pool.import(Transaction {
data: vec![2u8],
bytes: 1,
hash: 2,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![2]],
provides: vec![vec![0]],
propagate: true,
source: Source::External,
}).unwrap();
{
let mut it = pool.ready().into_iter().map(|tx| tx.data[0]);
assert_eq!(it.next(), None);
}
assert_eq!(pool.future.len(), 3);
let res = pool.import(Transaction {
data: vec![4u8],
bytes: 1,
hash: 4,
priority: 50u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![0]],
propagate: true,
source: Source::External,
}).unwrap();
let mut it = pool.ready().into_iter().map(|tx| tx.data[0]);
assert_eq!(it.next(), Some(4));
assert_eq!(it.next(), Some(1));
assert_eq!(it.next(), Some(3));
assert_eq!(it.next(), None);
assert_eq!(res, Imported::Ready {
hash: 4,
promoted: vec![1, 3],
failed: vec![2],
removed: vec![],
});
assert_eq!(pool.future.len(), 0);
}
#[test]
fn should_handle_a_cycle_with_low_priority() {
let mut pool = pool();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![3u8],
bytes: 1,
hash: 3,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![1]],
provides: vec![vec![2]],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.ready().count(), 0);
assert_eq!(pool.ready.len(), 0);
pool.import(Transaction {
data: vec![2u8],
bytes: 1,
hash: 2,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![2]],
provides: vec![vec![0]],
propagate: true,
source: Source::External,
}).unwrap();
{
let mut it = pool.ready().into_iter().map(|tx| tx.data[0]);
assert_eq!(it.next(), None);
}
assert_eq!(pool.future.len(), 3);
let err = pool.import(Transaction {
data: vec![4u8],
bytes: 1,
hash: 4,
priority: 1u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![0]],
propagate: true,
source: Source::External,
}).unwrap_err();
let mut it = pool.ready().into_iter().map(|tx| tx.data[0]);
assert_eq!(it.next(), None);
assert_eq!(pool.ready.len(), 0);
assert_eq!(pool.future.len(), 0);
if let error::Error::CycleDetected = err {
} else {
assert!(false, "Invalid error kind: {:?}", err);
}
}
#[test]
fn can_track_heap_size() {
let mut pool = pool();
pool.import(Transaction {
data: vec![5u8; 1024],
bytes: 1,
hash: 5,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![0], vec![4]],
propagate: true,
source: Source::External,
}).expect("import 1 should be ok");
pool.import(Transaction {
data: vec![3u8; 1024],
bytes: 1,
hash: 7,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![2], vec![7]],
propagate: true,
source: Source::External,
}).expect("import 2 should be ok");
assert!(parity_util_mem::malloc_size(&pool) > 5000);
}
#[test]
fn should_remove_invalid_transactions() {
let mut pool = pool();
pool.import(Transaction {
data: vec![5u8],
bytes: 1,
hash: 5,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![0], vec![4]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![3u8],
bytes: 1,
hash: 3,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![2]],
provides: vec![],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![2u8],
bytes: 1,
hash: 2,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![1]],
provides: vec![vec![3], vec![2]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![4u8],
bytes: 1,
hash: 4,
priority: 1_000u64,
valid_till: 64u64,
requires: vec![vec![3], vec![4]],
provides: vec![],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![6u8],
bytes: 1,
hash: 6,
priority: 1_000u64,
valid_till: 64u64,
requires: vec![vec![11]],
provides: vec![],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.ready().count(), 5);
assert_eq!(pool.future.len(), 1);
pool.remove_subtree(&[6, 1]);
assert_eq!(pool.ready().count(), 1);
assert_eq!(pool.future.len(), 0);
}
#[test]
fn should_prune_ready_transactions() {
let mut pool = pool();
pool.import(Transaction {
data: vec![5u8],
bytes: 1,
hash: 5,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![vec![100]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![1u8],
bytes: 1,
hash: 1,
priority: 5u64,
valid_till: 64u64,
requires: vec![],
provides: vec![vec![1]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![2u8],
bytes: 1,
hash: 2,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![2]],
provides: vec![vec![3]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![3u8],
bytes: 1,
hash: 3,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![1]],
provides: vec![vec![2]],
propagate: true,
source: Source::External,
}).unwrap();
pool.import(Transaction {
data: vec![4u8],
bytes: 1,
hash: 4,
priority: 1_000u64,
valid_till: 64u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.ready().count(), 4);
assert_eq!(pool.future.len(), 1);
let result = pool.prune_tags(vec![vec![0], vec![2]]);
assert_eq!(result.pruned.len(), 2);
assert_eq!(result.failed.len(), 0);
assert_eq!(result.promoted[0], Imported::Ready {
hash: 5,
promoted: vec![],
failed: vec![],
removed: vec![],
});
assert_eq!(result.promoted.len(), 1);
assert_eq!(pool.future.len(), 0);
assert_eq!(pool.ready.len(), 3);
assert_eq!(pool.ready().count(), 3);
}
#[test]
fn transaction_debug() {
assert_eq!(
format!("{:?}", Transaction {
data: vec![4u8],
bytes: 1,
hash: 4,
priority: 1_000u64,
valid_till: 64u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
propagate: true,
source: Source::External,
}),
"Transaction { \
hash: 4, priority: 1000, valid_till: 64, bytes: 1, propagate: true, \
source: TransactionSource::External, requires: [03,02], provides: [04], data: [4]}".to_owned()
);
}
#[test]
fn transaction_propagation() {
assert_eq!(Transaction {
data: vec![4u8],
bytes: 1,
hash: 4,
priority: 1_000u64,
valid_till: 64u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
propagate: true,
source: Source::External,
}.is_propagable(), true);
assert_eq!(Transaction {
data: vec![4u8],
bytes: 1,
hash: 4,
priority: 1_000u64,
valid_till: 64u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
propagate: false,
source: Source::External,
}.is_propagable(), false);
}
#[test]
fn should_reject_future_transactions() {
let mut pool = pool();
pool.reject_future_transactions = true;
let err = pool.import(Transaction {
data: vec![5u8],
bytes: 1,
hash: 5,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![],
propagate: true,
source: Source::External,
});
if let Err(error::Error::RejectedFutureTransaction) = err {
} else {
assert!(false, "Invalid error kind: {:?}", err);
}
}
#[test]
fn should_clear_future_queue() {
let mut pool = pool();
pool.import(Transaction {
data: vec![5u8],
bytes: 1,
hash: 5,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![],
propagate: true,
source: Source::External,
}).unwrap();
assert_eq!(pool.future.len(), 1);
assert_eq!(pool.clear_future().len(), 1);
assert_eq!(pool.future.len(), 0);
}
#[test]
fn should_accept_future_transactions_when_explicitly_asked_to() {
let mut pool = pool();
pool.reject_future_transactions = true;
let flag_value = pool.with_futures_enabled(|pool, flag| {
pool.import(Transaction {
data: vec![5u8],
bytes: 1,
hash: 5,
priority: 5u64,
valid_till: 64u64,
requires: vec![vec![0]],
provides: vec![],
propagate: true,
source: Source::External,
}).unwrap();
flag
});
assert_eq!(flag_value, true);
assert_eq!(pool.reject_future_transactions, true);
assert_eq!(pool.future.len(), 1);
}
}