chain_spec.rs 6.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.

17
18
//! Chain specifications for the test runtime.

19
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
20
21
22
use babe_primitives::AuthorityId as BabeId;
use grandpa::AuthorityId as GrandpaId;
use pallet_staking::Forcing;
23
use polkadot_primitives::v1::{ValidatorId, AccountId, AssignmentId, MAX_CODE_SIZE, MAX_POV_SIZE};
24
use polkadot_service::chain_spec::{get_account_id_from_seed, get_from_seed, Extensions};
25
use polkadot_test_runtime::{constants::currency::DOTS, BABE_GENESIS_EPOCH_CONFIG};
26
use sc_chain_spec::{ChainSpec, ChainType};
27
use sp_core::sr25519;
28
29
30
31
use sp_runtime::Perbill;

const DEFAULT_PROTOCOL_ID: &str = "dot";

Bernhard Schuster's avatar
Bernhard Schuster committed
32
/// The `ChainSpec` parameterized for polkadot test runtime.
33
34
35
pub type PolkadotChainSpec =
	service::GenericChainSpec<polkadot_test_runtime::GenesisConfig, Extensions>;

36
/// Local testnet config (multivalidator Alice + Bob)
37
38
39
40
41
pub fn polkadot_local_testnet_config() -> PolkadotChainSpec {
	PolkadotChainSpec::from_genesis(
		"Local Testnet",
		"local_testnet",
		ChainType::Local,
42
		|| polkadot_local_testnet_genesis(),
43
44
45
46
47
48
49
50
		vec![],
		None,
		Some(DEFAULT_PROTOCOL_ID),
		None,
		Default::default(),
	)
}

51
52
/// Local testnet genesis config (multivalidator Alice + Bob)
pub fn polkadot_local_testnet_genesis() -> polkadot_test_runtime::GenesisConfig {
53
54
55
56
57
58
59
60
61
62
63
64
65
	polkadot_testnet_genesis(
		vec![
			get_authority_keys_from_seed("Alice"),
			get_authority_keys_from_seed("Bob"),
		],
		get_account_id_from_seed::<sr25519::Public>("Alice"),
		None,
	)
}

/// Helper function to generate stash, controller and session key from seed
fn get_authority_keys_from_seed(
	seed: &str,
66
) -> (AccountId, AccountId, BabeId, GrandpaId, ValidatorId, AssignmentId, AuthorityDiscoveryId) {
67
68
69
70
71
72
	(
		get_account_id_from_seed::<sr25519::Public>(&format!("{}//stash", seed)),
		get_account_id_from_seed::<sr25519::Public>(seed),
		get_from_seed::<BabeId>(seed),
		get_from_seed::<GrandpaId>(seed),
		get_from_seed::<ValidatorId>(seed),
73
		get_from_seed::<AssignmentId>(seed),
74
		get_from_seed::<AuthorityDiscoveryId>(seed),
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	)
}

fn testnet_accounts() -> Vec<AccountId> {
	vec![
		get_account_id_from_seed::<sr25519::Public>("Alice"),
		get_account_id_from_seed::<sr25519::Public>("Bob"),
		get_account_id_from_seed::<sr25519::Public>("Charlie"),
		get_account_id_from_seed::<sr25519::Public>("Dave"),
		get_account_id_from_seed::<sr25519::Public>("Eve"),
		get_account_id_from_seed::<sr25519::Public>("Ferdie"),
		get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
		get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
		get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
		get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
		get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
		get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
	]
}

/// Helper function to create polkadot GenesisConfig for testing
fn polkadot_testnet_genesis(
97
98
99
100
101
102
103
104
105
	initial_authorities: Vec<(
		AccountId,
		AccountId,
		BabeId,
		GrandpaId,
		ValidatorId,
		AssignmentId,
		AuthorityDiscoveryId,
	)>,
106
107
108
	root_key: AccountId,
	endowed_accounts: Option<Vec<AccountId>>,
) -> polkadot_test_runtime::GenesisConfig {
109
	use polkadot_test_runtime as runtime;
110
111
112
113
114
115

	let endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(testnet_accounts);

	const ENDOWMENT: u128 = 1_000_000 * DOTS;
	const STASH: u128 = 100 * DOTS;

116
	runtime::GenesisConfig {
Keith Yeung's avatar
Keith Yeung committed
117
		system: runtime::SystemConfig {
118
			code: runtime::WASM_BINARY.expect("Wasm binary must be built for testing").to_vec(),
119
			..Default::default()
120
		},
Keith Yeung's avatar
Keith Yeung committed
121
122
		indices: runtime::IndicesConfig { indices: vec![] },
		balances: runtime::BalancesConfig {
123
124
125
126
			balances: endowed_accounts
				.iter()
				.map(|k| (k.clone(), ENDOWMENT))
				.collect(),
127
		},
Keith Yeung's avatar
Keith Yeung committed
128
		session: runtime::SessionConfig {
129
130
131
132
133
134
			keys: initial_authorities
				.iter()
				.map(|x| {
					(
						x.0.clone(),
						x.0.clone(),
135
						runtime::SessionKeys {
136
137
							babe: x.2.clone(),
							grandpa: x.3.clone(),
138
139
140
							para_validator: x.4.clone(),
							para_assignment: x.5.clone(),
							authority_discovery: x.6.clone(),
141
142
143
144
						},
					)
				})
				.collect::<Vec<_>>(),
145
		},
Keith Yeung's avatar
Keith Yeung committed
146
		staking: runtime::StakingConfig {
147
148
149
150
151
152
153
154
155
			minimum_validator_count: 1,
			validator_count: 2,
			stakers: initial_authorities
				.iter()
				.map(|x| {
					(
						x.0.clone(),
						x.1.clone(),
						STASH,
156
						runtime::StakerStatus::Validator,
157
158
159
160
161
162
163
					)
				})
				.collect(),
			invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(),
			force_era: Forcing::NotForcing,
			slash_reward_fraction: Perbill::from_percent(10),
			..Default::default()
164
		},
Keith Yeung's avatar
Keith Yeung committed
165
		babe: runtime::BabeConfig {
166
167
168
			authorities: vec![],
			epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
		},
Keith Yeung's avatar
Keith Yeung committed
169
170
		grandpa: Default::default(),
		authority_discovery: runtime::AuthorityDiscoveryConfig { keys: vec![] },
171
		claims: runtime::ClaimsConfig {
172
173
			claims: vec![],
			vesting: vec![],
174
		},
Keith Yeung's avatar
Keith Yeung committed
175
176
		vesting: runtime::VestingConfig { vesting: vec![] },
		sudo: runtime::SudoConfig { key: root_key },
177
		parachains_configuration: runtime::ParachainsConfigurationConfig {
178
179
180
			config: polkadot_runtime_parachains::configuration::HostConfiguration {
				validation_upgrade_frequency: 10u32,
				validation_upgrade_delay: 5,
181
				code_retention_period: 1200,
182
183
				max_code_size: MAX_CODE_SIZE,
				max_pov_size: MAX_POV_SIZE,
184
				max_head_data_size: 32 * 1024,
185
186
187
188
				group_rotation_frequency: 20,
				chain_availability_period: 4,
				thread_availability_period: 4,
				no_show_slots: 10,
189
190
				..Default::default()
			},
191
		},
192
193
194
195
196
197
198
199
200
201
202
203
204
205
	}
}

/// 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")
	}
}