test runner (#7665)

This commit is contained in:
Seun Lanlege
2021-03-24 11:28:26 +01:00
committed by GitHub
parent aeff56260c
commit 4ac621292a
11 changed files with 1502 additions and 157 deletions
+271 -154
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -6,6 +6,7 @@ members = [
"bin/node/bench",
"bin/node/browser-testing",
"bin/node/cli",
"bin/node/test-runner-example",
"bin/node/executor",
"bin/node/primitives",
"bin/node/rpc",
@@ -184,6 +185,7 @@ members = [
"test-utils/runtime",
"test-utils/runtime/client",
"test-utils/runtime/transaction-pool",
"test-utils/test-runner",
"test-utils/test-crate",
"utils/browser",
"utils/build-script-utils",
@@ -0,0 +1,40 @@
[package]
name = "test-runner-example"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
publish = false
[dependencies]
test-runner = { path = "../../../test-utils/test-runner", version = "0.9.0" }
frame-system = { version = "3.0.0", path = "../../../frame/system" }
frame-support = { path = "../../../frame/support", version = "3.0.0" }
frame-benchmarking = { path = "../../../frame/benchmarking", version = "3.0.0" }
pallet-balances = { path = "../../../frame/balances", version = "3.0.0" }
pallet-sudo = { path = "../../../frame/sudo", version = "3.0.0" }
pallet-transaction-payment = { version = "3.0.0", path = "../../../frame/transaction-payment" }
node-runtime = { path = "../runtime", version = "2.0.1" }
node-primitives = { version = "2.0.0", path = "../primitives" }
node-cli = { path = "../cli", version = "2.0.0" }
grandpa = { version = "0.9.0", package = "sc-finality-grandpa", path = "../../../client/finality-grandpa" }
sp-consensus-babe = { version = "0.9.0", path = "../../../primitives/consensus/babe" }
sc-consensus-babe = { version = "0.9.0", path = "../../../client/consensus/babe" }
sc-consensus-manual-seal = { version = "0.9.0", path = "../../../client/consensus/manual-seal" }
sc-service = { version = "0.9.0", default-features = false, path = "../../../client/service" }
sc-executor = { version = "0.9.0", path = "../../../client/executor" }
sc-client-api = { version = "3.0.0", path = "../../../client/api" }
sc-network = { version = "0.9.0", path = "../../../client/network" }
sc-informant = { version = "0.9.0", path = "../../../client/informant" }
sc-consensus = { version = "0.9.0", path = "../../../client/consensus/common" }
sp-runtime = { path = "../../../primitives/runtime", version = "3.0.0" }
sp-keyring = { version = "3.0.0", path = "../../../primitives/keyring" }
sp-api = { version = "3.0.0", path = "../../../primitives/api" }
sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" }
sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" }
rand = "0.8.3"
log = "0.4.14"
@@ -0,0 +1,202 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
//! Basic example of end to end runtime tests.
use test_runner::{Node, ChainInfo, SignatureVerificationOverride};
use grandpa::GrandpaBlockImport;
use sc_service::{TFullBackend, TFullClient, Configuration, TaskManager, new_full_parts};
use std::sync::Arc;
use sp_inherents::InherentDataProviders;
use sc_consensus_babe::BabeBlockImport;
use sp_keystore::SyncCryptoStorePtr;
use sp_keyring::sr25519::Keyring::Alice;
use sp_consensus_babe::AuthorityId;
use sc_consensus_manual_seal::{ConsensusDataProvider, consensus::babe::BabeConsensusDataProvider};
use sp_runtime::{traits::IdentifyAccount, MultiSigner, generic::Era};
type BlockImport<B, BE, C, SC> = BabeBlockImport<B, C, GrandpaBlockImport<BE, B, C, SC>>;
sc_executor::native_executor_instance!(
pub Executor,
node_runtime::api::dispatch,
node_runtime::native_version,
(
frame_benchmarking::benchmarking::HostFunctions,
SignatureVerificationOverride,
)
);
/// ChainInfo implementation.
struct NodeTemplateChainInfo;
impl ChainInfo for NodeTemplateChainInfo {
type Block = node_primitives::Block;
type Executor = Executor;
type Runtime = node_runtime::Runtime;
type RuntimeApi = node_runtime::RuntimeApi;
type SelectChain = sc_consensus::LongestChain<TFullBackend<Self::Block>, Self::Block>;
type BlockImport = BlockImport<
Self::Block,
TFullBackend<Self::Block>,
TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>,
Self::SelectChain,
>;
type SignedExtras = node_runtime::SignedExtra;
fn signed_extras(from: <Self::Runtime as frame_system::Config>::AccountId) -> Self::SignedExtras {
(
frame_system::CheckSpecVersion::<Self::Runtime>::new(),
frame_system::CheckTxVersion::<Self::Runtime>::new(),
frame_system::CheckGenesis::<Self::Runtime>::new(),
frame_system::CheckMortality::<Self::Runtime>::from(Era::Immortal),
frame_system::CheckNonce::<Self::Runtime>::from(frame_system::Pallet::<Self::Runtime>::account_nonce(from)),
frame_system::CheckWeight::<Self::Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Self::Runtime>::from(0),
)
}
fn create_client_parts(
config: &Configuration,
) -> Result<
(
Arc<TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>>,
Arc<TFullBackend<Self::Block>>,
SyncCryptoStorePtr,
TaskManager,
InherentDataProviders,
Option<
Box<
dyn ConsensusDataProvider<
Self::Block,
Transaction = sp_api::TransactionFor<
TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>,
Self::Block,
>,
>,
>,
>,
Self::SelectChain,
Self::BlockImport,
),
sc_service::Error,
> {
let (client, backend, keystore, task_manager) =
new_full_parts::<Self::Block, Self::RuntimeApi, Self::Executor>(config, None)?;
let client = Arc::new(client);
let inherent_providers = InherentDataProviders::new();
let select_chain = sc_consensus::LongestChain::new(backend.clone());
let (grandpa_block_import, ..) =
grandpa::block_import(
client.clone(),
&(client.clone() as Arc<_>),
select_chain.clone(),
None
)?;
let (block_import, babe_link) = sc_consensus_babe::block_import(
sc_consensus_babe::Config::get_or_compute(&*client)?,
grandpa_block_import,
client.clone(),
)?;
let consensus_data_provider = BabeConsensusDataProvider::new(
client.clone(),
keystore.sync_keystore(),
&inherent_providers,
babe_link.epoch_changes().clone(),
vec![(AuthorityId::from(Alice.public()), 1000)],
)
.expect("failed to create ConsensusDataProvider");
Ok((
client,
backend,
keystore.sync_keystore(),
task_manager,
inherent_providers,
Some(Box::new(consensus_data_provider)),
select_chain,
block_import,
))
}
fn dispatch_with_root(call: <Self::Runtime as frame_system::Config>::Call, node: &mut Node<Self>) {
let alice = MultiSigner::from(Alice.public()).into_account();
let call = pallet_sudo::Call::sudo(Box::new(call));
node.submit_extrinsic(call, alice);
node.seal_blocks(1);
}
}
#[cfg(test)]
mod tests {
use super::*;
use test_runner::NodeConfig;
use log::LevelFilter;
use sc_client_api::execution_extensions::ExecutionStrategies;
use node_cli::chain_spec::development_config;
#[test]
fn test_runner() {
let config = NodeConfig {
execution_strategies: ExecutionStrategies {
syncing: sc_client_api::ExecutionStrategy::AlwaysWasm,
importing: sc_client_api::ExecutionStrategy::AlwaysWasm,
block_construction: sc_client_api::ExecutionStrategy::AlwaysWasm,
offchain_worker: sc_client_api::ExecutionStrategy::AlwaysWasm,
other: sc_client_api::ExecutionStrategy::AlwaysWasm,
},
chain_spec: Box::new(development_config()),
log_targets: vec![
("yamux", LevelFilter::Off),
("multistream_select", LevelFilter::Off),
("libp2p", LevelFilter::Off),
("jsonrpc_client_transports", LevelFilter::Off),
("sc_network", LevelFilter::Off),
("tokio_reactor", LevelFilter::Off),
("parity-db", LevelFilter::Off),
("sub-libp2p", LevelFilter::Off),
("sync", LevelFilter::Off),
("peerset", LevelFilter::Off),
("ws", LevelFilter::Off),
("sc_network", LevelFilter::Off),
("sc_service", LevelFilter::Off),
("sc_basic_authorship", LevelFilter::Off),
("telemetry-logger", LevelFilter::Off),
("sc_peerset", LevelFilter::Off),
("rpc", LevelFilter::Off),
("runtime", LevelFilter::Trace),
("babe", LevelFilter::Debug)
],
};
let mut node = Node::<NodeTemplateChainInfo>::new(config).unwrap();
// seals blocks
node.seal_blocks(1);
// submit extrinsics
let alice = MultiSigner::from(Alice.public()).into_account();
node.submit_extrinsic(frame_system::Call::remark((b"hello world").to_vec()), alice);
// look ma, I can read state.
let _events = node.with_state(|| frame_system::Pallet::<node_runtime::Runtime>::events());
// get access to the underlying client.
let _client = node.client();
}
}
@@ -274,10 +274,20 @@ impl<D: NativeExecutionDispatch> NativeExecutor<D> {
default_heap_pages: Option<u64>,
max_runtime_instances: usize,
) -> Self {
let mut host_functions = D::ExtendHostFunctions::host_functions();
let extended = D::ExtendHostFunctions::host_functions();
let mut host_functions = sp_io::SubstrateHostFunctions::host_functions()
.into_iter()
// filter out any host function overrides provided.
.filter(|host_fn| {
extended.iter()
.find(|ext_host_fn| host_fn.name() == ext_host_fn.name())
.is_none()
})
.collect::<Vec<_>>();
// Add the custom host functions provided by the user.
host_functions.extend(sp_io::SubstrateHostFunctions::host_functions());
host_functions.extend(extended);
let wasm_executor = WasmExecutor::new(
fallback_method,
default_heap_pages,
-1
View File
@@ -356,7 +356,6 @@ fn can_fetch_current_and_next_epoch_data() {
Babe::current_epoch().authorities,
Babe::next_epoch().authorities,
);
// 1 era = 3 epochs
// 1 epoch = 3 slots
// Eras start from 0.
@@ -0,0 +1,61 @@
[package]
name = "test-runner"
version = "0.9.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
publish = false
[dependencies]
# client deps
sc-executor = { version = "0.9.0", path = "../../client/executor" }
sc-service = { version = "0.9.0", path = "../../client/service" }
sc-informant = { version = "0.9.0", path = "../../client/informant" }
sc-network = { version = "0.9.0", path = "../../client/network" }
sc-cli = { version = "0.9.0", path = "../../client/cli" }
sc-basic-authorship = { version = "0.9.0", path = "../../client/basic-authorship" }
sc-rpc = { version = "3.0.0", path = "../../client/rpc" }
sc-transaction-pool = { version = "3.0.0", path = "../../client/transaction-pool" }
sc-transaction-graph = { version = "3.0.0", path = "../../client/transaction-pool/graph" }
sc-client-api = { version = "3.0.0", path = "../../client/api" }
sc-rpc-server = { version = "3.0.0", path = "../../client/rpc-servers" }
manual-seal = { package = "sc-consensus-manual-seal", version = "0.9.0", path = "../../client/consensus/manual-seal" }
# primitive deps
sp-core = { version = "3.0.0", path = "../../primitives/core" }
sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" }
sp-block-builder = { version = "3.0.0", path = "../../primitives/block-builder" }
sp-api = { version = "3.0.0", path = "../../primitives/api" }
sp-io = { version = "3.0.0", path = "../../primitives/io" }
sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" }
sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" }
sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" }
sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" }
sp-session = { version = "3.0.0", path = "../../primitives/session" }
sp-offchain = { version = "3.0.0", path = "../../primitives/offchain" }
sp-inherents = { version = "3.0.0", path = "../../primitives/inherents" }
sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" }
sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" }
sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" }
sp-wasm-interface = { version = "3.0.0", path = "../../primitives/wasm-interface" }
sp-runtime-interface = { version = "3.0.0", path = "../../primitives/runtime-interface" }
# pallets
frame-system = { version = "3.0.0", path = "../../frame/system" }
parity-scale-codec = "1.3.1"
env_logger = "0.7.1"
log = "0.4.8"
futures01 = { package = "futures", version = "0.1.29" }
futures = { package = "futures", version = "0.3", features = ["compat"] }
rand = "0.7"
tokio = { version = "0.2", features = ["full"] }
libp2p = "0.35.1"
# Calling RPC
jsonrpc-core = "15.1"
[dev-dependencies]
sc-finality-grandpa = { version = "0.9.0", path = "../../client/finality-grandpa" }
sc-consensus-babe = { version = "0.9.0", path = "../../client/consensus/babe" }
sp-consensus-babe = { version = "0.9.0", path = "../../primitives/consensus/babe" }
node-cli = { version = "2.0.0", path = "../../bin/node/cli" }
@@ -0,0 +1,70 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
#[macro_export]
macro_rules! override_host_functions {
($($fn_name:expr, $name:ident,)*) => {{
let mut host_functions = vec![];
$(
struct $name;
impl sp_wasm_interface::Function for $name {
fn name(&self) -> &str {
&$fn_name
}
fn signature(&self) -> sp_wasm_interface::Signature {
sp_wasm_interface::Signature {
args: std::borrow::Cow::Owned(vec![
sp_wasm_interface::ValueType::I32,
sp_wasm_interface::ValueType::I64,
sp_wasm_interface::ValueType::I32,
]),
return_value: Some(sp_wasm_interface::ValueType::I32),
}
}
fn execute(
&self,
context: &mut dyn sp_wasm_interface::FunctionContext,
_args: &mut dyn Iterator<Item = sp_wasm_interface::Value>,
) -> Result<Option<sp_wasm_interface::Value>, String> {
<bool as sp_runtime_interface::host::IntoFFIValue>::into_ffi_value(true, context)
.map(sp_wasm_interface::IntoValue::into_value)
.map(Some)
}
}
host_functions.push(&$name as &'static dyn sp_wasm_interface::Function);
)*
host_functions
}};
}
/// Provides host functions that overrides runtime signature verification
/// to always return true.
pub struct SignatureVerificationOverride;
impl sp_wasm_interface::HostFunctions for SignatureVerificationOverride {
fn host_functions() -> Vec<&'static dyn sp_wasm_interface::Function> {
override_host_functions!(
"ext_crypto_ecdsa_verify_version_1", EcdsaVerify,
"ext_crypto_ed25519_verify_version_1", Ed25519Verify,
"ext_crypto_sr25519_verify_version_1", Sr25519Verify,
"ext_crypto_sr25519_verify_version_2", Sr25519VerifyV2,
)
}
}
+312
View File
@@ -0,0 +1,312 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
//! Test runner
//! # Substrate Test Runner
//!
//! Allows you to test
//! <br />
//!
//! - Migrations
//! - Runtime Upgrades
//! - Pallets and general runtime functionality.
//!
//! This works by running a full node with a Manual Seal-BABE™ hybrid consensus for block authoring.
//!
//! <h2>Note</h2>
//! The running node has no signature verification, which allows us author extrinsics for any account on chain.
//! <br/>
//! <br/>
//!
//! <h2>How do I Use this?</h2>
//!
//!
//! ```rust,ignore
//! use test_runner::{Node, ChainInfo, SignatureVerificationOverride, base_path, NodeConfig};
//! use sc_finality_grandpa::GrandpaBlockImport;
//! use sc_service::{
//! TFullBackend, TFullClient, Configuration, TaskManager, new_full_parts, BasePath,
//! DatabaseConfig, KeepBlocks, TransactionStorageMode, ChainSpec, Role,
//! config::{NetworkConfiguration, KeystoreConfig},
//! };
//! use std::sync::Arc;
//! use sp_inherents::InherentDataProviders;
//! use sc_consensus_babe::BabeBlockImport;
//! use sp_keystore::SyncCryptoStorePtr;
//! use sp_keyring::sr25519::Keyring::{Alice, Bob};
//! use node_cli::chain_spec::development_config;
//! use sp_consensus_babe::AuthorityId;
//! use manual_seal::{ConsensusDataProvider, consensus::babe::BabeConsensusDataProvider};
//! use sp_runtime::{traits::IdentifyAccount, MultiSigner, generic::Era};
//! use sc_executor::WasmExecutionMethod;
//! use sc_network::{multiaddr, config::TransportConfig};
//! use sc_client_api::execution_extensions::ExecutionStrategies;
//! use sc_informant::OutputFormat;
//! use sp_api::TransactionFor;
//!
//! type BlockImport<B, BE, C, SC> = BabeBlockImport<B, C, GrandpaBlockImport<BE, B, C, SC>>;
//!
//! sc_executor::native_executor_instance!(
//! pub Executor,
//! node_runtime::api::dispatch,
//! node_runtime::native_version,
//! SignatureVerificationOverride,
//! );
//!
//! struct Requirements;
//!
//! impl ChainInfo for Requirements {
//! /// Provide a Block type with an OpaqueExtrinsic
//! type Block = node_primitives::Block;
//! /// Provide an Executor type for the runtime
//! type Executor = Executor;
//! /// Provide the runtime itself
//! type Runtime = node_runtime::Runtime;
//! /// A touch of runtime api
//! type RuntimeApi = node_runtime::RuntimeApi;
//! /// A pinch of SelectChain implementation
//! type SelectChain = sc_consensus::LongestChain<TFullBackend<Self::Block>, Self::Block>;
//! /// A slice of concrete BlockImport type
//! type BlockImport = BlockImport<
//! Self::Block,
//! TFullBackend<Self::Block>,
//! TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>,
//! Self::SelectChain,
//! >;
//! /// and a dash of SignedExtensions
//! type SignedExtras = node_runtime::SignedExtra;
//!
//! /// Create your signed extras here.
//! fn signed_extras(
//! from: <Self::Runtime as frame_system::Config>::AccountId,
//! ) -> Self::SignedExtension {
//! let nonce = frame_system::Pallet::<Self::Runtime>::account_nonce(from);
//!
//! (
//! frame_system::CheckSpecVersion::<Self::Runtime>::new(),
//! frame_system::CheckTxVersion::<Self::Runtime>::new(),
//! frame_system::CheckGenesis::<Self::Runtime>::new(),
//! frame_system::CheckMortality::<Self::Runtime>::from(Era::Immortal),
//! frame_system::CheckNonce::<Self::Runtime>::from(nonce),
//! frame_system::CheckWeight::<Self::Runtime>::new(),
//! pallet_transaction_payment::ChargeTransactionPayment::<Self::Runtime>::from(0),
//! )
//! }
//!
//! /// The function signature tells you all you need to know. ;)
//! fn create_client_parts(config: &Configuration) -> Result<
//! (
//! Arc<TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>>,
//! Arc<TFullBackend<Self::Block>>,
//! KeyStorePtr,
//! TaskManager,
//! InherentDataProviders,
//! Option<Box<
//! dyn ConsensusDataProvider<
//! Self::Block,
//! Transaction = TransactionFor<
//! TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>,
//! Self::Block
//! >,
//! >
//! >>,
//! Self::SelectChain,
//! Self::BlockImport
//! ),
//! sc_service::Error
//! > {
//! let (
//! client,
//! backend,
//! keystore,
//! task_manager,
//! ) = new_full_parts::<Self::Block, Self::RuntimeApi, Self::Executor>(config)?;
//! let client = Arc::new(client);
//!
//! let inherent_providers = InherentDataProviders::new();
//! let select_chain = sc_consensus::LongestChain::new(backend.clone());
//!
//! let (grandpa_block_import, ..) =
//! sc_finality_grandpa::block_import(client.clone(), &(client.clone() as Arc<_>), select_chain.clone())?;
//!
//! let (block_import, babe_link) = sc_consensus_babe::block_import(
//! sc_consensus_babe::Config::get_or_compute(&*client)?,
//! grandpa_block_import,
//! client.clone(),
//! )?;
//!
//! let consensus_data_provider = BabeConsensusDataProvider::new(
//! client.clone(),
//! keystore.clone(),
//! &inherent_providers,
//! babe_link.epoch_changes().clone(),
//! vec![(AuthorityId::from(Alice.public()), 1000)]
//! )
//! .expect("failed to create ConsensusDataProvider");
//!
//! Ok((
//! client,
//! backend,
//! keystore,
//! task_manager,
//! inherent_providers,
//! Some(Box::new(consensus_data_provider)),
//! select_chain,
//! block_import
//! ))
//! }
//!
//! fn dispatch_with_root(call: <Self::Runtime as frame_system::Config>::Call, node: &mut Node<Self>) {
//! let alice = MultiSigner::from(Alice.public()).into_account();
//! // for chains that support sudo, otherwise, you'd have to use pallet-democracy here.
//! let call = pallet_sudo::Call::sudo(Box::new(call));
//! node.submit_extrinsic(call, alice);
//! node.seal_blocks(1);
//! }
//! }
//!
//! /// And now for the most basic test
//!
//! #[test]
//! fn simple_balances_test() {
//! // given
//! let config = NodeConfig {
//! execution_strategies: ExecutionStrategies {
//! syncing: sc_client_api::ExecutionStrategy::NativeWhenPossible,
//! importing: sc_client_api::ExecutionStrategy::NativeWhenPossible,
//! block_construction: sc_client_api::ExecutionStrategy::NativeWhenPossible,
//! offchain_worker: sc_client_api::ExecutionStrategy::NativeWhenPossible,
//! other: sc_client_api::ExecutionStrategy::NativeWhenPossible,
//! },
//! chain_spec: Box::new(development_config()),
//! log_targets: vec![],
//! };
//! let mut node = Node::<Requirements>::new(config).unwrap();
//!
//! type Balances = pallet_balances::Pallet<node_runtime::Runtime>;
//!
//! let (alice, bob) = (Alice.pair(), Bob.pair());
//! let (alice_account_id, bob_acount_id) = (
//! MultiSigner::from(alice.public()).into_account(),
//! MultiSigner::from(bob.public()).into_account()
//! );
//!
//! /// the function with_state allows us to read state, pretty cool right? :D
//! let old_balance = node.with_state(|| Balances::free_balance(alice_account_id.clone()));
//!
//! // 70 dots
//! let amount = 70_000_000_000_000;
//!
//! /// Send extrinsic in action.
//! node.submit_extrinsic(BalancesCall::transfer(bob_acount_id.clone(), amount), alice_account_id.clone());
//!
//! /// Produce blocks in action, Powered by manual-seal™.
//! node.seal_blocks(1);
//!
//! /// we can check the new state :D
//! let new_balance = node.with_state(|| Balances::free_balance(alice_account_id));
//!
//! /// we can now make assertions on how state has changed.
//! assert_eq!(old_balance + amount, new_balance);
//! }
//! ```
use manual_seal::consensus::ConsensusDataProvider;
use sc_executor::NativeExecutionDispatch;
use sc_service::{Configuration, TFullBackend, TFullClient, TaskManager};
use sp_api::{ConstructRuntimeApi, TransactionFor};
use sp_consensus::{BlockImport, SelectChain};
use sp_inherents::InherentDataProviders;
use sp_keystore::SyncCryptoStorePtr;
use sp_runtime::traits::{Block as BlockT, SignedExtension};
use std::sync::Arc;
mod node;
mod utils;
mod host_functions;
pub use host_functions::*;
pub use node::*;
/// Wrapper trait for concrete type required by this testing framework.
pub trait ChainInfo: Sized {
/// Opaque block type
type Block: BlockT;
/// Executor type
type Executor: NativeExecutionDispatch + 'static;
/// Runtime
type Runtime: frame_system::Config;
/// RuntimeApi
type RuntimeApi: Send
+ Sync
+ 'static
+ ConstructRuntimeApi<Self::Block, TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>>;
/// select chain type.
type SelectChain: SelectChain<Self::Block> + 'static;
/// Block import type.
type BlockImport: Send
+ Sync
+ Clone
+ BlockImport<
Self::Block,
Error = sp_consensus::Error,
Transaction = TransactionFor<TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>, Self::Block>,
> + 'static;
/// The signed extras required by the runtime
type SignedExtras: SignedExtension;
/// Signed extras, this function is caled in an externalities provided environment.
fn signed_extras(from: <Self::Runtime as frame_system::Config>::AccountId) -> Self::SignedExtras;
/// Attempt to create client parts, including block import,
/// select chain strategy and consensus data provider.
fn create_client_parts(
config: &Configuration,
) -> Result<
(
Arc<TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>>,
Arc<TFullBackend<Self::Block>>,
SyncCryptoStorePtr,
TaskManager,
InherentDataProviders,
Option<
Box<
dyn ConsensusDataProvider<
Self::Block,
Transaction = TransactionFor<
TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>,
Self::Block,
>,
>,
>,
>,
Self::SelectChain,
Self::BlockImport,
),
sc_service::Error,
>;
/// Given a call and a handle to the node, execute the call with root privileges.
fn dispatch_with_root(call: <Self::Runtime as frame_system::Config>::Call, node: &mut Node<Self>);
}
@@ -0,0 +1,476 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
use std::sync::Arc;
use futures::{FutureExt, SinkExt, channel::{mpsc, oneshot}};
use jsonrpc_core::MetaIoHandler;
use manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};
use sc_cli::build_runtime;
use sc_client_api::{
backend::{self, Backend}, CallExecutor, ExecutorProvider,
execution_extensions::ExecutionStrategies,
};
use sc_service::{
build_network, spawn_tasks, BuildNetworkParams, SpawnTasksParams, TFullBackend,
TFullCallExecutor, TFullClient, TaskManager, TaskType, ChainSpec, BasePath,
Configuration, DatabaseConfig, KeepBlocks, TransactionStorageMode, config::KeystoreConfig,
};
use sc_transaction_pool::BasicPool;
use sp_api::{ApiExt, ConstructRuntimeApi, Core, Metadata, OverlayedChanges, StorageTransactionCache};
use sp_block_builder::BlockBuilder;
use sp_blockchain::HeaderBackend;
use sp_core::ExecutionContext;
use sp_offchain::OffchainWorkerApi;
use sp_runtime::traits::{Block as BlockT, Extrinsic};
use sp_runtime::{generic::BlockId, transaction_validity::TransactionSource, MultiSignature, MultiAddress};
use sp_runtime::{generic::UncheckedExtrinsic, traits::NumberFor};
use sp_session::SessionKeys;
use sp_state_machine::Ext;
use sp_transaction_pool::runtime_api::TaggedTransactionQueue;
use sp_transaction_pool::TransactionPool;
pub use crate::utils::{logger, base_path};
use crate::ChainInfo;
use log::LevelFilter;
use sp_keyring::sr25519::Keyring::Alice;
use sc_network::{multiaddr, config::{NetworkConfiguration, TransportConfig, Role}};
use sc_informant::OutputFormat;
use sc_executor::WasmExecutionMethod;
/// This holds a reference to a running node on another thread,
/// the node process is dropped when this struct is dropped
/// also holds logs from the process.
pub struct Node<T: ChainInfo> {
/// rpc handler for communicating with the node over rpc.
rpc_handler: Arc<MetaIoHandler<sc_rpc::Metadata, sc_rpc_server::RpcMiddleware>>,
/// Stream of log lines
log_stream: mpsc::UnboundedReceiver<String>,
/// node tokio runtime
_runtime: tokio::runtime::Runtime,
/// handle to the running node.
_task_manager: Option<TaskManager>,
/// client instance
client: Arc<TFullClient<T::Block, T::RuntimeApi, T::Executor>>,
/// transaction pool
pool: Arc<
dyn TransactionPool<
Block = T::Block,
Hash = <T::Block as BlockT>::Hash,
Error = sc_transaction_pool::error::Error,
InPoolTransaction = sc_transaction_graph::base_pool::Transaction<
<T::Block as BlockT>::Hash,
<T::Block as BlockT>::Extrinsic,
>,
>,
>,
/// channel to communicate with manual seal on.
manual_seal_command_sink: mpsc::Sender<EngineCommand<<T::Block as BlockT>::Hash>>,
/// backend type.
backend: Arc<TFullBackend<T::Block>>,
/// Block number at initialization of this Node.
initial_block_number: NumberFor<T::Block>
}
/// Configuration options for the node.
pub struct NodeConfig {
/// A set of log targets you'd like to enable/disbale
pub log_targets: Vec<(&'static str, LevelFilter)>,
/// ChainSpec for the runtime
pub chain_spec: Box<dyn ChainSpec>,
/// wasm execution strategies.
pub execution_strategies: ExecutionStrategies,
}
type EventRecord<T> = frame_system::EventRecord<<T as frame_system::Config>::Event, <T as frame_system::Config>::Hash>;
impl<T: ChainInfo> Node<T> {
/// Starts a node with the manual-seal authorship.
pub fn new(node_config: NodeConfig) -> Result<Self, sc_service::Error>
where
<T::RuntimeApi as ConstructRuntimeApi<T::Block, TFullClient<T::Block, T::RuntimeApi, T::Executor>>>::RuntimeApi:
Core<T::Block>
+ Metadata<T::Block>
+ OffchainWorkerApi<T::Block>
+ SessionKeys<T::Block>
+ TaggedTransactionQueue<T::Block>
+ BlockBuilder<T::Block>
+ ApiExt<T::Block, StateBackend = <TFullBackend<T::Block> as Backend<T::Block>>::State>,
{
let NodeConfig { log_targets, mut chain_spec, execution_strategies } = node_config;
let tokio_runtime = build_runtime().unwrap();
// unbounded logs, should be fine, test is shortlived.
let (log_sink, log_stream) = mpsc::unbounded();
logger(log_targets, tokio_runtime.handle().clone(), log_sink);
let runtime_handle = tokio_runtime.handle().clone();
let task_executor = move |fut, task_type| match task_type {
TaskType::Async => runtime_handle.spawn(fut).map(drop),
TaskType::Blocking => runtime_handle
.spawn_blocking(move || futures::executor::block_on(fut))
.map(drop),
};
let base_path = if let Some(base) = base_path() {
BasePath::new(base)
} else {
BasePath::new_temp_dir().expect("couldn't create a temp dir")
};
let root_path = base_path.path().to_path_buf().join("chains").join(chain_spec.id());
let key_seed = Alice.to_seed();
let storage = chain_spec
.as_storage_builder()
.build_storage()
.expect("could not build storage");
chain_spec.set_storage(storage);
let mut network_config = NetworkConfiguration::new(
format!("Test Node for: {}", key_seed),
"network/test/0.1",
Default::default(),
None,
);
let informant_output_format = OutputFormat { enable_color: false };
network_config.allow_non_globals_in_dht = true;
network_config
.listen_addresses
.push(multiaddr::Protocol::Memory(rand::random()).into());
network_config.transport = TransportConfig::MemoryOnly;
let config = Configuration {
impl_name: "test-node".to_string(),
impl_version: "0.1".to_string(),
role: Role::Authority,
task_executor: task_executor.into(),
transaction_pool: Default::default(),
network: network_config,
keystore: KeystoreConfig::Path {
path: root_path.join("key"),
password: None,
},
database: DatabaseConfig::RocksDb {
path: root_path.join("db"),
cache_size: 128,
},
state_cache_size: 16777216,
state_cache_child_ratio: None,
chain_spec,
wasm_method: WasmExecutionMethod::Interpreted,
execution_strategies,
rpc_http: None,
rpc_ws: None,
rpc_ipc: None,
rpc_ws_max_connections: None,
rpc_cors: None,
rpc_methods: Default::default(),
prometheus_config: None,
telemetry_endpoints: None,
telemetry_external_transport: None,
default_heap_pages: None,
offchain_worker: Default::default(),
force_authoring: false,
disable_grandpa: false,
dev_key_seed: Some(key_seed),
tracing_targets: None,
tracing_receiver: Default::default(),
max_runtime_instances: 8,
announce_block: true,
base_path: Some(base_path),
wasm_runtime_overrides: None,
informant_output_format,
disable_log_reloading: false,
keystore_remote: None,
keep_blocks: KeepBlocks::All,
state_pruning: Default::default(),
transaction_storage: TransactionStorageMode::BlockBody,
};
let (
client,
backend,
keystore,
mut task_manager,
inherent_data_providers,
consensus_data_provider,
select_chain,
block_import,
) = T::create_client_parts(&config)?;
let import_queue =
manual_seal::import_queue(Box::new(block_import.clone()), &task_manager.spawn_essential_handle(), None);
let transaction_pool = BasicPool::new_full(
config.transaction_pool.clone(),
true.into(),
config.prometheus_registry(),
task_manager.spawn_handle(),
client.clone(),
);
let (network, network_status_sinks, system_rpc_tx, network_starter) = {
let params = BuildNetworkParams {
config: &config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
spawn_handle: task_manager.spawn_handle(),
import_queue,
on_demand: None,
block_announce_validator_builder: None,
};
build_network(params)?
};
sc_service::build_offchain_workers(
&config,
task_manager.spawn_handle(),
client.clone(),
network.clone(),
);
// Proposer object for block authorship.
let env = sc_basic_authorship::ProposerFactory::new(
task_manager.spawn_handle(),
client.clone(),
transaction_pool.clone(),
config.prometheus_registry(),
None
);
// Channel for the rpc handler to communicate with the authorship task.
let (command_sink, commands_stream) = mpsc::channel(10);
let rpc_handlers = {
let params = SpawnTasksParams {
config,
client: client.clone(),
backend: backend.clone(),
task_manager: &mut task_manager,
keystore,
on_demand: None,
transaction_pool: transaction_pool.clone(),
rpc_extensions_builder: Box::new(move |_, _| jsonrpc_core::IoHandler::default()),
remote_blockchain: None,
network,
network_status_sinks,
system_rpc_tx,
telemetry: None
};
spawn_tasks(params)?
};
// Background authorship future.
let authorship_future = run_manual_seal(ManualSealParams {
block_import,
env,
client: client.clone(),
pool: transaction_pool.pool().clone(),
commands_stream,
select_chain,
consensus_data_provider,
inherent_data_providers,
});
// spawn the authorship task as an essential task.
task_manager
.spawn_essential_handle()
.spawn("manual-seal", authorship_future);
network_starter.start_network();
let rpc_handler = rpc_handlers.io_handler();
let initial_number = client.info().best_number;
Ok(Self {
rpc_handler,
_task_manager: Some(task_manager),
_runtime: tokio_runtime,
client,
pool: transaction_pool,
backend,
log_stream,
manual_seal_command_sink: command_sink,
initial_block_number: initial_number,
})
}
/// Returns a reference to the rpc handlers.
pub fn rpc_handler(&self) -> Arc<MetaIoHandler<sc_rpc::Metadata, sc_rpc_server::RpcMiddleware>> {
self.rpc_handler.clone()
}
/// Return a reference to the Client
pub fn client(&self) -> Arc<TFullClient<T::Block, T::RuntimeApi, T::Executor>> {
self.client.clone()
}
/// Executes closure in an externalities provided environment.
pub fn with_state<R>(&self, closure: impl FnOnce() -> R) -> R
where
<TFullCallExecutor<T::Block, T::Executor> as CallExecutor<T::Block>>::Error: std::fmt::Debug,
{
let id = BlockId::Hash(self.client.info().best_hash);
let mut overlay = OverlayedChanges::default();
let changes_trie = backend::changes_tries_state_at_block(&id, self.backend.changes_trie_storage()).unwrap();
let mut cache =
StorageTransactionCache::<T::Block, <TFullBackend<T::Block> as Backend<T::Block>>::State>::default();
let mut extensions = self
.client
.execution_extensions()
.extensions(&id, ExecutionContext::BlockConstruction);
let state_backend = self
.backend
.state_at(id.clone())
.expect(&format!("State at block {} not found", id));
let mut ext = Ext::new(
&mut overlay,
&mut cache,
&state_backend,
changes_trie.clone(),
Some(&mut extensions),
);
sp_externalities::set_and_run_with_externalities(&mut ext, closure)
}
/// submit some extrinsic to the node, providing the sending account.
pub fn submit_extrinsic(
&mut self,
call: impl Into<<T::Runtime as frame_system::Config>::Call>,
from: <T::Runtime as frame_system::Config>::AccountId,
) -> <T::Block as BlockT>::Hash
where
<T::Block as BlockT>::Extrinsic: From<
UncheckedExtrinsic<
MultiAddress<
<T::Runtime as frame_system::Config>::AccountId,
<T::Runtime as frame_system::Config>::Index,
>,
<T::Runtime as frame_system::Config>::Call,
MultiSignature,
T::SignedExtras,
>,
>,
{
let extra = self.with_state(|| T::signed_extras(from.clone()));
let signed_data = Some((from.into(), MultiSignature::Sr25519(Default::default()), extra));
let ext = UncheckedExtrinsic::<
MultiAddress<
<T::Runtime as frame_system::Config>::AccountId,
<T::Runtime as frame_system::Config>::Index,
>,
<T::Runtime as frame_system::Config>::Call,
MultiSignature,
T::SignedExtras,
>::new(call.into(), signed_data)
.expect("UncheckedExtrinsic::new() always returns Some");
let at = self.client.info().best_hash;
self._runtime
.block_on(
self.pool.submit_one(&BlockId::Hash(at), TransactionSource::Local, ext.into()),
)
.unwrap()
}
/// Get the events of the most recently produced block
pub fn events(&self) -> Vec<EventRecord<T::Runtime>> {
self.with_state(|| frame_system::Pallet::<T::Runtime>::events())
}
/// Checks the node logs for a specific entry.
pub fn assert_log_line(&mut self, content: &str) {
futures::executor::block_on(async {
use futures::StreamExt;
while let Some(log_line) = self.log_stream.next().await {
if log_line.contains(content) {
return;
}
}
panic!("Could not find {} in logs content", content);
});
}
/// Instructs manual seal to seal new, possibly empty blocks.
pub fn seal_blocks(&mut self, num: usize) {
let (tokio, sink) = (&mut self._runtime, &mut self.manual_seal_command_sink);
for count in 0..num {
let (sender, future_block) = oneshot::channel();
let future = sink.send(EngineCommand::SealNewBlock {
create_empty: true,
finalize: false,
parent_hash: None,
sender: Some(sender),
});
tokio.block_on(async {
const ERROR: &'static str = "manual-seal authorship task is shutting down";
future.await.expect(ERROR);
match future_block.await.expect(ERROR) {
Ok(block) => log::info!("sealed {} (hash: {}) of {} blocks", count + 1, block.hash, num),
Err(err) => log::error!("failed to seal block {} of {}, error: {:?}", count + 1, num, err),
}
});
}
}
/// Revert count number of blocks from the chain.
pub fn revert_blocks(&self, count: NumberFor<T::Block>) {
self.backend.revert(count, true).expect("Failed to revert blocks: ");
}
/// Revert all blocks added since creation of the node.
pub fn clean(&self) {
// if a db path was specified, revert all blocks we've added
if let Some(_) = base_path() {
let diff = self.client.info().best_number - self.initial_block_number;
self.revert_blocks(diff);
}
}
/// Performs a runtime upgrade given a wasm blob.
pub fn upgrade_runtime(&mut self, wasm: Vec<u8>)
where
<T::Runtime as frame_system::Config>::Call: From<frame_system::Call<T::Runtime>>
{
let call = frame_system::Call::set_code(wasm);
T::dispatch_with_root(call.into(), self);
}
}
impl<T: ChainInfo> Drop for Node<T> {
fn drop(&mut self) {
self.clean();
if let Some(mut task_manager) = self._task_manager.take() {
// if this isn't called the node will live forever
task_manager.terminate()
}
}
}
@@ -0,0 +1,56 @@
// This file is part of Substrate.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
use futures::{Sink, SinkExt};
use std::fmt;
use std::io::Write;
use log::LevelFilter;
/// Base db path gotten from env
pub fn base_path() -> Option<String> {
std::env::var("DB_BASE_PATH").ok()
}
/// Builds the global logger.
pub fn logger<S>(
log_targets: Vec<(&'static str, LevelFilter)>,
executor: tokio::runtime::Handle,
log_sink: S,
)
where
S: Sink<String> + Clone + Unpin + Send + Sync + 'static,
S::Error: Send + Sync + fmt::Debug,
{
let mut builder = env_logger::builder();
builder.format(move |buf: &mut env_logger::fmt::Formatter, record: &log::Record| {
let entry = format!("{} {} {}", record.level(), record.target(), record.args());
let res = writeln!(buf, "{}", entry);
let mut log_sink_clone = log_sink.clone();
let _ = executor.spawn(async move {
log_sink_clone.send(entry).await.expect("log_stream is dropped");
});
res
});
builder.write_style(env_logger::WriteStyle::Always);
for (module, level) in log_targets {
builder.filter_module(module, level);
}
let _ = builder.is_test(true).try_init();
}