feat: initialize Kurdistan SDK - independent fork of Polkadot SDK
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
[package]
|
||||
name = "pezkuwi-test-client"
|
||||
publish = false
|
||||
version = "1.0.0"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
codec = { features = ["derive"], workspace = true }
|
||||
|
||||
# Pezkuwi dependencies
|
||||
pezkuwi-primitives = { workspace = true, default-features = true }
|
||||
pezkuwi-test-runtime = { workspace = true }
|
||||
pezkuwi-test-service = { workspace = true }
|
||||
|
||||
# Substrate dependencies
|
||||
frame-benchmarking = { workspace = true, default-features = true }
|
||||
sc-block-builder = { workspace = true, default-features = true }
|
||||
sc-consensus = { workspace = true, default-features = true }
|
||||
sc-service = { workspace = true, default-features = true }
|
||||
sp-api = { workspace = true, default-features = true }
|
||||
sp-blockchain = { workspace = true, default-features = true }
|
||||
sp-consensus = { workspace = true, default-features = true }
|
||||
sp-consensus-babe = { workspace = true, default-features = true }
|
||||
sp-inherents = { workspace = true, default-features = true }
|
||||
sp-io = { workspace = true, default-features = true }
|
||||
sp-runtime = { workspace = true, default-features = true }
|
||||
sp-state-machine = { workspace = true, default-features = true }
|
||||
sp-timestamp = { workspace = true, default-features = true }
|
||||
substrate-test-client = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
futures = { workspace = true }
|
||||
sp-keyring = { workspace = true, default-features = true }
|
||||
|
||||
[features]
|
||||
runtime-benchmarks = [
|
||||
"frame-benchmarking/runtime-benchmarks",
|
||||
"pezkuwi-primitives/runtime-benchmarks",
|
||||
"pezkuwi-test-runtime/runtime-benchmarks",
|
||||
"pezkuwi-test-service/runtime-benchmarks",
|
||||
"sc-block-builder/runtime-benchmarks",
|
||||
"sc-consensus/runtime-benchmarks",
|
||||
"sc-service/runtime-benchmarks",
|
||||
"sp-api/runtime-benchmarks",
|
||||
"sp-blockchain/runtime-benchmarks",
|
||||
"sp-consensus-babe/runtime-benchmarks",
|
||||
"sp-consensus/runtime-benchmarks",
|
||||
"sp-inherents/runtime-benchmarks",
|
||||
"sp-io/runtime-benchmarks",
|
||||
"sp-keyring/runtime-benchmarks",
|
||||
"sp-runtime/runtime-benchmarks",
|
||||
"sp-state-machine/runtime-benchmarks",
|
||||
"sp-timestamp/runtime-benchmarks",
|
||||
"substrate-test-client/runtime-benchmarks",
|
||||
]
|
||||
@@ -0,0 +1,158 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezkuwi.
|
||||
|
||||
// Pezkuwi 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.
|
||||
|
||||
// Pezkuwi 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 Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::Client;
|
||||
use codec::{Decode, Encode};
|
||||
use pezkuwi_primitives::{Block, InherentData as TeyrchainsInherentData};
|
||||
use pezkuwi_test_runtime::UncheckedExtrinsic;
|
||||
use pezkuwi_test_service::GetLastTimestamp;
|
||||
use sc_block_builder::{BlockBuilder, BlockBuilderBuilder};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_consensus_babe::{
|
||||
digests::{PreDigest, SecondaryPlainPreDigest},
|
||||
BABE_ENGINE_ID,
|
||||
};
|
||||
use sp_runtime::{traits::Block as BlockT, Digest, DigestItem};
|
||||
use sp_state_machine::BasicExternalities;
|
||||
|
||||
/// An extension for the test client to initialize a Pezkuwi specific block builder.
|
||||
pub trait InitPezkuwiBlockBuilder {
|
||||
/// Init a Pezkuwi specific block builder that works for the test runtime.
|
||||
///
|
||||
/// This will automatically create and push the inherents for you to make the block valid for
|
||||
/// the test runtime.
|
||||
fn init_pezkuwi_block_builder(&self) -> sc_block_builder::BlockBuilder<'_, Block, Client>;
|
||||
|
||||
/// Init a Pezkuwi specific block builder at a specific block that works for the test runtime.
|
||||
///
|
||||
/// Same as [`InitPezkuwiBlockBuilder::init_pezkuwi_block_builder`] besides that it takes a
|
||||
/// `Hash` to say which should be the parent block of the block that is being build.
|
||||
fn init_pezkuwi_block_builder_at(
|
||||
&self,
|
||||
hash: <Block as BlockT>::Hash,
|
||||
) -> sc_block_builder::BlockBuilder<'_, Block, Client>;
|
||||
}
|
||||
|
||||
impl InitPezkuwiBlockBuilder for Client {
|
||||
fn init_pezkuwi_block_builder(&self) -> BlockBuilder<'_, Block, Client> {
|
||||
let chain_info = self.chain_info();
|
||||
self.init_pezkuwi_block_builder_at(chain_info.best_hash)
|
||||
}
|
||||
|
||||
fn init_pezkuwi_block_builder_at(
|
||||
&self,
|
||||
hash: <Block as BlockT>::Hash,
|
||||
) -> BlockBuilder<'_, Block, Client> {
|
||||
let last_timestamp =
|
||||
self.runtime_api().get_last_timestamp(hash).expect("Get last timestamp");
|
||||
|
||||
// `MinimumPeriod` is a storage parameter type that requires externalities to access the
|
||||
// value.
|
||||
let minimum_period = BasicExternalities::new_empty()
|
||||
.execute_with(|| pezkuwi_test_runtime::MinimumPeriod::get());
|
||||
|
||||
let timestamp = if last_timestamp == 0 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
.expect("Time is always after UNIX_EPOCH; qed")
|
||||
.as_millis() as u64
|
||||
} else {
|
||||
last_timestamp + minimum_period
|
||||
};
|
||||
|
||||
// `SlotDuration` is a storage parameter type that requires externalities to access the
|
||||
// value.
|
||||
let slot_duration = BasicExternalities::new_empty()
|
||||
.execute_with(|| pezkuwi_test_runtime::SlotDuration::get());
|
||||
|
||||
let slot = (timestamp / slot_duration).into();
|
||||
|
||||
let digest = Digest {
|
||||
logs: vec![DigestItem::PreRuntime(
|
||||
BABE_ENGINE_ID,
|
||||
PreDigest::SecondaryPlain(SecondaryPlainPreDigest { slot, authority_index: 42 })
|
||||
.encode(),
|
||||
)],
|
||||
};
|
||||
|
||||
let mut block_builder = BlockBuilderBuilder::new(self)
|
||||
.on_parent_block(hash)
|
||||
.fetch_parent_block_number(&self)
|
||||
.expect("Fetches parent block number")
|
||||
.with_inherent_digests(digest)
|
||||
.build()
|
||||
.expect("Creates new block builder for test runtime");
|
||||
|
||||
let mut inherent_data = sp_inherents::InherentData::new();
|
||||
|
||||
inherent_data
|
||||
.put_data(sp_timestamp::INHERENT_IDENTIFIER, ×tamp)
|
||||
.expect("Put timestamp inherent data");
|
||||
|
||||
let parent_header = self
|
||||
.header(hash)
|
||||
.expect("Get the parent block header")
|
||||
.expect("The target block header must exist");
|
||||
|
||||
let teyrchains_inherent_data = TeyrchainsInherentData {
|
||||
bitfields: Vec::new(),
|
||||
backed_candidates: Vec::new(),
|
||||
disputes: Vec::new(),
|
||||
parent_header,
|
||||
};
|
||||
|
||||
inherent_data
|
||||
.put_data(pezkuwi_primitives::TEYRCHAINS_INHERENT_IDENTIFIER, &teyrchains_inherent_data)
|
||||
.expect("Put teyrchains inherent data");
|
||||
|
||||
let inherents = block_builder.create_inherents(inherent_data).expect("Creates inherents");
|
||||
|
||||
inherents
|
||||
.into_iter()
|
||||
.for_each(|ext| block_builder.push(ext).expect("Pushes inherent"));
|
||||
|
||||
block_builder
|
||||
}
|
||||
}
|
||||
|
||||
/// Pezkuwi specific extensions for the [`BlockBuilder`].
|
||||
pub trait BlockBuilderExt {
|
||||
/// Push a Pezkuwi test runtime specific extrinsic to the block.
|
||||
///
|
||||
/// This will internally use the [`BlockBuilder::push`] method, but this method expects a opaque
|
||||
/// extrinsic. So, we provide this wrapper which converts a test runtime specific extrinsic to a
|
||||
/// opaque extrinsic and pushes it to the block.
|
||||
///
|
||||
/// Returns the result of the application of the extrinsic.
|
||||
fn push_pezkuwi_extrinsic(
|
||||
&mut self,
|
||||
ext: UncheckedExtrinsic,
|
||||
) -> Result<(), sp_blockchain::Error>;
|
||||
}
|
||||
|
||||
impl BlockBuilderExt for BlockBuilder<'_, Block, Client> {
|
||||
fn push_pezkuwi_extrinsic(
|
||||
&mut self,
|
||||
ext: UncheckedExtrinsic,
|
||||
) -> Result<(), sp_blockchain::Error> {
|
||||
let encoded = ext.encode();
|
||||
self.push(
|
||||
Decode::decode(&mut &encoded[..]).expect(
|
||||
"The runtime specific extrinsic always decodes to an opaque extrinsic; qed",
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezkuwi.
|
||||
|
||||
// Pezkuwi 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.
|
||||
|
||||
// Pezkuwi 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 Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A Pezkuwi test client.
|
||||
//!
|
||||
//! This test client is using the Pezkuwi test runtime.
|
||||
|
||||
mod block_builder;
|
||||
|
||||
use pezkuwi_primitives::Block;
|
||||
use sp_runtime::BuildStorage;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub use block_builder::*;
|
||||
pub use pezkuwi_test_runtime as runtime;
|
||||
pub use pezkuwi_test_service::{
|
||||
construct_extrinsic, construct_transfer_extrinsic, Client, FullBackend,
|
||||
};
|
||||
pub use substrate_test_client::*;
|
||||
|
||||
/// Test client executor.
|
||||
pub type Executor = client::LocalCallExecutor<
|
||||
Block,
|
||||
FullBackend,
|
||||
WasmExecutor<(sp_io::SubstrateHostFunctions, frame_benchmarking::benchmarking::HostFunctions)>,
|
||||
>;
|
||||
|
||||
/// Test client builder for Pezkuwi.
|
||||
pub type TestClientBuilder =
|
||||
substrate_test_client::TestClientBuilder<Block, Executor, FullBackend, GenesisParameters>;
|
||||
|
||||
/// `LongestChain` type for the test runtime/client.
|
||||
pub type LongestChain = sc_consensus::LongestChain<FullBackend, Block>;
|
||||
|
||||
/// Parameters of test-client builder with test-runtime.
|
||||
#[derive(Default)]
|
||||
pub struct GenesisParameters;
|
||||
|
||||
impl substrate_test_client::GenesisInit for GenesisParameters {
|
||||
fn genesis_storage(&self) -> Storage {
|
||||
pezkuwi_test_service::chain_spec::pezkuwi_local_testnet_config()
|
||||
.build_storage()
|
||||
.expect("Builds test runtime genesis storage")
|
||||
}
|
||||
}
|
||||
|
||||
/// A `test-runtime` extensions to `TestClientBuilder`.
|
||||
pub trait TestClientBuilderExt: Sized {
|
||||
/// Build the test client.
|
||||
fn build(self) -> Client {
|
||||
self.build_with_longest_chain().0
|
||||
}
|
||||
|
||||
/// Build the test client and longest chain selector.
|
||||
fn build_with_longest_chain(self) -> (Client, LongestChain);
|
||||
}
|
||||
|
||||
impl TestClientBuilderExt for TestClientBuilder {
|
||||
fn build_with_longest_chain(self) -> (Client, LongestChain) {
|
||||
let executor = WasmExecutor::builder().build();
|
||||
let executor = client::LocalCallExecutor::new(
|
||||
self.backend().clone(),
|
||||
executor.clone(),
|
||||
Default::default(),
|
||||
ExecutionExtensions::new(Default::default(), Arc::new(executor)),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
self.build_with_executor(executor)
|
||||
}
|
||||
}
|
||||
|
||||
/// A `TestClientBuilder` with default backend and executor.
|
||||
pub trait DefaultTestClientBuilderExt: Sized {
|
||||
/// Create new `TestClientBuilder`
|
||||
fn new() -> Self;
|
||||
}
|
||||
|
||||
impl DefaultTestClientBuilderExt for TestClientBuilder {
|
||||
fn new() -> Self {
|
||||
Self::with_default_backend()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_consensus::BlockOrigin;
|
||||
|
||||
#[test]
|
||||
fn ensure_test_client_can_build_and_import_block() {
|
||||
let client = TestClientBuilder::new().build();
|
||||
|
||||
let block_builder = client.init_pezkuwi_block_builder();
|
||||
let block = block_builder.build().expect("Finalizes the block").block;
|
||||
|
||||
futures::executor::block_on(client.import(BlockOrigin::Own, block))
|
||||
.expect("Imports the block");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ensure_test_client_can_push_extrinsic() {
|
||||
let client = TestClientBuilder::new().build();
|
||||
|
||||
let transfer = construct_transfer_extrinsic(
|
||||
&client,
|
||||
sp_keyring::Sr25519Keyring::Alice,
|
||||
sp_keyring::Sr25519Keyring::Bob,
|
||||
1000,
|
||||
);
|
||||
let mut block_builder = client.init_pezkuwi_block_builder();
|
||||
block_builder.push_pezkuwi_extrinsic(transfer).expect("Pushes extrinsic");
|
||||
|
||||
let block = block_builder.build().expect("Finalizes the block").block;
|
||||
|
||||
futures::executor::block_on(client.import(BlockOrigin::Own, block))
|
||||
.expect("Imports the block");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
[package]
|
||||
name = "pezkuwi-test-service"
|
||||
publish = false
|
||||
version = "1.0.0"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
futures = { workspace = true }
|
||||
rand = { workspace = true, default-features = true }
|
||||
serde_json = { workspace = true, default-features = true }
|
||||
tokio = { workspace = true, default-features = true }
|
||||
|
||||
# Pezkuwi dependencies
|
||||
pezkuwi-node-primitives = { workspace = true, default-features = true }
|
||||
pezkuwi-node-subsystem = { workspace = true, default-features = true }
|
||||
pezkuwi-overseer = { workspace = true, default-features = true }
|
||||
pezkuwi-primitives = { workspace = true, default-features = true }
|
||||
pezkuwi-runtime-common = { workspace = true, default-features = true }
|
||||
pezkuwi-runtime-teyrchains = { workspace = true, default-features = true }
|
||||
pezkuwi-service = { workspace = true, default-features = true }
|
||||
pezkuwi-test-runtime = { workspace = true }
|
||||
pezkuwi-teyrchain-primitives = { workspace = true, default-features = true }
|
||||
test-runtime-constants = { workspace = true, default-features = true }
|
||||
|
||||
# Substrate dependencies
|
||||
frame-system = { workspace = true, default-features = true }
|
||||
pallet-balances = { workspace = true, default-features = true }
|
||||
pallet-staking = { workspace = true, default-features = true }
|
||||
pallet-transaction-payment = { workspace = true, default-features = true }
|
||||
sc-chain-spec = { workspace = true, default-features = true }
|
||||
sc-cli = { workspace = true, default-features = true }
|
||||
sc-client-api = { workspace = true, default-features = true }
|
||||
sc-consensus = { workspace = true, default-features = true }
|
||||
sc-consensus-grandpa = { workspace = true, default-features = true }
|
||||
sc-network = { workspace = true, default-features = true }
|
||||
sc-service = { workspace = true }
|
||||
sc-tracing = { workspace = true, default-features = true }
|
||||
sp-arithmetic = { workspace = true, default-features = true }
|
||||
sp-authority-discovery = { workspace = true, default-features = true }
|
||||
sp-blockchain = { workspace = true, default-features = true }
|
||||
sp-consensus = { workspace = true, default-features = true }
|
||||
sp-consensus-babe = { workspace = true, default-features = true }
|
||||
sp-core = { workspace = true, default-features = true }
|
||||
sp-keyring = { workspace = true, default-features = true }
|
||||
sp-runtime = { workspace = true, default-features = true }
|
||||
sp-state-machine = { workspace = true, default-features = true }
|
||||
substrate-test-client = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
pallet-balances = { workspace = true }
|
||||
tokio = { features = ["macros"], workspace = true, default-features = true }
|
||||
|
||||
[features]
|
||||
runtime-metrics = ["pezkuwi-test-runtime/runtime-metrics"]
|
||||
runtime-benchmarks = [
|
||||
"frame-system/runtime-benchmarks",
|
||||
"pallet-balances/runtime-benchmarks",
|
||||
"pallet-staking/runtime-benchmarks",
|
||||
"pallet-transaction-payment/runtime-benchmarks",
|
||||
"pezkuwi-node-primitives/runtime-benchmarks",
|
||||
"pezkuwi-node-subsystem/runtime-benchmarks",
|
||||
"pezkuwi-overseer/runtime-benchmarks",
|
||||
"pezkuwi-primitives/runtime-benchmarks",
|
||||
"pezkuwi-runtime-common/runtime-benchmarks",
|
||||
"pezkuwi-runtime-teyrchains/runtime-benchmarks",
|
||||
"pezkuwi-service/runtime-benchmarks",
|
||||
"pezkuwi-test-runtime/runtime-benchmarks",
|
||||
"pezkuwi-teyrchain-primitives/runtime-benchmarks",
|
||||
"sc-chain-spec/runtime-benchmarks",
|
||||
"sc-cli/runtime-benchmarks",
|
||||
"sc-client-api/runtime-benchmarks",
|
||||
"sc-consensus-grandpa/runtime-benchmarks",
|
||||
"sc-consensus/runtime-benchmarks",
|
||||
"sc-network/runtime-benchmarks",
|
||||
"sc-service/runtime-benchmarks",
|
||||
"sc-tracing/runtime-benchmarks",
|
||||
"sp-authority-discovery/runtime-benchmarks",
|
||||
"sp-blockchain/runtime-benchmarks",
|
||||
"sp-consensus-babe/runtime-benchmarks",
|
||||
"sp-consensus/runtime-benchmarks",
|
||||
"sp-keyring/runtime-benchmarks",
|
||||
"sp-runtime/runtime-benchmarks",
|
||||
"sp-state-machine/runtime-benchmarks",
|
||||
"substrate-test-client/runtime-benchmarks",
|
||||
"test-runtime-constants/runtime-benchmarks",
|
||||
]
|
||||
@@ -0,0 +1,9 @@
|
||||
# `pezkuwi-test-service`
|
||||
|
||||
## Testing
|
||||
|
||||
Before running `cargo test` in this crate, make sure the worker binaries are built first. This can be done with:
|
||||
|
||||
```sh
|
||||
cargo build --bin pezkuwi-execute-worker --bin pezkuwi-prepare-worker
|
||||
```
|
||||
@@ -0,0 +1,192 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezkuwi.
|
||||
|
||||
// Pezkuwi 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.
|
||||
|
||||
// Pezkuwi 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 Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Chain specifications for the test runtime.
|
||||
|
||||
use pallet_staking::Forcing;
|
||||
use pezkuwi_primitives::{
|
||||
node_features, AccountId, AssignmentId, NodeFeatures, SchedulerParams, ValidatorId,
|
||||
MAX_CODE_SIZE, MAX_POV_SIZE,
|
||||
};
|
||||
use pezkuwi_service::chain_spec::Extensions;
|
||||
use pezkuwi_test_runtime::BABE_GENESIS_EPOCH_CONFIG;
|
||||
use sc_chain_spec::{ChainSpec, ChainType};
|
||||
use sc_consensus_grandpa::AuthorityId as GrandpaId;
|
||||
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
|
||||
use sp_consensus_babe::AuthorityId as BabeId;
|
||||
use sp_core::{crypto::get_public_from_string_or_panic, sr25519};
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
use sp_runtime::Perbill;
|
||||
use test_runtime_constants::currency::DOTS;
|
||||
|
||||
const DEFAULT_PROTOCOL_ID: &str = "hez";
|
||||
|
||||
/// The `ChainSpec` parameterized for pezkuwi test runtime.
|
||||
pub type PezkuwiChainSpec = sc_service::GenericChainSpec<Extensions>;
|
||||
|
||||
/// Returns the properties for the [`PezkuwiChainSpec`].
|
||||
pub fn pezkuwi_chain_spec_properties() -> serde_json::map::Map<String, serde_json::Value> {
|
||||
serde_json::json!({
|
||||
"tokenDecimals": 10,
|
||||
})
|
||||
.as_object()
|
||||
.expect("Map given; qed")
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Local testnet config (multivalidator Alice + Bob)
|
||||
pub fn pezkuwi_local_testnet_config() -> PezkuwiChainSpec {
|
||||
PezkuwiChainSpec::builder(
|
||||
pezkuwi_test_runtime::WASM_BINARY.expect("Wasm binary must be built for testing"),
|
||||
Default::default(),
|
||||
)
|
||||
.with_name("Local Testnet")
|
||||
.with_id("local_testnet")
|
||||
.with_chain_type(ChainType::Local)
|
||||
.with_genesis_config_patch(pezkuwi_local_testnet_genesis())
|
||||
.with_protocol_id(DEFAULT_PROTOCOL_ID)
|
||||
.with_properties(pezkuwi_chain_spec_properties())
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Local testnet genesis config (multivalidator Alice + Bob)
|
||||
pub fn pezkuwi_local_testnet_genesis() -> serde_json::Value {
|
||||
pezkuwi_testnet_genesis(
|
||||
vec![get_authority_keys_from_seed("Alice"), get_authority_keys_from_seed("Bob")],
|
||||
Sr25519Keyring::Alice.to_account_id(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
/// Helper function to generate stash, controller and session key from seed
|
||||
fn get_authority_keys_from_seed(
|
||||
seed: &str,
|
||||
) -> (AccountId, AccountId, BabeId, GrandpaId, ValidatorId, AssignmentId, AuthorityDiscoveryId) {
|
||||
(
|
||||
get_public_from_string_or_panic::<sr25519::Public>(&format!("{}//stash", seed)).into(),
|
||||
get_public_from_string_or_panic::<sr25519::Public>(seed).into(),
|
||||
get_public_from_string_or_panic::<BabeId>(seed),
|
||||
get_public_from_string_or_panic::<GrandpaId>(seed),
|
||||
get_public_from_string_or_panic::<ValidatorId>(seed),
|
||||
get_public_from_string_or_panic::<AssignmentId>(seed),
|
||||
get_public_from_string_or_panic::<AuthorityDiscoveryId>(seed),
|
||||
)
|
||||
}
|
||||
|
||||
fn testnet_accounts() -> Vec<AccountId> {
|
||||
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect()
|
||||
}
|
||||
|
||||
/// Helper function to create pezkuwi `RuntimeGenesisConfig` for testing
|
||||
fn pezkuwi_testnet_genesis(
|
||||
initial_authorities: Vec<(
|
||||
AccountId,
|
||||
AccountId,
|
||||
BabeId,
|
||||
GrandpaId,
|
||||
ValidatorId,
|
||||
AssignmentId,
|
||||
AuthorityDiscoveryId,
|
||||
)>,
|
||||
root_key: AccountId,
|
||||
endowed_accounts: Option<Vec<AccountId>>,
|
||||
) -> serde_json::Value {
|
||||
use pezkuwi_test_runtime as runtime;
|
||||
|
||||
let endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(testnet_accounts);
|
||||
|
||||
const ENDOWMENT: u128 = 1_000_000 * DOTS;
|
||||
const STASH: u128 = 100 * DOTS;
|
||||
|
||||
// Prepare node features with V2 receipts
|
||||
// and elastic scaling enabled.
|
||||
let mut node_features = NodeFeatures::new();
|
||||
node_features.resize(node_features::FeatureIndex::FirstUnassigned as usize + 1, false);
|
||||
|
||||
node_features.set(node_features::FeatureIndex::CandidateReceiptV2 as u8 as usize, true);
|
||||
node_features.set(node_features::FeatureIndex::ElasticScalingMVP as u8 as usize, true);
|
||||
|
||||
serde_json::json!({
|
||||
"balances": {
|
||||
"balances": endowed_accounts.iter().map(|k| (k.clone(), ENDOWMENT)).collect::<Vec<_>>(),
|
||||
},
|
||||
"session": {
|
||||
"keys": initial_authorities
|
||||
.iter()
|
||||
.map(|x| {
|
||||
(
|
||||
x.0.clone(),
|
||||
x.0.clone(),
|
||||
runtime::SessionKeys {
|
||||
babe: x.2.clone(),
|
||||
grandpa: x.3.clone(),
|
||||
para_validator: x.4.clone(),
|
||||
para_assignment: x.5.clone(),
|
||||
authority_discovery: x.6.clone(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
},
|
||||
"staking": {
|
||||
"minimumValidatorCount": 1,
|
||||
"validatorCount": 2,
|
||||
"stakers": initial_authorities
|
||||
.iter()
|
||||
.map(|x| (x.0.clone(), x.0.clone(), STASH, runtime::StakerStatus::<AccountId>::Validator))
|
||||
.collect::<Vec<_>>(),
|
||||
"invulnerables": initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
|
||||
"forceEra": Forcing::NotForcing,
|
||||
"slashRewardFraction": Perbill::from_percent(10),
|
||||
},
|
||||
"babe": {
|
||||
"epochConfig": Some(BABE_GENESIS_EPOCH_CONFIG),
|
||||
},
|
||||
"sudo": { "key": Some(root_key) },
|
||||
"configuration": {
|
||||
"config": pezkuwi_runtime_teyrchains::configuration::HostConfiguration {
|
||||
validation_upgrade_cooldown: 10u32,
|
||||
validation_upgrade_delay: 5,
|
||||
code_retention_period: 1200,
|
||||
max_code_size: MAX_CODE_SIZE,
|
||||
max_pov_size: MAX_POV_SIZE,
|
||||
max_head_data_size: 32 * 1024,
|
||||
no_show_slots: 10,
|
||||
minimum_validation_upgrade_delay: 5,
|
||||
max_downward_message_size: 1024,
|
||||
node_features,
|
||||
scheduler_params: SchedulerParams {
|
||||
group_rotation_frequency: 20,
|
||||
paras_availability_period: 4,
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Can be called for a `Configuration` to check if it is a configuration for the `Test` network.
|
||||
pub trait IdentifyVariant {
|
||||
/// Returns if this is a configuration for the `Test` network.
|
||||
fn is_test(&self) -> bool;
|
||||
}
|
||||
|
||||
impl IdentifyVariant for Box<dyn ChainSpec> {
|
||||
fn is_test(&self) -> bool {
|
||||
self.id().starts_with("test")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,487 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezkuwi.
|
||||
|
||||
// Pezkuwi 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.
|
||||
|
||||
// Pezkuwi 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 Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Pezkuwi test service only.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
pub mod chain_spec;
|
||||
|
||||
pub use chain_spec::*;
|
||||
use futures::{future::Future, stream::StreamExt};
|
||||
use pezkuwi_node_primitives::{CollationGenerationConfig, CollatorFn};
|
||||
use pezkuwi_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage};
|
||||
use pezkuwi_overseer::Handle;
|
||||
use pezkuwi_primitives::{Balance, CollatorPair, HeadData, Id as ParaId, ValidationCode};
|
||||
use pezkuwi_runtime_common::BlockHashCount;
|
||||
use pezkuwi_runtime_teyrchains::paras::{ParaGenesisArgs, ParaKind};
|
||||
use pezkuwi_service::{Error, FullClient, IsTeyrchainNode, NewFull, OverseerGen, PrometheusConfig};
|
||||
use pezkuwi_test_runtime::{
|
||||
ParasCall, ParasSudoWrapperCall, Runtime, SignedPayload, SudoCall, TxExtension,
|
||||
UncheckedExtrinsic, VERSION,
|
||||
};
|
||||
|
||||
use sc_chain_spec::ChainSpec;
|
||||
use sc_client_api::BlockchainEvents;
|
||||
use sc_network::{
|
||||
config::{NetworkConfiguration, TransportConfig},
|
||||
multiaddr,
|
||||
service::traits::NetworkService,
|
||||
NetworkStateInfo,
|
||||
};
|
||||
use sc_service::{
|
||||
config::{
|
||||
DatabaseSource, KeystoreConfig, MultiaddrWithPeerId, RpcBatchRequestConfig,
|
||||
WasmExecutionMethod, WasmtimeInstantiationStrategy,
|
||||
},
|
||||
BasePath, BlocksPruning, Configuration, Role, RpcHandlers, TaskManager,
|
||||
};
|
||||
use sp_arithmetic::traits::SaturatedConversion;
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
use sp_runtime::{codec::Encode, generic, traits::IdentifyAccount, MultiSigner};
|
||||
use sp_state_machine::BasicExternalities;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
net::{Ipv4Addr, SocketAddr},
|
||||
path::PathBuf,
|
||||
sync::Arc,
|
||||
};
|
||||
use substrate_test_client::{
|
||||
BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput,
|
||||
};
|
||||
|
||||
/// The client type being used by the test service.
|
||||
pub type Client = FullClient;
|
||||
|
||||
pub use pezkuwi_service::{FullBackend, GetLastTimestamp};
|
||||
use sc_service::config::{ExecutorConfiguration, RpcConfiguration};
|
||||
|
||||
/// Create a new full node.
|
||||
#[sc_tracing::logging::prefix_logs_with(custom_log_prefix.unwrap_or(config.network.node_name.as_str()))]
|
||||
pub fn new_full<OverseerGenerator: OverseerGen>(
|
||||
config: Configuration,
|
||||
is_teyrchain_node: IsTeyrchainNode,
|
||||
workers_path: Option<PathBuf>,
|
||||
overseer_gen: OverseerGenerator,
|
||||
custom_log_prefix: Option<&'static str>,
|
||||
) -> Result<NewFull, Error> {
|
||||
let workers_path = Some(workers_path.unwrap_or_else(get_relative_workers_path_for_test));
|
||||
|
||||
let params = pezkuwi_service::NewFullParams {
|
||||
is_teyrchain_node,
|
||||
enable_beefy: true,
|
||||
force_authoring_backoff: false,
|
||||
telemetry_worker_handle: None,
|
||||
node_version: None,
|
||||
secure_validator_mode: false,
|
||||
workers_path,
|
||||
workers_names: None,
|
||||
overseer_gen,
|
||||
overseer_message_channel_capacity_override: None,
|
||||
malus_finality_delay: None,
|
||||
hwbench: None,
|
||||
execute_workers_max_num: None,
|
||||
prepare_workers_hard_max_num: None,
|
||||
prepare_workers_soft_max_num: None,
|
||||
keep_finalized_for: None,
|
||||
invulnerable_ah_collators: HashSet::new(),
|
||||
collator_protocol_hold_off: None,
|
||||
};
|
||||
|
||||
match config.network.network_backend {
|
||||
sc_network::config::NetworkBackendType::Libp2p =>
|
||||
pezkuwi_service::new_full::<_, sc_network::NetworkWorker<_, _>>(config, params),
|
||||
sc_network::config::NetworkBackendType::Litep2p =>
|
||||
pezkuwi_service::new_full::<_, sc_network::Litep2pNetworkBackend>(config, params),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_relative_workers_path_for_test() -> PathBuf {
|
||||
// If no explicit worker path is passed in, we need to specify it ourselves as test binaries
|
||||
// are in the "deps/" directory, one level below where the worker binaries are generated.
|
||||
let mut exe_path = std::env::current_exe()
|
||||
.expect("for test purposes it's reasonable to expect that this will not fail");
|
||||
let _ = exe_path.pop();
|
||||
let _ = exe_path.pop();
|
||||
exe_path
|
||||
}
|
||||
|
||||
/// Returns a prometheus config usable for testing.
|
||||
pub fn test_prometheus_config(port: u16) -> PrometheusConfig {
|
||||
PrometheusConfig::new_with_default_registry(
|
||||
SocketAddr::new(Ipv4Addr::LOCALHOST.into(), port),
|
||||
"test-chain".to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a Pezkuwi `Configuration`.
|
||||
///
|
||||
/// By default a TCP socket will be used, therefore you need to provide boot
|
||||
/// nodes if you want the future node to be connected to other nodes.
|
||||
///
|
||||
/// The `storage_update_func` function will be executed in an externalities provided environment
|
||||
/// and can be used to make adjustments to the runtime genesis storage.
|
||||
pub fn node_config(
|
||||
storage_update_func: impl Fn(),
|
||||
tokio_handle: tokio::runtime::Handle,
|
||||
key: Sr25519Keyring,
|
||||
boot_nodes: Vec<MultiaddrWithPeerId>,
|
||||
is_validator: bool,
|
||||
) -> Configuration {
|
||||
let base_path = BasePath::new_temp_dir().expect("could not create temporary directory");
|
||||
let root = base_path.path().join(key.to_string());
|
||||
let role = if is_validator { Role::Authority } else { Role::Full };
|
||||
let key_seed = key.to_seed();
|
||||
let mut spec = pezkuwi_local_testnet_config();
|
||||
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(
|
||||
key_seed.to_string(),
|
||||
"network/test/0.1",
|
||||
Default::default(),
|
||||
None,
|
||||
);
|
||||
|
||||
network_config.boot_nodes = boot_nodes;
|
||||
|
||||
network_config.allow_non_globals_in_dht = true;
|
||||
|
||||
// Libp2p needs to know the local address on which it should listen for incoming connections,
|
||||
// while Litep2p will use `/ip4/127.0.0.1/tcp/0` by default.
|
||||
let addr: multiaddr::Multiaddr = "/ip4/127.0.0.1/tcp/0".parse().expect("valid address; qed");
|
||||
network_config.listen_addresses.push(addr.clone());
|
||||
network_config.public_addresses.push(addr);
|
||||
network_config.transport =
|
||||
TransportConfig::Normal { enable_mdns: false, allow_private_ip: true };
|
||||
|
||||
Configuration {
|
||||
impl_name: "pezkuwi-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),
|
||||
warm_up_trie_cache: None,
|
||||
state_pruning: Default::default(),
|
||||
blocks_pruning: BlocksPruning::KeepFinalized,
|
||||
chain_spec: Box::new(spec),
|
||||
executor: ExecutorConfiguration {
|
||||
wasm_method: WasmExecutionMethod::Compiled {
|
||||
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
|
||||
},
|
||||
..ExecutorConfiguration::default()
|
||||
},
|
||||
wasm_runtime_overrides: Default::default(),
|
||||
rpc: RpcConfiguration {
|
||||
addr: Default::default(),
|
||||
max_request_size: Default::default(),
|
||||
max_response_size: Default::default(),
|
||||
max_connections: Default::default(),
|
||||
cors: None,
|
||||
methods: Default::default(),
|
||||
id_provider: None,
|
||||
max_subs_per_conn: Default::default(),
|
||||
port: 9944,
|
||||
message_buffer_capacity: Default::default(),
|
||||
batch_config: RpcBatchRequestConfig::Unlimited,
|
||||
rate_limit: None,
|
||||
rate_limit_whitelisted_ips: Default::default(),
|
||||
rate_limit_trust_proxy_headers: Default::default(),
|
||||
request_logger_limit: 1024,
|
||||
},
|
||||
prometheus_config: None,
|
||||
telemetry_endpoints: None,
|
||||
offchain_worker: Default::default(),
|
||||
force_authoring: false,
|
||||
disable_grandpa: false,
|
||||
dev_key_seed: Some(key_seed),
|
||||
tracing_targets: None,
|
||||
tracing_receiver: Default::default(),
|
||||
announce_block: true,
|
||||
data_path: root,
|
||||
base_path,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the listen multiaddr from the network service.
|
||||
///
|
||||
/// The address is used to connect to the node.
|
||||
pub async fn get_listen_address(network: Arc<dyn NetworkService>) -> sc_network::Multiaddr {
|
||||
loop {
|
||||
// Litep2p provides instantly the listen address of the TCP protocol and
|
||||
// ditched the `/0` port used by the `node_config` function.
|
||||
//
|
||||
// Libp2p backend needs to be polled in a separate tokio task a few times
|
||||
// before the listen address is available. The address is made available
|
||||
// through the `SwarmEvent::NewListenAddr` event.
|
||||
let listen_addresses = network.listen_addresses();
|
||||
|
||||
// The network backend must produce a valid TCP port.
|
||||
match listen_addresses.into_iter().find(|addr| {
|
||||
addr.iter().any(|protocol| match protocol {
|
||||
multiaddr::Protocol::Tcp(port) => port > 0,
|
||||
_ => false,
|
||||
})
|
||||
}) {
|
||||
Some(multiaddr) => return multiaddr,
|
||||
None => {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
|
||||
continue;
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Run a test validator node that uses the test runtime and specified `config`.
|
||||
pub async fn run_validator_node(
|
||||
config: Configuration,
|
||||
worker_program_path: Option<PathBuf>,
|
||||
) -> PezkuwiTestNode {
|
||||
let NewFull { task_manager, client, network, rpc_handlers, overseer_handle, .. } = new_full(
|
||||
config,
|
||||
IsTeyrchainNode::No,
|
||||
worker_program_path,
|
||||
pezkuwi_service::ValidatorOverseerGen,
|
||||
None,
|
||||
)
|
||||
.expect("could not create Pezkuwi test service");
|
||||
|
||||
let overseer_handle = overseer_handle.expect("test node must have an overseer handle");
|
||||
let peer_id = network.local_peer_id();
|
||||
let multiaddr = get_listen_address(network).await;
|
||||
|
||||
let addr = MultiaddrWithPeerId { multiaddr, peer_id };
|
||||
|
||||
PezkuwiTestNode { task_manager, client, overseer_handle, addr, rpc_handlers }
|
||||
}
|
||||
|
||||
/// Run a test collator node that uses the test runtime.
|
||||
///
|
||||
/// The node will be using an in-memory socket, therefore you need to provide boot nodes if you
|
||||
/// want it to be connected to other nodes.
|
||||
///
|
||||
/// The `storage_update_func` function will be executed in an externalities provided environment
|
||||
/// and can be used to make adjustments to the runtime genesis storage.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// The collator functionality still needs to be registered at the node! This can be done using
|
||||
/// [`PezkuwiTestNode::register_collator`].
|
||||
pub async fn run_collator_node(
|
||||
tokio_handle: tokio::runtime::Handle,
|
||||
key: Sr25519Keyring,
|
||||
storage_update_func: impl Fn(),
|
||||
boot_nodes: Vec<MultiaddrWithPeerId>,
|
||||
collator_pair: CollatorPair,
|
||||
) -> PezkuwiTestNode {
|
||||
let config = node_config(storage_update_func, tokio_handle, key, boot_nodes, false);
|
||||
let NewFull { task_manager, client, network, rpc_handlers, overseer_handle, .. } = new_full(
|
||||
config,
|
||||
IsTeyrchainNode::Collator(collator_pair),
|
||||
None,
|
||||
pezkuwi_service::CollatorOverseerGen,
|
||||
None,
|
||||
)
|
||||
.expect("could not create Pezkuwi test service");
|
||||
|
||||
let overseer_handle = overseer_handle.expect("test node must have an overseer handle");
|
||||
let peer_id = network.local_peer_id();
|
||||
|
||||
let multiaddr = get_listen_address(network).await;
|
||||
let addr = MultiaddrWithPeerId { multiaddr, peer_id };
|
||||
|
||||
PezkuwiTestNode { task_manager, client, overseer_handle, addr, rpc_handlers }
|
||||
}
|
||||
|
||||
/// A Pezkuwi test node instance used for testing.
|
||||
pub struct PezkuwiTestNode {
|
||||
/// `TaskManager`'s instance.
|
||||
pub task_manager: TaskManager,
|
||||
/// Client's instance.
|
||||
pub client: Arc<Client>,
|
||||
/// A handle to Overseer.
|
||||
pub overseer_handle: Handle,
|
||||
/// 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,
|
||||
}
|
||||
|
||||
impl PezkuwiTestNode {
|
||||
/// Send a sudo call to this node.
|
||||
async fn send_sudo(
|
||||
&self,
|
||||
call: impl Into<pezkuwi_test_runtime::RuntimeCall>,
|
||||
caller: Sr25519Keyring,
|
||||
nonce: u32,
|
||||
) -> Result<(), RpcTransactionError> {
|
||||
let sudo = SudoCall::sudo { call: Box::new(call.into()) };
|
||||
|
||||
let extrinsic = construct_extrinsic(&self.client, sudo, caller, nonce);
|
||||
self.rpc_handlers.send_transaction(extrinsic.into()).await.map(drop)
|
||||
}
|
||||
|
||||
/// Send an extrinsic to this node.
|
||||
pub async fn send_extrinsic(
|
||||
&self,
|
||||
function: impl Into<pezkuwi_test_runtime::RuntimeCall>,
|
||||
caller: Sr25519Keyring,
|
||||
) -> Result<RpcTransactionOutput, RpcTransactionError> {
|
||||
let extrinsic = construct_extrinsic(&self.client, function, caller, 0);
|
||||
|
||||
self.rpc_handlers.send_transaction(extrinsic.into()).await
|
||||
}
|
||||
|
||||
/// Register a teyrchain at this relay chain.
|
||||
pub async fn register_teyrchain(
|
||||
&self,
|
||||
id: ParaId,
|
||||
validation_code: impl Into<ValidationCode>,
|
||||
genesis_head: impl Into<HeadData>,
|
||||
) -> Result<(), RpcTransactionError> {
|
||||
let validation_code: ValidationCode = validation_code.into();
|
||||
let call = ParasSudoWrapperCall::sudo_schedule_para_initialize {
|
||||
id,
|
||||
genesis: ParaGenesisArgs {
|
||||
genesis_head: genesis_head.into(),
|
||||
validation_code: validation_code.clone(),
|
||||
para_kind: ParaKind::Teyrchain,
|
||||
},
|
||||
};
|
||||
|
||||
self.send_sudo(call, Sr25519Keyring::Alice, 0).await?;
|
||||
|
||||
// Bypass pvf-checking.
|
||||
let call = ParasCall::add_trusted_validation_code { validation_code };
|
||||
self.send_sudo(call, Sr25519Keyring::Alice, 1).await
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// Wait for `count` blocks to be finalized and then exit. Similarly with `wait_for_blocks` this
|
||||
/// function will not return if no block are ever finalized.
|
||||
pub async fn wait_for_finalized_blocks(&self, count: usize) {
|
||||
let mut import_notification_stream = self.client.finality_notification_stream();
|
||||
let mut blocks = HashSet::new();
|
||||
|
||||
while let Some(notification) = import_notification_stream.next().await {
|
||||
blocks.insert(notification.hash);
|
||||
if blocks.len() == count {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Register the collator functionality in the overseer of this node.
|
||||
pub async fn register_collator(
|
||||
&mut self,
|
||||
collator_key: CollatorPair,
|
||||
para_id: ParaId,
|
||||
collator: CollatorFn,
|
||||
) {
|
||||
let config =
|
||||
CollationGenerationConfig { key: collator_key, collator: Some(collator), para_id };
|
||||
|
||||
self.overseer_handle
|
||||
.send_msg(CollationGenerationMessage::Initialize(config), "Collator")
|
||||
.await;
|
||||
|
||||
self.overseer_handle
|
||||
.send_msg(CollatorProtocolMessage::CollateOn(para_id), "Collator")
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct an extrinsic that can be applied to the test runtime.
|
||||
pub fn construct_extrinsic(
|
||||
client: &Client,
|
||||
function: impl Into<pezkuwi_test_runtime::RuntimeCall>,
|
||||
caller: Sr25519Keyring,
|
||||
nonce: u32,
|
||||
) -> 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 period =
|
||||
BlockHashCount::get().checked_next_power_of_two().map(|c| c / 2).unwrap_or(2) as u64;
|
||||
let tip = 0;
|
||||
let tx_ext: TxExtension = (
|
||||
frame_system::AuthorizeCall::<Runtime>::new(),
|
||||
frame_system::CheckNonZeroSender::<Runtime>::new(),
|
||||
frame_system::CheckSpecVersion::<Runtime>::new(),
|
||||
frame_system::CheckTxVersion::<Runtime>::new(),
|
||||
frame_system::CheckGenesis::<Runtime>::new(),
|
||||
frame_system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
|
||||
frame_system::CheckNonce::<Runtime>::from(nonce),
|
||||
frame_system::CheckWeight::<Runtime>::new(),
|
||||
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
|
||||
frame_system::WeightReclaim::<Runtime>::new(),
|
||||
)
|
||||
.into();
|
||||
let raw_payload = SignedPayload::from_raw(
|
||||
function.clone(),
|
||||
tx_ext.clone(),
|
||||
(
|
||||
(),
|
||||
(),
|
||||
VERSION.spec_version,
|
||||
VERSION.transaction_version,
|
||||
genesis_block,
|
||||
current_block_hash,
|
||||
(),
|
||||
(),
|
||||
(),
|
||||
(),
|
||||
),
|
||||
);
|
||||
let signature = raw_payload.using_encoded(|e| caller.sign(e));
|
||||
UncheckedExtrinsic::new_signed(
|
||||
function.clone(),
|
||||
pezkuwi_test_runtime::Address::Id(caller.public().into()),
|
||||
pezkuwi_primitives::Signature::Sr25519(signature),
|
||||
tx_ext.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Construct a transfer extrinsic.
|
||||
pub fn construct_transfer_extrinsic(
|
||||
client: &Client,
|
||||
origin: sp_keyring::Sr25519Keyring,
|
||||
dest: sp_keyring::Sr25519Keyring,
|
||||
value: Balance,
|
||||
) -> UncheckedExtrinsic {
|
||||
let function =
|
||||
pezkuwi_test_runtime::RuntimeCall::Balances(pallet_balances::Call::transfer_allow_death {
|
||||
dest: MultiSigner::from(dest.public()).into_account().into(),
|
||||
value,
|
||||
});
|
||||
|
||||
construct_extrinsic(client, function, origin, 0)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezkuwi.
|
||||
|
||||
// Pezkuwi 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.
|
||||
|
||||
// Pezkuwi 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 Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use futures::{future, pin_mut, select, FutureExt};
|
||||
use pezkuwi_test_service::*;
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn ensure_test_service_build_blocks() {
|
||||
let mut builder = sc_cli::LoggerBuilder::new("");
|
||||
builder.with_colors(false);
|
||||
builder.init().expect("Sets up logger");
|
||||
let alice_config = node_config(
|
||||
|| {},
|
||||
tokio::runtime::Handle::current(),
|
||||
Sr25519Keyring::Alice,
|
||||
Vec::new(),
|
||||
true,
|
||||
);
|
||||
let mut alice = run_validator_node(alice_config, None).await;
|
||||
|
||||
let bob_config = node_config(
|
||||
|| {},
|
||||
tokio::runtime::Handle::current(),
|
||||
Sr25519Keyring::Bob,
|
||||
vec![alice.addr.clone()],
|
||||
true,
|
||||
);
|
||||
let mut bob = run_validator_node(bob_config, None).await;
|
||||
|
||||
{
|
||||
let t1 = future::join(alice.wait_for_blocks(3), bob.wait_for_blocks(3)).fuse();
|
||||
let t2 = alice.task_manager.future().fuse();
|
||||
let t3 = bob.task_manager.future().fuse();
|
||||
|
||||
pin_mut!(t1, t2, t3);
|
||||
|
||||
select! {
|
||||
_ = t1 => {},
|
||||
_ = t2 => panic!("service Alice failed"),
|
||||
_ = t3 => panic!("service Bob failed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezkuwi.
|
||||
|
||||
// Pezkuwi 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.
|
||||
|
||||
// Pezkuwi 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 Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use pezkuwi_test_service::*;
|
||||
use sp_keyring::Sr25519Keyring::{Alice, Bob, Charlie};
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn call_function_actually_work() {
|
||||
let alice_config =
|
||||
node_config(|| {}, tokio::runtime::Handle::current(), Alice, Vec::new(), true);
|
||||
|
||||
let alice = run_validator_node(alice_config, None).await;
|
||||
|
||||
let function =
|
||||
pezkuwi_test_runtime::RuntimeCall::Balances(pallet_balances::Call::transfer_allow_death {
|
||||
dest: Charlie.to_account_id().into(),
|
||||
value: 1,
|
||||
});
|
||||
let output = alice.send_extrinsic(function, Bob).await.unwrap();
|
||||
|
||||
let res = output.result;
|
||||
let json = serde_json::from_str::<serde_json::Value>(res.as_str()).expect("valid JSON");
|
||||
let object = json.as_object().expect("JSON is an object");
|
||||
assert!(object.contains_key("jsonrpc"), "key jsonrpc exists");
|
||||
let result = object.get("result");
|
||||
let result = result.expect("key result exists");
|
||||
assert_eq!(result.as_str().map(|x| x.starts_with("0x")), Some(true), "result starts with 0x");
|
||||
}
|
||||
Reference in New Issue
Block a user