Refactor and test spec block rules (#4670)

* Refactor and test spec block rules

* address review

* Update client/src/block_rules.rs

Co-Authored-By: André Silva <andre.beat@gmail.com>

Co-authored-by: André Silva <andre.beat@gmail.com>
This commit is contained in:
Nikolay Volf
2020-01-23 05:41:22 -08:00
committed by GitHub
parent 8370b99709
commit 81004eabfd
7 changed files with 225 additions and 41 deletions
+28 -13
View File
@@ -21,7 +21,10 @@
pub mod client_ext;
pub use sc_client::{blockchain, self};
pub use sc_client_api::execution_extensions::{ExecutionStrategies, ExecutionExtensions};
pub use sc_client_api::{
execution_extensions::{ExecutionStrategies, ExecutionExtensions},
ForkBlocks, BadBlocks,
};
pub use sc_client_db::{Backend, self};
pub use sp_consensus;
pub use sc_executor::{NativeExecutor, WasmExecutionMethod, self};
@@ -33,7 +36,6 @@ pub use sp_keyring::{
pub use sp_core::{Blake2Hasher, traits::BareCryptoStorePtr};
pub use sp_runtime::{Storage, StorageChild};
pub use sp_state_machine::ExecutionStrategy;
pub use self::client_ext::{ClientExt, ClientBlockImportExt};
use std::sync::Arc;
@@ -61,23 +63,25 @@ impl GenesisInit for () {
}
/// A builder for creating a test client instance.
pub struct TestClientBuilder<Executor, Backend, G: GenesisInit> {
pub struct TestClientBuilder<Block: BlockT, Executor, Backend, G: GenesisInit> {
execution_strategies: ExecutionStrategies,
genesis_init: G,
child_storage_extension: HashMap<Vec<u8>, StorageChild>,
backend: Arc<Backend>,
_executor: std::marker::PhantomData<Executor>,
keystore: Option<BareCryptoStorePtr>,
fork_blocks: ForkBlocks<Block>,
bad_blocks: BadBlocks<Block>,
}
impl<Block: BlockT, Executor, G: GenesisInit> Default
for TestClientBuilder<Executor, Backend<Block>, G> {
for TestClientBuilder<Block, Executor, Backend<Block>, G> {
fn default() -> Self {
Self::with_default_backend()
}
}
impl<Block: BlockT, Executor, G: GenesisInit> TestClientBuilder<Executor, Backend<Block>, G> {
impl<Block: BlockT, Executor, G: GenesisInit> TestClientBuilder<Block, Executor, Backend<Block>, G> {
/// Create new `TestClientBuilder` with default backend.
pub fn with_default_backend() -> Self {
let backend = Arc::new(Backend::new_test(std::u32::MAX, std::u64::MAX));
@@ -91,7 +95,7 @@ impl<Block: BlockT, Executor, G: GenesisInit> TestClientBuilder<Executor, Backen
}
}
impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G> {
impl<Block: BlockT, Executor, Backend, G: GenesisInit> TestClientBuilder<Block, Executor, Backend, G> {
/// Create a new instance of the test client builder.
pub fn with_backend(backend: Arc<Backend>) -> Self {
TestClientBuilder {
@@ -101,6 +105,8 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
genesis_init: Default::default(),
_executor: Default::default(),
keystore: None,
fork_blocks: None,
bad_blocks: None,
}
}
@@ -152,8 +158,18 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
self
}
/// Sets custom block rules.
pub fn set_block_rules(mut self,
fork_blocks: ForkBlocks<Block>,
bad_blocks: BadBlocks<Block>,
) -> Self {
self.fork_blocks = fork_blocks;
self.bad_blocks = bad_blocks;
self
}
/// Build the test client with the given native executor.
pub fn build_with_executor<Block, RuntimeApi>(
pub fn build_with_executor<RuntimeApi>(
self,
executor: Executor,
) -> (
@@ -167,7 +183,6 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
) where
Executor: sc_client::CallExecutor<Block> + 'static,
Backend: sc_client_api::backend::Backend<Block>,
Block: BlockT,
{
let storage = {
@@ -191,8 +206,8 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
self.backend.clone(),
executor,
&storage,
Default::default(),
Default::default(),
self.fork_blocks,
self.bad_blocks,
ExecutionExtensions::new(
self.execution_strategies,
self.keystore.clone(),
@@ -205,13 +220,14 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
}
}
impl<E, Backend, G: GenesisInit> TestClientBuilder<
impl<Block: BlockT, E, Backend, G: GenesisInit> TestClientBuilder<
Block,
sc_client::LocalCallExecutor<Backend, NativeExecutor<E>>,
Backend,
G,
> {
/// Build the test client with the given native executor.
pub fn build_with_native_executor<Block, RuntimeApi, I>(
pub fn build_with_native_executor<RuntimeApi, I>(
self,
executor: I,
) -> (
@@ -226,7 +242,6 @@ impl<E, Backend, G: GenesisInit> TestClientBuilder<
I: Into<Option<NativeExecutor<E>>>,
E: sc_executor::NativeExecutionDispatch + 'static,
Backend: sc_client_api::backend::Backend<Block> + 'static,
Block: BlockT,
{
let executor = executor.into().unwrap_or_else(||
NativeExecutor::new(WasmExecutionMethod::Interpreted, None)