mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-21 13:15:41 +00:00
Tagged transaction queue integration (#893)
* Make the graph generic. * Adapting pool API for the graph. * Merge pool & graph. * Restructure. * Fix test of transaction pool. * Get rid of node/transaction-pool. * Compilation fixes. * Test7 * Fix compilation of tests. * Revert runtime changes. * Add validate_transaction to test-runtime. * Fix RPC tests. * Add clearing of the old transactions. * Trigger pool events. * Use new queue API. * Fix wasm build, re-export Hasher. * No warning if validate transaction fails. * Get rid of Into<u64> and use As
This commit is contained in:
@@ -4,16 +4,16 @@ version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
error-chain = "0.12"
|
||||
futures = "0.1"
|
||||
log = "0.4"
|
||||
parity-codec = "2.0"
|
||||
parking_lot = "0.4"
|
||||
transaction-pool = "1.13.3"
|
||||
sr-primitives = { path = "../../core/sr-primitives" }
|
||||
sr-primitives = { path = "../sr-primitives" }
|
||||
substrate-client = { path = "../client" }
|
||||
substrate-primitives = { path = "../primitives" }
|
||||
substrate-transaction-graph = { path = "./graph" }
|
||||
|
||||
[dev-dependencies]
|
||||
substrate-test-client = { path = "../../core/test-client" }
|
||||
substrate-keyring = { path = "../../core/keyring" }
|
||||
parity-codec = "2.0"
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "substrate-transaction-graph"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
error-chain = "0.12"
|
||||
futures = "0.1"
|
||||
log = "0.4"
|
||||
parking_lot = "0.4"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
sr-primitives = { path = "../../sr-primitives" }
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
= transaction-graph
|
||||
|
||||
.Summary
|
||||
[source, toml]
|
||||
----
|
||||
include::Cargo.toml[lines=2..5]
|
||||
----
|
||||
|
||||
.Description
|
||||
----
|
||||
include::src/lib.rs[tag=description]
|
||||
----
|
||||
@@ -0,0 +1,709 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A basic version of the dependency graph.
|
||||
//!
|
||||
//! For a more full-featured pool, have a look at the `pool` module.
|
||||
|
||||
use std::{
|
||||
hash,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use sr_primitives::traits::Member;
|
||||
use sr_primitives::transaction_validity::{
|
||||
TransactionTag as Tag,
|
||||
TransactionLongevity as Longevity,
|
||||
TransactionPriority as Priority,
|
||||
};
|
||||
|
||||
use error;
|
||||
use future::{FutureTransactions, WaitingTransaction};
|
||||
use ready::ReadyTransactions;
|
||||
|
||||
/// Block number type.
|
||||
pub type BlockNumber = u64;
|
||||
|
||||
/// Successful import result.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum Imported<Hash, Ex> {
|
||||
/// Transaction was successfuly imported to Ready queue.
|
||||
Ready {
|
||||
/// Hash of transaction that was successfuly imported.
|
||||
hash: Hash,
|
||||
/// Transactions that got promoted from the Future queue.
|
||||
promoted: Vec<Hash>,
|
||||
/// Transactions that failed to be promoted from the Future queue and are now discarded.
|
||||
failed: Vec<Hash>,
|
||||
/// Transactions removed from the Ready pool (replaced).
|
||||
removed: Vec<Arc<Transaction<Hash, Ex>>>,
|
||||
},
|
||||
/// Transaction was successfuly imported to Future queue.
|
||||
Future {
|
||||
/// Hash of transaction that was successfuly imported.
|
||||
hash: Hash,
|
||||
}
|
||||
}
|
||||
|
||||
impl<Hash, Ex> Imported<Hash, Ex> {
|
||||
/// Returns the hash of imported transaction.
|
||||
pub fn hash(&self) -> &Hash {
|
||||
use self::Imported::*;
|
||||
match *self {
|
||||
Ready { ref hash, .. } => hash,
|
||||
Future { ref hash, .. } => hash,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Status of pruning the queue.
|
||||
#[derive(Debug)]
|
||||
pub struct PruneStatus<Hash, Ex> {
|
||||
/// A list of imports that satisfying the tag triggered.
|
||||
pub promoted: Vec<Imported<Hash, Ex>>,
|
||||
/// A list of transactions that failed to be promoted and now are discarded.
|
||||
pub failed: Vec<Hash>,
|
||||
/// A list of transactions that got pruned from the ready queue.
|
||||
pub pruned: Vec<Arc<Transaction<Hash, Ex>>>,
|
||||
}
|
||||
|
||||
/// Immutable transaction
|
||||
#[cfg_attr(test, derive(Clone))]
|
||||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Transaction<Hash, Extrinsic> {
|
||||
/// Raw extrinsic representing that transaction.
|
||||
pub data: Extrinsic,
|
||||
/// Transaction hash (unique)
|
||||
pub hash: Hash,
|
||||
/// Transaction priority (higher = better)
|
||||
pub priority: Priority,
|
||||
/// How many blocks the transaction is valid for.
|
||||
pub longevity: Longevity,
|
||||
/// Tags required by the transaction.
|
||||
pub requires: Vec<Tag>,
|
||||
/// Tags that this transaction provides.
|
||||
pub provides: Vec<Tag>,
|
||||
}
|
||||
|
||||
/// Transaction pool.
|
||||
///
|
||||
/// Builds a dependency graph for all transactions in the pool and returns
|
||||
/// the ones that are currently ready to be executed.
|
||||
///
|
||||
/// General note:
|
||||
/// If function returns some transactions it usually means that importing them
|
||||
/// as-is for the second time will fail or produce unwanted results.
|
||||
/// Most likely it is required to revalidate them and recompute set of
|
||||
/// required tags.
|
||||
#[derive(Debug)]
|
||||
pub struct BasePool<Hash: hash::Hash + Eq, Ex> {
|
||||
future: FutureTransactions<Hash, Ex>,
|
||||
ready: ReadyTransactions<Hash, Ex>,
|
||||
}
|
||||
|
||||
impl<Hash: hash::Hash + Eq, Ex> Default for BasePool<Hash, Ex> {
|
||||
fn default() -> Self {
|
||||
BasePool {
|
||||
future: Default::default(),
|
||||
ready: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Hash: hash::Hash + Member, Ex: ::std::fmt::Debug> BasePool<Hash, Ex> {
|
||||
/// Imports transaction to the pool.
|
||||
///
|
||||
/// The pool consists of two parts: Future and Ready.
|
||||
/// The former contains transactions that require some tags that are not yet provided by
|
||||
/// 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,
|
||||
block_number: BlockNumber,
|
||||
tx: Transaction<Hash, Ex>,
|
||||
) -> error::Result<Imported<Hash, Ex>> {
|
||||
if self.future.contains(&tx.hash) || self.ready.contains(&tx.hash) {
|
||||
bail!(error::ErrorKind::AlreadyImported)
|
||||
}
|
||||
|
||||
let tx = WaitingTransaction::new(tx, self.ready.provided_tags());
|
||||
trace!(target: "txpool", "[{:?}] {:?}", tx.transaction.hash, tx);
|
||||
debug!(target: "txpool", "[{:?}] Importing to {}", tx.transaction.hash, if tx.is_ready() { "ready" } else { "future" });
|
||||
|
||||
// If all tags are not satisfied import to future.
|
||||
if !tx.is_ready() {
|
||||
let hash = tx.transaction.hash.clone();
|
||||
self.future.import(tx);
|
||||
return Ok(Imported::Future { hash });
|
||||
}
|
||||
|
||||
self.import_to_ready(block_number, tx)
|
||||
}
|
||||
|
||||
/// Imports transaction to ready queue.
|
||||
///
|
||||
/// NOTE the transaction has to have all requirements satisfied.
|
||||
fn import_to_ready(&mut self, block_number: BlockNumber, 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 {
|
||||
// take first transaction from the list
|
||||
let tx = match to_import.pop() {
|
||||
Some(tx) => tx,
|
||||
None => break,
|
||||
};
|
||||
|
||||
// find transactions in Future that it unlocks
|
||||
to_import.append(&mut self.future.satisfy_tags(&tx.transaction.provides));
|
||||
|
||||
// import this transaction
|
||||
let current_hash = tx.transaction.hash.clone();
|
||||
match self.ready.import(block_number, tx) {
|
||||
Ok(mut replaced) => {
|
||||
if !first {
|
||||
promoted.push(current_hash);
|
||||
}
|
||||
// The transactions were removed from the ready pool. We might attempt to re-import them.
|
||||
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);
|
||||
},
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
|
||||
// An edge case when importing transaction caused
|
||||
// some future transactions to be imported and that
|
||||
// future transactions pushed out current transaction.
|
||||
// This means that there is a cycle and the transactions should
|
||||
// be moved back to future, since we can't resolve it.
|
||||
if removed.iter().any(|tx| tx.hash == hash) {
|
||||
// We still need to remove all transactions that we promoted
|
||||
// since they depend on each other and will never get to the best iterator.
|
||||
self.ready.remove_invalid(&promoted);
|
||||
|
||||
debug!(target: "txpool", "[{:?}] Cycle detected, bailing.", hash);
|
||||
bail!(error::ErrorKind::CycleDetected)
|
||||
}
|
||||
|
||||
Ok(Imported::Ready {
|
||||
hash,
|
||||
promoted,
|
||||
failed,
|
||||
removed,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns an iterator over ready transactions in the pool.
|
||||
pub fn ready<'a, 'b: 'a>(&'b self) -> impl Iterator<Item=Arc<Transaction<Hash, Ex>>> + 'a {
|
||||
self.ready.get()
|
||||
}
|
||||
|
||||
/// Removes all transactions represented by the hashes and all other transactions
|
||||
/// that depend on them.
|
||||
///
|
||||
/// Returns a list of actually removed transactions.
|
||||
/// NOTE some transactions might still be valid, but were just removed because
|
||||
/// they were part of a chain, you may attempt to re-import them later.
|
||||
/// NOTE If you want to remove ready transactions that were already used
|
||||
/// and you don't want them to be stored in the pool use `prune_tags` method.
|
||||
pub fn remove_invalid(&mut self, hashes: &[Hash]) -> Vec<Arc<Transaction<Hash, Ex>>> {
|
||||
let mut removed = self.ready.remove_invalid(hashes);
|
||||
removed.extend(self.future.remove(hashes).into_iter().map(Arc::new));
|
||||
removed
|
||||
}
|
||||
|
||||
/// Prunes transactions that provide given list of tags.
|
||||
///
|
||||
/// This will cause all transactions that provide these tags to be removed from the pool,
|
||||
/// but unlike `remove_invalid`, 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, block_number: BlockNumber, tags: impl IntoIterator<Item=Tag>) -> PruneStatus<Hash, Ex> {
|
||||
let mut to_import = vec![];
|
||||
let mut pruned = vec![];
|
||||
|
||||
for tag in tags {
|
||||
// make sure to promote any future transactions that could be unlocked
|
||||
to_import.append(&mut self.future.satisfy_tags(::std::iter::once(&tag)));
|
||||
// and actually prune transactions in ready queue
|
||||
pruned.append(&mut self.ready.prune_tags(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(block_number, 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,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get pool status.
|
||||
pub fn status(&self) -> Status {
|
||||
Status {
|
||||
ready: self.ready.len(),
|
||||
future: self.future.len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pool status
|
||||
pub struct Status {
|
||||
/// Number of transactions in the ready queue.
|
||||
pub ready: usize,
|
||||
/// Number of transactions in the future queue.
|
||||
pub future: usize,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
type Hash = u64;
|
||||
|
||||
fn pool() -> BasePool<Hash, Vec<u8>> {
|
||||
BasePool::default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_import_transaction_to_ready() {
|
||||
// given
|
||||
let mut pool = pool();
|
||||
|
||||
// when
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1u64,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap();
|
||||
|
||||
// then
|
||||
assert_eq!(pool.ready().count(), 1);
|
||||
assert_eq!(pool.ready.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_import_same_transaction_twice() {
|
||||
// given
|
||||
let mut pool = pool();
|
||||
|
||||
// when
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap_err();
|
||||
|
||||
// then
|
||||
assert_eq!(pool.ready().count(), 1);
|
||||
assert_eq!(pool.ready.len(), 1);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn should_import_transaction_to_future_and_promote_it_later() {
|
||||
// given
|
||||
let mut pool = pool();
|
||||
|
||||
// when
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![0]],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap();
|
||||
assert_eq!(pool.ready().count(), 0);
|
||||
assert_eq!(pool.ready.len(), 0);
|
||||
pool.import(1, Transaction {
|
||||
data: vec![2u8],
|
||||
hash: 2,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![0]],
|
||||
}).unwrap();
|
||||
|
||||
// then
|
||||
assert_eq!(pool.ready().count(), 2);
|
||||
assert_eq!(pool.ready.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_promote_a_subgraph() {
|
||||
// given
|
||||
let mut pool = pool();
|
||||
|
||||
// when
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![0]],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![3u8],
|
||||
hash: 3,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![2]],
|
||||
provides: vec![],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![2u8],
|
||||
hash: 2,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![1]],
|
||||
provides: vec![vec![3], vec![2]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![4u8],
|
||||
hash: 4,
|
||||
priority: 1_000u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![3], vec![4]],
|
||||
provides: vec![],
|
||||
}).unwrap();
|
||||
assert_eq!(pool.ready().count(), 0);
|
||||
assert_eq!(pool.ready.len(), 0);
|
||||
|
||||
let res = pool.import(1, Transaction {
|
||||
data: vec![5u8],
|
||||
hash: 5,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![0], vec![4]],
|
||||
}).unwrap();
|
||||
|
||||
// then
|
||||
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() {
|
||||
// given
|
||||
let mut pool = pool();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![0]],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![3u8],
|
||||
hash: 3,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![1]],
|
||||
provides: vec![vec![2]],
|
||||
}).unwrap();
|
||||
assert_eq!(pool.ready().count(), 0);
|
||||
assert_eq!(pool.ready.len(), 0);
|
||||
|
||||
// when
|
||||
pool.import(1, Transaction {
|
||||
data: vec![2u8],
|
||||
hash: 2,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![2]],
|
||||
provides: vec![vec![0]],
|
||||
}).unwrap();
|
||||
|
||||
// then
|
||||
{
|
||||
let mut it = pool.ready().into_iter().map(|tx| tx.data[0]);
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
// all transactions occupy the Future queue - it's fine
|
||||
assert_eq!(pool.future.len(), 3);
|
||||
|
||||
// let's close the cycle with one additional transaction
|
||||
let res = pool.import(1, Transaction {
|
||||
data: vec![4u8],
|
||||
hash: 4,
|
||||
priority: 50u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![0]],
|
||||
}).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() {
|
||||
// given
|
||||
let mut pool = pool();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![0]],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![3u8],
|
||||
hash: 3,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![1]],
|
||||
provides: vec![vec![2]],
|
||||
}).unwrap();
|
||||
assert_eq!(pool.ready().count(), 0);
|
||||
assert_eq!(pool.ready.len(), 0);
|
||||
|
||||
// when
|
||||
pool.import(1, Transaction {
|
||||
data: vec![2u8],
|
||||
hash: 2,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![2]],
|
||||
provides: vec![vec![0]],
|
||||
}).unwrap();
|
||||
|
||||
// then
|
||||
{
|
||||
let mut it = pool.ready().into_iter().map(|tx| tx.data[0]);
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
// all transactions occupy the Future queue - it's fine
|
||||
assert_eq!(pool.future.len(), 3);
|
||||
|
||||
// let's close the cycle with one additional transaction
|
||||
let err = pool.import(1, Transaction {
|
||||
data: vec![4u8],
|
||||
hash: 4,
|
||||
priority: 1u64, // lower priority than Tx(2)
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![0]],
|
||||
}).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::ErrorKind::CycleDetected = *err.kind() {
|
||||
} else {
|
||||
assert!(false, "Invalid error kind: {:?}", err.kind());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_remove_invalid_transactions() {
|
||||
// given
|
||||
let mut pool = pool();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![5u8],
|
||||
hash: 5,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![0], vec![4]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![0]],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![3u8],
|
||||
hash: 3,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![2]],
|
||||
provides: vec![],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![2u8],
|
||||
hash: 2,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![1]],
|
||||
provides: vec![vec![3], vec![2]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![4u8],
|
||||
hash: 4,
|
||||
priority: 1_000u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![3], vec![4]],
|
||||
provides: vec![],
|
||||
}).unwrap();
|
||||
// future
|
||||
pool.import(1, Transaction {
|
||||
data: vec![6u8],
|
||||
hash: 6,
|
||||
priority: 1_000u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![11]],
|
||||
provides: vec![],
|
||||
}).unwrap();
|
||||
assert_eq!(pool.ready().count(), 5);
|
||||
assert_eq!(pool.future.len(), 1);
|
||||
|
||||
// when
|
||||
pool.remove_invalid(&[6, 1]);
|
||||
|
||||
// then
|
||||
assert_eq!(pool.ready().count(), 1);
|
||||
assert_eq!(pool.future.len(), 0);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn should_prune_ready_transactions() {
|
||||
// given
|
||||
let mut pool = pool();
|
||||
// future (waiting for 0)
|
||||
pool.import(1, Transaction {
|
||||
data: vec![5u8],
|
||||
hash: 5,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![0]],
|
||||
provides: vec![vec![100]],
|
||||
}).unwrap();
|
||||
// ready
|
||||
pool.import(1, Transaction {
|
||||
data: vec![1u8],
|
||||
hash: 1,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![],
|
||||
provides: vec![vec![1]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![2u8],
|
||||
hash: 2,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![2]],
|
||||
provides: vec![vec![3]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![3u8],
|
||||
hash: 3,
|
||||
priority: 5u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![1]],
|
||||
provides: vec![vec![2]],
|
||||
}).unwrap();
|
||||
pool.import(1, Transaction {
|
||||
data: vec![4u8],
|
||||
hash: 4,
|
||||
priority: 1_000u64,
|
||||
longevity: 64u64,
|
||||
requires: vec![vec![3], vec![2]],
|
||||
provides: vec![vec![4]],
|
||||
}).unwrap();
|
||||
|
||||
assert_eq!(pool.ready().count(), 4);
|
||||
assert_eq!(pool.future.len(), 1);
|
||||
|
||||
// when
|
||||
let result = pool.prune_tags(1, vec![vec![0], vec![2]]);
|
||||
|
||||
// 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.len(), 1);
|
||||
assert_eq!(pool.future.len(), 0);
|
||||
assert_eq!(pool.ready.len(), 3);
|
||||
assert_eq!(pool.ready().count(), 3);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Transaction pool errors.
|
||||
|
||||
use sr_primitives::transaction_validity::TransactionPriority as Priority;
|
||||
|
||||
error_chain! {
|
||||
errors {
|
||||
/// Transaction is not verifiable yet, but might be in the future.
|
||||
UnknownTransactionValidity {
|
||||
description("Runtime cannot determine validity of the transaction yet."),
|
||||
display("Unkown Transaction Validity"),
|
||||
}
|
||||
/// Transaction is invalid
|
||||
InvalidTransaction {
|
||||
description("Runtime check for the transaction failed."),
|
||||
display("Invalid Transaction"),
|
||||
}
|
||||
/// The transaction is temporarily baned
|
||||
TemporarilyBanned {
|
||||
description("Transaction is temporarily banned from importing to the pool."),
|
||||
display("Temporarily Banned"),
|
||||
}
|
||||
/// The transaction is already in the pool.
|
||||
AlreadyImported {
|
||||
description("Transaction is already in the pool."),
|
||||
display("Already imported"),
|
||||
}
|
||||
/// The transaction cannot be imported cause it's a replacement and has too low priority.
|
||||
TooLowPriority(old: Priority, new: Priority) {
|
||||
description("The priority is too low to replace transactions already in the pool."),
|
||||
display("Too low priority ({} > {})", old, new)
|
||||
}
|
||||
/// Deps cycle detected and we couldn't import transaction.
|
||||
CycleDetected {
|
||||
description("Transaction was not imported because of detected cycle."),
|
||||
display("Cycle Detected"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Transaction pool error conversion.
|
||||
pub trait IntoPoolError: ::std::error::Error + Send + Sized {
|
||||
/// Try to extract original `Error`
|
||||
///
|
||||
/// This implementation is optional and used only to
|
||||
/// provide more descriptive error messages for end users
|
||||
/// of RPC API.
|
||||
fn into_pool_error(self) -> ::std::result::Result<Error, Self> { Err(self) }
|
||||
}
|
||||
|
||||
impl IntoPoolError for Error {
|
||||
fn into_pool_error(self) -> ::std::result::Result<Error, Self> { Ok(self) }
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
hash,
|
||||
};
|
||||
|
||||
use sr_primitives::transaction_validity::{
|
||||
TransactionTag as Tag,
|
||||
};
|
||||
|
||||
use base_pool::Transaction;
|
||||
|
||||
/// Transaction with partially satisfied dependencies.
|
||||
#[derive(Debug)]
|
||||
pub struct WaitingTransaction<Hash, Ex> {
|
||||
/// Transaction details.
|
||||
pub transaction: Transaction<Hash, Ex>,
|
||||
/// Tags that are required and have not been satisfied yet by other transactions in the pool.
|
||||
pub missing_tags: HashSet<Tag>,
|
||||
}
|
||||
|
||||
impl<Hash, Ex> WaitingTransaction<Hash, Ex> {
|
||||
/// Creates a new `WaitingTransaction`.
|
||||
///
|
||||
/// Computes the set of missing tags based on the requirements and tags that
|
||||
/// are provided by all transactions in the ready queue.
|
||||
pub fn new(transaction: Transaction<Hash, Ex>, provided: &HashMap<Tag, Hash>) -> Self {
|
||||
let missing_tags = transaction.requires
|
||||
.iter()
|
||||
.filter(|tag| !provided.contains_key(&**tag))
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
WaitingTransaction {
|
||||
transaction,
|
||||
missing_tags,
|
||||
}
|
||||
}
|
||||
|
||||
/// Marks the tag as satisfied.
|
||||
pub fn satisfy_tag(&mut self, tag: &Tag) {
|
||||
self.missing_tags.remove(tag);
|
||||
}
|
||||
|
||||
/// Returns true if transaction has all requirements satisfied.
|
||||
pub fn is_ready(&self) -> bool {
|
||||
self.missing_tags.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// A pool of transactions that are not yet ready to be included in the block.
|
||||
///
|
||||
/// Contains transactions that are still awaiting for some other transactions that
|
||||
/// could provide a tag that they require.
|
||||
#[derive(Debug)]
|
||||
pub struct FutureTransactions<Hash: hash::Hash + Eq, Ex> {
|
||||
/// tags that are not yet provided by any transaction and we await for them
|
||||
wanted_tags: HashMap<Tag, HashSet<Hash>>,
|
||||
/// Transactions waiting for a particular other transaction
|
||||
waiting: HashMap<Hash, WaitingTransaction<Hash, Ex>>,
|
||||
}
|
||||
|
||||
impl<Hash: hash::Hash + Eq, Ex> Default for FutureTransactions<Hash, Ex> {
|
||||
fn default() -> Self {
|
||||
FutureTransactions {
|
||||
wanted_tags: Default::default(),
|
||||
waiting: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const WAITING_PROOF: &str = r"#
|
||||
In import we always insert to `waiting` if we push to `wanted_tags`;
|
||||
when removing from `waiting` we always clear `wanted_tags`;
|
||||
every hash from `wanted_tags` is always present in `waiting`;
|
||||
qed
|
||||
#";
|
||||
|
||||
impl<Hash: hash::Hash + Eq + Clone, Ex> FutureTransactions<Hash, Ex> {
|
||||
/// Import transaction to Future queue.
|
||||
///
|
||||
/// Only transactions that don't have all their tags satisfied should occupy
|
||||
/// the Future queue.
|
||||
/// As soon as required tags are provided by some other transactions that are ready
|
||||
/// 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.");
|
||||
|
||||
// Add all tags that are missing
|
||||
for tag in &tx.missing_tags {
|
||||
let mut entry = self.wanted_tags.entry(tag.clone()).or_insert_with(HashSet::new);
|
||||
entry.insert(tx.transaction.hash.clone());
|
||||
}
|
||||
|
||||
// Add the transaction to a by-hash waiting map
|
||||
self.waiting.insert(tx.transaction.hash.clone(), tx);
|
||||
}
|
||||
|
||||
/// Returns true if given hash is part of the queue.
|
||||
pub fn contains(&self, hash: &Hash) -> bool {
|
||||
self.waiting.contains_key(hash)
|
||||
}
|
||||
|
||||
/// 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>> {
|
||||
let mut became_ready = vec![];
|
||||
|
||||
for tag in tags {
|
||||
if let Some(hashes) = self.wanted_tags.remove(tag.as_ref()) {
|
||||
for hash in hashes {
|
||||
let is_ready = {
|
||||
let mut tx = self.waiting.get_mut(&hash)
|
||||
.expect(WAITING_PROOF);
|
||||
tx.satisfy_tag(tag.as_ref());
|
||||
tx.is_ready()
|
||||
};
|
||||
|
||||
if is_ready {
|
||||
let tx = self.waiting.remove(&hash).expect(WAITING_PROOF);
|
||||
became_ready.push(tx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
became_ready
|
||||
}
|
||||
|
||||
/// Removes transactions for given list of hashes.
|
||||
///
|
||||
/// Returns a list of actually removed transactions.
|
||||
pub fn remove(&mut self, hashes: &[Hash]) -> Vec<Transaction<Hash, Ex>> {
|
||||
let mut removed = vec![];
|
||||
for hash in hashes {
|
||||
if let Some(waiting_tx) = self.waiting.remove(hash) {
|
||||
// remove from wanted_tags as well
|
||||
for tag in waiting_tx.missing_tags {
|
||||
let remove = if let Some(mut wanted) = self.wanted_tags.get_mut(&tag) {
|
||||
wanted.remove(hash);
|
||||
wanted.is_empty()
|
||||
} else { false };
|
||||
if remove {
|
||||
self.wanted_tags.remove(&tag);
|
||||
}
|
||||
}
|
||||
// add to result
|
||||
removed.push(waiting_tx.transaction)
|
||||
}
|
||||
}
|
||||
removed
|
||||
}
|
||||
|
||||
/// Returns number of transactions in the Future queue.
|
||||
pub fn len(&self) -> usize {
|
||||
self.waiting.len()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// tag::description[]
|
||||
//! Generic Transaction Pool
|
||||
//!
|
||||
//! The pool is based on dependency graph between transactions
|
||||
//! and their priority.
|
||||
//! The pool is able to return an iterator that traverses transaction
|
||||
//! graph in the correct order taking into account priorities and dependencies.
|
||||
//!
|
||||
//! TODO [ToDr]
|
||||
//! - [ ] Longevity handling (remove obsolete transactions periodically)
|
||||
//! - [ ] Multi-threading (getting ready transactions should not block the pool)
|
||||
// end::description[]
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![warn(unused_extern_crates)]
|
||||
|
||||
extern crate futures;
|
||||
extern crate parking_lot;
|
||||
extern crate sr_primitives;
|
||||
|
||||
#[macro_use] extern crate error_chain;
|
||||
#[macro_use] extern crate log;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
|
||||
mod future;
|
||||
mod listener;
|
||||
mod pool;
|
||||
mod ready;
|
||||
mod rotator;
|
||||
|
||||
pub mod base_pool;
|
||||
pub mod error;
|
||||
pub mod watcher;
|
||||
|
||||
pub use self::error::IntoPoolError;
|
||||
pub use self::base_pool::{Transaction, Status};
|
||||
pub use self::pool::{Pool, Options, ChainApi, EventStream, ExtrinsicFor, BlockHash, ExHash, NumberFor, TransactionFor};
|
||||
+42
-57
@@ -1,3 +1,4 @@
|
||||
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
@@ -15,53 +16,27 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{
|
||||
sync::Arc,
|
||||
fmt,
|
||||
collections::HashMap,
|
||||
hash,
|
||||
};
|
||||
use txpool;
|
||||
use watcher;
|
||||
|
||||
/// Returns the hash of the latest block.
|
||||
pub trait LatestHash {
|
||||
type Hash: Clone;
|
||||
|
||||
/// Hash of the latest block.
|
||||
fn latest_hash(&self) -> Self::Hash;
|
||||
}
|
||||
use sr_primitives::traits;
|
||||
|
||||
/// Extrinsic pool default listener.
|
||||
pub struct Listener<H: ::std::hash::Hash + Eq, C: LatestHash> {
|
||||
watchers: HashMap<H, watcher::Sender<H, C::Hash>>,
|
||||
chain: C,
|
||||
pub struct Listener<H: hash::Hash + Eq, H2> {
|
||||
watchers: HashMap<H, watcher::Sender<H, H2>>
|
||||
}
|
||||
|
||||
impl<H, C> Listener<H, C> where
|
||||
H: ::std::hash::Hash + Eq + Copy + fmt::Debug + fmt::LowerHex + Default,
|
||||
C: LatestHash,
|
||||
{
|
||||
/// Creates a new listener with given latest hash provider.
|
||||
pub fn new(chain: C) -> Self {
|
||||
impl<H: hash::Hash + Eq, H2> Default for Listener<H, H2> {
|
||||
fn default() -> Self {
|
||||
Listener {
|
||||
watchers: Default::default(),
|
||||
chain,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new watcher for given verified extrinsic.
|
||||
///
|
||||
/// The watcher can be used to subscribe to lifecycle events of that extrinsic.
|
||||
pub fn create_watcher<T: txpool::VerifiedTransaction<Hash=H>>(&mut self, xt: Arc<T>) -> watcher::Watcher<H, C::Hash> {
|
||||
let sender = self.watchers.entry(*xt.hash()).or_insert_with(watcher::Sender::default);
|
||||
sender.new_watcher()
|
||||
}
|
||||
|
||||
/// Notify the listeners about extrinsic broadcast.
|
||||
pub fn broadcasted(&mut self, hash: &H, peers: Vec<String>) {
|
||||
self.fire(hash, |watcher| watcher.broadcast(peers));
|
||||
}
|
||||
|
||||
fn fire<F>(&mut self, hash: &H, fun: F) where F: FnOnce(&mut watcher::Sender<H, C::Hash>) {
|
||||
impl<H: hash::Hash + traits::Member, H2: Clone> Listener<H, H2> {
|
||||
fn fire<F>(&mut self, hash: &H, fun: F) where F: FnOnce(&mut watcher::Sender<H, H2>) {
|
||||
let clean = if let Some(h) = self.watchers.get_mut(hash) {
|
||||
fun(h);
|
||||
h.is_done()
|
||||
@@ -73,41 +48,51 @@ impl<H, C> Listener<H, C> where
|
||||
self.watchers.remove(hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<H, T, C> txpool::Listener<T> for Listener<H, C> where
|
||||
H: ::std::hash::Hash + Eq + Copy + fmt::Debug + fmt::LowerHex + Default,
|
||||
T: txpool::VerifiedTransaction<Hash=H>,
|
||||
C: LatestHash,
|
||||
{
|
||||
fn added(&mut self, tx: &Arc<T>, old: Option<&Arc<T>>) {
|
||||
/// Creates a new watcher for given verified extrinsic.
|
||||
///
|
||||
/// The watcher can be used to subscribe to lifecycle events of that extrinsic.
|
||||
pub fn create_watcher(&mut self, hash: H) -> watcher::Watcher<H, H2> {
|
||||
let sender = self.watchers.entry(hash).or_insert_with(watcher::Sender::default);
|
||||
sender.new_watcher()
|
||||
}
|
||||
|
||||
/// Notify the listeners about extrinsic broadcast.
|
||||
pub fn broadcasted(&mut self, hash: &H, peers: Vec<String>) {
|
||||
self.fire(hash, |watcher| watcher.broadcast(peers));
|
||||
}
|
||||
|
||||
/// New transaction was added to the ready pool or promoted from the future pool.
|
||||
pub fn ready(&mut self, tx: &H, old: Option<&H>) {
|
||||
if let Some(old) = old {
|
||||
let hash = tx.hash();
|
||||
self.fire(old.hash(), |watcher| watcher.usurped(*hash));
|
||||
self.fire(old, |watcher| watcher.usurped(tx.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
fn dropped(&mut self, tx: &Arc<T>, by: Option<&T>) {
|
||||
self.fire(tx.hash(), |watcher| match by {
|
||||
Some(t) => watcher.usurped(*t.hash()),
|
||||
/// New transaction was added to the future pool.
|
||||
pub fn future(&mut self, _tx: &H) {
|
||||
}
|
||||
|
||||
/// Transaction was dropped from the pool because of the limit.
|
||||
pub fn dropped(&mut self, tx: &H, by: Option<&H>) {
|
||||
self.fire(tx, |watcher| match by {
|
||||
Some(t) => watcher.usurped(t.clone()),
|
||||
None => watcher.dropped(),
|
||||
})
|
||||
}
|
||||
|
||||
fn rejected(&mut self, tx: &Arc<T>, reason: &txpool::ErrorKind) {
|
||||
warn!(target: "transaction-pool", "Extrinsic rejected ({}): {:?}", reason, tx);
|
||||
/// Transaction was rejected from the pool.
|
||||
pub fn rejected(&mut self, tx: &H, is_invalid: bool) {
|
||||
warn!(target: "transaction-pool", "Extrinsic rejected ({}): {:?}", is_invalid, tx);
|
||||
}
|
||||
|
||||
fn invalid(&mut self, tx: &Arc<T>) {
|
||||
/// Transaction was removed as invalid.
|
||||
pub fn invalid(&mut self, tx: &H) {
|
||||
warn!(target: "transaction-pool", "Extrinsic invalid: {:?}", tx);
|
||||
}
|
||||
|
||||
fn canceled(&mut self, tx: &Arc<T>) {
|
||||
debug!(target: "transaction-pool", "Extrinsic canceled: {:?}", tx);
|
||||
}
|
||||
|
||||
fn culled(&mut self, tx: &Arc<T>) {
|
||||
let header_hash = self.chain.latest_hash();
|
||||
self.fire(tx.hash(), |watcher| watcher.finalised(header_hash))
|
||||
/// Transaction was pruned from the pool.
|
||||
pub fn pruned(&mut self, header_hash: H2, tx: &H) {
|
||||
self.fire(tx, |watcher| watcher.finalised(header_hash))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
hash,
|
||||
sync::Arc,
|
||||
time,
|
||||
};
|
||||
|
||||
use base_pool as base;
|
||||
use error;
|
||||
use listener::Listener;
|
||||
use rotator::PoolRotator;
|
||||
use watcher::Watcher;
|
||||
|
||||
use futures::sync::mpsc;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use sr_primitives::{
|
||||
generic::BlockId,
|
||||
traits::{self, As},
|
||||
transaction_validity::{TransactionValidity, TransactionTag as Tag},
|
||||
};
|
||||
|
||||
/// Modification notification event stream type;
|
||||
pub type EventStream = mpsc::UnboundedReceiver<()>;
|
||||
|
||||
/// Extrinsic hash type for a pool.
|
||||
pub type ExHash<A> = <A as ChainApi>::Hash;
|
||||
/// Block hash type for a pool.
|
||||
pub type BlockHash<A> = <<A as ChainApi>::Block as traits::Block>::Hash;
|
||||
/// Extrinsic type for a pool.
|
||||
pub type ExtrinsicFor<A> = <<A as ChainApi>::Block as traits::Block>::Extrinsic;
|
||||
/// Block number type for the ChainApi
|
||||
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<ExHash<A>, TxData<ExtrinsicFor<A>>>>;
|
||||
|
||||
/// Concrete extrinsic validation and query logic.
|
||||
pub trait ChainApi: Send + Sync {
|
||||
/// Block type.
|
||||
type Block: traits::Block;
|
||||
/// Hash type
|
||||
type Hash: hash::Hash + Eq + traits::Member;
|
||||
/// Error type.
|
||||
type Error: From<error::Error> + error::IntoPoolError;
|
||||
|
||||
/// Verify extrinsic at given block.
|
||||
fn validate_transaction(&self, at: &BlockId<Self::Block>, uxt: &ExtrinsicFor<Self>) -> Result<TransactionValidity, Self::Error>;
|
||||
|
||||
/// Returns a block number given the block id.
|
||||
fn block_id_to_number(&self, at: &BlockId<Self::Block>) -> Result<Option<NumberFor<Self>>, Self::Error>;
|
||||
|
||||
/// Returns a block hash given the block id.
|
||||
fn block_id_to_hash(&self, at: &BlockId<Self::Block>) -> Result<Option<BlockHash<Self>>, Self::Error>;
|
||||
|
||||
/// Hash the extrinsic.
|
||||
fn hash(&self, uxt: &ExtrinsicFor<Self>) -> Self::Hash;
|
||||
}
|
||||
|
||||
/// Maximum time the transaction will be kept in the pool.
|
||||
///
|
||||
/// Transactions that don't get included within the limit are removed from the pool.
|
||||
const POOL_TIME: time::Duration = time::Duration::from_secs(60 * 5);
|
||||
|
||||
/// Additional transaction data
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct TxData<Ex> {
|
||||
/// Raw data stored by the user.
|
||||
pub raw: Ex,
|
||||
/// Transaction validity deadline.
|
||||
/// TODO [ToDr] Should we use longevity instead?
|
||||
#[serde(skip)]
|
||||
pub valid_till: Option<time::Instant>,
|
||||
}
|
||||
|
||||
/// Pool configuration options.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Options;
|
||||
|
||||
/// Extrinsics pool.
|
||||
pub struct Pool<B: ChainApi> {
|
||||
api: B,
|
||||
listener: RwLock<Listener<ExHash<B>, BlockHash<B>>>,
|
||||
pool: RwLock<base::BasePool<
|
||||
ExHash<B>,
|
||||
TxData<ExtrinsicFor<B>>,
|
||||
>>,
|
||||
import_notification_sinks: Mutex<Vec<mpsc::UnboundedSender<()>>>,
|
||||
rotator: PoolRotator<ExHash<B>>,
|
||||
}
|
||||
|
||||
impl<B: ChainApi> Pool<B> {
|
||||
|
||||
/// Imports a bunch of unverified extrinsics to the pool
|
||||
pub fn submit_at<T>(&self, at: &BlockId<B::Block>, xts: T) -> Result<Vec<Result<ExHash<B>, B::Error>>, B::Error> where
|
||||
T: IntoIterator<Item=ExtrinsicFor<B>>
|
||||
{
|
||||
let block_number = self.api.block_id_to_number(at)?
|
||||
.ok_or_else(|| error::ErrorKind::Msg(format!("Invalid block id: {:?}", at)).into())?;
|
||||
|
||||
Ok(xts
|
||||
.into_iter()
|
||||
.map(|xt| -> Result<_, B::Error> {
|
||||
let hash = self.api.hash(&xt);
|
||||
if self.rotator.is_banned(&hash) {
|
||||
return Err(error::ErrorKind::TemporarilyBanned.into())?;
|
||||
}
|
||||
|
||||
match self.api.validate_transaction(at, &xt)? {
|
||||
TransactionValidity::Valid(priority, requires, provides, longevity)=> {
|
||||
Ok(base::Transaction {
|
||||
data: TxData {
|
||||
raw: xt,
|
||||
valid_till: Some(time::Instant::now() + POOL_TIME),
|
||||
},
|
||||
hash,
|
||||
priority,
|
||||
requires,
|
||||
provides,
|
||||
longevity,
|
||||
})
|
||||
},
|
||||
TransactionValidity::Invalid => {
|
||||
bail!(error::Error::from(error::ErrorKind::InvalidTransaction))
|
||||
},
|
||||
TransactionValidity::Unknown => {
|
||||
self.listener.write().rejected(&hash, false);
|
||||
bail!(error::Error::from(error::ErrorKind::UnknownTransactionValidity))
|
||||
},
|
||||
}
|
||||
})
|
||||
.map(|tx| {
|
||||
let imported = self.pool.write().import(block_number.as_(), tx?)?;
|
||||
|
||||
self.import_notification_sinks.lock().retain(|sink| sink.unbounded_send(()).is_ok());
|
||||
|
||||
let mut listener = self.listener.write();
|
||||
fire_events(&mut *listener, &imported);
|
||||
Ok(imported.hash().clone())
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Imports one unverified extrinsic to the pool
|
||||
pub fn submit_one(&self, at: &BlockId<B::Block>, xt: ExtrinsicFor<B>) -> Result<ExHash<B>, B::Error> {
|
||||
Ok(self.submit_at(at, ::std::iter::once(xt))?.pop().expect("One extrinsic passed; one result returned; qed")?)
|
||||
}
|
||||
|
||||
/// Import a single extrinsic and starts to watch their progress in the pool.
|
||||
pub fn submit_and_watch(&self, at: &BlockId<B::Block>, xt: ExtrinsicFor<B>) -> Result<Watcher<ExHash<B>, BlockHash<B>>, B::Error> {
|
||||
let xt = self.submit_one(at, xt)?;
|
||||
Ok(self.listener.write().create_watcher(xt))
|
||||
}
|
||||
|
||||
/// Prunes ready transactions that provide given list of tags.
|
||||
pub fn prune_tags(&self, at: &BlockId<B::Block>, tags: impl IntoIterator<Item=Tag>) -> Result<(), B::Error> {
|
||||
let block_number = self.api.block_id_to_number(at)?
|
||||
.ok_or_else(|| error::ErrorKind::Msg(format!("Invalid block id: {:?}", at)).into())?;
|
||||
|
||||
let status = self.pool.write().prune_tags(block_number.as_(), tags);
|
||||
{
|
||||
let mut listener = self.listener.write();
|
||||
for promoted in &status.promoted {
|
||||
fire_events(&mut *listener, promoted);
|
||||
}
|
||||
for f in &status.failed {
|
||||
listener.dropped(f, None);
|
||||
}
|
||||
}
|
||||
// try to re-submit pruned transactions since some of them might be still valid.
|
||||
let hashes = status.pruned.iter().map(|tx| tx.hash.clone()).collect::<Vec<_>>();
|
||||
let results = self.submit_at(at, status.pruned.into_iter().map(|tx| tx.data.raw.clone()))?;
|
||||
// Fire mined event for transactions that became invalid.
|
||||
let hashes = results.into_iter().enumerate().filter_map(|(idx, r)| match r.map_err(error::IntoPoolError::into_pool_error) {
|
||||
Err(Ok(err)) => match err.kind() {
|
||||
error::ErrorKind::InvalidTransaction => Some(hashes[idx].clone()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
});
|
||||
{
|
||||
let header_hash = self.api.block_id_to_hash(at)?
|
||||
.ok_or_else(|| error::ErrorKind::Msg(format!("Invalid block id: {:?}", at)).into())?;
|
||||
let mut listener = self.listener.write();
|
||||
for h in hashes {
|
||||
listener.pruned(header_hash, &h)
|
||||
}
|
||||
}
|
||||
// clear old transactions
|
||||
self.clear_stale(at)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Removes stale transactions from the pool.
|
||||
///
|
||||
/// Stale transactions are transaction beyond their longevity period.
|
||||
/// Note this function does not remove transactions that are already included in the chain.
|
||||
/// See `prune_tags` ifyou want this.
|
||||
pub fn clear_stale(&self, _at: &BlockId<B::Block>) -> Result<(), B::Error> {
|
||||
let now = time::Instant::now();
|
||||
let to_remove = self.ready(|pending| pending
|
||||
.filter(|tx| self.rotator.ban_if_stale(&now, &tx))
|
||||
.map(|tx| tx.hash.clone())
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
// removing old transactions
|
||||
self.remove_invalid(&to_remove);
|
||||
// clear banned transactions timeouts
|
||||
self.rotator.clear_timeouts(&now);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: ChainApi> Pool<B> {
|
||||
/// Create a new transaction pool.
|
||||
/// TODO [ToDr] Options
|
||||
pub fn new(_options: Options, api: B) -> Self {
|
||||
Pool {
|
||||
api,
|
||||
listener: Default::default(),
|
||||
pool: Default::default(),
|
||||
import_notification_sinks: Default::default(),
|
||||
rotator: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Return an event stream of transactions imported to the pool.
|
||||
pub fn import_notification_stream(&self) -> EventStream {
|
||||
let (sink, stream) = mpsc::unbounded();
|
||||
self.import_notification_sinks.lock().push(sink);
|
||||
stream
|
||||
}
|
||||
|
||||
/// Invoked when extrinsics are broadcasted.
|
||||
pub fn on_broadcasted(&self, propagated: HashMap<ExHash<B>, Vec<String>>) {
|
||||
let mut listener = self.listener.write();
|
||||
for (hash, peers) in propagated.into_iter() {
|
||||
listener.broadcasted(&hash, peers);
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove from the pool.
|
||||
pub fn remove_invalid(&self, hashes: &[ExHash<B>]) -> Vec<TransactionFor<B>> {
|
||||
// temporarily ban invalid transactions
|
||||
debug!(target: "txpool", "Banning invalid transactions: {:?}", hashes);
|
||||
self.rotator.ban(&time::Instant::now(), hashes);
|
||||
|
||||
let invalid = self.pool.write().remove_invalid(hashes);
|
||||
|
||||
let mut listener = self.listener.write();
|
||||
for tx in &invalid {
|
||||
listener.invalid(&tx.hash);
|
||||
}
|
||||
|
||||
invalid
|
||||
}
|
||||
|
||||
/// Get ready transactions ordered by priority
|
||||
pub fn ready<F, X>(&self, f: F) -> X where
|
||||
F: FnOnce(&mut Iterator<Item=TransactionFor<B>>) -> X,
|
||||
{
|
||||
let pool = self.pool.read();
|
||||
let mut ready = pool.ready();
|
||||
f(&mut ready)
|
||||
}
|
||||
|
||||
/// Returns all transactions in the pool.
|
||||
///
|
||||
/// Be careful with large limit values, as querying the entire pool might be time consuming.
|
||||
pub fn all(&self, limit: usize) -> Vec<ExtrinsicFor<B>> {
|
||||
self.ready(|it| it.take(limit).map(|ex| ex.data.raw.clone()).collect())
|
||||
}
|
||||
|
||||
/// Returns pool status.
|
||||
pub fn status(&self) -> base::Status {
|
||||
self.pool.read().status()
|
||||
}
|
||||
|
||||
/// Returns transaction hash
|
||||
pub fn hash_of(&self, xt: &ExtrinsicFor<B>) -> ExHash<B> {
|
||||
self.api.hash(xt)
|
||||
}
|
||||
}
|
||||
|
||||
fn fire_events<H, H2, Ex>(
|
||||
listener: &mut Listener<H, H2>,
|
||||
imported: &base::Imported<H, Ex>,
|
||||
) where
|
||||
H: hash::Hash + Eq + traits::Member,
|
||||
H2: Clone,
|
||||
{
|
||||
match *imported {
|
||||
base::Imported::Ready { ref promoted, ref failed, ref removed, ref hash } => {
|
||||
listener.ready(hash, None);
|
||||
for f in failed {
|
||||
listener.rejected(f, true);
|
||||
}
|
||||
for r in removed {
|
||||
listener.dropped(&r.hash, Some(hash));
|
||||
}
|
||||
for p in promoted {
|
||||
listener.ready(p, None);
|
||||
}
|
||||
},
|
||||
base::Imported::Future { ref hash } => {
|
||||
listener.future(hash)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn should_have_some_basic_tests() {
|
||||
assert_eq!(true, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,579 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet, BTreeSet},
|
||||
cmp,
|
||||
hash,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use sr_primitives::traits::Member;
|
||||
use sr_primitives::transaction_validity::{
|
||||
TransactionTag as Tag,
|
||||
};
|
||||
|
||||
use error;
|
||||
use future::WaitingTransaction;
|
||||
use base_pool::{BlockNumber, Transaction};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TransactionRef<Hash, Ex> {
|
||||
pub transaction: Arc<Transaction<Hash, Ex>>,
|
||||
pub valid_till: BlockNumber,
|
||||
pub insertion_id: u64,
|
||||
}
|
||||
|
||||
impl<Hash, Ex> Clone for TransactionRef<Hash, Ex> {
|
||||
fn clone(&self) -> Self {
|
||||
TransactionRef {
|
||||
transaction: self.transaction.clone(),
|
||||
valid_till: self.valid_till,
|
||||
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)
|
||||
.then(other.valid_till.cmp(&self.valid_till))
|
||||
.then(other.insertion_id.cmp(&self.insertion_id))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Hash, Ex> PartialOrd for TransactionRef<Hash, Ex> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Hash, Ex> PartialEq for TransactionRef<Hash, Ex> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.cmp(other) == cmp::Ordering::Equal
|
||||
}
|
||||
}
|
||||
impl<Hash, Ex> Eq for TransactionRef<Hash, Ex> {}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ReadyTx<Hash, Ex> {
|
||||
/// A reference to a transaction
|
||||
pub transaction: TransactionRef<Hash, Ex>,
|
||||
/// A list of transactions that get unlocked by this one
|
||||
pub unlocks: Vec<Hash>,
|
||||
/// How many required tags are provided inherently
|
||||
///
|
||||
/// Some transactions might be already pruned from the queue,
|
||||
/// so when we compute ready set we may consider this transactions ready earlier.
|
||||
pub requires_offset: usize,
|
||||
}
|
||||
|
||||
const HASH_READY: &str = r#"
|
||||
Every time transaction is imported its hash is placed in `ready` map and tags in `provided_tags`;
|
||||
Every time transaction is removed from the queue we remove the hash from `ready` map and from `provided_tags`;
|
||||
Hence every hash retrieved from `provided_tags` is always present in `ready`;
|
||||
qed
|
||||
"#;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ReadyTransactions<Hash: hash::Hash + Eq, Ex> {
|
||||
/// Insertion id
|
||||
insertion_id: u64,
|
||||
/// tags that are provided by Ready transactions
|
||||
provided_tags: HashMap<Tag, Hash>,
|
||||
/// Transactions that are ready (i.e. don't have any requirements external to the pool)
|
||||
ready: HashMap<Hash, ReadyTx<Hash, Ex>>,
|
||||
// ^^ TODO [ToDr] Consider wrapping this into `Arc<RwLock<>>` and allow multiple concurrent iterators
|
||||
/// Best transactions that are ready to be included to the block without any other previous transaction.
|
||||
best: BTreeSet<TransactionRef<Hash, Ex>>,
|
||||
}
|
||||
|
||||
impl<Hash: hash::Hash + Eq, Ex> Default for ReadyTransactions<Hash, Ex> {
|
||||
fn default() -> Self {
|
||||
ReadyTransactions {
|
||||
insertion_id: Default::default(),
|
||||
provided_tags: Default::default(),
|
||||
ready: Default::default(),
|
||||
best: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Hash: hash::Hash + Member, Ex> ReadyTransactions<Hash, Ex> {
|
||||
/// Borrows a map of tags that are provided by transactions in this queue.
|
||||
pub fn provided_tags(&self) -> &HashMap<Tag, Hash> {
|
||||
&self.provided_tags
|
||||
}
|
||||
|
||||
/// Returns an iterator of ready transactions.
|
||||
///
|
||||
/// 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 returned transactions
|
||||
/// 2. Then by priority:
|
||||
/// - If there are two transactions with all requirements satisfied the one with higher priority goes first.
|
||||
/// 3. Then by the ttl that's left
|
||||
/// - 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<'a>(&'a self) -> impl Iterator<Item=Arc<Transaction<Hash, Ex>>> + 'a {
|
||||
BestIterator {
|
||||
all: &self.ready,
|
||||
best: self.best.clone(),
|
||||
awaiting: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Imports transactions to the pool of ready transactions.
|
||||
///
|
||||
/// The transaction needs to have all tags satisfied (be ready) by transactions
|
||||
/// that are in this queue.
|
||||
pub fn import(
|
||||
&mut self,
|
||||
block_number: BlockNumber,
|
||||
tx: WaitingTransaction<Hash, Ex>,
|
||||
) -> error::Result<Vec<Arc<Transaction<Hash, Ex>>>> {
|
||||
assert!(tx.is_ready(), "Only ready transactions can be imported.");
|
||||
assert!(!self.ready.contains_key(&tx.transaction.hash), "Transaction is already imported.");
|
||||
|
||||
self.insertion_id += 1;
|
||||
let insertion_id = self.insertion_id;
|
||||
let hash = tx.transaction.hash.clone();
|
||||
let tx = tx.transaction;
|
||||
|
||||
let replaced = self.replace_previous(&tx)?;
|
||||
|
||||
let mut goes_to_best = true;
|
||||
// Add links to transactions that unlock the current one
|
||||
for tag in &tx.requires {
|
||||
// Check if the transaction that satisfies the tag is still in the queue.
|
||||
if let Some(other) = self.provided_tags.get(tag) {
|
||||
let mut tx = self.ready.get_mut(other).expect(HASH_READY);
|
||||
tx.unlocks.push(hash.clone());
|
||||
// this transaction depends on some other, so it doesn't go to best directly.
|
||||
goes_to_best = false;
|
||||
}
|
||||
}
|
||||
|
||||
// update provided_tags
|
||||
for tag in tx.provides.clone() {
|
||||
self.provided_tags.insert(tag, hash.clone());
|
||||
}
|
||||
|
||||
let transaction = TransactionRef {
|
||||
insertion_id,
|
||||
valid_till: block_number.saturating_add(tx.longevity),
|
||||
transaction: Arc::new(tx),
|
||||
};
|
||||
|
||||
// insert to best if it doesn't require any other transaction to be included before it
|
||||
if goes_to_best {
|
||||
self.best.insert(transaction.clone());
|
||||
}
|
||||
|
||||
// insert to Ready
|
||||
self.ready.insert(hash, ReadyTx {
|
||||
transaction,
|
||||
unlocks: vec![],
|
||||
requires_offset: 0,
|
||||
});
|
||||
|
||||
Ok(replaced)
|
||||
}
|
||||
|
||||
/// Returns true if given hash is part of the queue.
|
||||
pub fn contains(&self, hash: &Hash) -> bool {
|
||||
self.ready.contains_key(hash)
|
||||
}
|
||||
|
||||
/// Removes invalid transactions from the ready pool.
|
||||
///
|
||||
/// NOTE removing a transaction will also cause a removal of all transactions that depend on that one
|
||||
/// (i.e. the entire subgraph that this transaction is a start of will be removed).
|
||||
/// All removed transactions are returned.
|
||||
pub fn remove_invalid(&mut self, hashes: &[Hash]) -> Vec<Arc<Transaction<Hash, Ex>>> {
|
||||
let mut removed = vec![];
|
||||
let mut to_remove = hashes.iter().cloned().collect::<Vec<_>>();
|
||||
|
||||
loop {
|
||||
let hash = match to_remove.pop() {
|
||||
Some(hash) => hash,
|
||||
None => return removed,
|
||||
};
|
||||
|
||||
if let Some(mut tx) = self.ready.remove(&hash) {
|
||||
// remove entries from provided_tags
|
||||
for tag in &tx.transaction.transaction.provides {
|
||||
self.provided_tags.remove(tag);
|
||||
}
|
||||
// remove from unlocks
|
||||
for tag in &tx.transaction.transaction.requires {
|
||||
if let Some(hash) = self.provided_tags.get(tag) {
|
||||
if let Some(tx) = self.ready.get_mut(hash) {
|
||||
remove_item(&mut tx.unlocks, &hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remove from best
|
||||
self.best.remove(&tx.transaction);
|
||||
|
||||
// remove all transactions that the current one unlocks
|
||||
to_remove.append(&mut tx.unlocks);
|
||||
|
||||
// add to removed
|
||||
debug!(target: "txpool", "[{:?}] Removed as invalid: ", hash);
|
||||
removed.push(tx.transaction.transaction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes transactions that provide given tag.
|
||||
///
|
||||
/// All transactions that lead to a transaction, which provides this tag
|
||||
/// are going to be removed from the queue, but no other transactions are touched -
|
||||
/// i.e. all other subgraphs starting from given tag are still considered valid & ready.
|
||||
pub fn prune_tags(&mut self, tag: Tag) -> Vec<Arc<Transaction<Hash, Ex>>> {
|
||||
let mut removed = vec![];
|
||||
let mut to_remove = vec![tag];
|
||||
|
||||
loop {
|
||||
let tag = match to_remove.pop() {
|
||||
Some(tag) => tag,
|
||||
None => return removed,
|
||||
};
|
||||
|
||||
let res = self.provided_tags.remove(&tag)
|
||||
.and_then(|hash| self.ready.remove(&hash));
|
||||
|
||||
if let Some(tx) = res {
|
||||
let unlocks = tx.unlocks;
|
||||
let tx = tx.transaction.transaction;
|
||||
|
||||
// prune previous transactions as well
|
||||
{
|
||||
let hash = &tx.hash;
|
||||
let mut find_previous = |tag| -> Option<Vec<Tag>> {
|
||||
let prev_hash = self.provided_tags.get(tag)?;
|
||||
let tx2 = self.ready.get_mut(&prev_hash)?;
|
||||
remove_item(&mut tx2.unlocks, hash);
|
||||
// We eagerly prune previous transactions as well.
|
||||
// But it might not always be good.
|
||||
// Possible edge case:
|
||||
// - tx provides two tags
|
||||
// - the second tag enables some subgraph we don't know of yet
|
||||
// - we will prune the transaction
|
||||
// - when we learn about the subgraph it will go to future
|
||||
// - we will have to wait for re-propagation of that transaction
|
||||
// Alternatively the caller may attempt to re-import these transactions.
|
||||
if tx2.unlocks.is_empty() {
|
||||
Some(tx2.transaction.transaction.provides.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
// find previous transactions
|
||||
for tag in &tx.requires {
|
||||
if let Some(mut tags_to_remove) = find_previous(tag) {
|
||||
to_remove.append(&mut tags_to_remove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add the transactions that just got unlocked to `best`
|
||||
for hash in unlocks {
|
||||
if let Some(tx) = self.ready.get_mut(&hash) {
|
||||
tx.requires_offset += 1;
|
||||
// this transaction is ready
|
||||
if tx.requires_offset == tx.transaction.transaction.requires.len() {
|
||||
self.best.insert(tx.transaction.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug!(target: "txpool", "[{:?}] Pruned.", tx.hash);
|
||||
removed.push(tx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the transaction is providing the same tags as other transactions.
|
||||
///
|
||||
/// In case that's true it determines if the priority of transactions that
|
||||
/// we are about to replace is lower than the priority of the replacement transaction.
|
||||
/// We remove/replace old transactions in case they have lower priority.
|
||||
///
|
||||
/// In case replacement is succesful returns a list of removed transactions.
|
||||
fn replace_previous(&mut self, tx: &Transaction<Hash, Ex>) -> error::Result<Vec<Arc<Transaction<Hash, Ex>>>> {
|
||||
let mut to_remove = {
|
||||
// check if we are replacing a transaction
|
||||
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![]);
|
||||
}
|
||||
|
||||
// now check if collective priority is lower than the replacement transaction.
|
||||
let old_priority = replace_hashes
|
||||
.iter()
|
||||
.filter_map(|hash| self.ready.get(hash))
|
||||
.fold(0u64, |total, tx| total.saturating_add(tx.transaction.transaction.priority));
|
||||
|
||||
// bail - the transaction has too low priority to replace the old ones
|
||||
if old_priority >= tx.priority {
|
||||
bail!(error::ErrorKind::TooLowPriority(old_priority, tx.priority))
|
||||
}
|
||||
|
||||
replace_hashes.into_iter().cloned().collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
let new_provides = tx.provides.iter().cloned().collect::<HashSet<_>>();
|
||||
let mut removed = vec![];
|
||||
loop {
|
||||
let hash = match to_remove.pop() {
|
||||
Some(hash) => hash,
|
||||
None => return Ok(removed),
|
||||
};
|
||||
|
||||
let tx = self.ready.remove(&hash).expect(HASH_READY);
|
||||
// check if this transaction provides stuff that is not provided by the new one.
|
||||
let (mut unlocks, tx) = (tx.unlocks, tx.transaction.transaction);
|
||||
{
|
||||
let invalidated = tx.provides
|
||||
.iter()
|
||||
.filter(|tag| !new_provides.contains(&**tag));
|
||||
|
||||
for tag in invalidated {
|
||||
// remove the tag since it's no longer provided by any transaction
|
||||
self.provided_tags.remove(tag);
|
||||
// add more transactions to remove
|
||||
to_remove.append(&mut unlocks);
|
||||
}
|
||||
}
|
||||
|
||||
removed.push(tx);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns number of transactions in this queue.
|
||||
pub fn len(&self) -> usize {
|
||||
self.ready.len()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct BestIterator<'a, Hash: 'a, Ex: 'a> {
|
||||
all: &'a HashMap<Hash, ReadyTx<Hash, Ex>>,
|
||||
awaiting: HashMap<Hash, (usize, TransactionRef<Hash, Ex>)>,
|
||||
best: BTreeSet<TransactionRef<Hash, Ex>>,
|
||||
}
|
||||
|
||||
impl<'a, Hash: 'a + hash::Hash + Member, Ex: 'a> BestIterator<'a, Hash, Ex> {
|
||||
/// Depending on number of satisfied requirements insert given ref
|
||||
/// either to awaiting set or to best set.
|
||||
fn best_or_awaiting(&mut self, satisfied: usize, tx_ref: TransactionRef<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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Hash: 'a + hash::Hash + Member, Ex: 'a> Iterator for BestIterator<'a, Hash, Ex> {
|
||||
type Item = Arc<Transaction<Hash, Ex>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let best = self.best.iter().next_back()?.clone();
|
||||
let best = self.best.take(&best)?;
|
||||
|
||||
let ready = match self.all.get(&best.transaction.hash) {
|
||||
Some(ready) => ready,
|
||||
// The transaction is not in all, maybe it was removed in the meantime?
|
||||
None => return self.next(),
|
||||
};
|
||||
|
||||
// Insert transactions that just got unlocked.
|
||||
for hash in &ready.unlocks {
|
||||
// first check local awaiting transactions
|
||||
if let Some((mut satisfied, tx_ref)) = self.awaiting.remove(hash) {
|
||||
satisfied += 1;
|
||||
self.best_or_awaiting(satisfied, tx_ref);
|
||||
// then get from the pool
|
||||
} else if let Some(next) = self.all.get(hash) {
|
||||
self.best_or_awaiting(next.requires_offset + 1, next.transaction.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Some(best.transaction.clone())
|
||||
}
|
||||
}
|
||||
|
||||
// See: https://github.com/rust-lang/rust/issues/40062
|
||||
fn remove_item<T: PartialEq>(vec: &mut Vec<T>, item: &T) {
|
||||
if let Some(idx) = vec.iter().position(|i| i == item) {
|
||||
vec.swap_remove(idx);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn tx(id: u8) -> Transaction<u64, Vec<u8>> {
|
||||
Transaction {
|
||||
data: vec![id],
|
||||
hash: id as u64,
|
||||
priority: 1,
|
||||
longevity: 2,
|
||||
requires: vec![vec![1], vec![2]],
|
||||
provides: vec![vec![3], vec![4]],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_replace_transaction_that_provides_the_same_tag() {
|
||||
// given
|
||||
let mut ready = ReadyTransactions::default();
|
||||
let block_number = 1;
|
||||
let mut tx1 = tx(1);
|
||||
tx1.requires.clear();
|
||||
let mut tx2 = tx(2);
|
||||
tx2.requires.clear();
|
||||
tx2.provides = vec![vec![3]];
|
||||
let mut tx3 = tx(3);
|
||||
tx3.requires.clear();
|
||||
tx3.provides = vec![vec![4]];
|
||||
|
||||
// when
|
||||
let x = WaitingTransaction::new(tx2, &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap();
|
||||
let x = WaitingTransaction::new(tx3, &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap();
|
||||
assert_eq!(ready.get().count(), 2);
|
||||
|
||||
// too low priority
|
||||
let x = WaitingTransaction::new(tx1.clone(), &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap_err();
|
||||
|
||||
tx1.priority = 10;
|
||||
let x = WaitingTransaction::new(tx1.clone(), &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap();
|
||||
|
||||
// then
|
||||
assert_eq!(ready.get().count(), 1);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn should_return_best_transactions_in_correct_order() {
|
||||
// given
|
||||
let mut ready = ReadyTransactions::default();
|
||||
let mut tx1 = tx(1);
|
||||
tx1.requires.clear();
|
||||
let mut tx2 = tx(2);
|
||||
tx2.requires = tx1.provides.clone();
|
||||
tx2.provides = vec![vec![106]];
|
||||
let mut tx3 = tx(3);
|
||||
tx3.requires = vec![tx1.provides[0].clone(), vec![106]];
|
||||
tx3.provides = vec![];
|
||||
let mut tx4 = tx(4);
|
||||
tx4.requires = vec![tx1.provides[0].clone()];
|
||||
tx4.provides = vec![];
|
||||
let block_number = 1;
|
||||
let tx5 = Transaction {
|
||||
data: vec![5],
|
||||
hash: 5,
|
||||
priority: 1,
|
||||
longevity: u64::max_value(), // use the max_value() here for testing.
|
||||
requires: vec![tx1.provides[0].clone()],
|
||||
provides: vec![],
|
||||
};
|
||||
|
||||
// when
|
||||
let x = WaitingTransaction::new(tx1, &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap();
|
||||
let x = WaitingTransaction::new(tx2, &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap();
|
||||
let x = WaitingTransaction::new(tx3, &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap();
|
||||
let x = WaitingTransaction::new(tx4, &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap();
|
||||
let x = WaitingTransaction::new(tx5, &ready.provided_tags());
|
||||
ready.import(block_number, x).unwrap();
|
||||
|
||||
// then
|
||||
assert_eq!(ready.best.len(), 1);
|
||||
|
||||
let mut it = ready.get().map(|tx| tx.data[0]);
|
||||
|
||||
assert_eq!(it.next(), Some(1));
|
||||
assert_eq!(it.next(), Some(2));
|
||||
assert_eq!(it.next(), Some(3));
|
||||
assert_eq!(it.next(), Some(4));
|
||||
assert_eq!(it.next(), Some(5));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_order_refs() {
|
||||
let mut id = 1;
|
||||
let mut with_priority = |priority| {
|
||||
id += 1;
|
||||
let mut tx = tx(id);
|
||||
tx.priority = priority;
|
||||
tx
|
||||
};
|
||||
// higher priority = better
|
||||
assert!(TransactionRef {
|
||||
transaction: Arc::new(with_priority(3)),
|
||||
valid_till: 3,
|
||||
insertion_id: 1,
|
||||
} > TransactionRef {
|
||||
transaction: Arc::new(with_priority(2)),
|
||||
valid_till: 3,
|
||||
insertion_id: 2,
|
||||
});
|
||||
// lower validity = better
|
||||
assert!(TransactionRef {
|
||||
transaction: Arc::new(with_priority(3)),
|
||||
valid_till: 2,
|
||||
insertion_id: 1,
|
||||
} > TransactionRef {
|
||||
transaction: Arc::new(with_priority(3)),
|
||||
valid_till: 3,
|
||||
insertion_id: 2,
|
||||
});
|
||||
// lower insertion_id = better
|
||||
assert!(TransactionRef {
|
||||
transaction: Arc::new(with_priority(3)),
|
||||
valid_till: 3,
|
||||
insertion_id: 1,
|
||||
} > TransactionRef {
|
||||
transaction: Arc::new(with_priority(3)),
|
||||
valid_till: 3,
|
||||
insertion_id: 2,
|
||||
});
|
||||
}
|
||||
}
|
||||
+36
-30
@@ -21,13 +21,13 @@
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt,
|
||||
hash,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use parking_lot::RwLock;
|
||||
use txpool::VerifiedTransaction;
|
||||
use Verified;
|
||||
|
||||
use base_pool::Transaction;
|
||||
use pool::TxData;
|
||||
|
||||
/// Expected size of the banned extrinsics cache.
|
||||
const EXPECTED_SIZE: usize = 2048;
|
||||
@@ -75,18 +75,19 @@ 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, VEx>(&self, now: &Instant, xt: &Verified<Ex, VEx>) -> bool where
|
||||
VEx: VerifiedTransaction<Hash=Hash>,
|
||||
Hash: fmt::Debug + fmt::LowerHex,
|
||||
{
|
||||
if &xt.valid_till > now {
|
||||
return false;
|
||||
pub fn ban_if_stale<Ex>(&self, now: &Instant, xt: &Transaction<Hash, TxData<Ex>>) -> bool {
|
||||
match xt.data.valid_till {
|
||||
Some(ref valid_till) if valid_till > now => {
|
||||
return false;
|
||||
}
|
||||
_ => {},
|
||||
}
|
||||
|
||||
self.ban(now, &[xt.verified.hash().clone()]);
|
||||
self.ban(now, &[xt.hash.clone()]);
|
||||
true
|
||||
}
|
||||
|
||||
@@ -101,8 +102,9 @@ impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pool::tests::VerifiedTransaction;
|
||||
use test_client::runtime::Hash;
|
||||
|
||||
type Hash = u64;
|
||||
type Ex = ();
|
||||
|
||||
fn rotator() -> PoolRotator<Hash> {
|
||||
PoolRotator {
|
||||
@@ -111,16 +113,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn tx() -> (Hash, Verified<u64, VerifiedTransaction>) {
|
||||
let hash = 5.into();
|
||||
let tx = Verified {
|
||||
original: 5,
|
||||
verified: VerifiedTransaction {
|
||||
hash,
|
||||
sender: Default::default(),
|
||||
nonce: Default::default(),
|
||||
fn tx() -> (Hash, Transaction<Hash, TxData<Ex>>) {
|
||||
let hash = 5u64;
|
||||
let tx = Transaction {
|
||||
data: TxData {
|
||||
raw: (),
|
||||
valid_till: Some(Instant::now()),
|
||||
},
|
||||
valid_till: Instant::now(),
|
||||
hash: hash.clone(),
|
||||
priority: 5,
|
||||
longevity: 3,
|
||||
requires: vec![],
|
||||
provides: vec![],
|
||||
};
|
||||
|
||||
(hash, tx)
|
||||
@@ -175,16 +179,18 @@ mod tests {
|
||||
#[test]
|
||||
fn should_garbage_collect() {
|
||||
// given
|
||||
fn tx_with(i: u64, time: Instant) -> Verified<u64, VerifiedTransaction> {
|
||||
let hash = i.into();
|
||||
Verified {
|
||||
original: i,
|
||||
verified: VerifiedTransaction {
|
||||
hash,
|
||||
sender: Default::default(),
|
||||
nonce: Default::default(),
|
||||
fn tx_with(i: u64, time: Instant) -> Transaction<Hash, TxData<Ex>> {
|
||||
let hash = i;
|
||||
Transaction {
|
||||
data: TxData {
|
||||
raw: (),
|
||||
valid_till: Some(time),
|
||||
},
|
||||
valid_till: time,
|
||||
hash,
|
||||
priority: 5,
|
||||
longevity: 3,
|
||||
requires: vec![],
|
||||
provides: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -25,6 +25,10 @@ use futures::{
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum Status<H, H2> {
|
||||
/// Extrinsic is part of the future queue.
|
||||
Future,
|
||||
/// Extrinsic is part of the ready queue.
|
||||
Ready,
|
||||
/// Extrinsic has been finalised in block with given hash.
|
||||
Finalised(H2),
|
||||
/// Some state change (perhaps another extrinsic was included) rendered this extrinsic invalid.
|
||||
@@ -64,7 +68,7 @@ impl<H, H2> Default for Sender<H, H2> {
|
||||
fn default() -> Self {
|
||||
Sender {
|
||||
receivers: Default::default(),
|
||||
finalised: Default::default(),
|
||||
finalised: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Chain api required for the transaction pool.
|
||||
|
||||
use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
use client::{self, runtime_api::TaggedTransactionQueue};
|
||||
use parity_codec::Encode;
|
||||
use txpool;
|
||||
use substrate_primitives::{
|
||||
H256,
|
||||
Blake2Hasher,
|
||||
Hasher,
|
||||
};
|
||||
use sr_primitives::{
|
||||
generic::BlockId,
|
||||
traits,
|
||||
transaction_validity::TransactionValidity,
|
||||
};
|
||||
|
||||
use error;
|
||||
|
||||
/// The transaction pool logic
|
||||
pub struct ChainApi<B, E, Block: traits::Block> {
|
||||
client: Arc<client::Client<B, E, Block>>,
|
||||
}
|
||||
|
||||
impl<B, E, Block: traits::Block> ChainApi<B, E, Block> {
|
||||
/// Create new transaction pool logic.
|
||||
pub fn new(client: Arc<client::Client<B, E, Block>>) -> Self {
|
||||
ChainApi {
|
||||
client,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, E, Block> txpool::ChainApi for ChainApi<B, E, Block> where
|
||||
Block: traits::Block,
|
||||
B: client::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
|
||||
E: client::CallExecutor<Block, Blake2Hasher> + Send + Sync + Clone + 'static,
|
||||
{
|
||||
type Block = Block;
|
||||
type Hash = H256;
|
||||
type Error = error::Error;
|
||||
|
||||
fn validate_transaction(&self, at: &BlockId<Self::Block>, uxt: &txpool::ExtrinsicFor<Self>) -> error::Result<TransactionValidity> {
|
||||
Ok(self.client.validate_transaction(at, uxt)?)
|
||||
}
|
||||
|
||||
// TODO [toDr] Use proper lbock number type
|
||||
fn block_id_to_number(&self, at: &BlockId<Self::Block>) -> error::Result<Option<txpool::NumberFor<Self>>> {
|
||||
Ok(self.client.block_number_from_id(at)?)
|
||||
}
|
||||
|
||||
fn block_id_to_hash(&self, at: &BlockId<Self::Block>) -> error::Result<Option<txpool::BlockHash<Self>>> {
|
||||
Ok(self.client.block_hash_from_id(at)?)
|
||||
}
|
||||
|
||||
fn hash(&self, ex: &txpool::ExtrinsicFor<Self>) -> Self::Hash {
|
||||
Blake2Hasher::hash(&ex.encode())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,20 +14,23 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! External Error trait for extrinsic pool.
|
||||
//! Transaction pool error.
|
||||
|
||||
use client;
|
||||
use txpool;
|
||||
|
||||
/// Extrinsic pool error.
|
||||
pub trait IntoPoolError: ::std::error::Error + Send + Sized {
|
||||
/// Try to extract original `txpool::Error`
|
||||
///
|
||||
/// This implementation is optional and used only to
|
||||
/// provide more descriptive error messages for end users
|
||||
/// of RPC API.
|
||||
fn into_pool_error(self) -> Result<txpool::Error, Self> { Err(self) }
|
||||
error_chain! {
|
||||
links {
|
||||
Client(client::error::Error, client::error::ErrorKind) #[doc = "Client error"];
|
||||
Pool(txpool::error::Error, txpool::error::ErrorKind) #[doc = "Pool error"];
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPoolError for txpool::Error {
|
||||
fn into_pool_error(self) -> Result<txpool::Error, Self> { Ok(self) }
|
||||
impl txpool::IntoPoolError for Error {
|
||||
fn into_pool_error(self) -> ::std::result::Result<txpool::error::Error, Self> {
|
||||
match self {
|
||||
Error(ErrorKind::Pool(e), c) => Ok(txpool::error::Error(e, c)),
|
||||
e => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,35 +15,31 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// tag::description[]
|
||||
//! Generic extrinsic pool.
|
||||
//! Substrate transaction pool.
|
||||
// end::description[]
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![warn(unused_extern_crates)]
|
||||
|
||||
extern crate futures;
|
||||
extern crate parking_lot;
|
||||
extern crate sr_primitives as runtime_primitives;
|
||||
extern crate parity_codec;
|
||||
extern crate sr_primitives;
|
||||
extern crate substrate_client as client;
|
||||
extern crate substrate_primitives;
|
||||
|
||||
pub extern crate substrate_transaction_graph as txpool;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate transaction_pool as txpool;
|
||||
#[cfg(test)] extern crate substrate_test_client as test_client;
|
||||
#[cfg(test)] extern crate substrate_keyring as keyring;
|
||||
#[cfg(test)] extern crate parity_codec as codec;
|
||||
extern crate error_chain;
|
||||
|
||||
pub mod watcher;
|
||||
mod error;
|
||||
mod listener;
|
||||
mod pool;
|
||||
mod rotator;
|
||||
#[cfg(test)]
|
||||
extern crate substrate_test_client as test_client;
|
||||
#[cfg(test)]
|
||||
extern crate substrate_keyring as keyring;
|
||||
|
||||
pub use listener::Listener;
|
||||
pub use pool::{Pool, ChainApi, EventStream, Verified, VerifiedFor, ExtrinsicFor, ExHash, AllExtrinsics, HashOf};
|
||||
pub use txpool::scoring;
|
||||
pub use txpool::{Error, ErrorKind};
|
||||
pub use error::IntoPoolError;
|
||||
pub use txpool::{Options, Status, LightStatus, VerifiedTransaction, Readiness, Transaction};
|
||||
mod api;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub mod error;
|
||||
|
||||
pub use api::ChainApi;
|
||||
|
||||
@@ -1,626 +0,0 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap},
|
||||
fmt,
|
||||
sync::Arc,
|
||||
time,
|
||||
};
|
||||
use futures::sync::mpsc;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use txpool::{self, Scoring, Readiness};
|
||||
|
||||
use error::IntoPoolError;
|
||||
use listener::{self, Listener};
|
||||
use rotator::PoolRotator;
|
||||
use watcher::Watcher;
|
||||
|
||||
use runtime_primitives::{generic::BlockId, traits};
|
||||
|
||||
/// Modification notification event stream type;
|
||||
pub type EventStream = mpsc::UnboundedReceiver<()>;
|
||||
|
||||
/// Extrinsic hash type for a pool.
|
||||
pub type ExHash<A> = <A as ChainApi>::Hash;
|
||||
/// Extrinsic type for a pool.
|
||||
pub type ExtrinsicFor<A> = <<A as ChainApi>::Block as traits::Block>::Extrinsic;
|
||||
/// Verified extrinsic data for `ChainApi`.
|
||||
pub type VerifiedFor<A> = Verified<ExtrinsicFor<A>, <A as ChainApi>::VEx>;
|
||||
/// A collection of all extrinsics.
|
||||
pub type AllExtrinsics<A> = BTreeMap<<<A as ChainApi>::VEx as txpool::VerifiedTransaction>::Sender, Vec<ExtrinsicFor<A>>>;
|
||||
|
||||
/// Verified extrinsic struct. Wraps original extrinsic and verification info.
|
||||
#[derive(Debug)]
|
||||
pub struct Verified<Ex, VEx> {
|
||||
/// Original extrinsic.
|
||||
pub original: Ex,
|
||||
/// Verification data.
|
||||
pub verified: VEx,
|
||||
/// Pool deadline, after it's reached we remove the extrinsic from the pool.
|
||||
pub valid_till: time::Instant,
|
||||
}
|
||||
|
||||
impl<Ex, VEx> txpool::VerifiedTransaction for Verified<Ex, VEx>
|
||||
where
|
||||
Ex: fmt::Debug,
|
||||
VEx: txpool::VerifiedTransaction,
|
||||
{
|
||||
type Hash = <VEx as txpool::VerifiedTransaction>::Hash;
|
||||
type Sender = <VEx as txpool::VerifiedTransaction>::Sender;
|
||||
|
||||
fn hash(&self) -> &Self::Hash {
|
||||
self.verified.hash()
|
||||
}
|
||||
|
||||
fn sender(&self) -> &Self::Sender {
|
||||
self.verified.sender()
|
||||
}
|
||||
|
||||
fn mem_usage(&self) -> usize {
|
||||
// TODO: add `original` mem usage.
|
||||
self.verified.mem_usage()
|
||||
}
|
||||
}
|
||||
|
||||
/// Concrete extrinsic validation and query logic.
|
||||
pub trait ChainApi: Send + Sync {
|
||||
/// Block type.
|
||||
type Block: traits::Block;
|
||||
/// Extrinsic hash type.
|
||||
type Hash: ::std::hash::Hash + Eq + Copy + fmt::Debug + fmt::LowerHex + Serialize + DeserializeOwned + ::std::str::FromStr + Send + Sync + Default + 'static;
|
||||
/// Extrinsic sender type.
|
||||
type Sender: ::std::hash::Hash + fmt::Debug + Serialize + DeserializeOwned + Eq + Clone + Send + Sync + Ord + Default;
|
||||
/// Unchecked extrinsic type.
|
||||
/// Verified extrinsic type.
|
||||
type VEx: txpool::VerifiedTransaction<Hash=Self::Hash, Sender=Self::Sender> + Send + Sync + Clone;
|
||||
/// Readiness evaluator
|
||||
type Ready;
|
||||
/// Error type.
|
||||
type Error: From<txpool::Error> + IntoPoolError;
|
||||
/// Score type.
|
||||
type Score: ::std::cmp::Ord + Clone + Default + fmt::Debug + Send + Send + Sync + fmt::LowerHex;
|
||||
/// Custom scoring update event type.
|
||||
type Event: ::std::fmt::Debug;
|
||||
|
||||
/// Verify extrinsic at given block.
|
||||
fn verify_transaction(&self, at: &BlockId<Self::Block>, uxt: &ExtrinsicFor<Self>) -> Result<Self::VEx, Self::Error>;
|
||||
|
||||
/// Create new readiness evaluator.
|
||||
fn ready(&self) -> Self::Ready;
|
||||
|
||||
/// Check readiness for verified extrinsic at given block.
|
||||
fn is_ready(&self, at: &BlockId<Self::Block>, context: &mut Self::Ready, xt: &VerifiedFor<Self>) -> Readiness;
|
||||
|
||||
/// Decides on ordering of `T`s from a particular sender.
|
||||
fn compare(old: &VerifiedFor<Self>, other: &VerifiedFor<Self>) -> ::std::cmp::Ordering;
|
||||
|
||||
/// Decides how to deal with two transactions from a sender that seem to occupy the same slot in the queue.
|
||||
fn choose(old: &VerifiedFor<Self>, new: &VerifiedFor<Self>) -> txpool::scoring::Choice;
|
||||
|
||||
/// Updates the transaction scores given a list of transactions and a change to previous scoring.
|
||||
/// NOTE: you can safely assume that both slices have the same length.
|
||||
/// (i.e. score at index `i` represents transaction at the same index)
|
||||
fn update_scores(xts: &[txpool::Transaction<VerifiedFor<Self>>], scores: &mut [Self::Score], change: txpool::scoring::Change<Self::Event>);
|
||||
|
||||
/// Decides if `new` should push out `old` transaction from the pool.
|
||||
///
|
||||
/// NOTE returning `InsertNew` here can lead to some transactions being accepted above pool limits.
|
||||
fn should_replace(old: &VerifiedFor<Self>, new: &VerifiedFor<Self>) -> txpool::scoring::Choice;
|
||||
|
||||
/// Returns hash of the latest block in chain.
|
||||
fn latest_hash(&self) -> HashOf<Self::Block>;
|
||||
}
|
||||
|
||||
/// Returns block's hash type.
|
||||
pub type HashOf<B> = <B as traits::Block>::Hash;
|
||||
|
||||
impl<T: ChainApi> listener::LatestHash for Arc<T> {
|
||||
type Hash = HashOf<T::Block>;
|
||||
|
||||
fn latest_hash(&self) -> HashOf<T::Block> {
|
||||
ChainApi::latest_hash(&**self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Ready<'a, 'b, B: 'a + ChainApi> {
|
||||
api: &'a B,
|
||||
at: &'b BlockId<B::Block>,
|
||||
context: B::Ready,
|
||||
rotator: &'a PoolRotator<B::Hash>,
|
||||
now: time::Instant,
|
||||
}
|
||||
|
||||
impl<'a, 'b, B: ChainApi> txpool::Ready<VerifiedFor<B>> for Ready<'a, 'b, B> {
|
||||
fn is_ready(&mut self, xt: &VerifiedFor<B>) -> Readiness {
|
||||
if self.rotator.ban_if_stale(&self.now, xt) {
|
||||
debug!(target: "transaction-pool", "[{:?}] Banning as stale.", txpool::VerifiedTransaction::hash(xt));
|
||||
return Readiness::Stale;
|
||||
}
|
||||
|
||||
self.api.is_ready(self.at, &mut self.context, xt)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScoringAdapter<T>(::std::marker::PhantomData<T>);
|
||||
|
||||
impl<T> ::std::fmt::Debug for ScoringAdapter<T> {
|
||||
fn fmt(&self, _f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ChainApi> Scoring<VerifiedFor<T>> for ScoringAdapter<T> {
|
||||
type Score = <T as ChainApi>::Score;
|
||||
type Event = <T as ChainApi>::Event;
|
||||
|
||||
fn compare(&self, old: &VerifiedFor<T>, other: &VerifiedFor<T>) -> ::std::cmp::Ordering {
|
||||
T::compare(old, other)
|
||||
}
|
||||
|
||||
fn choose(&self, old: &VerifiedFor<T>, new: &VerifiedFor<T>) -> txpool::scoring::Choice {
|
||||
T::choose(old, new)
|
||||
}
|
||||
|
||||
fn update_scores(&self, xts: &[txpool::Transaction<VerifiedFor<T>>], scores: &mut [Self::Score], change: txpool::scoring::Change<Self::Event>) {
|
||||
T::update_scores(xts, scores, change)
|
||||
}
|
||||
|
||||
fn should_replace(&self, old: &VerifiedFor<T>, new: &VerifiedFor<T>) -> txpool::scoring::Choice {
|
||||
T::should_replace(old, new)
|
||||
}
|
||||
}
|
||||
|
||||
/// Maximum time the transaction will be kept in the pool.
|
||||
///
|
||||
/// Transactions that don't get included within the limit are removed from the pool.
|
||||
const POOL_TIME: time::Duration = time::Duration::from_secs(60 * 5);
|
||||
|
||||
/// Extrinsics pool.
|
||||
pub struct Pool<B: ChainApi> {
|
||||
api: Arc<B>,
|
||||
pool: RwLock<txpool::Pool<
|
||||
VerifiedFor<B>,
|
||||
ScoringAdapter<B>,
|
||||
Listener<B::Hash, Arc<B>>,
|
||||
>>,
|
||||
import_notification_sinks: Mutex<Vec<mpsc::UnboundedSender<()>>>,
|
||||
rotator: PoolRotator<B::Hash>,
|
||||
}
|
||||
|
||||
impl<B: ChainApi> Pool<B> {
|
||||
/// Create a new transaction pool.
|
||||
pub fn new(options: txpool::Options, api: B) -> Self {
|
||||
let api = Arc::new(api);
|
||||
Pool {
|
||||
pool: RwLock::new(txpool::Pool::new(Listener::new(api.clone()), ScoringAdapter::<B>(Default::default()), options)),
|
||||
import_notification_sinks: Default::default(),
|
||||
api,
|
||||
rotator: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Imports a pre-verified extrinsic to the pool.
|
||||
pub fn import(&self, xt: VerifiedFor<B>) -> Result<Arc<VerifiedFor<B>>, B::Error> {
|
||||
let result = self.pool.write().import(xt)?;
|
||||
|
||||
self.import_notification_sinks.lock()
|
||||
.retain(|sink| sink.unbounded_send(()).is_ok());
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Return an event stream of transactions imported to the pool.
|
||||
pub fn import_notification_stream(&self) -> EventStream {
|
||||
let (sink, stream) = mpsc::unbounded();
|
||||
self.import_notification_sinks.lock().push(sink);
|
||||
stream
|
||||
}
|
||||
|
||||
/// Invoked when extrinsics are broadcasted.
|
||||
pub fn on_broadcasted(&self, propagated: HashMap<B::Hash, Vec<String>>) {
|
||||
for (hash, peers) in propagated.into_iter() {
|
||||
self.pool.write().listener_mut().broadcasted(&hash, peers);
|
||||
}
|
||||
}
|
||||
|
||||
/// Imports a bunch of unverified extrinsics to the pool
|
||||
pub fn submit_at<T>(&self, at: &BlockId<B::Block>, xts: T) -> Result<Vec<Arc<VerifiedFor<B>>>, B::Error> where
|
||||
T: IntoIterator<Item=ExtrinsicFor<B>>
|
||||
{
|
||||
xts
|
||||
.into_iter()
|
||||
.map(|xt| {
|
||||
match self.api.verify_transaction(at, &xt) {
|
||||
Ok(ref verified) if self.rotator.is_banned(txpool::VerifiedTransaction::hash(verified)) => {
|
||||
return (Err(txpool::Error::from("Temporarily Banned".to_owned()).into()), xt)
|
||||
},
|
||||
result => (result, xt),
|
||||
}
|
||||
})
|
||||
.map(|(v, xt)| {
|
||||
let xt = Verified {
|
||||
original: xt,
|
||||
verified: v?,
|
||||
valid_till: time::Instant::now() + POOL_TIME,
|
||||
};
|
||||
Ok(self.pool.write().import(xt)?)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Imports one unverified extrinsic to the pool
|
||||
pub fn submit_one(&self, at: &BlockId<B::Block>, xt: ExtrinsicFor<B>) -> Result<Arc<VerifiedFor<B>>, B::Error> {
|
||||
Ok(self.submit_at(at, ::std::iter::once(xt))?.pop().expect("One extrinsic passed; one result returned; qed"))
|
||||
}
|
||||
|
||||
/// Import a single extrinsic and starts to watch their progress in the pool.
|
||||
pub fn submit_and_watch(&self, at: &BlockId<B::Block>, xt: ExtrinsicFor<B>) -> Result<Watcher<B::Hash, HashOf<B::Block>>, B::Error> {
|
||||
let xt = self.submit_at(at, Some(xt))?.pop().expect("One extrinsic passed; one result returned; qed");
|
||||
Ok(self.pool.write().listener_mut().create_watcher(xt))
|
||||
}
|
||||
|
||||
/// Remove from the pool.
|
||||
pub fn remove(&self, hashes: &[B::Hash], is_valid: bool) -> Vec<Option<Arc<VerifiedFor<B>>>> {
|
||||
let mut pool = self.pool.write();
|
||||
let mut results = Vec::with_capacity(hashes.len());
|
||||
|
||||
// temporarily ban invalid transactions
|
||||
if !is_valid {
|
||||
debug!(target: "transaction-pool", "Banning invalid transactions: {:?}", hashes);
|
||||
self.rotator.ban(&time::Instant::now(), hashes);
|
||||
}
|
||||
|
||||
for hash in hashes {
|
||||
results.push(pool.remove(hash, is_valid));
|
||||
}
|
||||
|
||||
results
|
||||
}
|
||||
|
||||
/// Cull transactions from the queue.
|
||||
pub fn cull_from(
|
||||
&self,
|
||||
at: &BlockId<B::Block>,
|
||||
senders: Option<&[<B::VEx as txpool::VerifiedTransaction>::Sender]>,
|
||||
) -> usize
|
||||
{
|
||||
self.rotator.clear_timeouts(&time::Instant::now());
|
||||
let ready = self.ready(at);
|
||||
self.pool.write().cull(senders, ready)
|
||||
}
|
||||
|
||||
/// Cull old transactions from the queue.
|
||||
pub fn cull(&self, at: &BlockId<B::Block>) -> Result<usize, B::Error> {
|
||||
Ok(self.cull_from(at, None))
|
||||
}
|
||||
|
||||
/// Cull transactions from the queue and then compute the pending set.
|
||||
pub fn cull_and_get_pending<F, T>(&self, at: &BlockId<B::Block>, f: F) -> Result<T, B::Error> where
|
||||
F: FnOnce(txpool::PendingIterator<VerifiedFor<B>, Ready<B>, ScoringAdapter<B>, Listener<B::Hash, Arc<B>>>) -> T,
|
||||
{
|
||||
self.cull_from(at, None);
|
||||
Ok(self.pending(at, f))
|
||||
}
|
||||
|
||||
/// Get the full status of the queue (including readiness)
|
||||
pub fn status<R: txpool::Ready<VerifiedFor<B>>>(&self, ready: R) -> txpool::Status {
|
||||
self.pool.read().status(ready)
|
||||
}
|
||||
|
||||
/// Returns light status of the pool.
|
||||
pub fn light_status(&self) -> txpool::LightStatus {
|
||||
self.pool.read().light_status()
|
||||
}
|
||||
|
||||
/// Removes all transactions from given sender
|
||||
pub fn remove_sender(&self, sender: <B::VEx as txpool::VerifiedTransaction>::Sender) -> Vec<Arc<VerifiedFor<B>>> {
|
||||
let mut pool = self.pool.write();
|
||||
let pending = pool.pending_from_sender(|_: &VerifiedFor<B>| txpool::Readiness::Ready, &sender).collect();
|
||||
// remove all transactions from this sender
|
||||
pool.cull(Some(&[sender]), |_: &VerifiedFor<B>| txpool::Readiness::Stale);
|
||||
pending
|
||||
}
|
||||
|
||||
/// Retrieve the pending set. Be careful to not leak the pool `ReadGuard` to prevent deadlocks.
|
||||
pub fn pending<F, T>(&self, at: &BlockId<B::Block>, f: F) -> T where
|
||||
F: FnOnce(txpool::PendingIterator<VerifiedFor<B>, Ready<B>, ScoringAdapter<B>, Listener<B::Hash, Arc<B>>>) -> T,
|
||||
{
|
||||
let ready = self.ready(at);
|
||||
f(self.pool.read().pending(ready))
|
||||
}
|
||||
|
||||
/// Retry to import all verified transactions from given sender.
|
||||
pub fn retry_verification(&self, at: &BlockId<B::Block>, sender: <B::VEx as txpool::VerifiedTransaction>::Sender) -> Result<(), B::Error> {
|
||||
let to_reverify = self.remove_sender(sender);
|
||||
self.submit_at(at, to_reverify.into_iter().map(|ex| Arc::try_unwrap(ex).expect("Removed items have no references").original))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reverify transaction that has been reported incorrect.
|
||||
///
|
||||
/// Returns `Ok(None)` in case the hash is missing, `Err(e)` in case of verification error and new transaction
|
||||
/// reference otherwise.
|
||||
///
|
||||
/// TODO [ToDr] That method is currently unused, should be used together with BlockBuilder
|
||||
/// when we detect that particular transaction has failed.
|
||||
/// In such case we will attempt to remove or re-verify it.
|
||||
pub fn reverify_transaction(&self, at: &BlockId<B::Block>, hash: B::Hash) -> Result<Option<Arc<VerifiedFor<B>>>, B::Error> {
|
||||
let result = self.remove(&[hash], false).pop().expect("One hash passed; one result received; qed");
|
||||
if let Some(ex) = result {
|
||||
self.submit_one(at, Arc::try_unwrap(ex).expect("Removed items have no references").original).map(Some)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve all transactions in the pool grouped by sender.
|
||||
pub fn all(&self) -> AllExtrinsics<B> {
|
||||
use txpool::VerifiedTransaction;
|
||||
let pool = self.pool.read();
|
||||
let all = pool.unordered_pending(AlwaysReady);
|
||||
all.fold(Default::default(), |mut map: AllExtrinsics<B>, tx| {
|
||||
// Map with `null` key is not serializable, so we fallback to default accountId.
|
||||
map.entry(tx.verified.sender().clone())
|
||||
.or_insert_with(Vec::new)
|
||||
// use bytes type to make it serialize nicer.
|
||||
.push(tx.original.clone());
|
||||
map
|
||||
})
|
||||
}
|
||||
|
||||
fn ready<'a, 'b>(&'a self, at: &'b BlockId<B::Block>) -> Ready<'a, 'b, B> {
|
||||
Ready {
|
||||
api: &self.api,
|
||||
rotator: &self.rotator,
|
||||
context: self.api.ready(),
|
||||
at,
|
||||
now: time::Instant::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A Readiness implementation that returns `Ready` for all transactions.
|
||||
pub struct AlwaysReady;
|
||||
impl<VEx> txpool::Ready<VEx> for AlwaysReady {
|
||||
fn is_ready(&mut self, _tx: &VEx) -> txpool::Readiness {
|
||||
txpool::Readiness::Ready
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use txpool;
|
||||
use super::{VerifiedFor, ExtrinsicFor, HashOf};
|
||||
use std::collections::HashMap;
|
||||
use std::cmp::Ordering;
|
||||
use {Pool, ChainApi, scoring, Readiness};
|
||||
use keyring::Keyring::{self, *};
|
||||
use codec::Encode;
|
||||
use test_client::runtime::{AccountId, Block, Hash, Index, Extrinsic, Transfer};
|
||||
use runtime_primitives::{generic, traits::{Hash as HashT, BlindCheckable, BlakeTwo256}};
|
||||
use VerifiedTransaction as VerifiedExtrinsic;
|
||||
|
||||
type BlockId = generic::BlockId<Block>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct VerifiedTransaction {
|
||||
pub hash: Hash,
|
||||
pub sender: AccountId,
|
||||
pub nonce: u64,
|
||||
}
|
||||
|
||||
impl txpool::VerifiedTransaction for VerifiedTransaction {
|
||||
type Hash = Hash;
|
||||
type Sender = AccountId;
|
||||
|
||||
fn hash(&self) -> &Self::Hash {
|
||||
&self.hash
|
||||
}
|
||||
|
||||
fn sender(&self) -> &Self::Sender {
|
||||
&self.sender
|
||||
}
|
||||
|
||||
fn mem_usage(&self) -> usize {
|
||||
256
|
||||
}
|
||||
}
|
||||
|
||||
struct TestApi;
|
||||
|
||||
impl TestApi {
|
||||
fn default() -> Self {
|
||||
TestApi
|
||||
}
|
||||
}
|
||||
|
||||
impl ChainApi for TestApi {
|
||||
type Block = Block;
|
||||
type Hash = Hash;
|
||||
type Sender = AccountId;
|
||||
type Error = txpool::Error;
|
||||
type VEx = VerifiedTransaction;
|
||||
type Ready = HashMap<AccountId, u64>;
|
||||
type Score = u64;
|
||||
type Event = ();
|
||||
|
||||
fn latest_hash(&self) -> HashOf<Self::Block> {
|
||||
1.into()
|
||||
}
|
||||
|
||||
fn verify_transaction(&self, _at: &BlockId, uxt: &ExtrinsicFor<Self>) -> Result<Self::VEx, Self::Error> {
|
||||
let hash = BlakeTwo256::hash(&uxt.encode());
|
||||
let xt = uxt.clone().check()?;
|
||||
Ok(VerifiedTransaction {
|
||||
hash,
|
||||
sender: xt.transfer.from,
|
||||
nonce: xt.transfer.nonce,
|
||||
})
|
||||
}
|
||||
|
||||
fn is_ready(&self, at: &BlockId, nonce_cache: &mut Self::Ready, xt: &VerifiedFor<Self>) -> Readiness {
|
||||
let sender = xt.verified.sender;
|
||||
let next_index = nonce_cache.entry(sender)
|
||||
.or_insert_with(|| index(at, sender));
|
||||
|
||||
let result = match xt.original.transfer.nonce.cmp(&next_index) {
|
||||
Ordering::Greater => Readiness::Future,
|
||||
Ordering::Equal => Readiness::Ready,
|
||||
Ordering::Less => Readiness::Stale,
|
||||
};
|
||||
|
||||
// remember to increment `next_index`
|
||||
*next_index = next_index.saturating_add(1);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn ready(&self) -> Self::Ready {
|
||||
HashMap::default()
|
||||
}
|
||||
|
||||
fn compare(old: &VerifiedFor<Self>, other: &VerifiedFor<Self>) -> Ordering {
|
||||
old.original.transfer.nonce.cmp(&other.original.transfer.nonce)
|
||||
}
|
||||
|
||||
fn choose(old: &VerifiedFor<Self>, new: &VerifiedFor<Self>) -> scoring::Choice {
|
||||
assert!(new.verified.sender == old.verified.sender, "Scoring::choose called with transactions from different senders");
|
||||
if old.original.transfer.nonce == new.original.transfer.nonce {
|
||||
return scoring::Choice::RejectNew;
|
||||
}
|
||||
scoring::Choice::InsertNew
|
||||
}
|
||||
|
||||
fn update_scores(
|
||||
xts: &[txpool::Transaction<VerifiedFor<Self>>],
|
||||
scores: &mut [Self::Score],
|
||||
_change: scoring::Change<()>
|
||||
) {
|
||||
for i in 0..xts.len() {
|
||||
scores[i] = xts[i].original.transfer.amount;
|
||||
}
|
||||
}
|
||||
|
||||
fn should_replace(_old: &VerifiedFor<Self>, _new: &VerifiedFor<Self>) -> scoring::Choice {
|
||||
scoring::Choice::InsertNew
|
||||
}
|
||||
}
|
||||
|
||||
fn index(at: &BlockId, _account: AccountId) -> u64 {
|
||||
(_account[0] as u64) + number_of(at)
|
||||
}
|
||||
|
||||
fn number_of(at: &BlockId) -> u64 {
|
||||
match at {
|
||||
generic::BlockId::Number(n) => *n as u64,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn uxt(who: Keyring, nonce: Index) -> Extrinsic {
|
||||
let transfer = Transfer {
|
||||
from: who.to_raw_public().into(),
|
||||
to: AccountId::default(),
|
||||
nonce,
|
||||
amount: 1,
|
||||
};
|
||||
let signature = transfer.using_encoded(|e| who.sign(e));
|
||||
Extrinsic {
|
||||
transfer,
|
||||
signature: signature.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn pool() -> Pool<TestApi> {
|
||||
Pool::new(Default::default(), TestApi::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn submission_should_work() {
|
||||
let pool = pool();
|
||||
assert_eq!(209, index(&BlockId::number(0), Alice.to_raw_public().into()));
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 209)).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
|
||||
assert_eq!(pending, vec![(Alice.to_raw_public().into(), 209)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_submission_should_work() {
|
||||
let pool = pool();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 209)).unwrap();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 210)).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
|
||||
assert_eq!(pending, vec![(Alice.to_raw_public().into(), 209), (Alice.to_raw_public().into(), 210)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn early_nonce_should_be_culled() {
|
||||
let pool = pool();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 208)).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
|
||||
assert_eq!(pending, vec![]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn late_nonce_should_be_queued() {
|
||||
let pool = pool();
|
||||
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 210)).unwrap();
|
||||
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
|
||||
assert_eq!(pending, vec![]);
|
||||
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 209)).unwrap();
|
||||
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
|
||||
assert_eq!(pending, vec![(Alice.to_raw_public().into(), 209), (Alice.to_raw_public().into(), 210)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retrying_verification_might_not_change_anything() {
|
||||
let pool = pool();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 209)).unwrap();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 210)).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
|
||||
assert_eq!(pending, vec![(Alice.to_raw_public().into(), 209), (Alice.to_raw_public().into(), 210)]);
|
||||
|
||||
pool.retry_verification(&BlockId::number(1), Alice.to_raw_public().into()).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
|
||||
assert_eq!(pending, vec![(Alice.to_raw_public().into(), 209), (Alice.to_raw_public().into(), 210)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_ban_invalid_transactions() {
|
||||
let pool = pool();
|
||||
let uxt = uxt(Alice, 209);
|
||||
let hash = *pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap().hash();
|
||||
pool.remove(&[hash], true);
|
||||
pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap();
|
||||
|
||||
// when
|
||||
pool.remove(&[hash], false);
|
||||
let pending: Vec<AccountId> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| *a.sender()).collect()).unwrap();
|
||||
assert_eq!(pending, vec![]);
|
||||
|
||||
// then
|
||||
pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap_err();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
use super::*;
|
||||
|
||||
use keyring::Keyring::{self, *};
|
||||
use parity_codec::Encode;
|
||||
use txpool::{self, Pool};
|
||||
use test_client::runtime::{AccountId, Block, Hash, Index, Extrinsic, Transfer};
|
||||
use sr_primitives::{
|
||||
generic::{self, BlockId},
|
||||
traits::{Hash as HashT, BlakeTwo256},
|
||||
transaction_validity::TransactionValidity,
|
||||
};
|
||||
|
||||
struct TestApi;
|
||||
|
||||
impl TestApi {
|
||||
fn default() -> Self {
|
||||
TestApi
|
||||
}
|
||||
}
|
||||
|
||||
impl txpool::ChainApi for TestApi {
|
||||
type Block = Block;
|
||||
type Hash = Hash;
|
||||
type Error = error::Error;
|
||||
|
||||
fn validate_transaction(&self, at: &BlockId<Self::Block>, uxt: &txpool::ExtrinsicFor<Self>) -> error::Result<TransactionValidity> {
|
||||
let expected = index(at);
|
||||
let requires = if expected == uxt.transfer.nonce {
|
||||
vec![]
|
||||
} else {
|
||||
vec![vec![uxt.transfer.nonce as u8 - 1]]
|
||||
};
|
||||
let provides = vec![vec![uxt.transfer.nonce as u8]];
|
||||
|
||||
Ok(TransactionValidity::Valid(
|
||||
/* priority: */1,
|
||||
requires,
|
||||
provides,
|
||||
/* longevity: */64
|
||||
))
|
||||
}
|
||||
|
||||
fn block_id_to_number(&self, at: &BlockId<Self::Block>) -> error::Result<Option<txpool::NumberFor<Self>>> {
|
||||
Ok(Some(number_of(at)))
|
||||
}
|
||||
|
||||
fn block_id_to_hash(&self, at: &BlockId<Self::Block>) -> error::Result<Option<txpool::BlockHash<Self>>> {
|
||||
Ok(match at {
|
||||
generic::BlockId::Hash(x) => Some(x.clone()),
|
||||
_ => Some(Default::default()),
|
||||
})
|
||||
}
|
||||
|
||||
fn hash(&self, ex: &txpool::ExtrinsicFor<Self>) -> Self::Hash {
|
||||
BlakeTwo256::hash(&ex.encode())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn index(at: &BlockId<Block>) -> u64 {
|
||||
209 + number_of(at)
|
||||
}
|
||||
|
||||
fn number_of(at: &BlockId<Block>) -> u64 {
|
||||
match at {
|
||||
generic::BlockId::Number(n) => *n as u64,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn uxt(who: Keyring, nonce: Index) -> Extrinsic {
|
||||
let transfer = Transfer {
|
||||
from: who.to_raw_public().into(),
|
||||
to: AccountId::default(),
|
||||
nonce,
|
||||
amount: 1,
|
||||
};
|
||||
let signature = transfer.using_encoded(|e| who.sign(e));
|
||||
Extrinsic {
|
||||
transfer,
|
||||
signature: signature.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn pool() -> Pool<TestApi> {
|
||||
Pool::new(Default::default(), TestApi::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn submission_should_work() {
|
||||
let pool = pool();
|
||||
assert_eq!(209, index(&BlockId::number(0)));
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 209)).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.ready(|p| p.map(|a| a.data.raw.transfer.nonce).collect());
|
||||
assert_eq!(pending, vec![209]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_submission_should_work() {
|
||||
let pool = pool();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 209)).unwrap();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 210)).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.ready(|p| p.map(|a| a.data.raw.transfer.nonce).collect());
|
||||
assert_eq!(pending, vec![209, 210]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn early_nonce_should_be_culled() {
|
||||
let pool = pool();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 208)).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.ready(|p| p.map(|a| a.data.raw.transfer.nonce).collect());
|
||||
assert_eq!(pending, Vec::<Index>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn late_nonce_should_be_queued() {
|
||||
let pool = pool();
|
||||
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 210)).unwrap();
|
||||
let pending: Vec<_> = pool.ready(|p| p.map(|a| a.data.raw.transfer.nonce).collect());
|
||||
assert_eq!(pending, Vec::<Index>::new());
|
||||
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 209)).unwrap();
|
||||
let pending: Vec<_> = pool.ready(|p| p.map(|a| a.data.raw.transfer.nonce).collect());
|
||||
assert_eq!(pending, vec![209, 210]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prune_tags_should_work() {
|
||||
let pool = pool();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 209)).unwrap();
|
||||
pool.submit_one(&BlockId::number(0), uxt(Alice, 210)).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.ready(|p| p.map(|a| a.data.raw.transfer.nonce).collect());
|
||||
assert_eq!(pending, vec![209, 210]);
|
||||
|
||||
pool.prune_tags(&BlockId::number(1), vec![vec![209]]).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.ready(|p| p.map(|a| a.data.raw.transfer.nonce).collect());
|
||||
assert_eq!(pending, vec![210]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_ban_invalid_transactions() {
|
||||
let pool = pool();
|
||||
let uxt = uxt(Alice, 209);
|
||||
let hash = pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap();
|
||||
pool.remove_invalid(&[hash]);
|
||||
pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap_err();
|
||||
|
||||
// when
|
||||
let pending: Vec<_> = pool.ready(|p| p.map(|a| a.data.raw.transfer.nonce).collect());
|
||||
assert_eq!(pending, Vec::<Index>::new());
|
||||
|
||||
// then
|
||||
pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap_err();
|
||||
}
|
||||
Reference in New Issue
Block a user