Improve and unify testing facilities (#1844)

* Improve and unify testing facilities

This improves the testing facilities by making the test client easier to
use. It also removes code that is not required for the test client.
Besides that it also moves the test service and test client under
`node/test`.

* Update Cargo.lock

* Update node/test/client/src/block_builder.rs

Co-authored-by: Andronik Ordian <write@reusable.software>

* Remove explicit lifetime annotation

* Fix warnings and add extra `BlockBuilderExt`

Co-authored-by: Andronik Ordian <write@reusable.software>
This commit is contained in:
Bastian Köcher
2020-10-25 15:45:30 +01:00
committed by GitHub
parent abb282dfd0
commit 2163988f54
14 changed files with 423 additions and 505 deletions
@@ -0,0 +1,102 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use crate::{Client, FullBackend};
use polkadot_test_runtime::{GetLastTimestamp, UncheckedExtrinsic};
use polkadot_primitives::v1::Block;
use sp_runtime::generic::BlockId;
use sp_api::ProvideRuntimeApi;
use sc_block_builder::{BlockBuilderProvider, BlockBuilder};
use sp_state_machine::BasicExternalities;
use codec::{Encode, Decode};
/// An extension for the test client to init a Polkadot specific block builder.
pub trait InitPolkadotBlockBuilder {
/// Init a Polkadot specific block builder that works for the test runtime.
///
/// This will automatically create and push the inherents for you to make the block valid for the test runtime.
fn init_polkadot_block_builder(&self) -> sc_block_builder::BlockBuilder<Block, Client, FullBackend>;
/// Init a Polkadot specific block builder at a specific block that works for the test runtime.
///
/// Same as [`InitPolkadotBlockBuilder::init_polkadot_block_builder`] besides that it takes a [`BlockId`] to say
/// which should be the parent block of the block that is being build.
fn init_polkadot_block_builder_at(
&self,
at: &BlockId<Block>,
) -> sc_block_builder::BlockBuilder<Block, Client, FullBackend>;
}
impl InitPolkadotBlockBuilder for Client {
fn init_polkadot_block_builder(
&self,
) -> BlockBuilder<Block, Client, FullBackend> {
let chain_info = self.chain_info();
self.init_polkadot_block_builder_at(&BlockId::Hash(chain_info.best_hash))
}
fn init_polkadot_block_builder_at(
&self,
at: &BlockId<Block>,
) -> BlockBuilder<Block, Client, FullBackend> {
let mut block_builder = self.new_block_at(at, Default::default(), false)
.expect("Creates new block builder for test runtime");
let mut inherent_data = sp_inherents::InherentData::new();
let last_timestamp = self
.runtime_api()
.get_last_timestamp(&at)
.expect("Get last timestamp");
// `MinimumPeriod` is a storage parameter type that requires externalities to access the value.
let minimum_period = BasicExternalities::new_empty()
.execute_with(|| polkadot_test_runtime::MinimumPeriod::get());
let timestamp = last_timestamp + minimum_period;
inherent_data
.put_data(sp_timestamp::INHERENT_IDENTIFIER, &timestamp)
.expect("Put timestamp failed");
let inherents = block_builder.create_inherents(inherent_data).expect("Creates inherents");
inherents.into_iter().for_each(|ext| block_builder.push(ext).expect("Pushes inherent"));
block_builder
}
}
/// Polkadot specific extensions for the [`BlockBuilder`].
pub trait BlockBuilderExt {
/// Push a Polkadot test runtime specific extrinsic to the block.
///
/// This will internally use the [`BlockBuilder::push`] method, but this method expects a opaque extrinsic. So,
/// we provide this wrapper which converts a test runtime specific extrinsic to a opaque extrinsic and pushes it to
/// the block.
///
/// Returns the result of the application of the extrinsic.
fn push_polkadot_extrinsic(&mut self, ext: UncheckedExtrinsic) -> Result<(), sp_blockchain::Error>;
}
impl BlockBuilderExt for BlockBuilder<'_, Block, Client, FullBackend> {
fn push_polkadot_extrinsic(&mut self, ext: UncheckedExtrinsic) -> Result<(), sp_blockchain::Error> {
let encoded = ext.encode();
self.push(
Decode::decode(&mut &encoded[..])
.expect("The runtime specific extrinsic always decodes to an opaque extrinsic; qed"),
)
}
}
+117
View File
@@ -0,0 +1,117 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! A Polkadot test client.
//!
//! This test client is using the Polkadot test runtime.
mod block_builder;
use polkadot_primitives::v1::Block;
use sc_service::client;
use sp_core::storage::Storage;
use sp_runtime::BuildStorage;
pub use block_builder::*;
pub use substrate_test_client::*;
pub use polkadot_test_service::{
Client, construct_extrinsic, construct_transfer_extrinsic, PolkadotTestExecutor, FullBackend,
};
pub use polkadot_test_runtime as runtime;
/// Test client executor.
pub type Executor = client::LocalCallExecutor<FullBackend, sc_executor::NativeExecutor<PolkadotTestExecutor>>;
/// Test client builder for Polkadot.
pub type TestClientBuilder = substrate_test_client::TestClientBuilder<Block, Executor, FullBackend, GenesisParameters>;
/// LongestChain type for the test runtime/client.
pub type LongestChain = sc_consensus::LongestChain<FullBackend, Block>;
/// Parameters of test-client builder with test-runtime.
#[derive(Default)]
pub struct GenesisParameters;
impl substrate_test_client::GenesisInit for GenesisParameters {
fn genesis_storage(&self) -> Storage {
polkadot_test_service::chain_spec::polkadot_local_testnet_genesis()
.build_storage()
.expect("Builds test runtime genesis storage")
}
}
/// A `test-runtime` extensions to `TestClientBuilder`.
pub trait TestClientBuilderExt: Sized {
/// Build the test client.
fn build(self) -> Client {
self.build_with_longest_chain().0
}
/// Build the test client and longest chain selector.
fn build_with_longest_chain(self) -> (Client, LongestChain);
}
impl TestClientBuilderExt for TestClientBuilder {
fn build_with_longest_chain(self) -> (Client, LongestChain) {
self.build_with_native_executor(None)
}
}
/// A `TestClientBuilder` with default backend and executor.
pub trait DefaultTestClientBuilderExt: Sized {
/// Create new `TestClientBuilder`
fn new() -> Self;
}
impl DefaultTestClientBuilderExt for TestClientBuilder {
fn new() -> Self {
Self::with_default_backend()
}
}
#[cfg(test)]
mod tests{
use super::*;
use sp_consensus::BlockOrigin;
#[test]
fn ensure_test_client_can_build_and_import_block() {
let mut client = TestClientBuilder::new().build();
let block_builder = client.init_polkadot_block_builder();
let block = block_builder.build().expect("Finalizes the block").block;
client.import(BlockOrigin::Own, block).expect("Imports the block");
}
#[test]
fn ensure_test_client_can_push_extrinsic() {
let mut client = TestClientBuilder::new().build();
let transfer = construct_transfer_extrinsic(
&client,
sp_keyring::Sr25519Keyring::Alice,
sp_keyring::Sr25519Keyring::Bob,
1000,
);
let mut block_builder = client.init_polkadot_block_builder();
block_builder.push_polkadot_extrinsic(transfer).expect("Pushes extrinsic");
let block = block_builder.build().expect("Finalizes the block").block;
client.import(BlockOrigin::Own, block).expect("Imports the block");
}
}