mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 21:37:56 +00:00
Update test-runner api (#9302)
* better apis * .... * ... * Genensis -> UnimportedGenesis * adds rpc for runtime upgrades * simplify test-runner * clean up test-runner api * remove unused imports * fix doc-test * fix line width * correct Node::clean * correct Node::clean * add deny rules * remove unused extern crates * remove mutex from node * Update test-utils/test-runner/Cargo.toml Co-authored-by: Andronik Ordian <write@reusable.software> * adds docs, removes Node::clean Co-authored-by: Andronik Ordian <write@reusable.software> Co-authored-by: Seun Lanlege <seun@parity.io>
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
// 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/>.
|
||||
//! Client parts
|
||||
use sp_transaction_pool::runtime_api::TaggedTransactionQueue;
|
||||
use sp_consensus_babe::BabeApi;
|
||||
use crate::{ChainInfo, default_config};
|
||||
use manual_seal::consensus::babe::{BabeConsensusDataProvider, SlotTimestampProvider};
|
||||
use sp_keyring::sr25519::Keyring::Alice;
|
||||
use std::str::FromStr;
|
||||
use sp_runtime::traits::Header;
|
||||
use futures::channel::mpsc;
|
||||
use jsonrpc_core::MetaIoHandler;
|
||||
use manual_seal::{run_manual_seal, EngineCommand, ManualSealParams, import_queue, rpc::{ManualSeal, ManualSealApi}};
|
||||
use sc_client_api::backend::Backend;
|
||||
use sc_service::{
|
||||
build_network, spawn_tasks, BuildNetworkParams, SpawnTasksParams, TFullBackend,
|
||||
TFullClient, TaskManager, new_full_parts, Configuration, ChainSpec, TaskExecutor,
|
||||
};
|
||||
use sc_transaction_pool::BasicPool;
|
||||
use sc_transaction_pool_api::TransactionPool;
|
||||
use sp_api::{ApiExt, ConstructRuntimeApi, Core, Metadata};
|
||||
use sp_block_builder::BlockBuilder;
|
||||
use sp_runtime::traits::Block as BlockT;
|
||||
use sp_session::SessionKeys;
|
||||
use sp_offchain::OffchainWorkerApi;
|
||||
use std::sync::Arc;
|
||||
|
||||
type ClientParts<T> = (
|
||||
Arc<MetaIoHandler<sc_rpc::Metadata, sc_rpc_server::RpcMiddleware>>,
|
||||
TaskManager,
|
||||
Arc<TFullClient<<T as ChainInfo>::Block, <T as ChainInfo>::RuntimeApi, <T as ChainInfo>::Executor>>,
|
||||
Arc<dyn TransactionPool<
|
||||
Block = <T as ChainInfo>::Block,
|
||||
Hash = <<T as ChainInfo>::Block as BlockT>::Hash,
|
||||
Error = sc_transaction_pool::error::Error,
|
||||
InPoolTransaction = sc_transaction_pool::Transaction<
|
||||
<<T as ChainInfo>::Block as BlockT>::Hash,
|
||||
<<T as ChainInfo>::Block as BlockT>::Extrinsic,
|
||||
>,
|
||||
>>,
|
||||
mpsc::Sender<EngineCommand<<<T as ChainInfo>::Block as BlockT>::Hash>>,
|
||||
Arc<TFullBackend<<T as ChainInfo>::Block>>,
|
||||
);
|
||||
|
||||
/// Provide the config or chain spec for a given chain
|
||||
pub enum ConfigOrChainSpec {
|
||||
/// Configuration object
|
||||
Config(Configuration),
|
||||
/// Chain spec object
|
||||
ChainSpec(Box<dyn ChainSpec>, TaskExecutor)
|
||||
}
|
||||
/// Creates all the client parts you need for [`Node`]
|
||||
pub fn client_parts<T>(config_or_chain_spec: ConfigOrChainSpec) -> Result<ClientParts<T>, sc_service::Error>
|
||||
where
|
||||
T: ChainInfo + 'static,
|
||||
<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> + BabeApi<T::Block>
|
||||
+ ApiExt<T::Block, StateBackend = <TFullBackend<T::Block> as Backend<T::Block>>::State>,
|
||||
<T::Runtime as frame_system::Config>::Call: From<frame_system::Call<T::Runtime>>,
|
||||
<<T as ChainInfo>::Block as BlockT>::Hash: FromStr,
|
||||
<<<T as ChainInfo>::Block as BlockT>::Header as Header>::Number: num_traits::cast::AsPrimitive<usize>,
|
||||
{
|
||||
use sp_consensus_babe::AuthorityId;
|
||||
let config = match config_or_chain_spec {
|
||||
ConfigOrChainSpec::Config(config) => config,
|
||||
ConfigOrChainSpec::ChainSpec(chain_spec, task_executor) => {
|
||||
default_config(task_executor, chain_spec)
|
||||
},
|
||||
};
|
||||
|
||||
let (client, backend, keystore, mut task_manager) =
|
||||
new_full_parts::<T::Block, T::RuntimeApi, T::Executor>(&config, None)?;
|
||||
let client = Arc::new(client);
|
||||
|
||||
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 slot_duration = sc_consensus_babe::Config::get_or_compute(&*client)?;
|
||||
let (block_import, babe_link) = sc_consensus_babe::block_import(
|
||||
slot_duration.clone(),
|
||||
grandpa_block_import,
|
||||
client.clone(),
|
||||
)?;
|
||||
|
||||
let consensus_data_provider = BabeConsensusDataProvider::new(
|
||||
client.clone(),
|
||||
keystore.sync_keystore(),
|
||||
babe_link.epoch_changes().clone(),
|
||||
vec![(AuthorityId::from(Alice.public()), 1000)],
|
||||
)
|
||||
.expect("failed to create ConsensusDataProvider");
|
||||
|
||||
let import_queue =
|
||||
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_essential_handle(),
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let (network, 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)?
|
||||
};
|
||||
|
||||
// offchain workers
|
||||
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_sink = command_sink.clone();
|
||||
|
||||
let rpc_handlers = {
|
||||
let params = SpawnTasksParams {
|
||||
config,
|
||||
client: client.clone(),
|
||||
backend: backend.clone(),
|
||||
task_manager: &mut task_manager,
|
||||
keystore: keystore.sync_keystore(),
|
||||
on_demand: None,
|
||||
transaction_pool: transaction_pool.clone(),
|
||||
rpc_extensions_builder: Box::new(move |_, _| {
|
||||
let mut io = jsonrpc_core::IoHandler::default();
|
||||
io.extend_with(
|
||||
ManualSealApi::to_delegate(ManualSeal::new(rpc_sink.clone()))
|
||||
);
|
||||
io
|
||||
}),
|
||||
remote_blockchain: None,
|
||||
network,
|
||||
system_rpc_tx,
|
||||
telemetry: None
|
||||
};
|
||||
spawn_tasks(params)?
|
||||
};
|
||||
|
||||
let cloned_client = client.clone();
|
||||
let create_inherent_data_providers = Box::new(move |_, _| {
|
||||
let client = cloned_client.clone();
|
||||
async move {
|
||||
let timestamp = SlotTimestampProvider::new(client.clone()).map_err(|err| format!("{:?}", err))?;
|
||||
let babe = sp_consensus_babe::inherents::InherentDataProvider::new(timestamp.slot().into());
|
||||
Ok((timestamp, babe))
|
||||
}
|
||||
});
|
||||
|
||||
// Background authorship future.
|
||||
let authorship_future = run_manual_seal(ManualSealParams {
|
||||
block_import,
|
||||
env,
|
||||
client: client.clone(),
|
||||
pool: transaction_pool.clone(),
|
||||
commands_stream,
|
||||
select_chain,
|
||||
consensus_data_provider: Some(Box::new(consensus_data_provider)),
|
||||
create_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();
|
||||
|
||||
Ok((
|
||||
rpc_handler,
|
||||
task_manager,
|
||||
client,
|
||||
transaction_pool,
|
||||
command_sink,
|
||||
backend,
|
||||
))
|
||||
}
|
||||
@@ -16,6 +16,20 @@
|
||||
// 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 this to override host functions.
|
||||
/// eg
|
||||
/// ```rust
|
||||
/// use test_runner::override_host_functions;
|
||||
/// 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,
|
||||
/// )
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! override_host_functions {
|
||||
($($fn_name:expr, $name:ident,)*) => {{
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#![deny(missing_docs, unused_extern_crates)]
|
||||
|
||||
//! Test runner
|
||||
//! # Substrate Test Runner
|
||||
@@ -226,16 +227,14 @@
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use manual_seal::consensus::ConsensusDataProvider;
|
||||
use sc_executor::NativeExecutionDispatch;
|
||||
use sc_service::{Configuration, TFullBackend, TFullClient, TaskManager, TaskExecutor};
|
||||
use sc_service::TFullClient;
|
||||
use sp_api::{ConstructRuntimeApi, TransactionFor};
|
||||
use sp_consensus::{BlockImport, SelectChain};
|
||||
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
|
||||
use sp_keystore::SyncCryptoStorePtr;
|
||||
use sp_inherents::InherentDataProvider;
|
||||
use sp_runtime::traits::{Block as BlockT, SignedExtension};
|
||||
use std::sync::Arc;
|
||||
|
||||
mod client;
|
||||
mod node;
|
||||
mod utils;
|
||||
mod host_functions;
|
||||
@@ -243,6 +242,7 @@ mod host_functions;
|
||||
pub use host_functions::*;
|
||||
pub use node::*;
|
||||
pub use utils::*;
|
||||
pub use client::*;
|
||||
|
||||
/// Wrapper trait for concrete type required by this testing framework.
|
||||
pub trait ChainInfo: Sized {
|
||||
@@ -282,44 +282,4 @@ pub trait ChainInfo: Sized {
|
||||
|
||||
/// Signed extras, this function is caled in an externalities provided environment.
|
||||
fn signed_extras(from: <Self::Runtime as frame_system::Config>::AccountId) -> Self::SignedExtras;
|
||||
|
||||
/// config factory
|
||||
fn config(task_executor: TaskExecutor) -> Configuration;
|
||||
|
||||
/// 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,
|
||||
Box<
|
||||
dyn CreateInherentDataProviders<
|
||||
Self::Block,
|
||||
(),
|
||||
InherentDataProviders = Self::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>);
|
||||
}
|
||||
|
||||
@@ -20,31 +20,20 @@ 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,
|
||||
};
|
||||
use sc_service::{
|
||||
build_network, spawn_tasks, BuildNetworkParams, SpawnTasksParams,
|
||||
TFullBackend, TFullCallExecutor, TFullClient, TaskManager, TaskType,
|
||||
};
|
||||
use sc_transaction_pool::BasicPool;
|
||||
use sp_api::{ApiExt, ConstructRuntimeApi, Core, Metadata, OverlayedChanges, StorageTransactionCache};
|
||||
use sp_block_builder::BlockBuilder;
|
||||
use manual_seal::EngineCommand;
|
||||
use sc_client_api::{backend::{self, Backend}, CallExecutor, ExecutorProvider};
|
||||
use sc_service::{TFullBackend, TFullCallExecutor, TFullClient, TaskManager};
|
||||
use sp_api::{OverlayedChanges, StorageTransactionCache};
|
||||
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_runtime::{
|
||||
generic::{BlockId, UncheckedExtrinsic},
|
||||
traits::{Block as BlockT, Header, Extrinsic, NumberFor},
|
||||
transaction_validity::TransactionSource, MultiSignature, MultiAddress
|
||||
};
|
||||
use crate::ChainInfo;
|
||||
use sc_transaction_pool_api::TransactionPool;
|
||||
|
||||
use crate::{ChainInfo, utils::logger};
|
||||
use log::LevelFilter;
|
||||
use sp_state_machine::Ext;
|
||||
|
||||
/// This holds a reference to a running node on another thread,
|
||||
/// the node process is dropped when this struct is dropped
|
||||
@@ -52,26 +41,20 @@ use log::LevelFilter;
|
||||
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>,
|
||||
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_pool::Transaction<
|
||||
<T::Block as BlockT>::Hash,
|
||||
<T::Block as BlockT>::Extrinsic,
|
||||
>,
|
||||
pool: Arc<dyn TransactionPool<
|
||||
Block = <T as ChainInfo>::Block,
|
||||
Hash = <<T as ChainInfo>::Block as BlockT>::Hash,
|
||||
Error = sc_transaction_pool::error::Error,
|
||||
InPoolTransaction = sc_transaction_pool::Transaction<
|
||||
<<T as ChainInfo>::Block as BlockT>::Hash,
|
||||
<<T as ChainInfo>::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.
|
||||
@@ -80,149 +63,48 @@ pub struct Node<T: ChainInfo> {
|
||||
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)>,
|
||||
}
|
||||
|
||||
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>
|
||||
impl<T> Node<T>
|
||||
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, } = node_config;
|
||||
let tokio_runtime = build_runtime().unwrap();
|
||||
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),
|
||||
};
|
||||
// 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 config = T::config(task_executor.into());
|
||||
|
||||
let (
|
||||
client,
|
||||
backend,
|
||||
keystore,
|
||||
mut task_manager,
|
||||
create_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_essential_handle(),
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let (network, 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,
|
||||
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.clone(),
|
||||
commands_stream,
|
||||
select_chain,
|
||||
consensus_data_provider,
|
||||
create_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 {
|
||||
T: ChainInfo,
|
||||
<<T::Block as BlockT>::Header as Header>::Number: From<u32>,
|
||||
{
|
||||
/// Creates a new node.
|
||||
pub fn new(
|
||||
rpc_handler: Arc<MetaIoHandler<sc_rpc::Metadata, sc_rpc_server::RpcMiddleware>>,
|
||||
task_manager: TaskManager,
|
||||
client: Arc<TFullClient<T::Block, T::RuntimeApi, T::Executor>>,
|
||||
pool: Arc<dyn TransactionPool<
|
||||
Block = <T as ChainInfo>::Block,
|
||||
Hash = <<T as ChainInfo>::Block as BlockT>::Hash,
|
||||
Error = sc_transaction_pool::error::Error,
|
||||
InPoolTransaction = sc_transaction_pool::Transaction<
|
||||
<<T as ChainInfo>::Block as BlockT>::Hash,
|
||||
<<T as ChainInfo>::Block as BlockT>::Extrinsic,
|
||||
>,
|
||||
>>,
|
||||
command_sink: mpsc::Sender<EngineCommand<<T::Block as BlockT>::Hash>>,
|
||||
backend: Arc<TFullBackend<T::Block>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
rpc_handler,
|
||||
_task_manager: Some(task_manager),
|
||||
_runtime: tokio_runtime,
|
||||
client,
|
||||
pool: transaction_pool,
|
||||
task_manager: Some(task_manager),
|
||||
client: client.clone(),
|
||||
pool,
|
||||
backend,
|
||||
log_stream,
|
||||
manual_seal_command_sink: command_sink,
|
||||
initial_block_number: initial_number,
|
||||
})
|
||||
initial_block_number: client.info().best_number,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the rpc handlers.
|
||||
/// Returns a reference to the rpc handlers, use this to send rpc requests.
|
||||
/// eg
|
||||
/// ```ignore
|
||||
/// let request = r#"{"jsonrpc":"2.0","method":"engine_createBlock","params": [true, true],"id":1}"#;
|
||||
/// let response = node.rpc_handler()
|
||||
/// .handle_request_sync(request, Default::default());
|
||||
/// ```
|
||||
pub fn rpc_handler(&self) -> Arc<MetaIoHandler<sc_rpc::Metadata, sc_rpc_server::RpcMiddleware>> {
|
||||
self.rpc_handler.clone()
|
||||
}
|
||||
@@ -262,11 +144,11 @@ impl<T: ChainInfo> Node<T> {
|
||||
}
|
||||
|
||||
/// submit some extrinsic to the node, providing the sending account.
|
||||
pub fn submit_extrinsic(
|
||||
&mut self,
|
||||
pub async fn submit_extrinsic(
|
||||
&self,
|
||||
call: impl Into<<T::Runtime as frame_system::Config>::Call>,
|
||||
from: <T::Runtime as frame_system::Config>::AccountId,
|
||||
) -> <T::Block as BlockT>::Hash
|
||||
) -> Result<<T::Block as BlockT>::Hash, sc_transaction_pool::error::Error>
|
||||
where
|
||||
<T::Block as BlockT>::Extrinsic: From<
|
||||
UncheckedExtrinsic<
|
||||
@@ -294,11 +176,7 @@ impl<T: ChainInfo> Node<T> {
|
||||
.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()
|
||||
self.pool.submit_one(&BlockId::Hash(at), TransactionSource::Local, ext.into()).await
|
||||
}
|
||||
|
||||
/// Get the events of the most recently produced block
|
||||
@@ -306,24 +184,9 @@ impl<T: ChainInfo> Node<T> {
|
||||
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);
|
||||
pub async fn seal_blocks(&self, num: usize) {
|
||||
let mut sink = self.manual_seal_command_sink.clone();
|
||||
|
||||
for count in 0..num {
|
||||
let (sender, future_block) = oneshot::channel();
|
||||
@@ -334,15 +197,13 @@ impl<T: ChainInfo> Node<T> {
|
||||
sender: Some(sender),
|
||||
});
|
||||
|
||||
tokio.block_on(async {
|
||||
const ERROR: &'static str = "manual-seal authorship task is shutting down";
|
||||
future.await.expect(ERROR);
|
||||
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),
|
||||
}
|
||||
});
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,32 +212,24 @@ impl<T: ChainInfo> Node<T> {
|
||||
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(_) = std::env::var("DB_BASE_PATH").ok() {
|
||||
let diff = self.client.info().best_number - self.initial_block_number;
|
||||
self.revert_blocks(diff);
|
||||
/// so you've decided to run the test runner as a binary, use this to shutdown gracefully.
|
||||
pub async fn until_shutdown(mut self) {
|
||||
let manager = self.task_manager.take();
|
||||
if let Some(mut task_manager) = manager {
|
||||
let task = task_manager.future().fuse();
|
||||
let signal = tokio::signal::ctrl_c();
|
||||
futures::pin_mut!(signal);
|
||||
futures::future::select(task, signal).await;
|
||||
// we don't really care whichever comes first.
|
||||
task_manager.clean_shutdown().await
|
||||
}
|
||||
}
|
||||
|
||||
/// 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()
|
||||
}
|
||||
// Revert all blocks added since creation of the node.
|
||||
let diff = self.client.info().best_number - self.initial_block_number;
|
||||
self.revert_blocks(diff);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,17 +16,20 @@
|
||||
// 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;
|
||||
use sc_service::{BasePath, ChainSpec, Configuration, TaskExecutor, DatabaseConfig, KeepBlocks, TransactionStorageMode};
|
||||
use sc_service::{
|
||||
BasePath, ChainSpec, Configuration, TaskExecutor,
|
||||
DatabaseConfig, KeepBlocks, TransactionStorageMode, TaskType,
|
||||
};
|
||||
use sp_keyring::sr25519::Keyring::Alice;
|
||||
use sc_network::{multiaddr, config::{NetworkConfiguration, TransportConfig, Role}};
|
||||
use sc_informant::OutputFormat;
|
||||
use sc_service::config::KeystoreConfig;
|
||||
use sc_executor::WasmExecutionMethod;
|
||||
use sc_client_api::execution_extensions::ExecutionStrategies;
|
||||
use tokio::runtime::Handle;
|
||||
use futures::FutureExt;
|
||||
|
||||
pub use sc_cli::build_runtime;
|
||||
|
||||
/// Base db path gotten from env
|
||||
pub fn base_path() -> BasePath {
|
||||
@@ -37,35 +40,6 @@ pub fn base_path() -> BasePath {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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();
|
||||
}
|
||||
|
||||
/// Produces a default configuration object, suitable for use with most set ups.
|
||||
pub fn default_config(task_executor: TaskExecutor, mut chain_spec: Box<dyn ChainSpec>) -> Configuration {
|
||||
let base_path = base_path();
|
||||
@@ -150,3 +124,13 @@ pub fn default_config(task_executor: TaskExecutor, mut chain_spec: Box<dyn Chain
|
||||
transaction_storage: TransactionStorageMode::BlockBody,
|
||||
}
|
||||
}
|
||||
|
||||
/// Produce a task executor given a handle to a tokio runtime
|
||||
pub fn task_executor(handle: Handle) -> TaskExecutor {
|
||||
let task_executor = move |fut, task_type| match task_type {
|
||||
TaskType::Async => handle.spawn(fut).map(drop),
|
||||
TaskType::Blocking => handle.spawn_blocking(move || futures::executor::block_on(fut)).map(drop),
|
||||
};
|
||||
|
||||
task_executor.into()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user