diff --git a/Cargo.lock b/Cargo.lock index 32bfc45c5391..0c017bc798ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4149,7 +4149,6 @@ dependencies = [ name = "polkadot-network-test" version = "0.8.0" dependencies = [ - "env_logger 0.7.1", "futures 0.3.4", "log 0.4.8", "parking_lot 0.10.2", @@ -4333,6 +4332,7 @@ dependencies = [ name = "polkadot-service" version = "0.7.30" dependencies = [ + "env_logger 0.7.1", "frame-benchmarking", "frame-system-rpc-runtime-api", "futures 0.3.4", @@ -4456,7 +4456,10 @@ name = "polkadot-test-runtime-client" version = "2.0.0" dependencies = [ "futures 0.3.4", + "pallet-timestamp", "parity-scale-codec", + "polkadot-primitives", + "polkadot-runtime-common", "polkadot-test-runtime", "sc-block-builder", "sc-client", @@ -4466,7 +4469,6 @@ dependencies = [ "sp-core", "sp-runtime", "substrate-test-client", - "substrate-test-runtime", ] [[package]] diff --git a/network/test/Cargo.toml b/network/test/Cargo.toml index 5aa7836f476d..c1a6fc9663d1 100644 --- a/network/test/Cargo.toml +++ b/network/test/Cargo.toml @@ -19,5 +19,4 @@ sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "mas sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-block-builder = { git = "https://github.com/paritytech/substrate", branch = "master" } -env_logger = "0.7.0" polkadot-test-runtime-client = { path = "../../runtime/test-runtime/client" } diff --git a/network/test/src/block_import.rs b/network/test/src/block_import.rs index 7bc8b655f0fe..2b61898e41e2 100644 --- a/network/test/src/block_import.rs +++ b/network/test/src/block_import.rs @@ -27,7 +27,15 @@ use super::*; fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) { let mut client = polkadot_test_runtime_client::new(); - let block = client.new_block(Default::default()).unwrap().build().unwrap().block; + let mut builder = client.new_block(Default::default()).unwrap(); + + let extrinsics = polkadot_test_runtime_client::needed_extrinsics(vec![]); + + for extrinsic in &extrinsics { + builder.push(extrinsic.clone()).unwrap(); + } + + let block = builder.build().unwrap().block; client.import(BlockOrigin::File, block).unwrap(); let (hash, number) = (client.block_hash(1).unwrap().unwrap(), 1); @@ -37,7 +45,7 @@ fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) (client, hash, number, peer_id.clone(), IncomingBlock { hash, header, - body: Some(Vec::new()), + body: Some(extrinsics), justification, origin: Some(peer_id.clone()), allow_missing_state: false, diff --git a/runtime/test-runtime/client/Cargo.toml b/runtime/test-runtime/client/Cargo.toml index cee2277bc6f6..b540adfb9b49 100644 --- a/runtime/test-runtime/client/Cargo.toml +++ b/runtime/test-runtime/client/Cargo.toml @@ -10,11 +10,13 @@ sc-block-builder = { git = "https://github.com/paritytech/substrate", branch = " substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } polkadot-test-runtime = { path = ".." } -substrate-test-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } +polkadot-runtime-common = { path = "../../common" } +polkadot-primitives = { path = "../../../primitives" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } codec = { package = "parity-scale-codec", version = "1.0.0" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-client = { git = "https://github.com/paritytech/substrate", branch = "master" } +pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master" } futures = "0.3.1" diff --git a/runtime/test-runtime/client/src/lib.rs b/runtime/test-runtime/client/src/lib.rs index 775e4f33b922..348caf829738 100644 --- a/runtime/test-runtime/client/src/lib.rs +++ b/runtime/test-runtime/client/src/lib.rs @@ -26,7 +26,7 @@ pub use sc_client::LongestChain; use sp_core::{sr25519, ChangesTrieConfiguration, map, twox_128}; use sp_core::storage::{ChildInfo, Storage, StorageChild}; -use substrate_test_runtime::genesismap::{GenesisConfig}; +use polkadot_test_runtime::genesismap::GenesisConfig; use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Hash as HashT, HashFor}; use sc_client::{ light::fetcher::{ @@ -81,7 +81,6 @@ pub type LightExecutor = sc_client::light::call_executor::GenesisCallExecutor< #[derive(Default)] pub struct GenesisParameters { changes_trie_config: Option, - heap_pages_override: Option, extra_storage: Storage, } @@ -94,13 +93,7 @@ impl GenesisParameters { sr25519::Public::from(Sr25519Keyring::Bob).into(), sr25519::Public::from(Sr25519Keyring::Charlie).into(), ], - vec![ - AccountKeyring::Alice.into(), - AccountKeyring::Bob.into(), - AccountKeyring::Charlie.into(), - ], 1000, - self.heap_pages_override, self.extra_storage.clone(), ) } @@ -176,12 +169,6 @@ pub trait TestClientBuilderExt: Sized { self } - /// Override the default value for Wasm heap pages. - fn set_heap_pages(mut self, heap_pages: u64) -> Self { - self.genesis_init_mut().heap_pages_override = Some(heap_pages); - self - } - /// Add an extra value into the genesis storage. /// /// # Panics @@ -256,7 +243,7 @@ type MaybeFetcherCallback = Option Result, Vec>, - body: MaybeFetcherCallback, Vec>, + body: MaybeFetcherCallback, Vec>, } impl LightFetcher { @@ -274,7 +261,7 @@ impl LightFetcher { /// Sets remote body callback. pub fn with_remote_body( self, - body: MaybeFetcherCallback, Vec>, + body: MaybeFetcherCallback, Vec>, ) -> Self { LightFetcher { call: self.call, @@ -321,3 +308,23 @@ pub fn new_light_fetcher() -> LightFetcher { pub fn new_native_executor() -> sc_executor::NativeExecutor { sc_executor::NativeExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, None, 8) } + +/// Extrinsics that must be included in each block. +pub fn needed_extrinsics(heads: Vec) -> Vec { + use polkadot_runtime_common::parachains; + + vec![ + polkadot_test_runtime::UncheckedExtrinsic { + function: polkadot_test_runtime::Call::Parachains(parachains::Call::set_heads(heads)), + signature: None, + }, + polkadot_test_runtime::UncheckedExtrinsic { + function: polkadot_test_runtime::Call::Timestamp(pallet_timestamp::Call::set({ + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) + .expect("now always later than unix epoch; qed") + .as_millis() as u64 + })), + signature: None, + } + ] +} diff --git a/runtime/test-runtime/src/genesismap.rs b/runtime/test-runtime/src/genesismap.rs new file mode 100644 index 000000000000..9ab33e8e414a --- /dev/null +++ b/runtime/test-runtime/src/genesismap.rs @@ -0,0 +1,72 @@ +// Copyright 2020 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate 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. + +// Substrate 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 Substrate. If not, see . + +//! Tool for creating the genesis block. + +use std::collections::BTreeMap; +use super::{AccountId, WASM_BINARY, constants::currency}; +use sp_core::ChangesTrieConfiguration; +use sp_core::storage::Storage; +use sp_runtime::BuildStorage; + +/// Configuration of a general Substrate test genesis block. +pub struct GenesisConfig { + changes_trie_config: Option, + balances: Vec<(AccountId, u128)>, + /// Additional storage key pairs that will be added to the genesis map. + extra_storage: Storage, +} + +impl GenesisConfig { + pub fn new( + changes_trie_config: Option, + endowed_accounts: Vec, + balance: u128, + extra_storage: Storage, + ) -> Self { + GenesisConfig { + changes_trie_config, + balances: endowed_accounts.into_iter().map(|a| (a, balance * currency::DOLLARS)).collect(), + extra_storage, + } + } + + pub fn genesis_map(&self) -> Storage { + // Assimilate the system genesis config. + let mut storage = Storage { top: BTreeMap::new(), children: self.extra_storage.children.clone()}; + let config = crate::GenesisConfig { + system: Some(system::GenesisConfig { + changes_trie_config: self.changes_trie_config.clone(), + code: WASM_BINARY.to_vec(), + }), + babe: None, + indices: None, + balances: Some(balances::GenesisConfig { + balances: self.balances.clone() + }), + staking: None, + session: None, + grandpa: None, + claims: None, + parachains: None, + registrar: None, + vesting: None, + }; + config.assimilate_storage(&mut storage).expect("Adding `system::GensisConfig` to the genesis"); + + storage + } +} diff --git a/runtime/test-runtime/src/lib.rs b/runtime/test-runtime/src/lib.rs index f339133edfe5..b63bf0ea3a85 100644 --- a/runtime/test-runtime/src/lib.rs +++ b/runtime/test-runtime/src/lib.rs @@ -69,6 +69,8 @@ pub use parachains::Call as ParachainsCall; /// Constant values used within the runtime. pub mod constants; +#[cfg(feature = "std")] +pub mod genesismap; use constants::{time::*, currency::*, fee::*}; // Make the WASM binary available. @@ -204,7 +206,7 @@ impl transaction_payment::Trait for Runtime { } parameter_types! { - pub const MinimumPeriod: u64 = SLOT_DURATION / 2; + pub const MinimumPeriod: u64 = 0; } impl timestamp::Trait for Runtime { type Moment = u64; diff --git a/service/Cargo.toml b/service/Cargo.toml index 0fb855e0d182..14fffc94f98d 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -59,6 +59,7 @@ frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = [dev-dependencies] polkadot-test-runtime-client = { path = "../runtime/test-runtime/client" } sc-block-builder = { git = "https://github.com/paritytech/substrate", branch = "master" } +env_logger = "0.7.0" [features] default = ["db", "full-node"] diff --git a/service/src/grandpa_support.rs b/service/src/grandpa_support.rs index 77e6468813fb..a875c4b45a37 100644 --- a/service/src/grandpa_support.rs +++ b/service/src/grandpa_support.rs @@ -244,13 +244,21 @@ mod tests { #[test] fn grandpa_pause_voting_rule_works() { + let _ = env_logger::try_init(); + let client = Arc::new(polkadot_test_runtime_client::new()); let mut push_blocks = { let mut client = client.clone(); move |n| { for _ in 0..n { - let block = client.new_block(Default::default()).unwrap().build().unwrap().block; + let mut builder = client.new_block(Default::default()).unwrap(); + + for extrinsic in polkadot_test_runtime_client::needed_extrinsics(vec![]) { + builder.push(extrinsic).unwrap() + } + + let block = builder.build().unwrap().block; client.import(BlockOrigin::Own, block).unwrap(); } }