// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Cumulus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Cumulus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see .
#![allow(missing_docs)]
use cumulus_primitives_core::ParaId;
use cumulus_test_runtime::{AccountId, RuntimeGenesisConfig, Signature};
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::ChainType;
use serde::{Deserialize, Serialize};
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};
/// Specialized `ChainSpec` for the normal parachain runtime.
pub type ChainSpec = sc_service::GenericChainSpec;
/// Helper function to generate a crypto pair from seed
pub fn get_from_seed(seed: &str) -> ::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}
/// The extensions for the [`ChainSpec`](crate::ChainSpec).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]
#[serde(deny_unknown_fields)]
pub struct Extensions {
/// The id of the Parachain.
pub para_id: u32,
}
impl Extensions {
/// Try to get the extension from the given `ChainSpec`.
pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {
sc_chain_spec::get_extension(chain_spec.extensions())
}
}
type AccountPublic = ::Signer;
/// Helper function to generate an account ID from seed.
pub fn get_account_id_from_seed(seed: &str) -> AccountId
where
AccountPublic: From<::Public>,
{
AccountPublic::from(get_from_seed::(seed)).into_account()
}
/// Get the chain spec for a specific parachain ID.
/// The given accounts are initialized with funds in addition
/// to the default known accounts.
pub fn get_chain_spec_with_extra_endowed(
id: Option,
extra_endowed_accounts: Vec,
) -> ChainSpec {
ChainSpec::builder(
cumulus_test_runtime::WASM_BINARY.expect("WASM binary was not built, please build it!"),
Extensions { para_id: id.unwrap_or(cumulus_test_runtime::PARACHAIN_ID.into()).into() },
)
.with_name("Local Testnet")
.with_id("local_testnet")
.with_chain_type(ChainType::Local)
.with_genesis_config_patch(testnet_genesis_with_default_endowed(
extra_endowed_accounts.clone(),
id,
))
.build()
}
/// Get the chain spec for a specific parachain ID.
pub fn get_chain_spec(id: Option) -> ChainSpec {
get_chain_spec_with_extra_endowed(id, Default::default())
}
/// Local testnet genesis for testing.
pub fn testnet_genesis_with_default_endowed(
mut extra_endowed_accounts: Vec,
self_para_id: Option,
) -> serde_json::Value {
let mut endowed = vec![
get_account_id_from_seed::("Alice"),
get_account_id_from_seed::("Bob"),
get_account_id_from_seed::("Charlie"),
get_account_id_from_seed::("Dave"),
get_account_id_from_seed::("Eve"),
get_account_id_from_seed::("Ferdie"),
get_account_id_from_seed::("Alice//stash"),
get_account_id_from_seed::("Bob//stash"),
get_account_id_from_seed::("Charlie//stash"),
get_account_id_from_seed::("Dave//stash"),
get_account_id_from_seed::("Eve//stash"),
get_account_id_from_seed::("Ferdie//stash"),
];
endowed.append(&mut extra_endowed_accounts);
testnet_genesis(get_account_id_from_seed::("Alice"), endowed, self_para_id)
}
/// Creates a local testnet genesis with endowed accounts.
pub fn testnet_genesis(
root_key: AccountId,
endowed_accounts: Vec,
self_para_id: Option,
) -> serde_json::Value {
serde_json::json!({
"balances": cumulus_test_runtime::BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
},
"sudo": cumulus_test_runtime::SudoConfig { key: Some(root_key) },
"testPallet": cumulus_test_runtime::TestPalletConfig { self_para_id, ..Default::default() }
})
}