Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -20,24 +20,19 @@
//!
//! For a more full-featured pool, have a look at the `pool` module.
use std::{
collections::HashSet,
fmt,
hash,
sync::Arc,
};
use std::{collections::HashSet, fmt, hash, sync::Arc};
use log::{trace, debug, warn};
use log::{debug, trace, warn};
use sc_transaction_pool_api::{error, InPoolTransaction, PoolStatus};
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_runtime::{
traits::Member,
transaction_validity::{
TransactionLongevity as Longevity, TransactionPriority as Priority,
TransactionSource as Source, TransactionTag as Tag,
},
};
use sc_transaction_pool_api::{error, PoolStatus, InPoolTransaction};
use super::{
future::{FutureTransactions, WaitingTransaction},
@@ -62,7 +57,7 @@ pub enum Imported<Hash, Ex> {
Future {
/// Hash of transaction that was successfully imported.
hash: Hash,
}
},
}
impl<Hash, Ex> Imported<Hash, Ex> {
@@ -133,7 +128,7 @@ impl<Hash, Extrinsic> InPoolTransaction for Transaction<Hash, Extrinsic> {
&self.priority
}
fn longevity(&self) ->&Longevity {
fn longevity(&self) -> &Longevity {
&self.valid_till
}
@@ -171,13 +166,17 @@ impl<Hash: Clone, Extrinsic: Clone> Transaction<Hash, Extrinsic> {
}
}
impl<Hash, Extrinsic> fmt::Debug for Transaction<Hash, Extrinsic> where
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 {
let join_tags = |tags: &[Tag]| {
tags.iter().map(|tag| HexDisplay::from(tag).to_string()).collect::<Vec<_>>().join(", ")
tags.iter()
.map(|tag| HexDisplay::from(tag).to_string())
.collect::<Vec<_>>()
.join(", ")
};
write!(fmt, "Transaction {{ ")?;
@@ -245,7 +244,10 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
///
/// The closure accepts the mutable reference to the pool and original value
/// of the `reject_future_transactions` flag.
pub(crate) fn with_futures_enabled<T>(&mut self, closure: impl FnOnce(&mut Self, bool) -> T) -> T {
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);
@@ -265,19 +267,12 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
/// other transactions in the pool.
/// The latter contains transactions that have all the requirements satisfied and are
/// ready to be included in the block.
pub fn import(
&mut self,
tx: Transaction<Hash, Ex>,
) -> error::Result<Imported<Hash, Ex>> {
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,
);
let tx = WaitingTransaction::new(tx, self.ready.provided_tags(), &self.recently_pruned);
trace!(target: "txpool", "[{:?}] {:?}", tx.transaction.hash, tx);
debug!(
target: "txpool",
@@ -289,12 +284,12 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
// If all tags are not satisfied import to future.
if !tx.is_ready() {
if self.reject_future_transactions {
return Err(error::Error::RejectedFutureTransaction);
return Err(error::Error::RejectedFutureTransaction)
}
let hash = tx.transaction.hash.clone();
self.future.import(tx);
return Ok(Imported::Future { hash });
return Ok(Imported::Future { hash })
}
self.import_to_ready(tx)
@@ -303,7 +298,10 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
/// Imports transaction to ready queue.
///
/// NOTE the transaction has to have all requirements satisfied.
fn import_to_ready(&mut self, tx: WaitingTransaction<Hash, Ex>) -> error::Result<Imported<Hash, Ex>> {
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![];
@@ -328,12 +326,13 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
removed.append(&mut replaced);
},
// transaction failed to be imported.
Err(e) => if first {
debug!(target: "txpool", "[{:?}] Error importing: {:?}", current_hash, e);
return Err(e)
} else {
failed.push(current_hash);
},
Err(e) =>
if first {
debug!(target: "txpool", "[{:?}] Error importing: {:?}", current_hash, e);
return Err(e)
} else {
failed.push(current_hash);
},
}
first = false;
}
@@ -352,21 +351,16 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
return Err(error::Error::CycleDetected)
}
Ok(Imported::Ready {
hash,
promoted,
failed,
removed,
})
Ok(Imported::Ready { hash, promoted, failed, removed })
}
/// Returns an iterator over ready transactions in the pool.
pub fn ready(&self) -> impl Iterator<Item=Arc<Transaction<Hash, Ex>>> {
pub fn ready(&self) -> impl Iterator<Item = Arc<Transaction<Hash, Ex>>> {
self.ready.get()
}
/// Returns an iterator over future transactions in the pool.
pub fn futures(&self) -> impl Iterator<Item=&Transaction<Hash, Ex>> {
pub fn futures(&self) -> impl Iterator<Item = &Transaction<Hash, Ex>> {
self.future.all()
}
@@ -378,11 +372,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
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()
ready.into_iter().zip(future).map(|(a, b)| a.or(b)).collect()
}
/// Returns pool transaction by hash.
@@ -395,47 +385,44 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
/// Removes and returns worst transactions from the queues and all transactions that depend on them.
/// Technically the worst transaction should be evaluated by computing the entire pending set.
/// We use a simplified approach to remove the transaction that occupies the pool for the longest time.
pub fn enforce_limits(&mut self, ready: &Limit, future: &Limit) -> Vec<Arc<Transaction<Hash, Ex>>> {
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()) {
// find the worst transaction
let minimal = self.ready
.fold(|minimal, current| {
let transaction = &current.transaction;
match minimal {
None => Some(transaction.clone()),
Some(ref tx) if tx.insertion_id > transaction.insertion_id => {
Some(transaction.clone())
},
other => other,
}
});
let minimal = self.ready.fold(|minimal, current| {
let transaction = &current.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;
break
}
}
while future.is_exceeded(self.future.len(), self.future.bytes()) {
// find the worst transaction
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,
}
});
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;
break
}
}
@@ -467,7 +454,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
/// but unlike `remove_subtree`, dependent transactions are not touched.
/// Additional transactions from future queue might be promoted to ready if you satisfy tags
/// that the pool didn't previously know about.
pub fn prune_tags(&mut self, tags: impl IntoIterator<Item=Tag>) -> PruneStatus<Hash, Ex> {
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];
@@ -496,11 +483,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex: std::fmt::Debug> BasePool<Hash,
}
}
PruneStatus {
pruned,
failed,
promoted,
}
PruneStatus { pruned, failed, promoted }
}
/// Get pool status.
@@ -540,7 +523,7 @@ mod tests {
BasePool::default()
}
const DEFAULT_TX: Transaction::<Hash, Vec<u8>> = Transaction {
const DEFAULT_TX: Transaction<Hash, Vec<u8>> = Transaction {
data: vec![],
bytes: 1,
hash: 1u64,
@@ -558,11 +541,8 @@ mod tests {
let mut pool = pool();
// when
pool.import(Transaction {
data: vec![1u8],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap();
pool.import(Transaction { data: vec![1u8], provides: vec![vec![1]], ..DEFAULT_TX.clone() })
.unwrap();
// then
assert_eq!(pool.ready().count(), 1);
@@ -575,16 +555,10 @@ mod tests {
let mut pool = pool();
// when
pool.import(Transaction {
data: vec![1u8],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap();
pool.import(Transaction {
data: vec![1u8],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap_err();
pool.import(Transaction { data: vec![1u8], provides: vec![vec![1]], ..DEFAULT_TX.clone() })
.unwrap();
pool.import(Transaction { data: vec![1u8], provides: vec![vec![1]], ..DEFAULT_TX.clone() })
.unwrap_err();
// then
assert_eq!(pool.ready().count(), 1);
@@ -601,16 +575,18 @@ mod tests {
data: vec![1u8],
requires: vec![vec![0]],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
assert_eq!(pool.ready().count(), 0);
assert_eq!(pool.ready.len(), 0);
pool.import(Transaction {
data: vec![2u8],
hash: 2,
provides: vec![vec![0]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
// then
assert_eq!(pool.ready().count(), 2);
@@ -627,37 +603,43 @@ mod tests {
data: vec![1u8],
requires: vec![vec![0]],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![3u8],
hash: 3,
requires: vec![vec![2]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![2u8],
hash: 2,
requires: vec![vec![1]],
provides: vec![vec![3], vec![2]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![4u8],
hash: 4,
priority: 1_000u64,
requires: vec![vec![3], vec![4]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
assert_eq!(pool.ready().count(), 0);
assert_eq!(pool.ready.len(), 0);
let res = pool.import(Transaction {
data: vec![5u8],
hash: 5,
provides: vec![vec![0], vec![4]],
.. DEFAULT_TX.clone()
}).unwrap();
let res = pool
.import(Transaction {
data: vec![5u8],
hash: 5,
provides: vec![vec![0], vec![4]],
..DEFAULT_TX.clone()
})
.unwrap();
// then
let mut it = pool.ready().into_iter().map(|tx| tx.data[0]);
@@ -668,12 +650,15 @@ mod tests {
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![],
});
assert_eq!(
res,
Imported::Ready {
hash: 5,
promoted: vec![1, 2, 3, 4],
failed: vec![],
removed: vec![],
}
);
}
#[test]
@@ -684,15 +669,17 @@ mod tests {
data: vec![1u8],
requires: vec![vec![0]],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![3u8],
hash: 3,
requires: vec![vec![1]],
provides: vec![vec![2]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
assert_eq!(pool.ready().count(), 0);
assert_eq!(pool.ready.len(), 0);
@@ -702,8 +689,9 @@ mod tests {
hash: 2,
requires: vec![vec![2]],
provides: vec![vec![0]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
// then
{
@@ -714,24 +702,24 @@ mod tests {
assert_eq!(pool.future.len(), 3);
// let's close the cycle with one additional transaction
let res = pool.import(Transaction {
data: vec![4u8],
hash: 4,
priority: 50u64,
provides: vec![vec![0]],
.. DEFAULT_TX.clone()
}).unwrap();
let res = pool
.import(Transaction {
data: vec![4u8],
hash: 4,
priority: 50u64,
provides: vec![vec![0]],
..DEFAULT_TX.clone()
})
.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!(
res,
Imported::Ready { hash: 4, promoted: vec![1, 3], failed: vec![2], removed: vec![] }
);
assert_eq!(pool.future.len(), 0);
}
@@ -743,15 +731,17 @@ mod tests {
data: vec![1u8],
requires: vec![vec![0]],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![3u8],
hash: 3,
requires: vec![vec![1]],
provides: vec![vec![2]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
assert_eq!(pool.ready().count(), 0);
assert_eq!(pool.ready.len(), 0);
@@ -761,8 +751,9 @@ mod tests {
hash: 2,
requires: vec![vec![2]],
provides: vec![vec![0]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
// then
{
@@ -773,13 +764,15 @@ mod tests {
assert_eq!(pool.future.len(), 3);
// let's close the cycle with one additional transaction
let err = pool.import(Transaction {
data: vec![4u8],
hash: 4,
priority: 1u64, // lower priority than Tx(2)
provides: vec![vec![0]],
.. DEFAULT_TX.clone()
}).unwrap_err();
let err = pool
.import(Transaction {
data: vec![4u8],
hash: 4,
priority: 1u64, // lower priority than Tx(2)
provides: vec![vec![0]],
..DEFAULT_TX.clone()
})
.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);
@@ -797,14 +790,16 @@ mod tests {
data: vec![5u8; 1024],
hash: 5,
provides: vec![vec![0], vec![4]],
.. DEFAULT_TX.clone()
}).expect("import 1 should be ok");
..DEFAULT_TX.clone()
})
.expect("import 1 should be ok");
pool.import(Transaction {
data: vec![3u8; 1024],
hash: 7,
provides: vec![vec![2], vec![7]],
.. DEFAULT_TX.clone()
}).expect("import 2 should be ok");
..DEFAULT_TX.clone()
})
.expect("import 2 should be ok");
assert!(parity_util_mem::malloc_size(&pool) > 5000);
}
@@ -817,42 +812,48 @@ mod tests {
data: vec![5u8],
hash: 5,
provides: vec![vec![0], vec![4]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![1u8],
requires: vec![vec![0]],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![3u8],
hash: 3,
requires: vec![vec![2]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![2u8],
hash: 2,
requires: vec![vec![1]],
provides: vec![vec![3], vec![2]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![4u8],
hash: 4,
priority: 1_000u64,
requires: vec![vec![3], vec![4]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
// future
pool.import(Transaction {
data: vec![6u8],
hash: 6,
priority: 1_000u64,
requires: vec![vec![11]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
assert_eq!(pool.ready().count(), 5);
assert_eq!(pool.future.len(), 1);
@@ -874,36 +875,37 @@ mod tests {
hash: 5,
requires: vec![vec![0]],
provides: vec![vec![100]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
// ready
pool.import(Transaction {
data: vec![1u8],
provides: vec![vec![1]],
.. DEFAULT_TX.clone()
}).unwrap();
pool.import(Transaction { data: vec![1u8], provides: vec![vec![1]], ..DEFAULT_TX.clone() })
.unwrap();
pool.import(Transaction {
data: vec![2u8],
hash: 2,
requires: vec![vec![2]],
provides: vec![vec![3]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![3u8],
hash: 3,
requires: vec![vec![1]],
provides: vec![vec![2]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
pool.import(Transaction {
data: vec![4u8],
hash: 4,
priority: 1_000u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
assert_eq!(pool.ready().count(), 4);
assert_eq!(pool.future.len(), 1);
@@ -914,12 +916,10 @@ mod tests {
// then
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[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);
@@ -929,40 +929,52 @@ mod tests {
#[test]
fn transaction_debug() {
assert_eq!(
format!("{:?}", Transaction {
data: vec![4u8],
hash: 4,
priority: 1_000u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
.. DEFAULT_TX.clone()
}),
format!(
"{:?}",
Transaction {
data: vec![4u8],
hash: 4,
priority: 1_000u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
..DEFAULT_TX.clone()
}
),
"Transaction { \
hash: 4, priority: 1000, valid_till: 64, bytes: 1, propagate: true, \
source: TransactionSource::External, requires: [03, 02], provides: [04], data: [4]}".to_owned()
source: TransactionSource::External, requires: [03, 02], provides: [04], data: [4]}"
.to_owned()
);
}
#[test]
fn transaction_propagation() {
assert_eq!(Transaction {
assert_eq!(
Transaction {
data: vec![4u8],
hash: 4,
priority: 1_000u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
.. DEFAULT_TX.clone()
}.is_propagable(), true);
..DEFAULT_TX.clone()
}
.is_propagable(),
true
);
assert_eq!(Transaction {
assert_eq!(
Transaction {
data: vec![4u8],
hash: 4,
priority: 1_000u64,
requires: vec![vec![3], vec![2]],
provides: vec![vec![4]],
propagate: false,
.. DEFAULT_TX.clone()
}.is_propagable(), false);
..DEFAULT_TX.clone()
}
.is_propagable(),
false
);
}
#[test]
@@ -978,7 +990,7 @@ source: TransactionSource::External, requires: [03, 02], provides: [04], data: [
data: vec![5u8],
hash: 5,
requires: vec![vec![0]],
.. DEFAULT_TX.clone()
..DEFAULT_TX.clone()
});
if let Err(error::Error::RejectedFutureTransaction) = err {
@@ -997,8 +1009,9 @@ source: TransactionSource::External, requires: [03, 02], provides: [04], data: [
data: vec![5u8],
hash: 5,
requires: vec![vec![0]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
// then
assert_eq!(pool.future.len(), 1);
@@ -1022,8 +1035,9 @@ source: TransactionSource::External, requires: [03, 02], provides: [04], data: [
data: vec![5u8],
hash: 5,
requires: vec![vec![0]],
.. DEFAULT_TX.clone()
}).unwrap();
..DEFAULT_TX.clone()
})
.unwrap();
flag
});
@@ -18,15 +18,12 @@
use std::{
collections::{HashMap, HashSet},
fmt,
hash,
fmt, hash,
sync::Arc,
};
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::transaction_validity::{
TransactionTag as Tag,
};
use sp_runtime::transaction_validity::TransactionTag as Tag;
use wasm_timer::Instant;
use super::base_pool::Transaction;
@@ -48,10 +45,13 @@ impl<Hash: fmt::Debug, Ex: fmt::Debug> fmt::Debug for WaitingTransaction<Hash, E
write!(fmt, "imported_at: {:?}, ", self.imported_at)?;
write!(fmt, "transaction: {:?}, ", self.transaction)?;
write!(
fmt,
"missing_tags: {{{}}}",
self.missing_tags.iter()
.map(|tag| HexDisplay::from(tag).to_string()).collect::<Vec<_>>().join(", "),
fmt,
"missing_tags: {{{}}}",
self.missing_tags
.iter()
.map(|tag| HexDisplay::from(tag).to_string())
.collect::<Vec<_>>()
.join(", "),
)?;
write!(fmt, "}}")
}
@@ -77,22 +77,20 @@ impl<Hash, Ex> WaitingTransaction<Hash, Ex> {
provided: &HashMap<Tag, Hash>,
recently_pruned: &[HashSet<Tag>],
) -> Self {
let missing_tags = transaction.requires
let missing_tags = transaction
.requires
.iter()
.filter(|tag| {
// is true if the tag is already satisfied either via transaction in the pool
// or one that was recently included.
let is_provided = provided.contains_key(&**tag) || recently_pruned.iter().any(|x| x.contains(&**tag));
let is_provided = provided.contains_key(&**tag) ||
recently_pruned.iter().any(|x| x.contains(&**tag));
!is_provided
})
.cloned()
.collect();
Self {
transaction: Arc::new(transaction),
missing_tags,
imported_at: Instant::now(),
}
Self { transaction: Arc::new(transaction), missing_tags, imported_at: Instant::now() }
}
/// Marks the tag as satisfied.
@@ -121,10 +119,7 @@ pub struct FutureTransactions<Hash: hash::Hash + Eq, Ex> {
impl<Hash: hash::Hash + Eq, Ex> Default for FutureTransactions<Hash, Ex> {
fn default() -> Self {
Self {
wanted_tags: Default::default(),
waiting: Default::default(),
}
Self { wanted_tags: Default::default(), waiting: Default::default() }
}
}
@@ -144,7 +139,10 @@ impl<Hash: hash::Hash + Eq + Clone, Ex> FutureTransactions<Hash, Ex> {
/// we should remove the transactions from here and move them to the Ready queue.
pub fn import(&mut self, tx: WaitingTransaction<Hash, Ex>) {
assert!(!tx.is_ready(), "Transaction is ready.");
assert!(!self.waiting.contains_key(&tx.transaction.hash), "Transaction is already imported.");
assert!(
!self.waiting.contains_key(&tx.transaction.hash),
"Transaction is already imported."
);
// Add all tags that are missing
for tag in &tx.missing_tags {
@@ -163,14 +161,20 @@ impl<Hash: hash::Hash + Eq + Clone, Ex> FutureTransactions<Hash, Ex> {
/// Returns a list of known transactions
pub fn by_hashes(&self, hashes: &[Hash]) -> Vec<Option<Arc<Transaction<Hash, Ex>>>> {
hashes.iter().map(|h| self.waiting.get(h).map(|x| x.transaction.clone())).collect()
hashes
.iter()
.map(|h| self.waiting.get(h).map(|x| x.transaction.clone()))
.collect()
}
/// Satisfies provided tags in transactions that are waiting for them.
///
/// Returns (and removes) transactions that became ready after their last tag got
/// satisfied and now we can remove them from Future and move to Ready queue.
pub fn satisfy_tags<T: AsRef<Tag>>(&mut self, tags: impl IntoIterator<Item=T>) -> Vec<WaitingTransaction<Hash, Ex>> {
pub fn satisfy_tags<T: AsRef<Tag>>(
&mut self,
tags: impl IntoIterator<Item = T>,
) -> Vec<WaitingTransaction<Hash, Ex>> {
let mut became_ready = vec![];
for tag in tags {
@@ -205,7 +209,9 @@ impl<Hash: hash::Hash + Eq + Clone, Ex> FutureTransactions<Hash, Ex> {
let remove = if let Some(wanted) = self.wanted_tags.get_mut(&tag) {
wanted.remove(hash);
wanted.is_empty()
} else { false };
} else {
false
};
if remove {
self.wanted_tags.remove(&tag);
}
@@ -218,14 +224,15 @@ impl<Hash: hash::Hash + Eq + Clone, Ex> FutureTransactions<Hash, Ex> {
}
/// Fold a list of future transactions to compute a single value.
pub fn fold<R, F: FnMut(Option<R>, &WaitingTransaction<Hash, Ex>) -> Option<R>>(&mut self, f: F) -> Option<R> {
self.waiting
.values()
.fold(None, f)
pub fn fold<R, F: FnMut(Option<R>, &WaitingTransaction<Hash, Ex>) -> Option<R>>(
&mut self,
f: F,
) -> Option<R> {
self.waiting.values().fold(None, f)
}
/// Returns iterator over all future transactions
pub fn all(&self) -> impl Iterator<Item=&Transaction<Hash, Ex>> {
pub fn all(&self) -> impl Iterator<Item = &Transaction<Hash, Ex>> {
self.waiting.values().map(|waiting| &*waiting.transaction)
}
@@ -265,7 +272,8 @@ mod tests {
provides: vec![vec![3], vec![4]],
propagate: true,
source: TransactionSource::External,
}.into(),
}
.into(),
missing_tags: vec![vec![1u8], vec![2u8]].into_iter().collect(),
imported_at: std::time::Instant::now(),
});
@@ -1,4 +1,3 @@
// This file is part of Substrate.
// Copyright (C) 2018-2021 Parity Technologies (UK) Ltd.
@@ -17,16 +16,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::{
collections::HashMap, hash, fmt::Debug,
};
use std::{collections::HashMap, fmt::Debug, hash};
use linked_hash_map::LinkedHashMap;
use serde::Serialize;
use log::{debug, trace};
use serde::Serialize;
use sp_runtime::traits;
use super::{watcher, ChainApi, ExtrinsicHash, BlockHash};
use super::{watcher, BlockHash, ChainApi, ExtrinsicHash};
/// Extrinsic pool default listener.
pub struct Listener<H: hash::Hash + Eq, C: ChainApi> {
@@ -39,15 +36,15 @@ const MAX_FINALITY_WATCHERS: usize = 512;
impl<H: hash::Hash + Eq + Debug, C: ChainApi> Default for Listener<H, C> {
fn default() -> Self {
Self {
watchers: Default::default(),
finality_watchers: Default::default(),
}
Self { watchers: Default::default(), finality_watchers: Default::default() }
}
}
impl<H: hash::Hash + traits::Member + Serialize, C: ChainApi> Listener<H, C> {
fn fire<F>(&mut self, hash: &H, fun: F) where F: FnOnce(&mut watcher::Sender<H, ExtrinsicHash<C>>) {
fn fire<F>(&mut self, hash: &H, fun: F)
where
F: FnOnce(&mut watcher::Sender<H, ExtrinsicHash<C>>),
{
let clean = if let Some(h) = self.watchers.get_mut(hash) {
fun(h);
h.is_done()
@@ -31,15 +31,17 @@ mod listener;
mod pool;
mod ready;
mod rotator;
mod validated_pool;
mod tracked_map;
mod validated_pool;
pub mod base_pool;
pub mod watcher;
pub use self::base_pool::Transaction;
pub use validated_pool::{IsValidator, ValidatedTransaction};
pub use self::pool::{
BlockHash, ChainApi, EventStream, ExtrinsicFor, ExtrinsicHash,
NumberFor, Options, Pool, TransactionFor,
pub use self::{
base_pool::Transaction,
pool::{
BlockHash, ChainApi, EventStream, ExtrinsicFor, ExtrinsicHash, NumberFor, Options, Pool,
TransactionFor,
},
};
pub use validated_pool::{IsValidator, ValidatedTransaction};
@@ -16,26 +16,23 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::{
collections::HashMap,
sync::Arc,
};
use std::{collections::HashMap, sync::Arc};
use futures::Future;
use futures::{channel::mpsc::Receiver, Future};
use sc_transaction_pool_api::error;
use sp_runtime::{
generic::BlockId,
traits::{self, SaturatedConversion, Block as BlockT},
traits::{self, Block as BlockT, SaturatedConversion},
transaction_validity::{
TransactionValidity, TransactionTag as Tag, TransactionValidityError, TransactionSource,
TransactionSource, TransactionTag as Tag, TransactionValidity, TransactionValidityError,
},
};
use sc_transaction_pool_api::error;
use wasm_timer::Instant;
use futures::channel::mpsc::Receiver;
use super::{
base_pool as base, watcher::Watcher,
validated_pool::{IsValidator, ValidatedTransaction, ValidatedPool},
base_pool as base,
validated_pool::{IsValidator, ValidatedPool, ValidatedTransaction},
watcher::Watcher,
};
/// Modification notification event stream type;
@@ -52,11 +49,8 @@ pub type NumberFor<A> = traits::NumberFor<<A as ChainApi>::Block>;
/// A type of transaction stored in the pool
pub type TransactionFor<A> = Arc<base::Transaction<ExtrinsicHash<A>, ExtrinsicFor<A>>>;
/// A type of validated transaction stored in the pool.
pub type ValidatedTransactionFor<A> = ValidatedTransaction<
ExtrinsicHash<A>,
ExtrinsicFor<A>,
<A as ChainApi>::Error,
>;
pub type ValidatedTransactionFor<A> =
ValidatedTransaction<ExtrinsicHash<A>, ExtrinsicFor<A>, <A as ChainApi>::Error>;
/// Concrete extrinsic validation and query logic.
pub trait ChainApi: Send + Sync {
@@ -65,11 +59,12 @@ pub trait ChainApi: Send + Sync {
/// Error type.
type Error: From<error::Error> + error::IntoPoolError;
/// Validate transaction future.
type ValidationFuture: Future<Output=Result<TransactionValidity, Self::Error>> + Send + Unpin;
type ValidationFuture: Future<Output = Result<TransactionValidity, Self::Error>> + Send + Unpin;
/// Body future (since block body might be remote)
type BodyFuture: Future<
Output = Result<Option<Vec<<Self::Block as traits::Block>::Extrinsic>>, Self::Error>
> + Unpin + Send + 'static;
type BodyFuture: Future<Output = Result<Option<Vec<<Self::Block as traits::Block>::Extrinsic>>, Self::Error>>
+ Unpin
+ Send
+ 'static;
/// Verify extrinsic at given block.
fn validate_transaction(
@@ -118,14 +113,8 @@ pub struct Options {
impl Default for Options {
fn default() -> Self {
Self {
ready: base::Limit {
count: 8192,
total_bytes: 20 * 1024 * 1024,
},
future: base::Limit {
count: 512,
total_bytes: 1 * 1024 * 1024,
},
ready: base::Limit { count: 8192, total_bytes: 20 * 1024 * 1024 },
future: base::Limit { count: 512, total_bytes: 1 * 1024 * 1024 },
reject_future_transactions: false,
}
}
@@ -157,9 +146,7 @@ where
impl<B: ChainApi> Pool<B> {
/// Create a new transaction pool.
pub fn new(options: Options, is_validator: IsValidator, api: Arc<B>) -> Self {
Self {
validated_pool: Arc::new(ValidatedPool::new(options, is_validator, api)),
}
Self { validated_pool: Arc::new(ValidatedPool::new(options, is_validator, api)) }
}
/// Imports a bunch of unverified extrinsics to the pool
@@ -167,7 +154,7 @@ impl<B: ChainApi> Pool<B> {
&self,
at: &BlockId<B::Block>,
source: TransactionSource,
xts: impl IntoIterator<Item=ExtrinsicFor<B>>,
xts: impl IntoIterator<Item = ExtrinsicFor<B>>,
) -> Result<Vec<Result<ExtrinsicHash<B>, B::Error>>, B::Error> {
let xts = xts.into_iter().map(|xt| (source, xt));
let validated_transactions = self.verify(at, xts, CheckBannedBeforeVerify::Yes).await?;
@@ -181,7 +168,7 @@ impl<B: ChainApi> Pool<B> {
&self,
at: &BlockId<B::Block>,
source: TransactionSource,
xts: impl IntoIterator<Item=ExtrinsicFor<B>>,
xts: impl IntoIterator<Item = ExtrinsicFor<B>>,
) -> Result<Vec<Result<ExtrinsicHash<B>, B::Error>>, B::Error> {
let xts = xts.into_iter().map(|xt| (source, xt));
let validated_transactions = self.verify(at, xts, CheckBannedBeforeVerify::No).await?;
@@ -207,13 +194,9 @@ impl<B: ChainApi> Pool<B> {
xt: ExtrinsicFor<B>,
) -> Result<Watcher<ExtrinsicHash<B>, ExtrinsicHash<B>>, B::Error> {
let block_number = self.resolve_block_number(at)?;
let (_, tx) = self.verify_one(
at,
block_number,
source,
xt,
CheckBannedBeforeVerify::Yes,
).await;
let (_, tx) = self
.verify_one(at, block_number, source, xt, CheckBannedBeforeVerify::Yes)
.await;
self.validated_pool.submit_and_watch(tx)
}
@@ -222,7 +205,6 @@ impl<B: ChainApi> Pool<B> {
&self,
revalidated_transactions: HashMap<ExtrinsicHash<B>, ValidatedTransactionFor<B>>,
) {
let now = Instant::now();
self.validated_pool.resubmit(revalidated_transactions);
log::debug!(target: "txpool",
@@ -243,13 +225,17 @@ impl<B: ChainApi> Pool<B> {
hashes: &[ExtrinsicHash<B>],
) -> Result<(), B::Error> {
// Get details of all extrinsics that are already in the pool
let in_pool_tags = self.validated_pool.extrinsics_tags(hashes)
.into_iter().filter_map(|x| x).flatten();
let in_pool_tags = self
.validated_pool
.extrinsics_tags(hashes)
.into_iter()
.filter_map(|x| x)
.flatten();
// Prune all transactions that provide given tags
let prune_status = self.validated_pool.prune_tags(in_pool_tags)?;
let pruned_transactions = hashes.iter().cloned()
.chain(prune_status.pruned.iter().map(|tx| tx.hash));
let pruned_transactions =
hashes.iter().cloned().chain(prune_status.pruned.iter().map(|tx| tx.hash));
self.validated_pool.fire_pruned(at, pruned_transactions)
}
@@ -272,7 +258,8 @@ impl<B: ChainApi> Pool<B> {
extrinsics.len()
);
// Get details of all extrinsics that are already in the pool
let in_pool_hashes = extrinsics.iter().map(|extrinsic| self.hash_of(extrinsic)).collect::<Vec<_>>();
let in_pool_hashes =
extrinsics.iter().map(|extrinsic| self.hash_of(extrinsic)).collect::<Vec<_>>();
let in_pool_tags = self.validated_pool.extrinsics_tags(&in_pool_hashes);
// Zip the ones from the pool with the full list (we get pairs `(Extrinsic, Option<Vec<Tag>>)`)
@@ -286,7 +273,9 @@ impl<B: ChainApi> Pool<B> {
// if it's not found in the pool query the runtime at parent block
// to get validity info and tags that the extrinsic provides.
None => {
let validity = self.validated_pool.api()
let validity = self
.validated_pool
.api()
.validate_transaction(parent, TransactionSource::InBlock, extrinsic.clone())
.await;
@@ -324,8 +313,8 @@ impl<B: ChainApi> Pool<B> {
pub async fn prune_tags(
&self,
at: &BlockId<B::Block>,
tags: impl IntoIterator<Item=Tag>,
known_imported_hashes: impl IntoIterator<Item=ExtrinsicHash<B>> + Clone,
tags: impl IntoIterator<Item = Tag>,
known_imported_hashes: impl IntoIterator<Item = ExtrinsicHash<B>> + Clone,
) -> Result<(), B::Error> {
log::debug!(target: "txpool", "Pruning at {:?}", at);
// Prune all transactions that provide given tags
@@ -334,22 +323,17 @@ impl<B: ChainApi> Pool<B> {
// Make sure that we don't revalidate extrinsics that were part of the recently
// imported block. This is especially important for UTXO-like chains cause the
// inputs are pruned so such transaction would go to future again.
self.validated_pool.ban(&Instant::now(), known_imported_hashes.clone().into_iter());
self.validated_pool
.ban(&Instant::now(), known_imported_hashes.clone().into_iter());
// Try to re-validate pruned transactions since some of them might be still valid.
// note that `known_imported_hashes` will be rejected here due to temporary ban.
let pruned_hashes = prune_status.pruned
.iter()
.map(|tx| tx.hash).collect::<Vec<_>>();
let pruned_transactions = prune_status.pruned
.into_iter()
.map(|tx| (tx.source, tx.data.clone()));
let pruned_hashes = prune_status.pruned.iter().map(|tx| tx.hash).collect::<Vec<_>>();
let pruned_transactions =
prune_status.pruned.into_iter().map(|tx| (tx.source, tx.data.clone()));
let reverified_transactions = self.verify(
at,
pruned_transactions,
CheckBannedBeforeVerify::Yes,
).await?;
let reverified_transactions =
self.verify(at, pruned_transactions, CheckBannedBeforeVerify::Yes).await?;
log::trace!(target: "txpool", "Pruning at {:?}. Resubmitting transactions.", at);
// And finally - submit reverified transactions back to the pool
@@ -369,16 +353,16 @@ impl<B: ChainApi> Pool<B> {
/// Resolves block number by id.
fn resolve_block_number(&self, at: &BlockId<B::Block>) -> Result<NumberFor<B>, B::Error> {
self.validated_pool.api().block_id_to_number(at)
.and_then(|number| number.ok_or_else(||
error::Error::InvalidBlockId(format!("{:?}", at)).into()))
self.validated_pool.api().block_id_to_number(at).and_then(|number| {
number.ok_or_else(|| error::Error::InvalidBlockId(format!("{:?}", at)).into())
})
}
/// Returns future that validates a bunch of transactions at given block.
async fn verify(
&self,
at: &BlockId<B::Block>,
xts: impl IntoIterator<Item=(TransactionSource, ExtrinsicFor<B>)>,
xts: impl IntoIterator<Item = (TransactionSource, ExtrinsicFor<B>)>,
check: CheckBannedBeforeVerify,
) -> Result<HashMap<ExtrinsicHash<B>, ValidatedTransactionFor<B>>, B::Error> {
// we need a block number to compute tx validity
@@ -386,8 +370,11 @@ impl<B: ChainApi> Pool<B> {
let res = futures::future::join_all(
xts.into_iter()
.map(|(source, xt)| self.verify_one(at, block_number, source, xt, check))
).await.into_iter().collect::<HashMap<_, _>>();
.map(|(source, xt)| self.verify_one(at, block_number, source, xt, check)),
)
.await
.into_iter()
.collect::<HashMap<_, _>>();
Ok(res)
}
@@ -408,11 +395,11 @@ impl<B: ChainApi> Pool<B> {
return (hash, ValidatedTransaction::Invalid(hash, err))
}
let validation_result = self.validated_pool.api().validate_transaction(
block_id,
source,
xt.clone(),
).await;
let validation_result = self
.validated_pool
.api()
.validate_transaction(block_id, source, xt.clone())
.await;
let status = match validation_result {
Ok(status) => status,
@@ -420,7 +407,7 @@ impl<B: ChainApi> Pool<B> {
};
let validity = match status {
Ok(validity) => {
Ok(validity) =>
if validity.provides.is_empty() {
ValidatedTransaction::Invalid(hash, error::Error::NoTagsProvided.into())
} else {
@@ -432,8 +419,7 @@ impl<B: ChainApi> Pool<B> {
bytes,
validity,
)
}
},
},
Err(TransactionValidityError::Invalid(e)) =>
ValidatedTransaction::Invalid(hash, error::Error::InvalidTransaction(e).into()),
Err(TransactionValidityError::Unknown(e)) =>
@@ -444,35 +430,32 @@ impl<B: ChainApi> Pool<B> {
}
/// get a reference to the underlying validated pool.
pub fn validated_pool(&self) -> &ValidatedPool<B> {
pub fn validated_pool(&self) -> &ValidatedPool<B> {
&self.validated_pool
}
}
impl<B: ChainApi> Clone for Pool<B> {
fn clone(&self) -> Self {
Self {
validated_pool: self.validated_pool.clone(),
}
Self { validated_pool: self.validated_pool.clone() }
}
}
#[cfg(test)]
mod tests {
use std::collections::{HashMap, HashSet};
use parking_lot::Mutex;
use super::{super::base_pool::Limit, *};
use assert_matches::assert_matches;
use codec::Encode;
use futures::executor::block_on;
use super::*;
use parking_lot::Mutex;
use sc_transaction_pool_api::TransactionStatus;
use sp_runtime::{
traits::Hash,
transaction_validity::{ValidTransaction, InvalidTransaction, TransactionSource},
transaction_validity::{InvalidTransaction, TransactionSource, ValidTransaction},
};
use codec::Encode;
use substrate_test_runtime::{Block, Extrinsic, Transfer, H256, AccountId, Hashing};
use assert_matches::assert_matches;
use std::collections::{HashMap, HashSet};
use substrate_test_runtime::{AccountId, Block, Extrinsic, Hashing, Transfer, H256};
use wasm_timer::Instant;
use super::super::base_pool::Limit;
const INVALID_NONCE: u64 = 254;
const SOURCE: TransactionSource = TransactionSource::External;
@@ -522,8 +505,16 @@ mod tests {
} else {
let mut transaction = ValidTransaction {
priority: 4,
requires: if nonce > block_number { vec![vec![nonce as u8 - 1]] } else { vec![] },
provides: if nonce == INVALID_NONCE { vec![] } else { vec![vec![nonce as u8]] },
requires: if nonce > block_number {
vec![vec![nonce as u8 - 1]]
} else {
vec![]
},
provides: if nonce == INVALID_NONCE {
vec![]
} else {
vec![vec![nonce as u8]]
},
longevity: 3,
propagate: true,
};
@@ -539,15 +530,13 @@ mod tests {
Ok(transaction)
}
},
Extrinsic::IncludeData(_) => {
Ok(ValidTransaction {
priority: 9001,
requires: vec![],
provides: vec![vec![42]],
longevity: 9001,
propagate: false,
})
},
Extrinsic::IncludeData(_) => Ok(ValidTransaction {
priority: 9001,
requires: vec![],
provides: vec![vec![42]],
longevity: 9001,
propagate: false,
}),
_ => unimplemented!(),
};
@@ -613,12 +602,17 @@ mod tests {
let pool = pool();
// when
let hash = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}))).unwrap();
let hash = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}),
))
.unwrap();
// then
assert_eq!(pool.validated_pool().ready().map(|v| v.hash).collect::<Vec<_>>(), vec![hash]);
@@ -673,25 +667,40 @@ mod tests {
let stream = pool.validated_pool().import_notification_stream();
// when
let hash0 = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}))).unwrap();
let hash1 = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}))).unwrap();
let hash0 = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}),
))
.unwrap();
let hash1 = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}),
))
.unwrap();
// future doesn't count
let _hash = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 3,
}))).unwrap();
let _hash = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 3,
}),
))
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 2);
assert_eq!(pool.validated_pool().status().future, 1);
@@ -710,24 +719,39 @@ mod tests {
fn should_clear_stale_transactions() {
// given
let pool = pool();
let hash1 = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}))).unwrap();
let hash2 = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}))).unwrap();
let hash3 = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 3,
}))).unwrap();
let hash1 = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}),
))
.unwrap();
let hash2 = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}),
))
.unwrap();
let hash3 = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 3,
}),
))
.unwrap();
// when
pool.validated_pool.clear_stale(&BlockId::Number(5)).unwrap();
@@ -746,12 +770,17 @@ mod tests {
fn should_ban_mined_transactions() {
// given
let pool = pool();
let hash1 = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}))).unwrap();
let hash1 = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}),
))
.unwrap();
// when
block_on(pool.prune_tags(&BlockId::Number(1), vec![vec![0]], vec![hash1.clone()])).unwrap();
@@ -763,34 +792,37 @@ mod tests {
#[test]
fn should_limit_futures() {
// given
let limit = Limit {
count: 100,
total_bytes: 200,
};
let limit = Limit { count: 100, total_bytes: 200 };
let options = Options {
ready: limit.clone(),
future: limit.clone(),
..Default::default()
};
let options = Options { ready: limit.clone(), future: limit.clone(), ..Default::default() };
let pool = Pool::new(options, true.into(), TestApi::default().into());
let hash1 = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}))).unwrap();
let hash1 = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}),
))
.unwrap();
assert_eq!(pool.validated_pool().status().future, 1);
// when
let hash2 = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(2)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 10,
}))).unwrap();
let hash2 = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(2)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 10,
}),
))
.unwrap();
// then
assert_eq!(pool.validated_pool().status().future, 1);
@@ -801,26 +833,24 @@ mod tests {
#[test]
fn should_error_if_reject_immediately() {
// given
let limit = Limit {
count: 100,
total_bytes: 10,
};
let limit = Limit { count: 100, total_bytes: 10 };
let options = Options {
ready: limit.clone(),
future: limit.clone(),
..Default::default()
};
let options = Options { ready: limit.clone(), future: limit.clone(), ..Default::default() };
let pool = Pool::new(options, true.into(), TestApi::default().into());
// when
block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}))).unwrap_err();
block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}),
))
.unwrap_err();
// then
assert_eq!(pool.validated_pool().status().ready, 0);
@@ -833,12 +863,17 @@ mod tests {
let pool = pool();
// when
let err = block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: INVALID_NONCE,
}))).unwrap_err();
let err = block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: INVALID_NONCE,
}),
))
.unwrap_err();
// then
assert_eq!(pool.validated_pool().status().ready, 0);
@@ -853,12 +888,17 @@ mod tests {
fn should_trigger_ready_and_finalized() {
// given
let pool = pool();
let watcher = block_on(pool.submit_and_watch(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}))).unwrap();
let watcher = block_on(pool.submit_and_watch(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}),
))
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 1);
assert_eq!(pool.validated_pool().status().future, 0);
@@ -880,19 +920,27 @@ mod tests {
fn should_trigger_ready_and_finalized_when_pruning_via_hash() {
// given
let pool = pool();
let watcher = block_on(pool.submit_and_watch(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}))).unwrap();
let watcher = block_on(pool.submit_and_watch(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}),
))
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 1);
assert_eq!(pool.validated_pool().status().future, 0);
// when
block_on(
pool.prune_tags(&BlockId::Number(2), vec![vec![0u8]], vec![watcher.hash().clone()]),
).unwrap();
block_on(pool.prune_tags(
&BlockId::Number(2),
vec![vec![0u8]],
vec![watcher.hash().clone()],
))
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 0);
assert_eq!(pool.validated_pool().status().future, 0);
@@ -909,22 +957,32 @@ mod tests {
fn should_trigger_future_and_ready_after_promoted() {
// given
let pool = pool();
let watcher = block_on(pool.submit_and_watch(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}))).unwrap();
let watcher = block_on(pool.submit_and_watch(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 1,
}),
))
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 0);
assert_eq!(pool.validated_pool().status().future, 1);
// when
block_on(pool.submit_one(&BlockId::Number(0), SOURCE, uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}))).unwrap();
block_on(pool.submit_one(
&BlockId::Number(0),
SOURCE,
uxt(Transfer {
from: AccountId::from_h256(H256::from_low_u64_be(1)),
to: AccountId::from_h256(H256::from_low_u64_be(2)),
amount: 5,
nonce: 0,
}),
))
.unwrap();
assert_eq!(pool.validated_pool().status().ready, 2);
// then
@@ -943,13 +1001,13 @@ mod tests {
amount: 5,
nonce: 0,
});
let watcher = block_on(pool.submit_and_watch(&BlockId::Number(0), SOURCE, uxt)).unwrap();
let watcher =
block_on(pool.submit_and_watch(&BlockId::Number(0), SOURCE, uxt)).unwrap();
assert_eq!(pool.validated_pool().status().ready, 1);
// when
pool.validated_pool.remove_invalid(&[*watcher.hash()]);
// then
let mut stream = futures::executor::block_on_stream(watcher.into_stream());
assert_eq!(stream.next(), Some(TransactionStatus::Ready));
@@ -967,7 +1025,8 @@ mod tests {
amount: 5,
nonce: 0,
});
let watcher = block_on(pool.submit_and_watch(&BlockId::Number(0), SOURCE, uxt)).unwrap();
let watcher =
block_on(pool.submit_and_watch(&BlockId::Number(0), SOURCE, uxt)).unwrap();
assert_eq!(pool.validated_pool().status().ready, 1);
// when
@@ -976,7 +1035,6 @@ mod tests {
map.insert(*watcher.hash(), peers.clone());
pool.validated_pool().on_broadcasted(map);
// then
let mut stream = futures::executor::block_on_stream(watcher.into_stream());
assert_eq!(stream.next(), Some(TransactionStatus::Ready));
@@ -986,15 +1044,9 @@ mod tests {
#[test]
fn should_trigger_dropped() {
// given
let limit = Limit {
count: 1,
total_bytes: 1000,
};
let options = Options {
ready: limit.clone(),
future: limit.clone(),
..Default::default()
};
let limit = Limit { count: 1, total_bytes: 1000 };
let options =
Options { ready: limit.clone(), future: limit.clone(), ..Default::default() };
let pool = Pool::new(options, true.into(), TestApi::default().into());
@@ -1064,7 +1116,6 @@ mod tests {
block_on(pool.prune_tags(&BlockId::Number(1), vec![provides], vec![])).unwrap();
assert_eq!(pool.validated_pool().status().ready, 0);
// so when we release the verification of the previous one it will have
// something in `requires`, but should go to ready directly, since the previous transaction was imported
// correctly.
@@ -17,19 +17,16 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::{
collections::{HashMap, HashSet, BTreeSet},
cmp,
collections::{BTreeSet, HashMap, HashSet},
hash,
sync::Arc,
};
use serde::Serialize;
use log::trace;
use sp_runtime::traits::Member;
use sp_runtime::transaction_validity::{
TransactionTag as Tag,
};
use sc_transaction_pool_api::error;
use serde::Serialize;
use sp_runtime::{traits::Member, transaction_validity::TransactionTag as Tag};
use super::{
base_pool::Transaction,
@@ -50,16 +47,15 @@ pub struct TransactionRef<Hash, Ex> {
impl<Hash, Ex> Clone for TransactionRef<Hash, Ex> {
fn clone(&self) -> Self {
Self {
transaction: self.transaction.clone(),
insertion_id: self.insertion_id,
}
Self { transaction: self.transaction.clone(), insertion_id: self.insertion_id }
}
}
impl<Hash, Ex> Ord for TransactionRef<Hash, Ex> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.transaction.priority.cmp(&other.transaction.priority)
self.transaction
.priority
.cmp(&other.transaction.priority)
.then_with(|| other.transaction.valid_till.cmp(&self.transaction.valid_till))
.then_with(|| other.insertion_id.cmp(&self.insertion_id))
}
@@ -149,7 +145,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
///
/// Transactions are returned in order:
/// 1. First by the dependencies:
/// - never return transaction that requires a tag, which was not provided by one of the previously
/// - never return transaction that requires a tag, which was not provided by one of the previously
/// returned transactions
/// 2. Then by priority:
/// - If there are two transactions with all requirements satisfied the one with higher priority goes first.
@@ -157,7 +153,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
/// - transactions that are valid for a shorter time go first
/// 4. Lastly we sort by the time in the queue
/// - transactions that are longer in the queue go first
pub fn get(&self) -> impl Iterator<Item=Arc<Transaction<Hash, Ex>>> {
pub fn get(&self) -> impl Iterator<Item = Arc<Transaction<Hash, Ex>>> {
BestIterator {
all: self.ready.clone(),
best: self.best.clone(),
@@ -176,9 +172,13 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
) -> error::Result<Vec<Arc<Transaction<Hash, Ex>>>> {
assert!(
tx.is_ready(),
"Only ready transactions can be imported. Missing: {:?}", tx.missing_tags
"Only ready transactions can be imported. Missing: {:?}",
tx.missing_tags
);
assert!(
!self.ready.read().contains_key(&tx.transaction.hash),
"Transaction is already imported."
);
assert!(!self.ready.read().contains_key(&tx.transaction.hash), "Transaction is already imported.");
self.insertion_id += 1;
let insertion_id = self.insertion_id;
@@ -201,7 +201,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
} else {
requires_offset += 1;
}
}
}
// update provided_tags
// call to replace_previous guarantees that we will be overwriting
@@ -210,10 +210,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
self.provided_tags.insert(tag.clone(), hash.clone());
}
let transaction = TransactionRef {
insertion_id,
transaction
};
let transaction = TransactionRef { insertion_id, transaction };
// insert to best if it doesn't require any other transaction to be included before it
if goes_to_best {
@@ -221,21 +218,17 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
}
// insert to Ready
ready.insert(hash, ReadyTx {
transaction,
unlocks,
requires_offset,
});
ready.insert(hash, ReadyTx { transaction, unlocks, requires_offset });
Ok(replaced)
}
/// Fold a list of ready transactions to compute a single value.
pub fn fold<R, F: FnMut(Option<R>, &ReadyTx<Hash, Ex>) -> Option<R>>(&mut self, f: F) -> Option<R> {
self.ready
.read()
.values()
.fold(None, f)
pub fn fold<R, F: FnMut(Option<R>, &ReadyTx<Hash, Ex>) -> Option<R>>(
&mut self,
f: F,
) -> Option<R> {
self.ready.read().values().fold(None, f)
}
/// Returns true if given transaction is part of the queue.
@@ -251,9 +244,10 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
/// Retrieve transactions by hash
pub fn by_hashes(&self, hashes: &[Hash]) -> Vec<Option<Arc<Transaction<Hash, Ex>>>> {
let ready = self.ready.read();
hashes.iter().map(|hash| {
ready.get(hash).map(|x| x.transaction.transaction.clone())
}).collect()
hashes
.iter()
.map(|hash| ready.get(hash).map(|x| x.transaction.transaction.clone()))
.collect()
}
/// Removes a subtree of transactions from the ready pool.
@@ -280,13 +274,12 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
let mut ready = self.ready.write();
while let Some(hash) = to_remove.pop() {
if let Some(mut tx) = ready.remove(&hash) {
let invalidated = tx.transaction.transaction.provides
.iter()
.filter(|tag| provides_tag_filter
let invalidated = tx.transaction.transaction.provides.iter().filter(|tag| {
provides_tag_filter
.as_ref()
.map(|filter| !filter.contains(&**tag))
.unwrap_or(true)
);
});
let mut removed_some_tags = false;
// remove entries from provided_tags
@@ -331,7 +324,9 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
let mut to_remove = vec![tag];
while let Some(tag) = to_remove.pop() {
let res = self.provided_tags.remove(&tag)
let res = self
.provided_tags
.remove(&tag)
.and_then(|hash| self.ready.write().remove(&hash));
if let Some(tx) = res {
@@ -417,19 +412,18 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
fn replace_previous(
&mut self,
tx: &Transaction<Hash, Ex>,
) -> error::Result<
(Vec<Arc<Transaction<Hash, Ex>>>, Vec<Hash>)
> {
) -> error::Result<(Vec<Arc<Transaction<Hash, Ex>>>, Vec<Hash>)> {
let (to_remove, unlocks) = {
// check if we are replacing a transaction
let replace_hashes = tx.provides
let replace_hashes = tx
.provides
.iter()
.filter_map(|tag| self.provided_tags.get(tag))
.collect::<HashSet<_>>();
// early exit if we are not replacing anything.
if replace_hashes.is_empty() {
return Ok((vec![], vec![]));
return Ok((vec![], vec![]))
}
// now check if collective priority is lower than the replacement transaction.
@@ -438,9 +432,9 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
replace_hashes
.iter()
.filter_map(|hash| ready.get(hash))
.fold(0u64, |total, tx|
.fold(0u64, |total, tx| {
total.saturating_add(tx.transaction.transaction.priority)
)
})
};
// bail - the transaction has too low priority to replace the old ones
@@ -451,28 +445,22 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
// construct a list of unlocked transactions
let unlocks = {
let ready = self.ready.read();
replace_hashes
.iter()
.filter_map(|hash| ready.get(hash))
.fold(vec![], |mut list, tx| {
replace_hashes.iter().filter_map(|hash| ready.get(hash)).fold(
vec![],
|mut list, tx| {
list.extend(tx.unlocks.iter().cloned());
list
})
},
)
};
(
replace_hashes.into_iter().cloned().collect::<Vec<_>>(),
unlocks
)
(replace_hashes.into_iter().cloned().collect::<Vec<_>>(), unlocks)
};
let new_provides = tx.provides.iter().cloned().collect::<HashSet<_>>();
let removed = self.remove_subtree_with_tag_filter(to_remove, Some(new_provides));
Ok((
removed,
unlocks
))
Ok((removed, unlocks))
}
/// Returns number of transactions in this queue.
@@ -500,7 +488,6 @@ impl<Hash: hash::Hash + Member, Ex> BestIterator<Hash, Ex> {
if satisfied >= tx_ref.transaction.requires.len() {
// If we have satisfied all deps insert to best
self.best.insert(tx_ref);
} else {
// otherwise we're still awaiting for some deps
self.awaiting.insert(tx_ref.transaction.hash.clone(), (satisfied, tx_ref));
@@ -531,7 +518,10 @@ impl<Hash: hash::Hash + Member, Ex> Iterator for BestIterator<Hash, Ex> {
Some((satisfied, tx_ref))
// then get from the pool
} else {
self.all.read().get(hash).map(|next| (next.requires_offset + 1, next.transaction.clone()))
self.all
.read()
.get(hash)
.map(|next| (next.requires_offset + 1, next.transaction.clone()))
};
if let Some((satisfied, tx_ref)) = res {
self.best_or_awaiting(satisfied, tx_ref)
@@ -571,7 +561,7 @@ mod tests {
fn import<H: hash::Hash + Eq + Member + Serialize, Ex>(
ready: &mut ReadyTransactions<H, Ex>,
tx: Transaction<H, Ex>
tx: Transaction<H, Ex>,
) -> error::Result<Vec<Arc<Transaction<H, Ex>>>> {
let x = WaitingTransaction::new(tx, ready.provided_tags(), &[]);
ready.import(x)
@@ -662,7 +652,7 @@ mod tests {
bytes: 1,
hash: 5,
priority: 1,
valid_till: u64::MAX, // use the max here for testing.
valid_till: u64::MAX, // use the max here for testing.
requires: vec![tx1.provides[0].clone()],
provides: vec![],
propagate: true,
@@ -695,7 +685,7 @@ mod tests {
bytes: 1,
hash: 5,
priority: 1,
valid_till: u64::MAX, // use the max here for testing.
valid_till: u64::MAX, // use the max here for testing.
requires: vec![],
provides: vec![],
propagate: true,
@@ -717,28 +707,19 @@ mod tests {
tx
};
// higher priority = better
assert!(TransactionRef {
transaction: Arc::new(with_priority(3, 3)),
insertion_id: 1,
} > TransactionRef {
transaction: Arc::new(with_priority(2, 3)),
insertion_id: 2,
});
assert!(
TransactionRef { transaction: Arc::new(with_priority(3, 3)), insertion_id: 1 } >
TransactionRef { transaction: Arc::new(with_priority(2, 3)), insertion_id: 2 }
);
// lower validity = better
assert!(TransactionRef {
transaction: Arc::new(with_priority(3, 2)),
insertion_id: 1,
} > TransactionRef {
transaction: Arc::new(with_priority(3, 3)),
insertion_id: 2,
});
assert!(
TransactionRef { transaction: Arc::new(with_priority(3, 2)), insertion_id: 1 } >
TransactionRef { transaction: Arc::new(with_priority(3, 3)), insertion_id: 2 }
);
// lower insertion_id = better
assert!(TransactionRef {
transaction: Arc::new(with_priority(3, 3)),
insertion_id: 1,
} > TransactionRef {
transaction: Arc::new(with_priority(3, 3)),
insertion_id: 2,
});
assert!(
TransactionRef { transaction: Arc::new(with_priority(3, 3)), insertion_id: 1 } >
TransactionRef { transaction: Arc::new(with_priority(3, 3)), insertion_id: 2 }
);
}
}
@@ -21,13 +21,8 @@
//! Keeps only recent extrinsic and discard the ones kept for a significant amount of time.
//! Discarded extrinsics are banned so that they don't get re-imported again.
use std::{
collections::HashMap,
hash,
iter,
time::Duration,
};
use parking_lot::RwLock;
use std::{collections::HashMap, hash, iter, time::Duration};
use wasm_timer::Instant;
use super::base_pool::Transaction;
@@ -48,10 +43,7 @@ pub struct PoolRotator<Hash> {
impl<Hash: hash::Hash + Eq> Default for PoolRotator<Hash> {
fn default() -> Self {
Self {
ban_time: Duration::from_secs(60 * 30),
banned_until: Default::default(),
}
Self { ban_time: Duration::from_secs(60 * 30), banned_until: Default::default() }
}
}
@@ -62,7 +54,7 @@ impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
}
/// Bans given set of hashes.
pub fn ban(&self, now: &Instant, hashes: impl IntoIterator<Item=Hash>) {
pub fn ban(&self, now: &Instant, hashes: impl IntoIterator<Item = Hash>) {
let mut banned = self.banned_until.write();
for hash in hashes {
@@ -81,9 +73,14 @@ impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
/// Bans extrinsic if it's stale.
///
/// Returns `true` if extrinsic is stale and got banned.
pub fn ban_if_stale<Ex>(&self, now: &Instant, current_block: u64, xt: &Transaction<Hash, Ex>) -> bool {
pub fn ban_if_stale<Ex>(
&self,
now: &Instant,
current_block: u64,
xt: &Transaction<Hash, Ex>,
) -> bool {
if xt.valid_till > current_block {
return false;
return false
}
self.ban(now, iter::once(xt.hash.clone()));
@@ -107,10 +104,7 @@ mod tests {
type Ex = ();
fn rotator() -> PoolRotator<Hash> {
PoolRotator {
ban_time: Duration::from_millis(10),
..Default::default()
}
PoolRotator { ban_time: Duration::from_millis(10), ..Default::default() }
}
fn tx() -> (Hash, Transaction<Hash, Ex>) {
@@ -160,7 +154,6 @@ mod tests {
assert!(rotator.is_banned(&hash));
}
#[test]
fn should_clear_banned() {
// given
@@ -201,14 +194,14 @@ mod tests {
let past_block = 0;
// when
for i in 0..2*EXPECTED_SIZE {
for i in 0..2 * EXPECTED_SIZE {
let tx = tx_with(i as u64, past_block);
assert!(rotator.ban_if_stale(&now, past_block, &tx));
}
assert_eq!(rotator.banned_until.read().len(), 2*EXPECTED_SIZE);
assert_eq!(rotator.banned_until.read().len(), 2 * EXPECTED_SIZE);
// then
let tx = tx_with(2*EXPECTED_SIZE as u64, past_block);
let tx = tx_with(2 * EXPECTED_SIZE as u64, past_block);
// trigger a garbage collection
assert!(rotator.ban_if_stale(&now, past_block, &tx));
assert_eq!(rotator.banned_until.read().len(), EXPECTED_SIZE);
@@ -16,11 +16,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{
collections::HashMap,
sync::{Arc, atomic::{AtomicIsize, Ordering as AtomicOrdering}},
sync::{
atomic::{AtomicIsize, Ordering as AtomicOrdering},
Arc,
},
};
use parking_lot::{RwLock, RwLockWriteGuard, RwLockReadGuard};
/// Something that can report its size.
pub trait Size {
@@ -39,11 +42,7 @@ pub struct TrackedMap<K, V> {
impl<K, V> Default for TrackedMap<K, V> {
fn default() -> Self {
Self {
index: Arc::new(HashMap::default().into()),
bytes: 0.into(),
length: 0.into(),
}
Self { index: Arc::new(HashMap::default().into()), bytes: 0.into(), length: 0.into() }
}
}
@@ -65,9 +64,7 @@ impl<K, V> TrackedMap<K, V> {
/// Lock map for read.
pub fn read(&self) -> TrackedMapReadAccess<K, V> {
TrackedMapReadAccess {
inner_guard: self.index.read(),
}
TrackedMapReadAccess { inner_guard: self.index.read() }
}
/// Lock map for write.
@@ -87,13 +84,11 @@ pub struct ReadOnlyTrackedMap<K, V>(Arc<RwLock<HashMap<K, V>>>);
impl<K, V> ReadOnlyTrackedMap<K, V>
where
K: Eq + std::hash::Hash
K: Eq + std::hash::Hash,
{
/// Lock map for read.
pub fn read(&self) -> TrackedMapReadAccess<K, V> {
TrackedMapReadAccess {
inner_guard: self.0.read(),
}
TrackedMapReadAccess { inner_guard: self.0.read() }
}
}
@@ -103,7 +98,7 @@ pub struct TrackedMapReadAccess<'a, K, V> {
impl<'a, K, V> TrackedMapReadAccess<'a, K, V>
where
K: Eq + std::hash::Hash
K: Eq + std::hash::Hash,
{
/// Returns true if map contains key.
pub fn contains_key(&self, key: &K) -> bool {
@@ -129,7 +124,8 @@ pub struct TrackedMapWriteAccess<'a, K, V> {
impl<'a, K, V> TrackedMapWriteAccess<'a, K, V>
where
K: Eq + std::hash::Hash, V: Size
K: Eq + std::hash::Hash,
V: Size,
{
/// Insert value and return previous (if any).
pub fn insert(&mut self, key: K, val: V) -> Option<V> {
@@ -165,7 +161,9 @@ mod tests {
use super::*;
impl Size for i32 {
fn size(&self) -> usize { *self as usize / 10 }
fn size(&self) -> usize {
*self as usize / 10
}
}
#[test]
@@ -17,27 +17,31 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::{
collections::{HashSet, HashMap},
collections::{HashMap, HashSet},
hash,
sync::Arc,
};
use serde::Serialize;
use futures::channel::mpsc::{channel, Sender};
use parking_lot::{Mutex, RwLock};
use retain_mut::RetainMut;
use sc_transaction_pool_api::{error, PoolStatus};
use serde::Serialize;
use sp_runtime::{
generic::BlockId,
traits::{self, SaturatedConversion},
transaction_validity::{TransactionTag as Tag, ValidTransaction, TransactionSource},
transaction_validity::{TransactionSource, TransactionTag as Tag, ValidTransaction},
};
use sc_transaction_pool_api::{error, PoolStatus};
use wasm_timer::Instant;
use futures::channel::mpsc::{channel, Sender};
use retain_mut::RetainMut;
use super::{
base_pool::{self as base, PruneStatus}, watcher::Watcher,
listener::Listener, rotator::PoolRotator,
pool::{EventStream, Options, ChainApi, BlockHash, ExtrinsicHash, ExtrinsicFor, TransactionFor},
base_pool::{self as base, PruneStatus},
listener::Listener,
pool::{
BlockHash, ChainApi, EventStream, ExtrinsicFor, ExtrinsicHash, Options, TransactionFor,
},
rotator::PoolRotator,
watcher::Watcher,
};
/// Pre-validated transaction. Validated pool only accepts transactions wrapped in this enum.
@@ -72,19 +76,14 @@ impl<Hash, Ex, Error> ValidatedTransaction<Hash, Ex, Error> {
requires: validity.requires,
provides: validity.provides,
propagate: validity.propagate,
valid_till: at
.saturated_into::<u64>()
.saturating_add(validity.longevity),
valid_till: at.saturated_into::<u64>().saturating_add(validity.longevity),
})
}
}
/// A type of validated transaction stored in the pool.
pub type ValidatedTransactionFor<B> = ValidatedTransaction<
ExtrinsicHash<B>,
ExtrinsicFor<B>,
<B as ChainApi>::Error,
>;
pub type ValidatedTransactionFor<B> =
ValidatedTransaction<ExtrinsicHash<B>, ExtrinsicFor<B>, <B as ChainApi>::Error>;
/// A closure that returns true if the local node is a validator that can author blocks.
pub struct IsValidator(Box<dyn Fn() -> bool + Send + Sync>);
@@ -107,10 +106,7 @@ pub struct ValidatedPool<B: ChainApi> {
is_validator: IsValidator,
options: Options,
listener: RwLock<Listener<ExtrinsicHash<B>, B>>,
pool: RwLock<base::BasePool<
ExtrinsicHash<B>,
ExtrinsicFor<B>,
>>,
pool: RwLock<base::BasePool<ExtrinsicHash<B>, ExtrinsicFor<B>>>,
import_notification_sinks: Mutex<Vec<Sender<ExtrinsicHash<B>>>>,
rotator: PoolRotator<ExtrinsicHash<B>>,
}
@@ -142,7 +138,7 @@ impl<B: ChainApi> ValidatedPool<B> {
}
/// Bans given set of hashes.
pub fn ban(&self, now: &Instant, hashes: impl IntoIterator<Item=ExtrinsicHash<B>>) {
pub fn ban(&self, now: &Instant, hashes: impl IntoIterator<Item = ExtrinsicHash<B>>) {
self.rotator.ban(now, hashes)
}
@@ -173,9 +169,10 @@ impl<B: ChainApi> ValidatedPool<B> {
/// Imports a bunch of pre-validated transactions to the pool.
pub fn submit(
&self,
txs: impl IntoIterator<Item=ValidatedTransactionFor<B>>,
txs: impl IntoIterator<Item = ValidatedTransactionFor<B>>,
) -> Vec<Result<ExtrinsicHash<B>, B::Error>> {
let results = txs.into_iter()
let results = txs
.into_iter()
.map(|validated_tx| self.submit_one(validated_tx))
.collect::<Vec<_>>();
@@ -186,10 +183,14 @@ impl<B: ChainApi> ValidatedPool<B> {
Default::default()
};
results.into_iter().map(|res| match res {
Ok(ref hash) if removed.contains(hash) => Err(error::Error::ImmediatelyDropped.into()),
other => other,
}).collect()
results
.into_iter()
.map(|res| match res {
Ok(ref hash) if removed.contains(hash) =>
Err(error::Error::ImmediatelyDropped.into()),
other => other,
})
.collect()
}
/// Submit single pre-validated transaction to the pool.
@@ -197,30 +198,28 @@ impl<B: ChainApi> ValidatedPool<B> {
match tx {
ValidatedTransaction::Valid(tx) => {
if !tx.propagate && !(self.is_validator.0)() {
return Err(error::Error::Unactionable.into());
return Err(error::Error::Unactionable.into())
}
let imported = self.pool.write().import(tx)?;
if let base::Imported::Ready { ref hash, .. } = imported {
self.import_notification_sinks.lock()
.retain_mut(|sink| {
match sink.try_send(*hash) {
Ok(()) => true,
Err(e) => {
if e.is_full() {
log::warn!(
target: "txpool",
"[{:?}] Trying to notify an import but the channel is full",
hash,
);
true
} else {
false
}
self.import_notification_sinks.lock().retain_mut(|sink| {
match sink.try_send(*hash) {
Ok(()) => true,
Err(e) =>
if e.is_full() {
log::warn!(
target: "txpool",
"[{:?}] Trying to notify an import but the channel is full",
hash,
);
true
} else {
false
},
}
});
}
});
}
let mut listener = self.listener.write();
@@ -244,8 +243,8 @@ impl<B: ChainApi> ValidatedPool<B> {
let future_limit = &self.options.future;
log::debug!(target: "txpool", "Pool Status: {:?}", status);
if ready_limit.is_exceeded(status.ready, status.ready_bytes)
|| future_limit.is_exceeded(status.future, status.future_bytes)
if ready_limit.is_exceeded(status.ready, status.ready_bytes) ||
future_limit.is_exceeded(status.future, status.future_bytes)
{
log::debug!(
target: "txpool",
@@ -257,8 +256,11 @@ impl<B: ChainApi> ValidatedPool<B> {
// clean up the pool
let removed = {
let mut pool = self.pool.write();
let removed = pool.enforce_limits(ready_limit, future_limit)
.into_iter().map(|x| x.hash).collect::<HashSet<_>>();
let removed = pool
.enforce_limits(ready_limit, future_limit)
.into_iter()
.map(|x| x.hash)
.collect::<HashSet<_>>();
// ban all removed transactions
self.rotator.ban(&Instant::now(), removed.iter().copied());
removed
@@ -305,9 +307,17 @@ impl<B: ChainApi> ValidatedPool<B> {
///
/// Removes and then submits passed transactions and all dependent transactions.
/// Transactions that are missing from the pool are not submitted.
pub fn resubmit(&self, mut updated_transactions: HashMap<ExtrinsicHash<B>, ValidatedTransactionFor<B>>) {
pub fn resubmit(
&self,
mut updated_transactions: HashMap<ExtrinsicHash<B>, ValidatedTransactionFor<B>>,
) {
#[derive(Debug, Clone, Copy, PartialEq)]
enum Status { Future, Ready, Failed, Dropped }
enum Status {
Future,
Ready,
Failed,
Dropped,
}
let (mut initial_statuses, final_statuses) = {
let mut pool = self.pool.write();
@@ -322,7 +332,11 @@ impl<B: ChainApi> ValidatedPool<B> {
let mut initial_statuses = HashMap::new();
let mut txs_to_resubmit = Vec::with_capacity(updated_transactions.len());
while !updated_transactions.is_empty() {
let hash = updated_transactions.keys().next().cloned().expect("transactions is not empty; qed");
let hash = updated_transactions
.keys()
.next()
.cloned()
.expect("transactions is not empty; qed");
// note we are not considering tx with hash invalid here - we just want
// to remove it along with dependent transactions and `remove_subtree()`
@@ -390,7 +404,8 @@ impl<B: ChainApi> ValidatedPool<B> {
final_statuses.insert(hash, Status::Failed);
},
},
ValidatedTransaction::Invalid(_, _) | ValidatedTransaction::Unknown(_, _) => {
ValidatedTransaction::Invalid(_, _) |
ValidatedTransaction::Unknown(_, _) => {
final_statuses.insert(hash, Status::Failed);
},
}
@@ -425,12 +440,13 @@ impl<B: ChainApi> ValidatedPool<B> {
/// For each extrinsic, returns tags that it provides (if known), or None (if it is unknown).
pub fn extrinsics_tags(&self, hashes: &[ExtrinsicHash<B>]) -> Vec<Option<Vec<Tag>>> {
self.pool.read()
self.pool
.read()
.by_hashes(&hashes)
.into_iter()
.map(|existing_in_pool|
.map(|existing_in_pool| {
existing_in_pool.map(|transaction| transaction.provides.to_vec())
)
})
.collect()
}
@@ -442,7 +458,7 @@ impl<B: ChainApi> ValidatedPool<B> {
/// Prunes ready transactions that provide given list of tags.
pub fn prune_tags(
&self,
tags: impl IntoIterator<Item=Tag>,
tags: impl IntoIterator<Item = Tag>,
) -> Result<PruneStatus<ExtrinsicHash<B>, ExtrinsicFor<B>>, B::Error> {
// Perform tag-based pruning in the base pool
let status = self.pool.write().prune_tags(tags);
@@ -465,7 +481,7 @@ impl<B: ChainApi> ValidatedPool<B> {
pub fn resubmit_pruned(
&self,
at: &BlockId<B::Block>,
known_imported_hashes: impl IntoIterator<Item=ExtrinsicHash<B>> + Clone,
known_imported_hashes: impl IntoIterator<Item = ExtrinsicHash<B>> + Clone,
pruned_hashes: Vec<ExtrinsicHash<B>>,
pruned_xts: Vec<ValidatedTransactionFor<B>>,
) -> Result<(), B::Error> {
@@ -475,13 +491,12 @@ impl<B: ChainApi> ValidatedPool<B> {
let results = self.submit(pruned_xts);
// Collect the hashes of transactions that now became invalid (meaning that they are successfully pruned).
let hashes = results
.into_iter()
.enumerate()
.filter_map(|(idx, r)| match r.map_err(error::IntoPoolError::into_pool_error) {
let hashes = results.into_iter().enumerate().filter_map(|(idx, r)| {
match r.map_err(error::IntoPoolError::into_pool_error) {
Err(Ok(error::Error::InvalidTransaction(_))) => Some(pruned_hashes[idx]),
_ => None,
});
}
});
// Fire `pruned` notifications for collected hashes and make sure to include
// `known_imported_hashes` since they were just imported as part of the block.
let hashes = hashes.chain(known_imported_hashes.into_iter());
@@ -497,9 +512,11 @@ impl<B: ChainApi> ValidatedPool<B> {
pub fn fire_pruned(
&self,
at: &BlockId<B::Block>,
hashes: impl Iterator<Item=ExtrinsicHash<B>>,
hashes: impl Iterator<Item = ExtrinsicHash<B>>,
) -> Result<(), B::Error> {
let header_hash = self.api.block_id_to_hash(at)?
let header_hash = self
.api
.block_id_to_hash(at)?
.ok_or_else(|| error::Error::InvalidBlockId(format!("{:?}", at)))?;
let mut listener = self.listener.write();
let mut set = HashSet::with_capacity(hashes.size_hint().0);
@@ -520,7 +537,9 @@ impl<B: ChainApi> ValidatedPool<B> {
/// Note this function does not remove transactions that are already included in the chain.
/// See `prune_tags` if you want this.
pub fn clear_stale(&self, at: &BlockId<B::Block>) -> Result<(), B::Error> {
let block_number = self.api.block_id_to_number(at)?
let block_number = self
.api
.block_id_to_number(at)?
.ok_or_else(|| error::Error::InvalidBlockId(format!("{:?}", at)))?
.saturated_into::<u64>();
let now = Instant::now();
@@ -589,7 +608,7 @@ impl<B: ChainApi> ValidatedPool<B> {
pub fn remove_invalid(&self, hashes: &[ExtrinsicHash<B>]) -> Vec<TransactionFor<B>> {
// early exit in case there is no invalid transactions.
if hashes.is_empty() {
return vec![];
return vec![]
}
log::debug!(target: "txpool", "Removing invalid transactions: {:?}", hashes);
@@ -610,13 +629,15 @@ impl<B: ChainApi> ValidatedPool<B> {
}
/// Get an iterator for ready transactions ordered by priority
pub fn ready(&self) -> impl Iterator<Item=TransactionFor<B>> + Send {
pub fn ready(&self) -> impl Iterator<Item = TransactionFor<B>> + Send {
self.pool.read().ready()
}
/// Returns a Vec of hashes and extrinsics in the future pool.
pub fn futures(&self) -> Vec<(ExtrinsicHash<B>, ExtrinsicFor<B>)> {
self.pool.read().futures()
self.pool
.read()
.futures()
.map(|tx| (tx.hash.clone(), tx.data.clone()))
.collect()
}
@@ -639,10 +660,8 @@ impl<B: ChainApi> ValidatedPool<B> {
}
}
fn fire_events<H, B, Ex>(
listener: &mut Listener<H, B>,
imported: &base::Imported<H, Ex>,
) where
fn fire_events<H, B, Ex>(listener: &mut Listener<H, B>, imported: &base::Imported<H, Ex>)
where
H: hash::Hash + Eq + traits::Member + Serialize,
B: ChainApi,
{
@@ -653,8 +672,6 @@ fn fire_events<H, B, Ex>(
removed.into_iter().for_each(|r| listener.dropped(&r.hash, Some(hash)));
promoted.into_iter().for_each(|p| listener.ready(p, None));
},
base::Imported::Future { ref hash } => {
listener.future(hash)
},
base::Imported::Future { ref hash } => listener.future(hash),
}
}
@@ -20,7 +20,7 @@
use futures::Stream;
use sc_transaction_pool_api::TransactionStatus;
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedSender, TracingUnboundedReceiver};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
/// Extrinsic watcher.
///
@@ -41,7 +41,7 @@ impl<H, BH> Watcher<H, BH> {
/// Pipe the notifications to given sink.
///
/// Make sure to drive the future to completion.
pub fn into_stream(self) -> impl Stream<Item=TransactionStatus<H, BH>> {
pub fn into_stream(self) -> impl Stream<Item = TransactionStatus<H, BH>> {
self.receiver
}
}
@@ -55,10 +55,7 @@ pub struct Sender<H, BH> {
impl<H, BH> Default for Sender<H, BH> {
fn default() -> Self {
Sender {
receivers: Default::default(),
is_finalized: false,
}
Sender { receivers: Default::default(), is_finalized: false }
}
}
@@ -67,10 +64,7 @@ impl<H: Clone, BH: Clone> Sender<H, BH> {
pub fn new_watcher(&mut self, hash: H) -> Watcher<H, BH> {
let (tx, receiver) = tracing_unbounded("mpsc_txpool_watcher");
self.receivers.push(tx);
Watcher {
receiver,
hash,
}
Watcher { receiver, hash }
}
/// Transaction became ready.