Use DB backend in test client (#1779)

* use DB backend in test client

* Update core/client/db/src/lib.rs

Co-Authored-By: svyatonik <svyatonik@gmail.com>
This commit is contained in:
Svyatoslav Nikolsky
2019-02-15 13:03:22 +03:00
committed by Bastian Köcher
parent cf71350186
commit 6a6c3155a6
9 changed files with 189 additions and 37 deletions
+2
View File
@@ -6,6 +6,8 @@ edition = "2018"
[dependencies]
client = { package = "substrate-client", path = "../client" }
client-db = { package = "substrate-client-db", path = "../client/db", features = ["test-helpers"] }
futures = { version = "0.1.17" }
parity-codec = "3.0"
executor = { package = "substrate-executor", path = "../executor" }
consensus = { package = "substrate-consensus-common", path = "../consensus/common" }
+85 -5
View File
@@ -34,9 +34,10 @@ pub use runtime;
pub use consensus;
use std::sync::Arc;
use futures::future::FutureResult;
use primitives::Blake2Hasher;
use runtime_primitives::StorageOverlay;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, Hash as HashT};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, Hash as HashT, NumberFor};
use runtime::genesismap::{GenesisConfig, additional_storage_with_genesis};
use keyring::Keyring;
use state_machine::ExecutionStrategy;
@@ -59,7 +60,7 @@ mod local_executor {
pub use local_executor::LocalExecutor;
/// Test client database backend.
pub type Backend = client::in_mem::Backend<runtime::Block, Blake2Hasher>;
pub type Backend = client_db::Backend<runtime::Block>;
/// Test client executor.
pub type Executor = client::LocalCallExecutor<
@@ -67,16 +68,60 @@ pub type Executor = client::LocalCallExecutor<
executor::NativeExecutor<LocalExecutor>,
>;
/// Test client light database backend.
pub type LightBackend = client::light::backend::Backend<
client_db::light::LightStorage<runtime::Block>,
LightFetcher,
Blake2Hasher,
>;
/// Test client light fetcher.
pub struct LightFetcher;
/// Test client light executor.
pub type LightExecutor = client::light::call_executor::RemoteOrLocalCallExecutor<
runtime::Block,
LightBackend,
client::light::call_executor::RemoteCallExecutor<
client::light::blockchain::Blockchain<
client_db::light::LightStorage<runtime::Block>,
LightFetcher
>,
LightFetcher
>,
client::LocalCallExecutor<
client::light::backend::Backend<
client_db::light::LightStorage<runtime::Block>,
LightFetcher,
Blake2Hasher
>,
executor::NativeExecutor<LocalExecutor>
>
>;
/// Creates new client instance used for tests.
pub fn new() -> client::Client<Backend, Executor, runtime::Block, runtime::RuntimeApi> {
new_with_backend(Arc::new(Backend::new()), false)
new_with_backend(Arc::new(Backend::new_test(::std::u32::MAX, ::std::u64::MAX)), false)
}
/// Creates new light client instance used for tests.
pub fn new_light() -> client::Client<LightBackend, LightExecutor, runtime::Block, runtime::RuntimeApi> {
let storage = client_db::light::LightStorage::new_test();
let blockchain = Arc::new(client::light::blockchain::Blockchain::new(storage));
let backend = Arc::new(LightBackend::new(blockchain.clone()));
let executor = NativeExecutor::new(None);
let fetcher = Arc::new(LightFetcher);
let remote_call_executor = client::light::call_executor::RemoteCallExecutor::new(blockchain.clone(), fetcher);
let local_call_executor = client::LocalCallExecutor::new(backend.clone(), executor);
let call_executor = LightExecutor::new(backend.clone(), remote_call_executor, local_call_executor);
client::Client::new(backend, call_executor, genesis_storage(false), Default::default()).unwrap()
}
/// Creates new client instance used for tests with the given api execution strategy.
pub fn new_with_execution_strategy(
execution_strategy: ExecutionStrategy
) -> client::Client<Backend, Executor, runtime::Block, runtime::RuntimeApi> {
let backend = Arc::new(Backend::new());
let backend = Arc::new(Backend::new_test(::std::u32::MAX, ::std::u64::MAX));
let executor = NativeExecutor::new(None);
let executor = LocalCallExecutor::new(backend.clone(), executor);
@@ -99,7 +144,7 @@ pub fn new_with_execution_strategy(
pub fn new_with_changes_trie()
-> client::Client<Backend, Executor, runtime::Block, runtime::RuntimeApi>
{
new_with_backend(Arc::new(Backend::new()), true)
new_with_backend(Arc::new(Backend::new_test(::std::u32::MAX, ::std::u64::MAX)), true)
}
/// Creates new client instance used for tests with an explicitly provided backend.
@@ -133,3 +178,38 @@ fn genesis_storage(support_changes_trie: bool) -> StorageOverlay {
storage.extend(additional_storage_with_genesis(&block));
storage
}
impl<Block: BlockT> client::light::fetcher::Fetcher<Block> for LightFetcher {
type RemoteHeaderResult = FutureResult<Block::Header, client::error::Error>;
type RemoteReadResult = FutureResult<Option<Vec<u8>>, client::error::Error>;
type RemoteCallResult = FutureResult<Vec<u8>, client::error::Error>;
type RemoteChangesResult = FutureResult<Vec<(NumberFor<Block>, u32)>, client::error::Error>;
fn remote_header(
&self,
_request: client::light::fetcher::RemoteHeaderRequest<Block::Header>,
) -> Self::RemoteHeaderResult {
unimplemented!("not (yet) used in tests")
}
fn remote_read(
&self,
_request: client::light::fetcher::RemoteReadRequest<Block::Header>,
) -> Self::RemoteReadResult {
unimplemented!("not (yet) used in tests")
}
fn remote_call(
&self,
_request: client::light::fetcher::RemoteCallRequest<Block::Header>,
) -> Self::RemoteCallResult {
unimplemented!("not (yet) used in tests")
}
fn remote_changes(
&self,
_request: client::light::fetcher::RemoteChangesRequest<Block::Header>,
) -> Self::RemoteChangesResult {
unimplemented!("not (yet) used in tests")
}
}