Adds AuraConsensusDataProvider (#10503)

* adds support for parachains to test-runner

* adds file header

* Apply suggestions from code review

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* proper docs, remove unused _client

* fixes

* Update client/consensus/manual-seal/src/consensus/timestamp.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update client/consensus/manual-seal/src/consensus/timestamp.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* pr fixes

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Seun Lanlege
2022-01-10 15:39:04 +01:00
committed by GitHub
parent 2178cb1939
commit a4057bb9e2
17 changed files with 278 additions and 1368 deletions
@@ -1,59 +0,0 @@
[package]
name = "test-runner"
version = "0.9.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
publish = false
[dependencies]
# client deps
sc-executor = { path = "../../client/executor" }
sc-service = { path = "../../client/service" }
sc-informant = { path = "../../client/informant" }
sc-network = { path = "../../client/network" }
sc-cli = { path = "../../client/cli" }
sc-basic-authorship = { path = "../../client/basic-authorship" }
sc-rpc = { path = "../../client/rpc" }
sc-transaction-pool = { path = "../../client/transaction-pool" }
grandpa = { package = "sc-finality-grandpa", path = "../../client/finality-grandpa" }
sp-finality-grandpa = { path = "../../primitives/finality-grandpa" }
sp-consensus-babe = { path = "../../primitives/consensus/babe" }
sc-consensus-babe = { path = "../../client/consensus/babe" }
sc-consensus = { path = "../../client/consensus/common" }
sc-transaction-pool-api = { path = "../../client/transaction-pool/api" }
sc-client-api = { path = "../../client/api" }
sc-rpc-server = { path = "../../client/rpc-servers" }
manual-seal = { package = "sc-consensus-manual-seal", path = "../../client/consensus/manual-seal" }
# primitive deps
sp-core = { path = "../../primitives/core" }
sp-blockchain = { path = "../../primitives/blockchain" }
sp-block-builder = { path = "../../primitives/block-builder" }
sp-api = { path = "../../primitives/api" }
sp-transaction-pool = { path = "../../primitives/transaction-pool" }
sp-consensus = { path = "../../primitives/consensus/common" }
sp-runtime = { path = "../../primitives/runtime" }
sp-session = { path = "../../primitives/session" }
sp-offchain = { path = "../../primitives/offchain" }
sp-inherents = { path = "../../primitives/inherents" }
sp-keyring = { path = "../../primitives/keyring" }
sp-externalities = { path = "../../primitives/externalities" }
sp-state-machine = { path = "../../primitives/state-machine" }
sp-wasm-interface = { path = "../../primitives/wasm-interface" }
sp-runtime-interface = { path = "../../primitives/runtime-interface" }
# pallets
frame-system = { path = "../../frame/system" }
log = "0.4.8"
futures = "0.3.16"
tokio = { version = "1.15", features = ["signal"] }
# Calling RPC
jsonrpc-core = "18.0"
num-traits = "0.2.14"
[features]
default = ["std"]
# This is here so that we can use the `runtime_interface` procedural macro
std = []
@@ -1,244 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2021-2022 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 crate::{default_config, ChainInfo};
use futures::channel::mpsc;
use jsonrpc_core::MetaIoHandler;
use manual_seal::{
consensus::babe::{BabeConsensusDataProvider, SlotTimestampProvider},
import_queue,
rpc::{ManualSeal, ManualSealApi},
run_manual_seal, EngineCommand, ManualSealParams,
};
use sc_client_api::backend::Backend;
use sc_executor::NativeElseWasmExecutor;
use sc_service::{
build_network, new_full_parts, spawn_tasks, BuildNetworkParams, ChainSpec, Configuration,
SpawnTasksParams, TFullBackend, TFullClient, TaskManager,
};
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_consensus_babe::BabeApi;
use sp_finality_grandpa::GrandpaApi;
use sp_keyring::sr25519::Keyring::Alice;
use sp_offchain::OffchainWorkerApi;
use sp_runtime::traits::{Block as BlockT, Header};
use sp_session::SessionKeys;
use sp_transaction_pool::runtime_api::TaggedTransactionQueue;
use std::{str::FromStr, 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,
NativeElseWasmExecutor<<T as ChainInfo>::ExecutorDispatch>,
>,
>,
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>, tokio::runtime::Handle),
}
/// Creates all the client parts you need for [`Node`](crate::node::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, NativeElseWasmExecutor<T::ExecutorDispatch>>,
>>::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>
+ GrandpaApi<T::Block>,
<T::Runtime as frame_system::Config>::Call: From<frame_system::Call<T::Runtime>>,
<<T as ChainInfo>::Block as BlockT>::Hash: FromStr + Unpin,
<<T as ChainInfo>::Block as BlockT>::Header: Unpin,
<<<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, tokio_handle) =>
default_config(tokio_handle, chain_spec),
};
let executor = NativeElseWasmExecutor::<T::ExecutorDispatch>::new(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
config.runtime_cache_size,
);
let (client, backend, keystore, mut task_manager) =
new_full_parts::<T::Block, T::RuntimeApi, _>(&config, None, executor)?;
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(&*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,
block_announce_validator_builder: None,
warp_sync: 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(),
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())));
Ok(io)
}),
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", None, authorship_future);
network_starter.start_network();
let rpc_handler = rpc_handlers.io_handler();
Ok((rpc_handler, task_manager, client, transaction_pool, command_sink, backend))
}
@@ -1,53 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2021-2022 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 sp_core::{ecdsa, ed25519, sr25519};
use sp_runtime_interface::runtime_interface;
#[runtime_interface]
trait Crypto {
fn ecdsa_verify(_sig: &ecdsa::Signature, _msg: &[u8], _pub_key: &ecdsa::Public) -> bool {
true
}
#[version(2)]
fn ecdsa_verify(_sig: &ecdsa::Signature, _msg: &[u8], _pub_key: &ecdsa::Public) -> bool {
true
}
fn ed25519_verify(_sig: &ed25519::Signature, _msg: &[u8], _pub_key: &ed25519::Public) -> bool {
true
}
fn sr25519_verify(_sig: &sr25519::Signature, _msg: &[u8], _pub_key: &sr25519::Public) -> bool {
true
}
#[version(2)]
fn sr25519_verify(_sig: &sr25519::Signature, _msg: &[u8], _pub_key: &sr25519::Public) -> bool {
true
}
}
/// Provides host functions that overrides runtime signature verification
/// to always return true.
pub type SignatureVerificationOverride = crypto::HostFunctions;
// This is here to get rid of the warnings.
#[allow(unused_imports, dead_code)]
use self::crypto::{ecdsa_verify, ed25519_verify, sr25519_verify};
-310
View File
@@ -1,310 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2021-2022 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/>.
#![deny(missing_docs, unused_extern_crates)]
//! 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,
//! DatabaseSource, 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>>;
//!
//! pub struct ExecutorDispatch;
//!
//! impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
//! type ExtendHostFunctions = SignatureVerificationOverride;
//!
//! fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
//! node_runtime::api::dispatch(method, data)
//! }
//!
//! fn native_version() -> sc_executor::NativeVersion {
//! node_runtime::native_version()
//! }
//! }
//!
//! struct Requirements;
//!
//! impl ChainInfo for Requirements {
//! /// Provide a Block type with an OpaqueExtrinsic
//! type Block = node_primitives::Block;
//! /// Provide an ExecutorDispatch type for the runtime
//! type ExecutorDispatch = ExecutorDispatch;
//! /// 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, NativeElseWasmExecutor<Self::ExecutorDispatch>>,
//! 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::CheckNonZeroSender::<Self::Runtime>::new(),
//! 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, NativeElseWasmExecutor<Self::ExecutorDispatch>>>,
//! Arc<TFullBackend<Self::Block>>,
//! KeyStorePtr,
//! TaskManager,
//! InherentDataProviders,
//! Option<Box<
//! dyn ConsensusDataProvider<
//! Self::Block,
//! Transaction = TransactionFor<
//! TFullClient<Self::Block, Self::RuntimeApi, NativeElseWasmExecutor<Self::ExecutorDispatch>>,
//! Self::Block
//! >,
//! >
//! >>,
//! Self::SelectChain,
//! Self::BlockImport
//! ),
//! sc_service::Error
//! > {
//! let (
//! client,
//! backend,
//! keystore,
//! task_manager,
//! ) = new_full_parts::<Self::Block, Self::RuntimeApi, NativeElseWasmExecutor<Self::ExecutorDispatch>>(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(&*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 sc_consensus::BlockImport;
use sc_executor::{NativeElseWasmExecutor, NativeExecutionDispatch};
use sc_service::TFullClient;
use sp_api::{ConstructRuntimeApi, TransactionFor};
use sp_consensus::SelectChain;
use sp_inherents::InherentDataProvider;
use sp_runtime::traits::{Block as BlockT, SignedExtension};
mod client;
mod host_functions;
mod node;
mod utils;
pub use client::*;
pub use host_functions::*;
pub use node::*;
pub use utils::*;
/// Wrapper trait for concrete type required by this testing framework.
pub trait ChainInfo: Sized {
/// Opaque block type
type Block: BlockT;
/// ExecutorDispatch dispatch type
type ExecutorDispatch: NativeExecutionDispatch + 'static;
/// Runtime
type Runtime: frame_system::Config;
/// RuntimeApi
type RuntimeApi: Send
+ Sync
+ 'static
+ ConstructRuntimeApi<
Self::Block,
TFullClient<
Self::Block,
Self::RuntimeApi,
NativeElseWasmExecutor<Self::ExecutorDispatch>,
>,
>;
/// 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,
NativeElseWasmExecutor<Self::ExecutorDispatch>,
>,
Self::Block,
>,
> + 'static;
/// The signed extras required by the runtime
type SignedExtras: SignedExtension;
/// The inherent data providers.
type InherentDataProviders: InherentDataProvider + 'static;
/// Signed extras, this function is caled in an externalities provided environment.
fn signed_extras(
from: <Self::Runtime as frame_system::Config>::AccountId,
) -> Self::SignedExtras;
}
@@ -1,278 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2021-2022 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 crate::ChainInfo;
use futures::{
channel::{mpsc, oneshot},
FutureExt, SinkExt,
};
use jsonrpc_core::MetaIoHandler;
use manual_seal::EngineCommand;
use sc_client_api::{backend::Backend, CallExecutor, ExecutorProvider};
use sc_executor::NativeElseWasmExecutor;
use sc_service::{TFullBackend, TFullCallExecutor, TFullClient, TaskManager};
use sc_transaction_pool_api::TransactionPool;
use sp_api::{OverlayedChanges, StorageTransactionCache};
use sp_blockchain::HeaderBackend;
use sp_core::ExecutionContext;
use sp_runtime::{
generic::{BlockId, UncheckedExtrinsic},
traits::{Block as BlockT, Extrinsic, Header, NumberFor},
transaction_validity::TransactionSource,
MultiAddress, MultiSignature,
};
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
/// 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>>,
/// handle to the running node.
task_manager: Option<TaskManager>,
/// client instance
client: Arc<TFullClient<T::Block, T::RuntimeApi, NativeElseWasmExecutor<T::ExecutorDispatch>>>,
/// transaction pool
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.
backend: Arc<TFullBackend<T::Block>>,
/// Block number at initialization of this Node.
initial_block_number: NumberFor<T::Block>,
}
type EventRecord<T> = frame_system::EventRecord<
<T as frame_system::Config>::Event,
<T as frame_system::Config>::Hash,
>;
impl<T> Node<T>
where
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, NativeElseWasmExecutor<T::ExecutorDispatch>>,
>,
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),
client: client.clone(),
pool,
backend,
manual_seal_command_sink: command_sink,
initial_block_number: client.info().best_number,
}
}
/// 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()
}
/// Return a reference to the Client
pub fn client(
&self,
) -> Arc<TFullClient<T::Block, T::RuntimeApi, NativeElseWasmExecutor<T::ExecutorDispatch>>> {
self.client.clone()
}
/// Return a reference to the pool.
pub fn pool(
&self,
) -> 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,
>,
>,
> {
self.pool.clone()
}
/// Executes closure in an externalities provided environment.
pub fn with_state<R>(&self, closure: impl FnOnce() -> R) -> R
where
<TFullCallExecutor<T::Block, NativeElseWasmExecutor<T::ExecutorDispatch>> as CallExecutor<T::Block>>::Error:
std::fmt::Debug,
{
let id = BlockId::Hash(self.client.info().best_hash);
let mut overlay = OverlayedChanges::default();
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, Some(&mut extensions));
sp_externalities::set_and_run_with_externalities(&mut ext, closure)
}
/// submit some extrinsic to the node. if signer is None, will submit unsigned_extrinsic.
pub async fn submit_extrinsic(
&self,
call: impl Into<<T::Runtime as frame_system::Config>::Call>,
signer: Option<<T::Runtime as frame_system::Config>::AccountId>,
) -> Result<<T::Block as BlockT>::Hash, sc_transaction_pool::error::Error>
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 signed_data = if let Some(signer) = signer {
let extra = self.with_state(|| T::signed_extras(signer.clone()));
Some((
signer.into(),
MultiSignature::Sr25519(sp_core::sr25519::Signature::from_raw([0u8; 64])),
extra,
))
} else {
None
};
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.pool
.submit_one(&BlockId::Hash(at), TransactionSource::Local, ext.into())
.await
}
/// 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())
}
/// Instructs manual seal to seal new, possibly empty blocks.
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();
let future = sink.send(EngineCommand::SealNewBlock {
create_empty: true,
finalize: false,
parent_hash: None,
sender: Some(sender),
});
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: ");
}
/// 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;
}
}
}
impl<T: ChainInfo> Drop for Node<T> {
fn drop(&mut self) {
// Revert all blocks added since creation of the node.
let diff = self.client.info().best_number - self.initial_block_number;
self.revert_blocks(diff);
}
}
@@ -1,118 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2020-2022 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 sc_client_api::execution_extensions::ExecutionStrategies;
use sc_executor::WasmExecutionMethod;
use sc_informant::OutputFormat;
use sc_network::{
config::{NetworkConfiguration, Role, TransportConfig},
multiaddr,
};
use sc_service::{
config::KeystoreConfig, BasePath, ChainSpec, Configuration, DatabaseSource, KeepBlocks,
TransactionStorageMode,
};
use sp_keyring::sr25519::Keyring::Alice;
use tokio::runtime::Handle;
pub use sc_cli::build_runtime;
/// Base db path gotten from env
pub fn base_path() -> BasePath {
if let Some(base) = std::env::var("DB_BASE_PATH").ok() {
BasePath::new(base)
} else {
BasePath::new_temp_dir().expect("couldn't create a temp dir")
}
}
/// Produces a default configuration object, suitable for use with most set ups.
pub fn default_config(tokio_handle: Handle, mut chain_spec: Box<dyn ChainSpec>) -> Configuration {
let base_path = base_path();
let root_path = base_path.path().to_path_buf().join("chains").join(chain_spec.id());
let storage = chain_spec
.as_storage_builder()
.build_storage()
.expect("could not build storage");
chain_spec.set_storage(storage);
let key_seed = Alice.to_seed();
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(0).into());
network_config.transport = TransportConfig::MemoryOnly;
Configuration {
impl_name: "test-node".to_string(),
impl_version: "0.1".to_string(),
role: Role::Authority,
tokio_handle,
transaction_pool: Default::default(),
network: network_config,
keystore: KeystoreConfig::Path { path: root_path.join("key"), password: None },
database: DatabaseSource::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: 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,
},
rpc_http: None,
rpc_ws: None,
rpc_ipc: None,
rpc_ws_max_connections: None,
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_payload: None,
ws_max_out_buffer_capacity: None,
prometheus_config: None,
telemetry_endpoints: 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,
keystore_remote: None,
keep_blocks: KeepBlocks::All,
state_pruning: Default::default(),
transaction_storage: TransactionStorageMode::BlockBody,
runtime_cache_size: 2,
}
}