mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-08-07 14:45:45 +00:00
fd5f9292f5
Closes #2160 First part of [Extrinsic Horizon](https://github.com/paritytech/polkadot-sdk/issues/2415) Introduces a new trait `TransactionExtension` to replace `SignedExtension`. Introduce the idea of transactions which obey the runtime's extensions and have according Extension data (né Extra data) yet do not have hard-coded signatures. Deprecate the terminology of "Unsigned" when used for transactions/extrinsics owing to there now being "proper" unsigned transactions which obey the extension framework and "old-style" unsigned which do not. Instead we have __*General*__ for the former and __*Bare*__ for the latter. (Ultimately, the latter will be phased out as a type of transaction, and Bare will only be used for Inherents.) Types of extrinsic are now therefore: - Bare (no hardcoded signature, no Extra data; used to be known as "Unsigned") - Bare transactions (deprecated): Gossiped, validated with `ValidateUnsigned` (deprecated) and the `_bare_compat` bits of `TransactionExtension` (deprecated). - Inherents: Not gossiped, validated with `ProvideInherent`. - Extended (Extra data): Gossiped, validated via `TransactionExtension`. - Signed transactions (with a hardcoded signature). - General transactions (without a hardcoded signature). `TransactionExtension` differs from `SignedExtension` because: - A signature on the underlying transaction may validly not be present. - It may alter the origin during validation. - `pre_dispatch` is renamed to `prepare` and need not contain the checks present in `validate`. - `validate` and `prepare` is passed an `Origin` rather than a `AccountId`. - `validate` may pass arbitrary information into `prepare` via a new user-specifiable type `Val`. - `AdditionalSigned`/`additional_signed` is renamed to `Implicit`/`implicit`. It is encoded *for the entire transaction* and passed in to each extension as a new argument to `validate`. This facilitates the ability of extensions to acts as underlying crypto. There is a new `DispatchTransaction` trait which contains only default function impls and is impl'ed for any `TransactionExtension` impler. It provides several utility functions which reduce some of the tedium from using `TransactionExtension` (indeed, none of its regular functions should now need to be called directly). Three transaction version discriminator ("versions") are now permissible: - 0b000000100: Bare (used to be called "Unsigned"): contains Signature or Extra (extension data). After bare transactions are no longer supported, this will strictly identify an Inherents only. - 0b100000100: Old-school "Signed" Transaction: contains Signature and Extra (extension data). - 0b010000100: New-school "General" Transaction: contains Extra (extension data), but no Signature. For the New-school General Transaction, it becomes trivial for authors to publish extensions to the mechanism for authorizing an Origin, e.g. through new kinds of key-signing schemes, ZK proofs, pallet state, mutations over pre-authenticated origins or any combination of the above. ## Code Migration ### NOW: Getting it to build Wrap your `SignedExtension`s in `AsTransactionExtension`. This should be accompanied by renaming your aggregate type in line with the new terminology. E.g. Before: ```rust /// The SignedExtension to the basic transaction logic. pub type SignedExtra = ( /* snip */ MySpecialSignedExtension, ); /// Unchecked extrinsic type as expected by this runtime. pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>; ``` After: ```rust /// The extension to the basic transaction logic. pub type TxExtension = ( /* snip */ AsTransactionExtension<MySpecialSignedExtension>, ); /// Unchecked extrinsic type as expected by this runtime. pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>; ``` You'll also need to alter any transaction building logic to add a `.into()` to make the conversion happen. E.g. Before: ```rust fn construct_extrinsic( /* snip */ ) -> UncheckedExtrinsic { let extra: SignedExtra = ( /* snip */ MySpecialSignedExtension::new(/* snip */), ); let payload = SignedPayload::new(call.clone(), extra.clone()).unwrap(); let signature = payload.using_encoded(|e| sender.sign(e)); UncheckedExtrinsic::new_signed( /* snip */ Signature::Sr25519(signature), extra, ) } ``` After: ```rust fn construct_extrinsic( /* snip */ ) -> UncheckedExtrinsic { let tx_ext: TxExtension = ( /* snip */ MySpecialSignedExtension::new(/* snip */).into(), ); let payload = SignedPayload::new(call.clone(), tx_ext.clone()).unwrap(); let signature = payload.using_encoded(|e| sender.sign(e)); UncheckedExtrinsic::new_signed( /* snip */ Signature::Sr25519(signature), tx_ext, ) } ``` ### SOON: Migrating to `TransactionExtension` Most `SignedExtension`s can be trivially converted to become a `TransactionExtension`. There are a few things to know. - Instead of a single trait like `SignedExtension`, you should now implement two traits individually: `TransactionExtensionBase` and `TransactionExtension`. - Weights are now a thing and must be provided via the new function `fn weight`. #### `TransactionExtensionBase` This trait takes care of anything which is not dependent on types specific to your runtime, most notably `Call`. - `AdditionalSigned`/`additional_signed` is renamed to `Implicit`/`implicit`. - Weight must be returned by implementing the `weight` function. If your extension is associated with a pallet, you'll probably want to do this via the pallet's existing benchmarking infrastructure. #### `TransactionExtension` Generally: - `pre_dispatch` is now `prepare` and you *should not reexecute the `validate` functionality in there*! - You don't get an account ID any more; you get an origin instead. If you need to presume an account ID, then you can use the trait function `AsSystemOriginSigner::as_system_origin_signer`. - You get an additional ticket, similar to `Pre`, called `Val`. This defines data which is passed from `validate` into `prepare`. This is important since you should not be duplicating logic from `validate` to `prepare`, you need a way of passing your working from the former into the latter. This is it. - This trait takes two type parameters: `Call` and `Context`. `Call` is the runtime call type which used to be an associated type; you can just move it to become a type parameter for your trait impl. `Context` is not currently used and you can safely implement over it as an unbounded type. - There's no `AccountId` associated type any more. Just remove it. Regarding `validate`: - You get three new parameters in `validate`; all can be ignored when migrating from `SignedExtension`. - `validate` returns a tuple on success; the second item in the tuple is the new ticket type `Self::Val` which gets passed in to `prepare`. If you use any information extracted during `validate` (off-chain and on-chain, non-mutating) in `prepare` (on-chain, mutating) then you can pass it through with this. For the tuple's last item, just return the `origin` argument. Regarding `prepare`: - This is renamed from `pre_dispatch`, but there is one change: - FUNCTIONALITY TO VALIDATE THE TRANSACTION NEED NOT BE DUPLICATED FROM `validate`!! - (This is different to `SignedExtension` which was required to run the same checks in `pre_dispatch` as in `validate`.) Regarding `post_dispatch`: - Since there are no unsigned transactions handled by `TransactionExtension`, `Pre` is always defined, so the first parameter is `Self::Pre` rather than `Option<Self::Pre>`. If you make use of `SignedExtension::validate_unsigned` or `SignedExtension::pre_dispatch_unsigned`, then: - Just use the regular versions of these functions instead. - Have your logic execute in the case that the `origin` is `None`. - Ensure your transaction creation logic creates a General Transaction rather than a Bare Transaction; this means having to include all `TransactionExtension`s' data. - `ValidateUnsigned` can still be used (for now) if you need to be able to construct transactions which contain none of the extension data, however these will be phased out in stage 2 of the Transactions Horizon, so you should consider moving to an extension-centric design. ## TODO - [x] Introduce `CheckSignature` impl of `TransactionExtension` to ensure it's possible to have crypto be done wholly in a `TransactionExtension`. - [x] Deprecate `SignedExtension` and move all uses in codebase to `TransactionExtension`. - [x] `ChargeTransactionPayment` - [x] `DummyExtension` - [x] `ChargeAssetTxPayment` (asset-tx-payment) - [x] `ChargeAssetTxPayment` (asset-conversion-tx-payment) - [x] `CheckWeight` - [x] `CheckTxVersion` - [x] `CheckSpecVersion` - [x] `CheckNonce` - [x] `CheckNonZeroSender` - [x] `CheckMortality` - [x] `CheckGenesis` - [x] `CheckOnlySudoAccount` - [x] `WatchDummy` - [x] `PrevalidateAttests` - [x] `GenericSignedExtension` - [x] `SignedExtension` (chain-polkadot-bulletin) - [x] `RefundSignedExtensionAdapter` - [x] Implement `fn weight` across the board. - [ ] Go through all pre-existing extensions which assume an account signer and explicitly handle the possibility of another kind of origin. - [x] `CheckNonce` should probably succeed in the case of a non-account origin. - [x] `CheckNonZeroSender` should succeed in the case of a non-account origin. - [x] `ChargeTransactionPayment` and family should fail in the case of a non-account origin. - [ ] - [x] Fix any broken tests. --------- Signed-off-by: georgepisaltu <george.pisaltu@parity.io> Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io> Signed-off-by: Andrei Sandu <andrei-mihail@parity.io> Co-authored-by: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Co-authored-by: georgepisaltu <52418509+georgepisaltu@users.noreply.github.com> Co-authored-by: Chevdor <chevdor@users.noreply.github.com> Co-authored-by: Bastian Köcher <git@kchr.de> Co-authored-by: Maciej <maciej.zyszkiewicz@parity.io> Co-authored-by: Javier Viola <javier@parity.io> Co-authored-by: Marcin S. <marcin@realemail.net> Co-authored-by: Tsvetomir Dimitrov <tsvetomir@parity.io> Co-authored-by: Javier Bullrich <javier@bullrich.dev> Co-authored-by: Koute <koute@users.noreply.github.com> Co-authored-by: Adrian Catangiu <adrian@parity.io> Co-authored-by: Vladimir Istyufeev <vladimir@parity.io> Co-authored-by: Ross Bulat <ross@parity.io> Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com> Co-authored-by: Liam Aharon <liam.aharon@hotmail.com> Co-authored-by: Svyatoslav Nikolsky <svyatonik@gmail.com> Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: s0me0ne-unkn0wn <48632512+s0me0ne-unkn0wn@users.noreply.github.com> Co-authored-by: ordian <write@reusable.software> Co-authored-by: Sebastian Kunert <skunert49@gmail.com> Co-authored-by: Aaro Altonen <48052676+altonen@users.noreply.github.com> Co-authored-by: Dmitry Markin <dmitry@markin.tech> Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Co-authored-by: Alexander Samusev <41779041+alvicsam@users.noreply.github.com> Co-authored-by: Julian Eager <eagr@tutanota.com> Co-authored-by: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Co-authored-by: Davide Galassi <davxy@datawok.net> Co-authored-by: Dónal Murray <donal.murray@parity.io> Co-authored-by: yjh <yjh465402634@gmail.com> Co-authored-by: Tom Mi <tommi@niemi.lol> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Will | Paradox | ParaNodes.io <79228812+paradox-tt@users.noreply.github.com> Co-authored-by: Bastian Köcher <info@kchr.de> Co-authored-by: Joshy Orndorff <JoshOrndorff@users.noreply.github.com> Co-authored-by: Joshy Orndorff <git-user-email.h0ly5@simplelogin.com> Co-authored-by: PG Herveou <pgherveou@gmail.com> Co-authored-by: Alexander Theißen <alex.theissen@me.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Juan Girini <juangirini@gmail.com> Co-authored-by: bader y <ibnbassem@gmail.com> Co-authored-by: James Wilson <james@jsdw.me> Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Co-authored-by: asynchronous rob <rphmeier@gmail.com> Co-authored-by: Parth <desaiparth08@gmail.com> Co-authored-by: Andrew Jones <ascjones@gmail.com> Co-authored-by: Jonathan Udd <jonathan@dwellir.com> Co-authored-by: Serban Iorga <serban@parity.io> Co-authored-by: Egor_P <egor@parity.io> Co-authored-by: Branislav Kontur <bkontur@gmail.com> Co-authored-by: Evgeny Snitko <evgeny@parity.io> Co-authored-by: Just van Stam <vstam1@users.noreply.github.com> Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com> Co-authored-by: gupnik <nikhilgupta.iitk@gmail.com> Co-authored-by: dzmitry-lahoda <dzmitry@lahoda.pro> Co-authored-by: zhiqiangxu <652732310@qq.com> Co-authored-by: Nazar Mokrynskyi <nazar@mokrynskyi.com> Co-authored-by: Anwesh <anweshknayak@gmail.com> Co-authored-by: cheme <emericchevalier.pro@gmail.com> Co-authored-by: Sam Johnson <sam@durosoft.com> Co-authored-by: kianenigma <kian@parity.io> Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com> Co-authored-by: Muharem <ismailov.m.h@gmail.com> Co-authored-by: joepetrowski <joe@parity.io> Co-authored-by: Alexandru Gheorghe <49718502+alexggh@users.noreply.github.com> Co-authored-by: Gabriel Facco de Arruda <arrudagates@gmail.com> Co-authored-by: Squirrel <gilescope@gmail.com> Co-authored-by: Andrei Sandu <54316454+sandreim@users.noreply.github.com> Co-authored-by: georgepisaltu <george.pisaltu@parity.io> Co-authored-by: command-bot <>
945 lines
30 KiB
Rust
945 lines
30 KiB
Rust
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// This file is part of Cumulus.
|
|
|
|
// Cumulus 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.
|
|
|
|
// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Crate used for testing with Cumulus.
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
/// Utilities used for benchmarking
|
|
pub mod bench_utils;
|
|
|
|
pub mod chain_spec;
|
|
|
|
use runtime::AccountId;
|
|
use sc_executor::{HeapAllocStrategy, WasmExecutor, DEFAULT_HEAP_ALLOC_STRATEGY};
|
|
use std::{
|
|
collections::HashSet,
|
|
future::Future,
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
time::Duration,
|
|
};
|
|
use url::Url;
|
|
|
|
use crate::runtime::Weight;
|
|
use cumulus_client_cli::{CollatorOptions, RelayChainMode};
|
|
use cumulus_client_consensus_common::{
|
|
ParachainBlockImport as TParachainBlockImport, ParachainCandidate, ParachainConsensus,
|
|
};
|
|
use cumulus_client_pov_recovery::RecoveryHandle;
|
|
#[allow(deprecated)]
|
|
use cumulus_client_service::old_consensus;
|
|
use cumulus_client_service::{
|
|
build_network, prepare_node_config, start_relay_chain_tasks, BuildNetworkParams,
|
|
CollatorSybilResistance, DARecoveryProfile, StartRelayChainTasksParams,
|
|
};
|
|
use cumulus_primitives_core::ParaId;
|
|
use cumulus_relay_chain_inprocess_interface::RelayChainInProcessInterface;
|
|
use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult};
|
|
use cumulus_relay_chain_minimal_node::{
|
|
build_minimal_relay_chain_node_light_client, build_minimal_relay_chain_node_with_rpc,
|
|
};
|
|
|
|
use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi};
|
|
|
|
use frame_system_rpc_runtime_api::AccountNonceApi;
|
|
use polkadot_node_subsystem::{errors::RecoveryError, messages::AvailabilityRecoveryMessage};
|
|
use polkadot_overseer::Handle as OverseerHandle;
|
|
use polkadot_primitives::{CandidateHash, CollatorPair, Hash as PHash, PersistedValidationData};
|
|
use polkadot_service::ProvideRuntimeApi;
|
|
use sc_consensus::ImportQueue;
|
|
use sc_network::{
|
|
config::{FullNetworkConfiguration, TransportConfig},
|
|
multiaddr, NetworkBlock, NetworkService, NetworkStateInfo,
|
|
};
|
|
use sc_service::{
|
|
config::{
|
|
BlocksPruning, DatabaseSource, KeystoreConfig, MultiaddrWithPeerId, NetworkConfiguration,
|
|
OffchainWorkerConfig, PruningMode, RpcBatchRequestConfig, WasmExecutionMethod,
|
|
},
|
|
BasePath, ChainSpec as ChainSpecService, Configuration, Error as ServiceError,
|
|
PartialComponents, Role, RpcHandlers, TFullBackend, TFullClient, TaskManager,
|
|
};
|
|
use sp_arithmetic::traits::SaturatedConversion;
|
|
use sp_blockchain::HeaderBackend;
|
|
use sp_core::{Pair, H256};
|
|
use sp_keyring::Sr25519Keyring;
|
|
use sp_runtime::{codec::Encode, generic};
|
|
use sp_state_machine::BasicExternalities;
|
|
use std::sync::Arc;
|
|
use substrate_test_client::{
|
|
BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput,
|
|
};
|
|
|
|
pub use chain_spec::*;
|
|
pub use cumulus_test_runtime as runtime;
|
|
pub use sp_keyring::Sr25519Keyring as Keyring;
|
|
|
|
const LOG_TARGET: &str = "cumulus-test-service";
|
|
|
|
/// A consensus that will never produce any block.
|
|
#[derive(Clone)]
|
|
struct NullConsensus;
|
|
|
|
#[async_trait::async_trait]
|
|
impl ParachainConsensus<Block> for NullConsensus {
|
|
async fn produce_candidate(
|
|
&mut self,
|
|
_: &Header,
|
|
_: PHash,
|
|
_: &PersistedValidationData,
|
|
) -> Option<ParachainCandidate<Block>> {
|
|
None
|
|
}
|
|
}
|
|
|
|
/// The signature of the announce block fn.
|
|
pub type AnnounceBlockFn = Arc<dyn Fn(Hash, Option<Vec<u8>>) + Send + Sync>;
|
|
|
|
/// Native executor instance.
|
|
pub struct RuntimeExecutor;
|
|
|
|
impl sc_executor::NativeExecutionDispatch for RuntimeExecutor {
|
|
type ExtendHostFunctions = cumulus_client_service::storage_proof_size::HostFunctions;
|
|
|
|
fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
|
|
cumulus_test_runtime::api::dispatch(method, data)
|
|
}
|
|
|
|
fn native_version() -> sc_executor::NativeVersion {
|
|
cumulus_test_runtime::native_version()
|
|
}
|
|
}
|
|
|
|
/// The client type being used by the test service.
|
|
pub type Client = TFullClient<
|
|
runtime::NodeBlock,
|
|
runtime::RuntimeApi,
|
|
sc_executor::NativeElseWasmExecutor<RuntimeExecutor>,
|
|
>;
|
|
|
|
/// The backend type being used by the test service.
|
|
pub type Backend = TFullBackend<Block>;
|
|
|
|
/// The block-import type being used by the test service.
|
|
pub type ParachainBlockImport = TParachainBlockImport<Block, Arc<Client>, Backend>;
|
|
|
|
/// Transaction pool type used by the test service
|
|
pub type TransactionPool = Arc<sc_transaction_pool::FullPool<Block, Client>>;
|
|
|
|
/// Recovery handle that fails regularly to simulate unavailable povs.
|
|
pub struct FailingRecoveryHandle {
|
|
overseer_handle: OverseerHandle,
|
|
counter: u32,
|
|
failed_hashes: HashSet<CandidateHash>,
|
|
}
|
|
|
|
impl FailingRecoveryHandle {
|
|
/// Create a new FailingRecoveryHandle
|
|
pub fn new(overseer_handle: OverseerHandle) -> Self {
|
|
Self { overseer_handle, counter: 0, failed_hashes: Default::default() }
|
|
}
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl RecoveryHandle for FailingRecoveryHandle {
|
|
async fn send_recovery_msg(
|
|
&mut self,
|
|
message: AvailabilityRecoveryMessage,
|
|
origin: &'static str,
|
|
) {
|
|
let AvailabilityRecoveryMessage::RecoverAvailableData(ref receipt, _, _, _) = message;
|
|
let candidate_hash = receipt.hash();
|
|
|
|
// For every 3rd block we immediately signal unavailability to trigger
|
|
// a retry. The same candidate is never failed multiple times to ensure progress.
|
|
if self.counter % 3 == 0 && self.failed_hashes.insert(candidate_hash) {
|
|
tracing::info!(target: LOG_TARGET, ?candidate_hash, "Failing pov recovery.");
|
|
|
|
let AvailabilityRecoveryMessage::RecoverAvailableData(_, _, _, back_sender) = message;
|
|
back_sender
|
|
.send(Err(RecoveryError::Unavailable))
|
|
.expect("Return channel should work here.");
|
|
} else {
|
|
self.overseer_handle.send_msg(message, origin).await;
|
|
}
|
|
self.counter += 1;
|
|
}
|
|
}
|
|
|
|
/// Assembly of PartialComponents (enough to run chain ops subcommands)
|
|
pub type Service = PartialComponents<
|
|
Client,
|
|
Backend,
|
|
(),
|
|
sc_consensus::import_queue::BasicQueue<Block>,
|
|
sc_transaction_pool::FullPool<Block, Client>,
|
|
ParachainBlockImport,
|
|
>;
|
|
|
|
/// Starts a `ServiceBuilder` for a full service.
|
|
///
|
|
/// Use this macro if you don't actually need the full service, but just the builder in order to
|
|
/// be able to perform chain operations.
|
|
pub fn new_partial(
|
|
config: &mut Configuration,
|
|
enable_import_proof_record: bool,
|
|
) -> Result<Service, sc_service::Error> {
|
|
let heap_pages = config
|
|
.default_heap_pages
|
|
.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |h| HeapAllocStrategy::Static { extra_pages: h as _ });
|
|
|
|
let wasm = WasmExecutor::builder()
|
|
.with_execution_method(config.wasm_method)
|
|
.with_onchain_heap_alloc_strategy(heap_pages)
|
|
.with_offchain_heap_alloc_strategy(heap_pages)
|
|
.with_max_runtime_instances(config.max_runtime_instances)
|
|
.with_runtime_cache_size(config.runtime_cache_size)
|
|
.build();
|
|
|
|
let executor =
|
|
sc_executor::NativeElseWasmExecutor::<RuntimeExecutor>::new_with_wasm_executor(wasm);
|
|
|
|
let (client, backend, keystore_container, task_manager) =
|
|
sc_service::new_full_parts_record_import::<Block, RuntimeApi, _>(
|
|
config,
|
|
None,
|
|
executor,
|
|
enable_import_proof_record,
|
|
)?;
|
|
let client = Arc::new(client);
|
|
|
|
let block_import =
|
|
ParachainBlockImport::new_with_delayed_best_block(client.clone(), backend.clone());
|
|
|
|
let registry = config.prometheus_registry();
|
|
|
|
let transaction_pool = sc_transaction_pool::BasicPool::new_full(
|
|
config.transaction_pool.clone(),
|
|
config.role.is_authority().into(),
|
|
config.prometheus_registry(),
|
|
task_manager.spawn_essential_handle(),
|
|
client.clone(),
|
|
);
|
|
|
|
let import_queue = cumulus_client_consensus_relay_chain::import_queue(
|
|
client.clone(),
|
|
block_import.clone(),
|
|
|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) },
|
|
&task_manager.spawn_essential_handle(),
|
|
registry,
|
|
)?;
|
|
|
|
let params = PartialComponents {
|
|
backend,
|
|
client,
|
|
import_queue,
|
|
keystore_container,
|
|
task_manager,
|
|
transaction_pool,
|
|
select_chain: (),
|
|
other: block_import,
|
|
};
|
|
|
|
Ok(params)
|
|
}
|
|
|
|
async fn build_relay_chain_interface(
|
|
relay_chain_config: Configuration,
|
|
collator_key: Option<CollatorPair>,
|
|
collator_options: CollatorOptions,
|
|
task_manager: &mut TaskManager,
|
|
) -> RelayChainResult<Arc<dyn RelayChainInterface + 'static>> {
|
|
let relay_chain_full_node = match collator_options.relay_chain_mode {
|
|
cumulus_client_cli::RelayChainMode::Embedded => polkadot_test_service::new_full(
|
|
relay_chain_config,
|
|
if let Some(ref key) = collator_key {
|
|
polkadot_service::IsParachainNode::Collator(key.clone())
|
|
} else {
|
|
polkadot_service::IsParachainNode::Collator(CollatorPair::generate().0)
|
|
},
|
|
None,
|
|
polkadot_service::CollatorOverseerGen,
|
|
)
|
|
.map_err(|e| RelayChainError::Application(Box::new(e) as Box<_>))?,
|
|
cumulus_client_cli::RelayChainMode::ExternalRpc(rpc_target_urls) =>
|
|
return build_minimal_relay_chain_node_with_rpc(
|
|
relay_chain_config,
|
|
task_manager,
|
|
rpc_target_urls,
|
|
)
|
|
.await
|
|
.map(|r| r.0),
|
|
cumulus_client_cli::RelayChainMode::LightClient =>
|
|
return build_minimal_relay_chain_node_light_client(relay_chain_config, task_manager)
|
|
.await
|
|
.map(|r| r.0),
|
|
};
|
|
|
|
task_manager.add_child(relay_chain_full_node.task_manager);
|
|
tracing::info!("Using inprocess node.");
|
|
Ok(Arc::new(RelayChainInProcessInterface::new(
|
|
relay_chain_full_node.client.clone(),
|
|
relay_chain_full_node.backend.clone(),
|
|
relay_chain_full_node.sync_service.clone(),
|
|
relay_chain_full_node.overseer_handle.ok_or(RelayChainError::GenericError(
|
|
"Overseer should be running in full node.".to_string(),
|
|
))?,
|
|
)))
|
|
}
|
|
|
|
/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.
|
|
///
|
|
/// This is the actual implementation that is abstract over the executor and the runtime api.
|
|
#[sc_tracing::logging::prefix_logs_with(parachain_config.network.node_name.as_str())]
|
|
pub async fn start_node_impl<RB>(
|
|
parachain_config: Configuration,
|
|
collator_key: Option<CollatorPair>,
|
|
relay_chain_config: Configuration,
|
|
para_id: ParaId,
|
|
wrap_announce_block: Option<Box<dyn FnOnce(AnnounceBlockFn) -> AnnounceBlockFn>>,
|
|
fail_pov_recovery: bool,
|
|
rpc_ext_builder: RB,
|
|
consensus: Consensus,
|
|
collator_options: CollatorOptions,
|
|
proof_recording_during_import: bool,
|
|
) -> sc_service::error::Result<(
|
|
TaskManager,
|
|
Arc<Client>,
|
|
Arc<NetworkService<Block, H256>>,
|
|
RpcHandlers,
|
|
TransactionPool,
|
|
Arc<Backend>,
|
|
)>
|
|
where
|
|
RB: Fn(Arc<Client>) -> Result<jsonrpsee::RpcModule<()>, sc_service::Error> + Send + 'static,
|
|
{
|
|
let mut parachain_config = prepare_node_config(parachain_config);
|
|
|
|
let params = new_partial(&mut parachain_config, proof_recording_during_import)?;
|
|
|
|
let transaction_pool = params.transaction_pool.clone();
|
|
let mut task_manager = params.task_manager;
|
|
|
|
let client = params.client.clone();
|
|
let backend = params.backend.clone();
|
|
|
|
let block_import = params.other;
|
|
|
|
let relay_chain_interface = build_relay_chain_interface(
|
|
relay_chain_config,
|
|
collator_key.clone(),
|
|
collator_options.clone(),
|
|
&mut task_manager,
|
|
)
|
|
.await
|
|
.map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;
|
|
|
|
let import_queue_service = params.import_queue.service();
|
|
let net_config = FullNetworkConfiguration::new(¶chain_config.network);
|
|
|
|
let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =
|
|
build_network(BuildNetworkParams {
|
|
parachain_config: ¶chain_config,
|
|
net_config,
|
|
client: client.clone(),
|
|
transaction_pool: transaction_pool.clone(),
|
|
para_id,
|
|
spawn_handle: task_manager.spawn_handle(),
|
|
relay_chain_interface: relay_chain_interface.clone(),
|
|
import_queue: params.import_queue,
|
|
sybil_resistance_level: CollatorSybilResistance::Unresistant, // no consensus
|
|
})
|
|
.await?;
|
|
|
|
let prometheus_registry = parachain_config.prometheus_registry().cloned();
|
|
|
|
let rpc_builder = {
|
|
let client = client.clone();
|
|
Box::new(move |_, _| rpc_ext_builder(client.clone()))
|
|
};
|
|
|
|
let rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams {
|
|
rpc_builder,
|
|
client: client.clone(),
|
|
transaction_pool: transaction_pool.clone(),
|
|
task_manager: &mut task_manager,
|
|
config: parachain_config,
|
|
keystore: params.keystore_container.keystore(),
|
|
backend: backend.clone(),
|
|
network: network.clone(),
|
|
sync_service: sync_service.clone(),
|
|
system_rpc_tx,
|
|
tx_handler_controller,
|
|
telemetry: None,
|
|
})?;
|
|
|
|
let announce_block = {
|
|
let sync_service = sync_service.clone();
|
|
Arc::new(move |hash, data| sync_service.announce_block(hash, data))
|
|
};
|
|
|
|
let announce_block = wrap_announce_block
|
|
.map(|w| (w)(announce_block.clone()))
|
|
.unwrap_or_else(|| announce_block);
|
|
|
|
let relay_chain_interface_for_closure = relay_chain_interface.clone();
|
|
|
|
let overseer_handle = relay_chain_interface
|
|
.overseer_handle()
|
|
.map_err(|e| sc_service::Error::Application(Box::new(e)))?;
|
|
|
|
let recovery_handle: Box<dyn RecoveryHandle> = if fail_pov_recovery {
|
|
Box::new(FailingRecoveryHandle::new(overseer_handle.clone()))
|
|
} else {
|
|
Box::new(overseer_handle.clone())
|
|
};
|
|
let is_collator = collator_key.is_some();
|
|
let relay_chain_slot_duration = Duration::from_secs(6);
|
|
|
|
start_relay_chain_tasks(StartRelayChainTasksParams {
|
|
client: client.clone(),
|
|
announce_block: announce_block.clone(),
|
|
para_id,
|
|
relay_chain_interface: relay_chain_interface.clone(),
|
|
task_manager: &mut task_manager,
|
|
da_recovery_profile: if is_collator {
|
|
DARecoveryProfile::Collator
|
|
} else {
|
|
DARecoveryProfile::FullNode
|
|
},
|
|
import_queue: import_queue_service,
|
|
relay_chain_slot_duration,
|
|
recovery_handle,
|
|
sync_service: sync_service.clone(),
|
|
})?;
|
|
|
|
if let Some(collator_key) = collator_key {
|
|
let parachain_consensus: Box<dyn ParachainConsensus<Block>> = match consensus {
|
|
Consensus::RelayChain => {
|
|
let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(
|
|
task_manager.spawn_handle(),
|
|
client.clone(),
|
|
transaction_pool.clone(),
|
|
prometheus_registry.as_ref(),
|
|
None,
|
|
);
|
|
let relay_chain_interface2 = relay_chain_interface_for_closure.clone();
|
|
Box::new(cumulus_client_consensus_relay_chain::RelayChainConsensus::new(
|
|
para_id,
|
|
proposer_factory,
|
|
move |_, (relay_parent, validation_data)| {
|
|
let relay_chain_interface = relay_chain_interface_for_closure.clone();
|
|
async move {
|
|
let parachain_inherent =
|
|
cumulus_client_parachain_inherent::ParachainInherentDataProvider::create_at(
|
|
relay_parent,
|
|
&relay_chain_interface,
|
|
&validation_data,
|
|
para_id,
|
|
).await;
|
|
|
|
let time = sp_timestamp::InherentDataProvider::from_system_time();
|
|
|
|
let parachain_inherent = parachain_inherent.ok_or_else(|| {
|
|
Box::<dyn std::error::Error + Send + Sync>::from(String::from(
|
|
"error",
|
|
))
|
|
})?;
|
|
Ok((time, parachain_inherent))
|
|
}
|
|
},
|
|
block_import,
|
|
relay_chain_interface2,
|
|
))
|
|
},
|
|
Consensus::Null => Box::new(NullConsensus),
|
|
};
|
|
|
|
#[allow(deprecated)]
|
|
old_consensus::start_collator(old_consensus::StartCollatorParams {
|
|
block_status: client.clone(),
|
|
announce_block,
|
|
runtime_api: client.clone(),
|
|
spawner: task_manager.spawn_handle(),
|
|
para_id,
|
|
parachain_consensus,
|
|
key: collator_key,
|
|
overseer_handle,
|
|
})
|
|
.await;
|
|
}
|
|
|
|
start_network.start_network();
|
|
|
|
Ok((task_manager, client, network, rpc_handlers, transaction_pool, backend))
|
|
}
|
|
|
|
/// A Cumulus test node instance used for testing.
|
|
pub struct TestNode {
|
|
/// TaskManager's instance.
|
|
pub task_manager: TaskManager,
|
|
/// Client's instance.
|
|
pub client: Arc<Client>,
|
|
/// Node's network.
|
|
pub network: Arc<NetworkService<Block, H256>>,
|
|
/// The `MultiaddrWithPeerId` to this node. This is useful if you want to pass it as "boot
|
|
/// node" to other nodes.
|
|
pub addr: MultiaddrWithPeerId,
|
|
/// RPCHandlers to make RPC queries.
|
|
pub rpc_handlers: RpcHandlers,
|
|
/// Node's transaction pool
|
|
pub transaction_pool: TransactionPool,
|
|
/// Node's backend
|
|
pub backend: Arc<Backend>,
|
|
}
|
|
|
|
#[allow(missing_docs)]
|
|
pub enum Consensus {
|
|
/// Use the relay-chain provided consensus.
|
|
RelayChain,
|
|
/// Use the null consensus that will never produce any block.
|
|
Null,
|
|
}
|
|
|
|
/// A builder to create a [`TestNode`].
|
|
pub struct TestNodeBuilder {
|
|
para_id: ParaId,
|
|
tokio_handle: tokio::runtime::Handle,
|
|
key: Sr25519Keyring,
|
|
collator_key: Option<CollatorPair>,
|
|
parachain_nodes: Vec<MultiaddrWithPeerId>,
|
|
parachain_nodes_exclusive: bool,
|
|
relay_chain_nodes: Vec<MultiaddrWithPeerId>,
|
|
wrap_announce_block: Option<Box<dyn FnOnce(AnnounceBlockFn) -> AnnounceBlockFn>>,
|
|
storage_update_func_parachain: Option<Box<dyn Fn()>>,
|
|
storage_update_func_relay_chain: Option<Box<dyn Fn()>>,
|
|
consensus: Consensus,
|
|
relay_chain_mode: RelayChainMode,
|
|
endowed_accounts: Vec<AccountId>,
|
|
record_proof_during_import: bool,
|
|
}
|
|
|
|
impl TestNodeBuilder {
|
|
/// Create a new instance of `Self`.
|
|
///
|
|
/// `para_id` - The parachain id this node is running for.
|
|
/// `tokio_handle` - The tokio handler to use.
|
|
/// `key` - The key that will be used to generate the name and that will be passed as
|
|
/// `dev_seed`.
|
|
pub fn new(para_id: ParaId, tokio_handle: tokio::runtime::Handle, key: Sr25519Keyring) -> Self {
|
|
TestNodeBuilder {
|
|
key,
|
|
para_id,
|
|
tokio_handle,
|
|
collator_key: None,
|
|
parachain_nodes: Vec::new(),
|
|
parachain_nodes_exclusive: false,
|
|
relay_chain_nodes: Vec::new(),
|
|
wrap_announce_block: None,
|
|
storage_update_func_parachain: None,
|
|
storage_update_func_relay_chain: None,
|
|
consensus: Consensus::RelayChain,
|
|
endowed_accounts: Default::default(),
|
|
relay_chain_mode: RelayChainMode::Embedded,
|
|
record_proof_during_import: true,
|
|
}
|
|
}
|
|
|
|
/// Enable collator for this node.
|
|
pub fn enable_collator(mut self) -> Self {
|
|
let collator_key = CollatorPair::generate().0;
|
|
self.collator_key = Some(collator_key);
|
|
self
|
|
}
|
|
|
|
/// Instruct the node to exclusively connect to registered parachain nodes.
|
|
///
|
|
/// Parachain nodes can be registered using [`Self::connect_to_parachain_node`] and
|
|
/// [`Self::connect_to_parachain_nodes`].
|
|
pub fn exclusively_connect_to_registered_parachain_nodes(mut self) -> Self {
|
|
self.parachain_nodes_exclusive = true;
|
|
self
|
|
}
|
|
|
|
/// Make the node connect to the given parachain node.
|
|
///
|
|
/// By default the node will not be connected to any node or will be able to discover any other
|
|
/// node.
|
|
pub fn connect_to_parachain_node(mut self, node: &TestNode) -> Self {
|
|
self.parachain_nodes.push(node.addr.clone());
|
|
self
|
|
}
|
|
|
|
/// Make the node connect to the given parachain nodes.
|
|
///
|
|
/// By default the node will not be connected to any node or will be able to discover any other
|
|
/// node.
|
|
pub fn connect_to_parachain_nodes<'a>(
|
|
mut self,
|
|
nodes: impl IntoIterator<Item = &'a TestNode>,
|
|
) -> Self {
|
|
self.parachain_nodes.extend(nodes.into_iter().map(|n| n.addr.clone()));
|
|
self
|
|
}
|
|
|
|
/// Make the node connect to the given relay chain node.
|
|
///
|
|
/// By default the node will not be connected to any node or will be able to discover any other
|
|
/// node.
|
|
pub fn connect_to_relay_chain_node(
|
|
mut self,
|
|
node: &polkadot_test_service::PolkadotTestNode,
|
|
) -> Self {
|
|
self.relay_chain_nodes.push(node.addr.clone());
|
|
self
|
|
}
|
|
|
|
/// Make the node connect to the given relay chain nodes.
|
|
///
|
|
/// By default the node will not be connected to any node or will be able to discover any other
|
|
/// node.
|
|
pub fn connect_to_relay_chain_nodes<'a>(
|
|
mut self,
|
|
nodes: impl IntoIterator<Item = &'a polkadot_test_service::PolkadotTestNode>,
|
|
) -> Self {
|
|
self.relay_chain_nodes.extend(nodes.into_iter().map(|n| n.addr.clone()));
|
|
self
|
|
}
|
|
|
|
/// Wrap the announce block function of this node.
|
|
pub fn wrap_announce_block(
|
|
mut self,
|
|
wrap: impl FnOnce(AnnounceBlockFn) -> AnnounceBlockFn + 'static,
|
|
) -> Self {
|
|
self.wrap_announce_block = Some(Box::new(wrap));
|
|
self
|
|
}
|
|
|
|
/// Allows accessing the parachain storage before the test node is built.
|
|
pub fn update_storage_parachain(mut self, updater: impl Fn() + 'static) -> Self {
|
|
self.storage_update_func_parachain = Some(Box::new(updater));
|
|
self
|
|
}
|
|
|
|
/// Allows accessing the relay chain storage before the test node is built.
|
|
pub fn update_storage_relay_chain(mut self, updater: impl Fn() + 'static) -> Self {
|
|
self.storage_update_func_relay_chain = Some(Box::new(updater));
|
|
self
|
|
}
|
|
|
|
/// Use the null consensus that will never author any block.
|
|
pub fn use_null_consensus(mut self) -> Self {
|
|
self.consensus = Consensus::Null;
|
|
self
|
|
}
|
|
|
|
/// Connect to full node via RPC.
|
|
pub fn use_external_relay_chain_node_at_url(mut self, network_address: Url) -> Self {
|
|
self.relay_chain_mode = RelayChainMode::ExternalRpc(vec![network_address]);
|
|
self
|
|
}
|
|
|
|
/// Connect to full node via RPC.
|
|
pub fn use_external_relay_chain_node_at_port(mut self, port: u16) -> Self {
|
|
let mut localhost_url =
|
|
Url::parse("ws://localhost").expect("Should be able to parse localhost Url");
|
|
localhost_url.set_port(Some(port)).expect("Should be able to set port");
|
|
self.relay_chain_mode = RelayChainMode::ExternalRpc(vec![localhost_url]);
|
|
self
|
|
}
|
|
|
|
/// Accounts which will have an initial balance.
|
|
pub fn endowed_accounts(mut self, accounts: Vec<AccountId>) -> TestNodeBuilder {
|
|
self.endowed_accounts = accounts;
|
|
self
|
|
}
|
|
|
|
/// Record proofs during import.
|
|
pub fn import_proof_recording(mut self, should_record_proof: bool) -> TestNodeBuilder {
|
|
self.record_proof_during_import = should_record_proof;
|
|
self
|
|
}
|
|
|
|
/// Build the [`TestNode`].
|
|
pub async fn build(self) -> TestNode {
|
|
let parachain_config = node_config(
|
|
self.storage_update_func_parachain.unwrap_or_else(|| Box::new(|| ())),
|
|
self.tokio_handle.clone(),
|
|
self.key,
|
|
self.parachain_nodes,
|
|
self.parachain_nodes_exclusive,
|
|
self.para_id,
|
|
self.collator_key.is_some(),
|
|
self.endowed_accounts,
|
|
)
|
|
.expect("could not generate Configuration");
|
|
|
|
let mut relay_chain_config = polkadot_test_service::node_config(
|
|
self.storage_update_func_relay_chain.unwrap_or_else(|| Box::new(|| ())),
|
|
self.tokio_handle,
|
|
self.key,
|
|
self.relay_chain_nodes,
|
|
false,
|
|
);
|
|
|
|
let collator_options = CollatorOptions { relay_chain_mode: self.relay_chain_mode };
|
|
|
|
relay_chain_config.network.node_name =
|
|
format!("{} (relay chain)", relay_chain_config.network.node_name);
|
|
|
|
let multiaddr = parachain_config.network.listen_addresses[0].clone();
|
|
let (task_manager, client, network, rpc_handlers, transaction_pool, backend) =
|
|
start_node_impl(
|
|
parachain_config,
|
|
self.collator_key,
|
|
relay_chain_config,
|
|
self.para_id,
|
|
self.wrap_announce_block,
|
|
false,
|
|
|_| Ok(jsonrpsee::RpcModule::new(())),
|
|
self.consensus,
|
|
collator_options,
|
|
self.record_proof_during_import,
|
|
)
|
|
.await
|
|
.expect("could not create Cumulus test service");
|
|
|
|
let peer_id = network.local_peer_id();
|
|
let addr = MultiaddrWithPeerId { multiaddr, peer_id };
|
|
|
|
TestNode { task_manager, client, network, addr, rpc_handlers, transaction_pool, backend }
|
|
}
|
|
}
|
|
|
|
/// Create a Cumulus `Configuration`.
|
|
///
|
|
/// By default an in-memory socket will be used, therefore you need to provide nodes if you want the
|
|
/// node to be connected to other nodes. If `nodes_exclusive` is `true`, the node will only connect
|
|
/// to the given `nodes` and not to any other node. The `storage_update_func` can be used to make
|
|
/// adjustments to the runtime genesis.
|
|
pub fn node_config(
|
|
storage_update_func: impl Fn(),
|
|
tokio_handle: tokio::runtime::Handle,
|
|
key: Sr25519Keyring,
|
|
nodes: Vec<MultiaddrWithPeerId>,
|
|
nodes_exlusive: bool,
|
|
para_id: ParaId,
|
|
is_collator: bool,
|
|
endowed_accounts: Vec<AccountId>,
|
|
) -> Result<Configuration, ServiceError> {
|
|
let base_path = BasePath::new_temp_dir()?;
|
|
let root = base_path.path().join(format!("cumulus_test_service_{}", key));
|
|
let role = if is_collator { Role::Authority } else { Role::Full };
|
|
let key_seed = key.to_seed();
|
|
let mut spec =
|
|
Box::new(chain_spec::get_chain_spec_with_extra_endowed(Some(para_id), endowed_accounts));
|
|
|
|
let mut storage = spec.as_storage_builder().build_storage().expect("could not build storage");
|
|
|
|
BasicExternalities::execute_with_storage(&mut storage, storage_update_func);
|
|
spec.set_storage(storage);
|
|
|
|
let mut network_config = NetworkConfiguration::new(
|
|
format!("{} (parachain)", key_seed),
|
|
"network/test/0.1",
|
|
Default::default(),
|
|
None,
|
|
);
|
|
|
|
if nodes_exlusive {
|
|
network_config.default_peers_set.reserved_nodes = nodes;
|
|
network_config.default_peers_set.non_reserved_mode =
|
|
sc_network::config::NonReservedPeerMode::Deny;
|
|
} else {
|
|
network_config.boot_nodes = nodes;
|
|
}
|
|
|
|
network_config.allow_non_globals_in_dht = true;
|
|
|
|
network_config
|
|
.listen_addresses
|
|
.push(multiaddr::Protocol::Memory(rand::random()).into());
|
|
|
|
network_config.transport = TransportConfig::MemoryOnly;
|
|
|
|
Ok(Configuration {
|
|
impl_name: "cumulus-test-node".to_string(),
|
|
impl_version: "0.1".to_string(),
|
|
role,
|
|
tokio_handle,
|
|
transaction_pool: Default::default(),
|
|
network: network_config,
|
|
keystore: KeystoreConfig::InMemory,
|
|
database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128 },
|
|
trie_cache_maximum_size: Some(64 * 1024 * 1024),
|
|
state_pruning: Some(PruningMode::ArchiveAll),
|
|
blocks_pruning: BlocksPruning::KeepAll,
|
|
chain_spec: spec,
|
|
wasm_method: WasmExecutionMethod::Compiled {
|
|
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::PoolingCopyOnWrite,
|
|
},
|
|
rpc_addr: None,
|
|
rpc_max_connections: Default::default(),
|
|
rpc_cors: None,
|
|
rpc_methods: Default::default(),
|
|
rpc_max_request_size: Default::default(),
|
|
rpc_max_response_size: Default::default(),
|
|
rpc_id_provider: None,
|
|
rpc_max_subs_per_conn: Default::default(),
|
|
rpc_port: 9945,
|
|
rpc_message_buffer_capacity: Default::default(),
|
|
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
|
|
rpc_rate_limit: None,
|
|
prometheus_config: None,
|
|
telemetry_endpoints: None,
|
|
default_heap_pages: None,
|
|
offchain_worker: OffchainWorkerConfig { enabled: true, indexing_enabled: false },
|
|
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,
|
|
data_path: root,
|
|
base_path,
|
|
informant_output_format: Default::default(),
|
|
wasm_runtime_overrides: None,
|
|
runtime_cache_size: 2,
|
|
})
|
|
}
|
|
|
|
impl TestNode {
|
|
/// Wait for `count` blocks to be imported in the node and then exit. This function will not
|
|
/// return if no blocks are ever created, thus you should restrict the maximum amount of time of
|
|
/// the test execution.
|
|
pub fn wait_for_blocks(&self, count: usize) -> impl Future<Output = ()> {
|
|
self.client.wait_for_blocks(count)
|
|
}
|
|
|
|
/// Send an extrinsic to this node.
|
|
pub async fn send_extrinsic(
|
|
&self,
|
|
function: impl Into<runtime::RuntimeCall>,
|
|
caller: Sr25519Keyring,
|
|
) -> Result<RpcTransactionOutput, RpcTransactionError> {
|
|
let extrinsic = construct_extrinsic(&self.client, function, caller.pair(), Some(0));
|
|
|
|
self.rpc_handlers.send_transaction(extrinsic.into()).await
|
|
}
|
|
|
|
/// Register a parachain at this relay chain.
|
|
pub async fn schedule_upgrade(&self, validation: Vec<u8>) -> Result<(), RpcTransactionError> {
|
|
let call = frame_system::Call::set_code { code: validation };
|
|
|
|
self.send_extrinsic(
|
|
runtime::SudoCall::sudo_unchecked_weight {
|
|
call: Box::new(call.into()),
|
|
weight: Weight::from_parts(1_000, 0),
|
|
},
|
|
Sr25519Keyring::Alice,
|
|
)
|
|
.await
|
|
.map(drop)
|
|
}
|
|
}
|
|
|
|
/// Fetch account nonce for key pair
|
|
pub fn fetch_nonce(client: &Client, account: sp_core::sr25519::Public) -> u32 {
|
|
let best_hash = client.chain_info().best_hash;
|
|
client
|
|
.runtime_api()
|
|
.account_nonce(best_hash, account.into())
|
|
.expect("Fetching account nonce works; qed")
|
|
}
|
|
|
|
/// Construct an extrinsic that can be applied to the test runtime.
|
|
pub fn construct_extrinsic(
|
|
client: &Client,
|
|
function: impl Into<runtime::RuntimeCall>,
|
|
caller: sp_core::sr25519::Pair,
|
|
nonce: Option<u32>,
|
|
) -> runtime::UncheckedExtrinsic {
|
|
let function = function.into();
|
|
let current_block_hash = client.info().best_hash;
|
|
let current_block = client.info().best_number.saturated_into();
|
|
let genesis_block = client.hash(0).unwrap().unwrap();
|
|
let nonce = nonce.unwrap_or_else(|| fetch_nonce(client, caller.public()));
|
|
let period = runtime::BlockHashCount::get()
|
|
.checked_next_power_of_two()
|
|
.map(|c| c / 2)
|
|
.unwrap_or(2) as u64;
|
|
let tip = 0;
|
|
let tx_ext: runtime::TxExtension = (
|
|
frame_system::CheckNonZeroSender::<runtime::Runtime>::new(),
|
|
frame_system::CheckSpecVersion::<runtime::Runtime>::new(),
|
|
frame_system::CheckGenesis::<runtime::Runtime>::new(),
|
|
frame_system::CheckEra::<runtime::Runtime>::from(generic::Era::mortal(
|
|
period,
|
|
current_block,
|
|
)),
|
|
frame_system::CheckNonce::<runtime::Runtime>::from(nonce),
|
|
frame_system::CheckWeight::<runtime::Runtime>::new(),
|
|
pallet_transaction_payment::ChargeTransactionPayment::<runtime::Runtime>::from(tip),
|
|
cumulus_primitives_storage_weight_reclaim::StorageWeightReclaim::<runtime::Runtime>::new(),
|
|
)
|
|
.into();
|
|
let raw_payload = runtime::SignedPayload::from_raw(
|
|
function.clone(),
|
|
tx_ext.clone(),
|
|
((), runtime::VERSION.spec_version, genesis_block, current_block_hash, (), (), (), ()),
|
|
);
|
|
let signature = raw_payload.using_encoded(|e| caller.sign(e));
|
|
runtime::UncheckedExtrinsic::new_signed(
|
|
function,
|
|
caller.public().into(),
|
|
runtime::Signature::Sr25519(signature),
|
|
tx_ext,
|
|
)
|
|
}
|
|
|
|
/// Run a relay-chain validator node.
|
|
///
|
|
/// This is essentially a wrapper around
|
|
/// [`run_validator_node`](polkadot_test_service::run_validator_node).
|
|
pub fn run_relay_chain_validator_node(
|
|
tokio_handle: tokio::runtime::Handle,
|
|
key: Sr25519Keyring,
|
|
storage_update_func: impl Fn(),
|
|
boot_nodes: Vec<MultiaddrWithPeerId>,
|
|
port: Option<u16>,
|
|
) -> polkadot_test_service::PolkadotTestNode {
|
|
let mut config = polkadot_test_service::node_config(
|
|
storage_update_func,
|
|
tokio_handle.clone(),
|
|
key,
|
|
boot_nodes,
|
|
true,
|
|
);
|
|
|
|
if let Some(port) = port {
|
|
config.rpc_addr = Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port));
|
|
}
|
|
|
|
let mut workers_path = std::env::current_exe().unwrap();
|
|
workers_path.pop();
|
|
workers_path.pop();
|
|
|
|
tokio_handle.block_on(async move {
|
|
polkadot_test_service::run_validator_node(config, Some(workers_path))
|
|
})
|
|
}
|