mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 17:31:05 +00:00
Manual Seal (#4143)
* instant/manual seal unbounded queues are evil Apply suggestions from code review Co-Authored-By: Robert Habermeier <rphmeier@gmail.com> add fork tests, docs, remove todos moar docs Update client/consensus/manual-seal/src/rpc.rs Co-Authored-By: Robert Habermeier <rphmeier@gmail.com> remove unbound generic, parameter, docs, deps, code style changes Apply suggestions from code review Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> code style chnges remove unused deps, remove dep renames, check if block is empty before importing, use ? for error propagation fix tests log errors for instant seal use debug code style changes, updated copyright dates use txpool::Pool instead of BasicPool, code style changes fixed tests * fix tests * requested changes from review * check inherents len * rebase
This commit is contained in:
@@ -16,163 +16,19 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
use crate::{BasicPool, MaintainedTransactionPool};
|
||||
use codec::Encode;
|
||||
use futures::executor::block_on;
|
||||
use parking_lot::RwLock;
|
||||
use sc_transaction_graph::{self, ExHash, Pool};
|
||||
use txpool::{self, Pool};
|
||||
use substrate_test_runtime_client::{runtime::Index, AccountKeyring::*};
|
||||
use sp_runtime::{
|
||||
generic::{self, BlockId},
|
||||
traits::{BlakeTwo256, Hash as HashT},
|
||||
transaction_validity::{TransactionValidity, ValidTransaction, TransactionValidityError, InvalidTransaction},
|
||||
generic::BlockId,
|
||||
transaction_validity::ValidTransaction,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use substrate_test_runtime_client::{
|
||||
runtime::{AccountId, Block, BlockNumber, Extrinsic, Hash, Header, Index, Transfer},
|
||||
AccountKeyring::{self, *},
|
||||
};
|
||||
|
||||
struct TestApi {
|
||||
pub modifier: RwLock<Box<dyn Fn(&mut ValidTransaction) + Send + Sync>>,
|
||||
pub chain_block_by_number: RwLock<HashMap<BlockNumber, Vec<Extrinsic>>>,
|
||||
pub chain_headers_by_number: RwLock<HashMap<BlockNumber, Header>>,
|
||||
pub invalid_hashes: RwLock<HashSet<ExHash<Self>>>,
|
||||
pub validation_requests: RwLock<Vec<Extrinsic>>,
|
||||
}
|
||||
|
||||
impl TestApi {
|
||||
fn default() -> Self {
|
||||
TestApi {
|
||||
modifier: RwLock::new(Box::new(|_| {})),
|
||||
chain_block_by_number: RwLock::new(HashMap::new()),
|
||||
invalid_hashes: RwLock::new(HashSet::new()),
|
||||
chain_headers_by_number: RwLock::new(HashMap::new()),
|
||||
validation_requests: RwLock::new(Default::default()),
|
||||
}
|
||||
}
|
||||
|
||||
fn push_block(&self, block_number: BlockNumber, xts: Vec<Extrinsic>) {
|
||||
self.chain_block_by_number.write().insert(block_number, xts);
|
||||
self.chain_headers_by_number.write().insert(block_number, Header {
|
||||
number: block_number,
|
||||
digest: Default::default(),
|
||||
extrinsics_root: Default::default(),
|
||||
parent_hash: Default::default(),
|
||||
state_root: Default::default(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl sc_transaction_graph::ChainApi for TestApi {
|
||||
type Block = Block;
|
||||
type Hash = Hash;
|
||||
type Error = error::Error;
|
||||
type ValidationFuture = futures::future::Ready<error::Result<TransactionValidity>>;
|
||||
type BodyFuture = futures::future::Ready<error::Result<Option<Vec<Extrinsic>>>>;
|
||||
|
||||
fn validate_transaction(
|
||||
&self,
|
||||
at: &BlockId<Self::Block>,
|
||||
uxt: sc_transaction_graph::ExtrinsicFor<Self>,
|
||||
) -> Self::ValidationFuture {
|
||||
|
||||
self.validation_requests.write().push(uxt.clone());
|
||||
|
||||
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]];
|
||||
|
||||
if self.invalid_hashes.read().contains(&self.hash_and_length(&uxt).0) {
|
||||
return futures::future::ready(Ok(
|
||||
Err(TransactionValidityError::Invalid(InvalidTransaction::Custom(0)))
|
||||
))
|
||||
}
|
||||
|
||||
let mut validity = ValidTransaction {
|
||||
priority: 1,
|
||||
requires,
|
||||
provides,
|
||||
longevity: 64,
|
||||
propagate: true,
|
||||
};
|
||||
|
||||
(self.modifier.read())(&mut validity);
|
||||
|
||||
futures::future::ready(Ok(Ok(validity)))
|
||||
}
|
||||
|
||||
fn block_id_to_number(
|
||||
&self,
|
||||
at: &BlockId<Self::Block>,
|
||||
) -> error::Result<Option<sc_transaction_graph::NumberFor<Self>>> {
|
||||
Ok(Some(number_of(at)))
|
||||
}
|
||||
|
||||
fn block_id_to_hash(
|
||||
&self,
|
||||
at: &BlockId<Self::Block>,
|
||||
) -> error::Result<Option<sc_transaction_graph::BlockHash<Self>>> {
|
||||
Ok(match at {
|
||||
generic::BlockId::Hash(x) => Some(x.clone()),
|
||||
_ => Some(Default::default()),
|
||||
})
|
||||
}
|
||||
|
||||
fn hash_and_length(
|
||||
&self,
|
||||
ex: &sc_transaction_graph::ExtrinsicFor<Self>,
|
||||
) -> (Self::Hash, usize) {
|
||||
let encoded = ex.encode();
|
||||
(BlakeTwo256::hash(&encoded), encoded.len())
|
||||
}
|
||||
|
||||
fn block_body(&self, id: &BlockId<Self::Block>) -> Self::BodyFuture {
|
||||
futures::future::ready(Ok(if let BlockId::Number(num) = id {
|
||||
self.chain_block_by_number.read().get(num).cloned()
|
||||
} else {
|
||||
None
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
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: AccountKeyring, nonce: Index) -> Extrinsic {
|
||||
let transfer = Transfer {
|
||||
from: who.into(),
|
||||
to: AccountId::default(),
|
||||
nonce,
|
||||
amount: 1,
|
||||
};
|
||||
let signature = transfer.using_encoded(|e| who.sign(e));
|
||||
Extrinsic::Transfer(transfer, signature.into())
|
||||
}
|
||||
|
||||
fn pool() -> Pool<TestApi> {
|
||||
Pool::new(Default::default(), TestApi::default().into())
|
||||
}
|
||||
|
||||
fn maintained_pool() -> BasicPool<TestApi, Block> {
|
||||
BasicPool::new(Default::default(), TestApi::default())
|
||||
}
|
||||
use testing::*;
|
||||
|
||||
#[test]
|
||||
fn submission_should_work() {
|
||||
let pool = pool();
|
||||
assert_eq!(209, index(&BlockId::number(0)));
|
||||
assert_eq!(209, index(&BlockId::number(0), 209));
|
||||
block_on(pool.submit_one(&BlockId::number(0), uxt(Alice, 209))).unwrap();
|
||||
|
||||
let pending: Vec<_> = pool.ready().map(|a| a.data.transfer().nonce).collect();
|
||||
|
||||
Reference in New Issue
Block a user