Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Remove usage of substrate-test-runtime. (#969)
Browse files Browse the repository at this point in the history
* Switch from using the substrate_test_runtime Extrinsic to the polkadot_test_runtime one

* Copy genesismap into test-runtime

* Add UncheckedExtrinsics

* Fix tests :^)

* Remove unused functions from genesismap

* DRY, clean up

* Clean up

* Update service/src/grandpa_support.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix indentation

* Update runtime/test-runtime/src/genesismap.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
  • Loading branch information
expenses and bkchr committed Apr 21, 2020
1 parent c79e4bd commit e843467
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 24 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion network/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
12 changes: 10 additions & 2 deletions network/test/src/block_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ use super::*;

fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock<Block>) {
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);
Expand All @@ -37,7 +45,7 @@ fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock<Block>)
(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,
Expand Down
4 changes: 3 additions & 1 deletion runtime/test-runtime/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
39 changes: 23 additions & 16 deletions runtime/test-runtime/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -81,7 +81,6 @@ pub type LightExecutor = sc_client::light::call_executor::GenesisCallExecutor<
#[derive(Default)]
pub struct GenesisParameters {
changes_trie_config: Option<ChangesTrieConfiguration>,
heap_pages_override: Option<u64>,
extra_storage: Storage,
}

Expand All @@ -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(),
)
}
Expand Down Expand Up @@ -176,12 +169,6 @@ pub trait TestClientBuilderExt<B>: 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
Expand Down Expand Up @@ -256,7 +243,7 @@ type MaybeFetcherCallback<Req, Resp> = Option<Box<dyn Fn(Req) -> Result<Resp, sp
#[derive(Default)]
pub struct LightFetcher {
call: MaybeFetcherCallback<RemoteCallRequest<polkadot_test_runtime::Header>, Vec<u8>>,
body: MaybeFetcherCallback<RemoteBodyRequest<polkadot_test_runtime::Header>, Vec<substrate_test_runtime::Extrinsic>>,
body: MaybeFetcherCallback<RemoteBodyRequest<polkadot_test_runtime::Header>, Vec<polkadot_test_runtime::Extrinsic>>,
}

impl LightFetcher {
Expand All @@ -274,7 +261,7 @@ impl LightFetcher {
/// Sets remote body callback.
pub fn with_remote_body(
self,
body: MaybeFetcherCallback<RemoteBodyRequest<polkadot_test_runtime::Header>, Vec<substrate_test_runtime::Extrinsic>>,
body: MaybeFetcherCallback<RemoteBodyRequest<polkadot_test_runtime::Header>, Vec<polkadot_test_runtime::Extrinsic>>,
) -> Self {
LightFetcher {
call: self.call,
Expand Down Expand Up @@ -321,3 +308,23 @@ pub fn new_light_fetcher() -> LightFetcher {
pub fn new_native_executor() -> sc_executor::NativeExecutor<LocalExecutor> {
sc_executor::NativeExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, None, 8)
}

/// Extrinsics that must be included in each block.
pub fn needed_extrinsics(heads: Vec<polkadot_primitives::parachain::AttestedCandidate>) -> Vec<polkadot_test_runtime::UncheckedExtrinsic> {
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,
}
]
}
72 changes: 72 additions & 0 deletions runtime/test-runtime/src/genesismap.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

//! 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<ChangesTrieConfiguration>,
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<ChangesTrieConfiguration>,
endowed_accounts: Vec<AccountId>,
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
}
}
4 changes: 3 additions & 1 deletion runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 9 additions & 1 deletion service/src/grandpa_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit e843467

Please sign in to comment.