From 928697f3b307be406425fe75d8769d400198d4c3 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Fri, 3 Apr 2020 12:10:53 +0200 Subject: [PATCH 01/10] Switch from using the substrate_test_runtime Extrinsic to the polkadot_test_runtime one --- runtime/test-runtime/client/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/test-runtime/client/src/lib.rs b/runtime/test-runtime/client/src/lib.rs index 775e4f33b922..39590063fb4c 100644 --- a/runtime/test-runtime/client/src/lib.rs +++ b/runtime/test-runtime/client/src/lib.rs @@ -256,7 +256,7 @@ type MaybeFetcherCallback = Option Result, Vec>, - body: MaybeFetcherCallback, Vec>, + body: MaybeFetcherCallback, Vec>, } impl LightFetcher { @@ -274,7 +274,7 @@ impl LightFetcher { /// Sets remote body callback. pub fn with_remote_body( self, - body: MaybeFetcherCallback, Vec>, + body: MaybeFetcherCallback, Vec>, ) -> Self { LightFetcher { call: self.call, From 439b60c2ebeaaedc4b8d0fe07ba2c80e6aeb5b55 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Fri, 3 Apr 2020 13:35:29 +0200 Subject: [PATCH 02/10] Copy genesismap into test-runtime --- Cargo.lock | 2 +- runtime/test-runtime/Cargo.toml | 3 +- runtime/test-runtime/client/Cargo.toml | 1 - runtime/test-runtime/client/src/lib.rs | 9 +- runtime/test-runtime/src/genesismap.rs | 117 +++++++++++++++++++++++++ runtime/test-runtime/src/lib.rs | 2 + 6 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 runtime/test-runtime/src/genesismap.rs diff --git a/Cargo.lock b/Cargo.lock index 54218adcc278..07b5edcc7b77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4520,6 +4520,7 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "rustc-hex", + "sc-client", "serde", "serde_derive", "serde_json", @@ -4557,7 +4558,6 @@ dependencies = [ "sp-core", "sp-runtime", "substrate-test-client", - "substrate-test-runtime", ] [[package]] diff --git a/runtime/test-runtime/Cargo.toml b/runtime/test-runtime/Cargo.toml index f1217d2a1e74..729a2c9215f8 100644 --- a/runtime/test-runtime/Cargo.toml +++ b/runtime/test-runtime/Cargo.toml @@ -25,7 +25,7 @@ sp-session = { git = "https://github.com/paritytech/substrate", branch = "master version = { package = "sp-version", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } tx-pool-api = { package = "sp-transaction-pool", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } block-builder-api = { package = "sp-block-builder", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } - +sc-client = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } authorship = { package = "pallet-authorship", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } babe = { package = "pallet-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } balances = { package = "pallet-balances", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } @@ -78,6 +78,7 @@ std = [ "sp-api/std", "tx-pool-api/std", "block-builder-api/std", + "sc-client", "rstd/std", "sp-io/std", "frame-support/std", diff --git a/runtime/test-runtime/client/Cargo.toml b/runtime/test-runtime/client/Cargo.toml index cee2277bc6f6..959154f59eb1 100644 --- a/runtime/test-runtime/client/Cargo.toml +++ b/runtime/test-runtime/client/Cargo.toml @@ -10,7 +10,6 @@ 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" } 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" } diff --git a/runtime/test-runtime/client/src/lib.rs b/runtime/test-runtime/client/src/lib.rs index 39590063fb4c..8dea75f4d8e8 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::{ @@ -94,11 +94,6 @@ 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(), @@ -274,7 +269,7 @@ impl LightFetcher { /// Sets remote body callback. pub fn with_remote_body( self, - body: MaybeFetcherCallback, Vec>, + body: MaybeFetcherCallback, Vec>, ) -> Self { LightFetcher { call: self.call, diff --git a/runtime/test-runtime/src/genesismap.rs b/runtime/test-runtime/src/genesismap.rs new file mode 100644 index 000000000000..ae1e21521a54 --- /dev/null +++ b/runtime/test-runtime/src/genesismap.rs @@ -0,0 +1,117 @@ +// Copyright 2017-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 sp_io::hashing::{blake2_256, twox_128}; +use super::{AccountId, WASM_BINARY}; +use codec::{Encode, KeyedVec, Joiner}; +use sp_core::{ChangesTrieConfiguration, map}; +use sp_core::storage::{well_known_keys, Storage}; +use sp_runtime::BuildStorage; +use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT}; + +/// Configuration of a general Substrate test genesis block. +pub struct GenesisConfig { + changes_trie_config: Option, + balances: Vec<(AccountId, u64)>, + heap_pages_override: Option, + /// 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: u64, + heap_pages_override: Option, + extra_storage: Storage, + ) -> Self { + GenesisConfig { + changes_trie_config, + balances: endowed_accounts.into_iter().map(|a| (a, balance)).collect(), + heap_pages_override, + extra_storage, + } + } + + pub fn genesis_map(&self) -> Storage { + let wasm_runtime = WASM_BINARY.to_vec(); + let mut map: BTreeMap, Vec> = self.balances.iter() + .map(|&(ref account, balance)| (account.to_keyed_vec(b"balance:"), vec![].and(&balance))) + .map(|(k, v)| (blake2_256(&k[..])[..].to_vec(), v.to_vec())) + .chain(vec![ + (well_known_keys::CODE.into(), wasm_runtime), + ( + well_known_keys::HEAP_PAGES.into(), + vec![].and(&(self.heap_pages_override.unwrap_or(16 as u64))), + ), + ].into_iter()) + .collect(); + if let Some(ref changes_trie_config) = self.changes_trie_config { + map.insert(well_known_keys::CHANGES_TRIE_CONFIG.to_vec(), changes_trie_config.encode()); + } + // Add the extra storage entries. + map.extend(self.extra_storage.top.clone().into_iter()); + + // Assimilate the system genesis config. + let mut storage = Storage { top: map, children: self.extra_storage.children.clone()}; + let config = crate::GenesisConfig { + system: None, + babe: None, + indices: None, + balances: None, + 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 + } +} + +pub fn insert_genesis_block( + storage: &mut Storage, +) -> sp_core::hash::H256 { + let child_roots = storage.children.iter().map(|(sk, child_content)| { + let state_root = <<::Header as HeaderT>::Hashing as HashT>::trie_root( + child_content.data.clone().into_iter().collect(), + ); + (sk.clone(), state_root.encode()) + }); + // add child roots to storage + storage.top.extend(child_roots); + let state_root = <<::Header as HeaderT>::Hashing as HashT>::trie_root( + storage.top.clone().into_iter().collect() + ); + let block: crate::Block = sc_client::genesis::construct_genesis_block(state_root); + let genesis_hash = block.header.hash(); + storage.top.extend(additional_storage_with_genesis(&block)); + genesis_hash +} + +pub fn additional_storage_with_genesis(genesis_block: &crate::Block) -> BTreeMap, Vec> { + map![ + twox_128(&b"latest"[..]).to_vec() => genesis_block.hash().as_fixed_bytes().to_vec() + ] +} diff --git a/runtime/test-runtime/src/lib.rs b/runtime/test-runtime/src/lib.rs index 8230c4ad6581..2ac9d5826760 100644 --- a/runtime/test-runtime/src/lib.rs +++ b/runtime/test-runtime/src/lib.rs @@ -66,6 +66,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. From 299aad9d24de7e68f76577dc2ee9ee8c9b74827b Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Tue, 7 Apr 2020 19:20:31 +0200 Subject: [PATCH 03/10] Add UncheckedExtrinsics --- Cargo.lock | 2 ++ network/test/Cargo.toml | 2 ++ network/test/src/block_import.rs | 18 ++++++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 07b5edcc7b77..16402f8b905e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4238,7 +4238,9 @@ dependencies = [ "env_logger 0.7.1", "futures 0.3.4", "log 0.4.8", + "pallet-timestamp", "parking_lot 0.10.0", + "polkadot-runtime-common", "polkadot-test-runtime-client", "rand 0.7.3", "sc-block-builder", diff --git a/network/test/Cargo.toml b/network/test/Cargo.toml index 5aa7836f476d..e931b6f22666 100644 --- a/network/test/Cargo.toml +++ b/network/test/Cargo.toml @@ -19,5 +19,7 @@ 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" } +pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master" } env_logger = "0.7.0" polkadot-test-runtime-client = { path = "../../runtime/test-runtime/client" } +polkadot-runtime-common = { path = "../../runtime/common" } diff --git a/network/test/src/block_import.rs b/network/test/src/block_import.rs index 7bc8b655f0fe..e005991cf8b9 100644 --- a/network/test/src/block_import.rs +++ b/network/test/src/block_import.rs @@ -20,14 +20,26 @@ use sp_consensus::ImportedAux; use sp_consensus::import_queue::{ import_single_block, BasicQueue, BlockImportError, BlockImportResult, IncomingBlock, }; +use sp_runtime::generic::UncheckedExtrinsic; use polkadot_test_runtime_client::{self, prelude::*}; -use polkadot_test_runtime_client::runtime::{Block, Hash}; +use polkadot_test_runtime_client::runtime::{Block, Hash, Call}; +use polkadot_runtime_common::ParachainsCall; +use pallet_timestamp::Call as TimestampCall; use sp_runtime::generic::BlockId; 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(); + builder.push(UncheckedExtrinsic { + function: Call::Parachains(ParachainsCall::set_heads(Vec::new())), + signature: None, + }).unwrap(); + builder.push(UncheckedExtrinsic { + function: Call::Timestamp(TimestampCall::set(0)), + signature: None, + }).unwrap(); + let block = builder.build().unwrap().block; client.import(BlockOrigin::File, block).unwrap(); let (hash, number) = (client.block_hash(1).unwrap().unwrap(), 1); @@ -47,6 +59,8 @@ fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) #[test] fn import_single_good_block_works() { + env_logger::init(); + let (_, _hash, number, peer_id, block) = prepare_good_block(); let mut expected_aux = ImportedAux::default(); From 14548bc8bfdad136903b722abe45efa9945a24f1 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Wed, 8 Apr 2020 15:48:54 +0200 Subject: [PATCH 04/10] Fix tests :^) --- network/test/src/block_import.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/network/test/src/block_import.rs b/network/test/src/block_import.rs index e005991cf8b9..7736281d17d6 100644 --- a/network/test/src/block_import.rs +++ b/network/test/src/block_import.rs @@ -31,14 +31,19 @@ use super::*; fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) { let mut client = polkadot_test_runtime_client::new(); let mut builder = client.new_block(Default::default()).unwrap(); - builder.push(UncheckedExtrinsic { + + let set_heads = UncheckedExtrinsic { function: Call::Parachains(ParachainsCall::set_heads(Vec::new())), signature: None, - }).unwrap(); - builder.push(UncheckedExtrinsic { + }; + + let timestamp = UncheckedExtrinsic { function: Call::Timestamp(TimestampCall::set(0)), signature: None, - }).unwrap(); + }; + + builder.push(set_heads.clone()).unwrap(); + builder.push(timestamp.clone()).unwrap(); let block = builder.build().unwrap().block; client.import(BlockOrigin::File, block).unwrap(); @@ -49,7 +54,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(vec![set_heads, timestamp]), justification, origin: Some(peer_id.clone()), allow_missing_state: false, From a47e601d12a52990e5146050026bcb21b380599c Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Wed, 8 Apr 2020 15:52:23 +0200 Subject: [PATCH 05/10] Remove unused functions from genesismap --- runtime/test-runtime/src/genesismap.rs | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/runtime/test-runtime/src/genesismap.rs b/runtime/test-runtime/src/genesismap.rs index ae1e21521a54..30a7551c2bc1 100644 --- a/runtime/test-runtime/src/genesismap.rs +++ b/runtime/test-runtime/src/genesismap.rs @@ -89,29 +89,3 @@ impl GenesisConfig { storage } } - -pub fn insert_genesis_block( - storage: &mut Storage, -) -> sp_core::hash::H256 { - let child_roots = storage.children.iter().map(|(sk, child_content)| { - let state_root = <<::Header as HeaderT>::Hashing as HashT>::trie_root( - child_content.data.clone().into_iter().collect(), - ); - (sk.clone(), state_root.encode()) - }); - // add child roots to storage - storage.top.extend(child_roots); - let state_root = <<::Header as HeaderT>::Hashing as HashT>::trie_root( - storage.top.clone().into_iter().collect() - ); - let block: crate::Block = sc_client::genesis::construct_genesis_block(state_root); - let genesis_hash = block.header.hash(); - storage.top.extend(additional_storage_with_genesis(&block)); - genesis_hash -} - -pub fn additional_storage_with_genesis(genesis_block: &crate::Block) -> BTreeMap, Vec> { - map![ - twox_128(&b"latest"[..]).to_vec() => genesis_block.hash().as_fixed_bytes().to_vec() - ] -} From 1df43e999c0b50ce845d5626a231b39a5dc08c79 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Wed, 8 Apr 2020 17:26:49 +0200 Subject: [PATCH 06/10] DRY, clean up --- Cargo.lock | 6 ++---- network/test/Cargo.toml | 3 --- network/test/src/block_import.rs | 18 ++---------------- runtime/test-runtime/Cargo.toml | 3 +-- runtime/test-runtime/client/Cargo.toml | 2 ++ runtime/test-runtime/client/src/lib.rs | 17 +++++++++++++++++ runtime/test-runtime/src/genesismap.rs | 5 ++--- service/src/grandpa_support.rs | 8 +++++++- 8 files changed, 33 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 16402f8b905e..90d9422abef6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4235,12 +4235,9 @@ dependencies = [ name = "polkadot-network-test" version = "0.8.0" dependencies = [ - "env_logger 0.7.1", "futures 0.3.4", "log 0.4.8", - "pallet-timestamp", "parking_lot 0.10.0", - "polkadot-runtime-common", "polkadot-test-runtime-client", "rand 0.7.3", "sc-block-builder", @@ -4522,7 +4519,6 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "rustc-hex", - "sc-client", "serde", "serde_derive", "serde_json", @@ -4550,7 +4546,9 @@ name = "polkadot-test-runtime-client" version = "2.0.0" dependencies = [ "futures 0.3.4", + "pallet-timestamp", "parity-scale-codec", + "polkadot-runtime-common", "polkadot-test-runtime", "sc-block-builder", "sc-client", diff --git a/network/test/Cargo.toml b/network/test/Cargo.toml index e931b6f22666..c1a6fc9663d1 100644 --- a/network/test/Cargo.toml +++ b/network/test/Cargo.toml @@ -19,7 +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" } -pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master" } -env_logger = "0.7.0" polkadot-test-runtime-client = { path = "../../runtime/test-runtime/client" } -polkadot-runtime-common = { path = "../../runtime/common" } diff --git a/network/test/src/block_import.rs b/network/test/src/block_import.rs index 7736281d17d6..eda4fe67f8ac 100644 --- a/network/test/src/block_import.rs +++ b/network/test/src/block_import.rs @@ -20,11 +20,8 @@ use sp_consensus::ImportedAux; use sp_consensus::import_queue::{ import_single_block, BasicQueue, BlockImportError, BlockImportResult, IncomingBlock, }; -use sp_runtime::generic::UncheckedExtrinsic; use polkadot_test_runtime_client::{self, prelude::*}; -use polkadot_test_runtime_client::runtime::{Block, Hash, Call}; -use polkadot_runtime_common::ParachainsCall; -use pallet_timestamp::Call as TimestampCall; +use polkadot_test_runtime_client::runtime::{Block, Hash}; use sp_runtime::generic::BlockId; use super::*; @@ -32,16 +29,7 @@ fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) let mut client = polkadot_test_runtime_client::new(); let mut builder = client.new_block(Default::default()).unwrap(); - let set_heads = UncheckedExtrinsic { - function: Call::Parachains(ParachainsCall::set_heads(Vec::new())), - signature: None, - }; - - let timestamp = UncheckedExtrinsic { - function: Call::Timestamp(TimestampCall::set(0)), - signature: None, - }; - + let (set_heads, timestamp) = polkadot_test_runtime_client::needed_extrinsics(); builder.push(set_heads.clone()).unwrap(); builder.push(timestamp.clone()).unwrap(); let block = builder.build().unwrap().block; @@ -64,8 +52,6 @@ fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) #[test] fn import_single_good_block_works() { - env_logger::init(); - let (_, _hash, number, peer_id, block) = prepare_good_block(); let mut expected_aux = ImportedAux::default(); diff --git a/runtime/test-runtime/Cargo.toml b/runtime/test-runtime/Cargo.toml index 729a2c9215f8..f1217d2a1e74 100644 --- a/runtime/test-runtime/Cargo.toml +++ b/runtime/test-runtime/Cargo.toml @@ -25,7 +25,7 @@ sp-session = { git = "https://github.com/paritytech/substrate", branch = "master version = { package = "sp-version", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } tx-pool-api = { package = "sp-transaction-pool", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } block-builder-api = { package = "sp-block-builder", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } -sc-client = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } + authorship = { package = "pallet-authorship", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } babe = { package = "pallet-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } balances = { package = "pallet-balances", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } @@ -78,7 +78,6 @@ std = [ "sp-api/std", "tx-pool-api/std", "block-builder-api/std", - "sc-client", "rstd/std", "sp-io/std", "frame-support/std", diff --git a/runtime/test-runtime/client/Cargo.toml b/runtime/test-runtime/client/Cargo.toml index 959154f59eb1..e2c955ea4871 100644 --- a/runtime/test-runtime/client/Cargo.toml +++ b/runtime/test-runtime/client/Cargo.toml @@ -10,10 +10,12 @@ 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 = ".." } +polkadot-runtime-common = { path = "../../common" } 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 8dea75f4d8e8..a94673f718a6 100644 --- a/runtime/test-runtime/client/src/lib.rs +++ b/runtime/test-runtime/client/src/lib.rs @@ -316,3 +316,20 @@ 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() -> (polkadot_test_runtime::UncheckedExtrinsic, polkadot_test_runtime::UncheckedExtrinsic) { + use polkadot_runtime_common::parachains; + + let set_heads = polkadot_test_runtime::UncheckedExtrinsic { + function: polkadot_test_runtime::Call::Parachains(parachains::Call::set_heads(Vec::new())), + signature: None, + }; + + let timestamp = polkadot_test_runtime::UncheckedExtrinsic { + function: polkadot_test_runtime::Call::Timestamp(pallet_timestamp::Call::set(0)), + signature: None, + }; + + (set_heads, timestamp) +} diff --git a/runtime/test-runtime/src/genesismap.rs b/runtime/test-runtime/src/genesismap.rs index 30a7551c2bc1..faf43b7b0f5f 100644 --- a/runtime/test-runtime/src/genesismap.rs +++ b/runtime/test-runtime/src/genesismap.rs @@ -17,13 +17,12 @@ //! Tool for creating the genesis block. use std::collections::BTreeMap; -use sp_io::hashing::{blake2_256, twox_128}; +use sp_io::hashing::blake2_256; use super::{AccountId, WASM_BINARY}; use codec::{Encode, KeyedVec, Joiner}; -use sp_core::{ChangesTrieConfiguration, map}; +use sp_core::ChangesTrieConfiguration; use sp_core::storage::{well_known_keys, Storage}; use sp_runtime::BuildStorage; -use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT}; /// Configuration of a general Substrate test genesis block. pub struct GenesisConfig { diff --git a/service/src/grandpa_support.rs b/service/src/grandpa_support.rs index 77e6468813fb..8eac5daaec4f 100644 --- a/service/src/grandpa_support.rs +++ b/service/src/grandpa_support.rs @@ -250,7 +250,13 @@ mod tests { 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(); + + let (set_heads, timestamp) = polkadot_test_runtime_client::needed_extrinsics(); + builder.push(set_heads).unwrap(); + builder.push(timestamp).unwrap(); + + let block = builder.build().unwrap().block; client.import(BlockOrigin::Own, block).unwrap(); } } From 50cc5c1cb6aa9ecf892ba88c0e06e95bd35fdd7a Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 9 Apr 2020 22:31:25 +0200 Subject: [PATCH 07/10] Clean up --- Cargo.lock | 2 ++ network/test/src/block_import.rs | 11 ++++--- runtime/test-runtime/client/Cargo.toml | 1 + runtime/test-runtime/client/src/lib.rs | 35 +++++++++----------- runtime/test-runtime/src/genesismap.rs | 44 ++++++++------------------ runtime/test-runtime/src/lib.rs | 2 +- service/Cargo.toml | 1 + service/src/grandpa_support.rs | 8 +++-- 8 files changed, 45 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 90d9422abef6..c8b5b8e736dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4424,6 +4424,7 @@ dependencies = [ name = "polkadot-service" version = "0.7.29-pre1" dependencies = [ + "env_logger 0.7.1", "frame-benchmarking", "frame-system-rpc-runtime-api", "futures 0.3.4", @@ -4548,6 +4549,7 @@ dependencies = [ "futures 0.3.4", "pallet-timestamp", "parity-scale-codec", + "polkadot-primitives", "polkadot-runtime-common", "polkadot-test-runtime", "sc-block-builder", diff --git a/network/test/src/block_import.rs b/network/test/src/block_import.rs index eda4fe67f8ac..2b61898e41e2 100644 --- a/network/test/src/block_import.rs +++ b/network/test/src/block_import.rs @@ -29,9 +29,12 @@ fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) let mut client = polkadot_test_runtime_client::new(); let mut builder = client.new_block(Default::default()).unwrap(); - let (set_heads, timestamp) = polkadot_test_runtime_client::needed_extrinsics(); - builder.push(set_heads.clone()).unwrap(); - builder.push(timestamp.clone()).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(); @@ -42,7 +45,7 @@ fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) (client, hash, number, peer_id.clone(), IncomingBlock { hash, header, - body: Some(vec![set_heads, timestamp]), + 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 e2c955ea4871..b540adfb9b49 100644 --- a/runtime/test-runtime/client/Cargo.toml +++ b/runtime/test-runtime/client/Cargo.toml @@ -11,6 +11,7 @@ substrate-test-client = { git = "https://github.com/paritytech/substrate", branc sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } polkadot-test-runtime = { path = ".." } 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" } diff --git a/runtime/test-runtime/client/src/lib.rs b/runtime/test-runtime/client/src/lib.rs index a94673f718a6..348caf829738 100644 --- a/runtime/test-runtime/client/src/lib.rs +++ b/runtime/test-runtime/client/src/lib.rs @@ -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, } @@ -95,7 +94,6 @@ impl GenesisParameters { sr25519::Public::from(Sr25519Keyring::Charlie).into(), ], 1000, - self.heap_pages_override, self.extra_storage.clone(), ) } @@ -171,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 @@ -318,18 +310,21 @@ pub fn new_native_executor() -> sc_executor::NativeExecutor { } /// Extrinsics that must be included in each block. -pub fn needed_extrinsics() -> (polkadot_test_runtime::UncheckedExtrinsic, polkadot_test_runtime::UncheckedExtrinsic) { +pub fn needed_extrinsics(heads: Vec) -> Vec { use polkadot_runtime_common::parachains; - let set_heads = polkadot_test_runtime::UncheckedExtrinsic { - function: polkadot_test_runtime::Call::Parachains(parachains::Call::set_heads(Vec::new())), - signature: None, - }; - - let timestamp = polkadot_test_runtime::UncheckedExtrinsic { - function: polkadot_test_runtime::Call::Timestamp(pallet_timestamp::Call::set(0)), - signature: None, - }; - - (set_heads, timestamp) + 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 index faf43b7b0f5f..3aa2f6287ed5 100644 --- a/runtime/test-runtime/src/genesismap.rs +++ b/runtime/test-runtime/src/genesismap.rs @@ -17,18 +17,15 @@ //! Tool for creating the genesis block. use std::collections::BTreeMap; -use sp_io::hashing::blake2_256; -use super::{AccountId, WASM_BINARY}; -use codec::{Encode, KeyedVec, Joiner}; +use super::{AccountId, WASM_BINARY, constants::currency}; use sp_core::ChangesTrieConfiguration; -use sp_core::storage::{well_known_keys, Storage}; +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, u64)>, - heap_pages_override: Option, + balances: Vec<(AccountId, u128)>, /// Additional storage key pairs that will be added to the genesis map. extra_storage: Storage, } @@ -37,44 +34,29 @@ impl GenesisConfig { pub fn new( changes_trie_config: Option, endowed_accounts: Vec, - balance: u64, - heap_pages_override: Option, + balance: u128, extra_storage: Storage, ) -> Self { GenesisConfig { changes_trie_config, - balances: endowed_accounts.into_iter().map(|a| (a, balance)).collect(), - heap_pages_override, + balances: endowed_accounts.into_iter().map(|a| (a, balance * currency::DOLLARS)).collect(), extra_storage, } } pub fn genesis_map(&self) -> Storage { - let wasm_runtime = WASM_BINARY.to_vec(); - let mut map: BTreeMap, Vec> = self.balances.iter() - .map(|&(ref account, balance)| (account.to_keyed_vec(b"balance:"), vec![].and(&balance))) - .map(|(k, v)| (blake2_256(&k[..])[..].to_vec(), v.to_vec())) - .chain(vec![ - (well_known_keys::CODE.into(), wasm_runtime), - ( - well_known_keys::HEAP_PAGES.into(), - vec![].and(&(self.heap_pages_override.unwrap_or(16 as u64))), - ), - ].into_iter()) - .collect(); - if let Some(ref changes_trie_config) = self.changes_trie_config { - map.insert(well_known_keys::CHANGES_TRIE_CONFIG.to_vec(), changes_trie_config.encode()); - } - // Add the extra storage entries. - map.extend(self.extra_storage.top.clone().into_iter()); - // Assimilate the system genesis config. - let mut storage = Storage { top: map, children: self.extra_storage.children.clone()}; + let mut storage = Storage { top: BTreeMap::new(), children: self.extra_storage.children.clone()}; let config = crate::GenesisConfig { - system: None, + system: Some(system::GenesisConfig { + changes_trie_config: self.changes_trie_config.clone(), + code: WASM_BINARY.to_vec(), + }), babe: None, indices: None, - balances: None, + balances: Some(balances::GenesisConfig { + balances: self.balances.clone() + }), staking: None, session: None, grandpa: None, diff --git a/runtime/test-runtime/src/lib.rs b/runtime/test-runtime/src/lib.rs index 2ac9d5826760..a915a7a12a92 100644 --- a/runtime/test-runtime/src/lib.rs +++ b/runtime/test-runtime/src/lib.rs @@ -196,7 +196,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 4bec42a3249d..b78119561a4f 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -58,6 +58,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 = ["rocksdb", "full-node"] diff --git a/service/src/grandpa_support.rs b/service/src/grandpa_support.rs index 8eac5daaec4f..f2c96aa68302 100644 --- a/service/src/grandpa_support.rs +++ b/service/src/grandpa_support.rs @@ -244,6 +244,8 @@ mod tests { #[test] fn grandpa_pause_voting_rule_works() { + env_logger::init(); + let client = Arc::new(polkadot_test_runtime_client::new()); let mut push_blocks = { @@ -252,9 +254,9 @@ mod tests { for _ in 0..n { let mut builder = client.new_block(Default::default()).unwrap(); - let (set_heads, timestamp) = polkadot_test_runtime_client::needed_extrinsics(); - builder.push(set_heads).unwrap(); - builder.push(timestamp).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(); From 669606129ae27f2c350ac7bd7c3b0e64b185a284 Mon Sep 17 00:00:00 2001 From: Ashley Date: Mon, 20 Apr 2020 09:18:55 +0200 Subject: [PATCH 08/10] Update service/src/grandpa_support.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Bastian Köcher --- service/src/grandpa_support.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/grandpa_support.rs b/service/src/grandpa_support.rs index f2c96aa68302..a875c4b45a37 100644 --- a/service/src/grandpa_support.rs +++ b/service/src/grandpa_support.rs @@ -244,7 +244,7 @@ mod tests { #[test] fn grandpa_pause_voting_rule_works() { - env_logger::init(); + let _ = env_logger::try_init(); let client = Arc::new(polkadot_test_runtime_client::new()); From a2df14e9ee7eede4067f5afe34e4e7f1c7c87647 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Mon, 20 Apr 2020 09:45:00 +0200 Subject: [PATCH 09/10] Fix indentation --- runtime/test-runtime/src/genesismap.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/runtime/test-runtime/src/genesismap.rs b/runtime/test-runtime/src/genesismap.rs index 3aa2f6287ed5..7e243934f921 100644 --- a/runtime/test-runtime/src/genesismap.rs +++ b/runtime/test-runtime/src/genesismap.rs @@ -48,23 +48,23 @@ impl GenesisConfig { // 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 { + 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 { + 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, - }; + 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 From 0ed611b3c04a4dc80e2bf2f7d9b3ea9d92ae97d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 20 Apr 2020 10:46:15 +0200 Subject: [PATCH 10/10] Update runtime/test-runtime/src/genesismap.rs --- runtime/test-runtime/src/genesismap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/test-runtime/src/genesismap.rs b/runtime/test-runtime/src/genesismap.rs index 7e243934f921..9ab33e8e414a 100644 --- a/runtime/test-runtime/src/genesismap.rs +++ b/runtime/test-runtime/src/genesismap.rs @@ -1,4 +1,4 @@ -// Copyright 2017-2020 Parity Technologies (UK) Ltd. +// Copyright 2020 Parity Technologies (UK) Ltd. // This file is part of Substrate. // Substrate is free software: you can redistribute it and/or modify