Unverified Commit f74668b6 authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

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's avatarAndronik Ordian <write@reusable.software>

* Remove explicit lifetime annotation

* Fix warnings and add extra `BlockBuilderExt`
Co-authored-by: Andronik Ordian's avatarAndronik Ordian <write@reusable.software>
parent 4a17a2bc
Pipeline #111786 passed with stages
in 25 minutes and 7 seconds
......@@ -464,7 +464,7 @@ dependencies = [
"cfg-if",
"clang-sys",
"clap",
"env_logger",
"env_logger 0.7.1",
"lazy_static",
"lazycell",
"log 0.4.11",
......@@ -1312,7 +1312,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"humantime 1.3.0",
"log 0.4.11",
"regex",
"termcolor",
]
[[package]]
name = "env_logger"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54532e3223c5af90a6a757c90b5c5521564b07e5e7a958681bcd2afad421cdcd"
dependencies = [
"atty",
"humantime 2.0.1",
"log 0.4.11",
"regex",
"termcolor",
......@@ -1454,7 +1467,7 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b3937f028664bd0e13df401ba49a4567ccda587420365823242977f06609ed1"
dependencies = [
"env_logger",
"env_logger 0.7.1",
"log 0.4.11",
]
......@@ -2268,6 +2281,12 @@ dependencies = [
"quick-error",
]
[[package]]
name = "humantime"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
[[package]]
name = "hyper"
version = "0.12.35"
......@@ -4763,7 +4782,7 @@ version = "0.1.0"
dependencies = [
"assert_matches",
"bitvec",
"env_logger",
"env_logger 0.7.1",
"futures 0.3.5",
"futures-timer 3.0.2",
"log 0.4.11",
......@@ -4794,7 +4813,7 @@ dependencies = [
"assert_matches",
"bitvec",
"derive_more 0.99.11",
"env_logger",
"env_logger 0.7.1",
"futures 0.3.5",
"futures-timer 3.0.2",
"log 0.4.11",
......@@ -4848,7 +4867,7 @@ version = "0.1.0"
dependencies = [
"assert_matches",
"derive_more 0.99.11",
"env_logger",
"env_logger 0.7.1",
"futures 0.3.5",
"futures-timer 3.0.2",
"log 0.4.11",
......@@ -4932,7 +4951,7 @@ version = "0.1.0"
dependencies = [
"assert_matches",
"derive_more 0.99.11",
"env_logger",
"env_logger 0.7.1",
"futures 0.3.5",
"futures-timer 3.0.2",
"kvdb",
......@@ -5177,7 +5196,7 @@ dependencies = [
"assert_matches",
"async-trait",
"derive_more 0.99.11",
"env_logger",
"env_logger 0.7.1",
"futures 0.3.5",
"futures-timer 3.0.2",
"log 0.4.11",
......@@ -5485,7 +5504,7 @@ dependencies = [
name = "polkadot-service"
version = "0.8.3"
dependencies = [
"env_logger",
"env_logger 0.8.1",
"frame-benchmarking",
"frame-system-rpc-runtime-api",
"futures 0.3.5",
......@@ -5506,7 +5525,7 @@ dependencies = [
"polkadot-primitives",
"polkadot-rpc",
"polkadot-runtime",
"polkadot-test-runtime-client",
"polkadot-test-client",
"rococo-v1-runtime",
"sc-authority-discovery",
"sc-block-builder",
......@@ -5581,6 +5600,29 @@ dependencies = [
"sp-core",
]
[[package]]
name = "polkadot-test-client"
version = "0.8.25"
dependencies = [
"parity-scale-codec",
"polkadot-primitives",
"polkadot-test-runtime",
"polkadot-test-service",
"sc-block-builder",
"sc-consensus",
"sc-service",
"sp-api",
"sp-blockchain",
"sp-consensus",
"sp-core",
"sp-inherents",
"sp-keyring",
"sp-runtime",
"sp-state-machine",
"sp-timestamp",
"substrate-test-client",
]
[[package]]
name = "polkadot-test-runtime"
version = "0.8.25"
......@@ -5640,32 +5682,9 @@ dependencies = [
"tiny-keccak 1.5.0",
]
[[package]]
name = "polkadot-test-runtime-client"
version = "2.0.0"
dependencies = [
"futures 0.3.5",
"pallet-timestamp",
"parity-scale-codec",
"polkadot-primitives",
"polkadot-runtime-common",
"polkadot-test-runtime",
"polkadot-test-service",
"sc-block-builder",
"sc-client-api",
"sc-consensus",
"sc-light",
"sc-service",
"sp-api",
"sp-blockchain",
"sp-core",
"sp-runtime",
"substrate-test-client",
]
[[package]]
name = "polkadot-test-service"
version = "0.8.2"
version = "0.8.25"
dependencies = [
"frame-benchmarking",
"frame-system",
......
......@@ -35,7 +35,6 @@ members = [
"runtime/rococo-v1",
"runtime/westend",
"runtime/test-runtime",
"runtime/test-runtime/client",
"statement-table",
"validation",
"xcm",
......@@ -64,8 +63,8 @@ members = [
"node/subsystem",
"node/subsystem-test-helpers",
"node/subsystem-util",
"node/test-service",
"node/test/client",
"node/test/service",
"parachain/test-parachains",
"parachain/test-parachains/adder",
]
......
......@@ -73,9 +73,8 @@ rococo-runtime = { package = "rococo-v1-runtime", path = "../../runtime/rococo-v
westend-runtime = { path = "../../runtime/westend" }
[dev-dependencies]
polkadot-test-runtime-client = { path = "../../runtime/test-runtime/client" }
sc-block-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
env_logger = "0.7.0"
polkadot-test-client = { path = "../test/client" }
env_logger = "0.8.1"
[features]
default = ["db", "full-node"]
......
......@@ -232,37 +232,29 @@ pub(crate) fn kusama_hard_forks() -> Vec<(
#[cfg(test)]
mod tests {
use grandpa::VotingRule;
use polkadot_test_runtime_client::prelude::*;
use polkadot_test_runtime_client::sp_consensus::BlockOrigin;
use sc_block_builder::BlockBuilderProvider;
use polkadot_test_client::{
TestClientBuilder, TestClientBuilderExt, DefaultTestClientBuilderExt, InitPolkadotBlockBuilder,
ClientBlockImportExt,
};
use sp_blockchain::HeaderBackend;
use sp_runtime::generic::BlockId;
use sp_runtime::traits::Header;
use sp_runtime::{generic::BlockId, traits::Header};
use consensus_common::BlockOrigin;
use std::sync::Arc;
#[test]
fn grandpa_pause_voting_rule_works() {
let _ = env_logger::try_init();
let client = Arc::new(polkadot_test_runtime_client::new());
let client = Arc::new(TestClientBuilder::new().build());
let mut push_blocks = {
let mut client = client.clone();
let mut base = 0;
move |n| {
for i in 0..n {
let mut builder = client.new_block(Default::default()).unwrap();
for extrinsic in polkadot_test_runtime_client::needed_extrinsics(base + i) {
builder.push(extrinsic).unwrap()
}
let block = builder.build().unwrap().block;
for _ in 0..n {
let block = client.init_polkadot_block_builder().build().unwrap().block;
client.import(BlockOrigin::Own, block).unwrap();
}
base += n;
}
};
......
[package]
name = "polkadot-test-runtime-client"
version = "2.0.0"
name = "polkadot-test-client"
version = "0.8.25"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "GPL-3.0"
[dependencies]
futures = "0.3.1"
codec = { package = "parity-scale-codec", version = "1.3.4" }
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] }
# Polkadot dependencies
polkadot-test-runtime = { path = "../../../runtime/test-runtime" }
polkadot-test-service = { path = "../service" }
polkadot-primitives = { path = "../../../primitives" }
polkadot-runtime-common = { path = "../../common" }
polkadot-test-runtime = { path = ".." }
polkadot-test-service = { path = "../../../node/test-service" }
# Substrate dependencies
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master" }
substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-block-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-light = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "master", features = ["test-helpers"], default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" }
[dev-dependencies]
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" }
// 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"),
)
}
}
// 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");
}
}
[package]
name = "polkadot-test-service"
version = "0.8.2"
version = "0.8.25"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
......@@ -13,13 +13,13 @@ rand = "0.7.3"
tempfile = "3.1.0"
# Polkadot dependencies
polkadot-overseer = { path = "../overseer" }
polkadot-primitives = { path = "../../primitives" }
polkadot-rpc = { path = "../../rpc" }
polkadot-runtime-common = { path = "../../runtime/common" }
polkadot-service = { path = "../service" }
polkadot-test-runtime = { path = "../../runtime/test-runtime" }
polkadot-runtime-parachains = { path = "../../runtime/parachains" }
polkadot-overseer = { path = "../../overseer" }
polkadot-primitives = { path = "../../../primitives" }
polkadot-rpc = { path = "../../../rpc" }
polkadot-runtime-common = { path = "../../../runtime/common" }
polkadot-service = { path = "../../service" }
polkadot-test-runtime = { path = "../../../runtime/test-runtime" }
polkadot-runtime-parachains = { path = "../../../runtime/parachains" }
# Substrate dependencies
authority-discovery = { package = "sc-authority-discovery", git = "https://github.com/paritytech/substrate", branch = "master" }
......@@ -32,6 +32,7 @@ grandpa = { package = "sc-finality-grandpa", git = "https://github.com/paritytec
grandpa_primitives = { package = "sp-finality-grandpa", git = "https://github.com/paritytech/substrate", branch = "master" }
inherents = { package = "sp-inherents", git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-staking = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-chain-spec = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-cli = { git = "https://github.com/paritytech/substrate", branch = "master" }
......
......@@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Chain specifications for the test runtime.
use babe_primitives::AuthorityId as BabeId;
use grandpa::AuthorityId as GrandpaId;
use pallet_staking::Forcing;
......@@ -21,22 +23,22 @@ use polkadot_primitives::v0::{ValidatorId, AccountId};
use polkadot_service::chain_spec::{get_account_id_from_seed, get_from_seed, Extensions};
use polkadot_test_runtime::constants::currency::DOTS;
use sc_chain_spec::{ChainSpec, ChainType};
use sp_core::{sr25519, ChangesTrieConfiguration};
use sp_core::sr25519;
use sp_runtime::Perbill;
const DEFAULT_PROTOCOL_ID: &str = "dot";
/// The `ChainSpec parametrised for polkadot runtime`.
/// The `ChainSpec` parametrized for polkadot test runtime.
pub type PolkadotChainSpec =
service::GenericChainSpec<polkadot_test_runtime::GenesisConfig, Extensions>;
/// Polkadot local testnet config (multivalidator Alice + Bob)
/// Local testnet config (multivalidator Alice + Bob)
pub fn polkadot_local_testnet_config() -> PolkadotChainSpec {
PolkadotChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
ChainType::Local,
|| polkadot_local_testnet_genesis(None),
|| polkadot_local_testnet_genesis(),
vec![],
None,
Some(DEFAULT_PROTOCOL_ID),
......@@ -45,10 +47,8 @@ pub fn polkadot_local_testnet_config() -> PolkadotChainSpec {
)
}
/// Polkadot local testnet genesis config (multivalidator Alice + Bob)
pub fn polkadot_local_testnet_genesis(
changes_trie_config: Option<ChangesTrieConfiguration>,
) -> polkadot_test_runtime::GenesisConfig {
/// Local testnet genesis config (multivalidator Alice + Bob)
pub fn polkadot_local_testnet_genesis() -> polkadot_test_runtime::GenesisConfig {
polkadot_testnet_genesis(
vec![
get_authority_keys_from_seed("Alice"),
......@@ -57,7 +57,6 @@ pub fn polkadot_local_testnet_genesis(
],
get_account_id_from_seed::<sr25519::Public>("Alice"),
None,
changes_trie_config,
)
}
......@@ -96,7 +95,6 @@ fn polkadot_testnet_genesis(
initial_authorities: Vec<(AccountId, AccountId, BabeId, GrandpaId, ValidatorId)>,
root_key: AccountId,
endowed_accounts: Option<Vec<AccountId>>,
changes_trie_config: Option<ChangesTrieConfiguration>,
) -> polkadot_test_runtime::GenesisConfig {
use polkadot_test_runtime as polkadot;
......@@ -108,7 +106,7 @@ fn polkadot_testnet_genesis(
polkadot::GenesisConfig {
frame_system: Some(polkadot::SystemConfig {