Adds fork-awareness and finalization notifications to transaction pool watchers. (#4740)

* adds finalization support to sc-transaction-pool using MaintainedTransactionPool for finalization events

* adds TransactionStatus::Retracted, notify watchers of retracted blocks, finalized now finalizes, transactions for current finalized -> last finalized block

* adds last_finalized to ChainApi, use generic BlockT for ChainEvent

* fix tests

* Apply suggestions from code review

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* tests

* fix tests, docs, lazily dedupe pruned hashes

* fix tests, Cargo.lock

* Apply suggestions from code review

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* remove tree_route, last_finalized from ChainApi, add block hash to Finalization and Retracted events

* prune finality watchers

* fix tests

* remove HeaderBackend bound from FullChainApi

* code style nits, terminate stream in finality_timeout

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
Seun Lanlege
2020-02-14 14:44:58 +01:00
committed by GitHub
parent b999911bcf
commit d3a3e288b6
18 changed files with 639 additions and 283 deletions
@@ -50,7 +50,7 @@ impl std::error::Error for Error {
}
#[derive(Default)]
struct ChainState {
pub struct ChainState {
pub block_by_number: HashMap<BlockNumber, Vec<Extrinsic>>,
pub block_by_hash: HashMap<Hash, Vec<Extrinsic>>,
pub header_by_number: HashMap<BlockNumber, Header>,
@@ -96,16 +96,24 @@ impl TestApi {
}
/// Push block as a part of canonical chain under given number.
pub fn push_block(&self, block_number: BlockNumber, xts: Vec<Extrinsic>) {
pub fn push_block(&self, block_number: BlockNumber, xts: Vec<Extrinsic>) -> Header {
let mut chain = self.chain.write();
chain.block_by_number.insert(block_number, xts);
chain.header_by_number.insert(block_number, Header {
chain.block_by_number.insert(block_number, xts.clone());
let header = Header {
number: block_number,
digest: Default::default(),
extrinsics_root: Default::default(),
parent_hash: Default::default(),
parent_hash: block_number
.checked_sub(1)
.and_then(|num| {
chain.header_by_number.get(&num)
.cloned().map(|h| h.hash())
}).unwrap_or_default(),
state_root: Default::default(),
});
};
chain.block_by_hash.insert(header.hash(), xts);
chain.header_by_number.insert(block_number, header.clone());
header
}
/// Push a block without a number.
@@ -116,6 +124,20 @@ impl TestApi {
chain.block_by_hash.insert(block_hash, xts);
}
pub fn push_fork_block_with_parent(&self, parent: Hash, xts: Vec<Extrinsic>) -> Header {
let mut chain = self.chain.write();
let blocknum = chain.block_by_number.keys().max().expect("block_by_number shouldn't be empty");
let header = Header {
number: *blocknum,
digest: Default::default(),
extrinsics_root: Default::default(),
parent_hash: parent,
state_root: Default::default(),
};
chain.block_by_hash.insert(header.hash(), xts);
header
}
fn hash_and_length_inner(ex: &Extrinsic) -> (Hash, usize) {
let encoded = ex.encode();
(BlakeTwo256::hash(&encoded), encoded.len())
@@ -136,6 +158,11 @@ impl TestApi {
self.validation_requests.read().clone()
}
/// get a reference to the chain state
pub fn chain(&self) -> &RwLock<ChainState> {
&self.chain
}
/// Increment nonce in the inner state.
pub fn increment_nonce(&self, account: AccountId) {
let mut chain = self.chain.write();
@@ -197,7 +224,12 @@ impl sc_transaction_graph::ChainApi for TestApi {
) -> Result<Option<sc_transaction_graph::BlockHash<Self>>, Error> {
Ok(match at {
generic::BlockId::Hash(x) => Some(x.clone()),
_ => Some(Default::default()),
generic::BlockId::Number(num) => {
self.chain.read()
.header_by_number.get(num)
.map(|h| h.hash())
.or_else(|| Some(Default::default()))
},
})
}
@@ -209,10 +241,9 @@ impl sc_transaction_graph::ChainApi for TestApi {
}
fn block_body(&self, id: &BlockId<Self::Block>) -> Self::BodyFuture {
futures::future::ready(Ok(if let BlockId::Number(num) = id {
self.chain.read().block_by_number.get(num).cloned()
} else {
None
futures::future::ready(Ok(match id {
BlockId::Number(num) => self.chain.read().block_by_number.get(num).cloned(),
BlockId::Hash(hash) => self.chain.read().block_by_hash.get(hash).cloned(),
}))
}
}