-
Notifications
You must be signed in to change notification settings - Fork 699
/
mod.rs
379 lines (336 loc) · 12.9 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// 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 <http://www.gnu.org/licenses/>.
use cumulus_primitives_core::ParaId;
use parachains_common::{AccountId, Signature};
use polkadot_parachain_lib::{
chain_spec::{GenericChainSpec, LoadSpec},
runtime::{
AuraConsensusId, BlockNumber, Consensus, Runtime, RuntimeResolver as RuntimeResolverT,
},
};
use sc_chain_spec::ChainSpec;
use sp_core::{Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};
pub mod asset_hubs;
pub mod bridge_hubs;
pub mod collectives;
pub mod coretime;
pub mod glutton;
pub mod penpal;
pub mod people;
pub mod rococo_parachain;
pub mod seedling;
pub mod shell;
/// The default XCM version to set in genesis config.
const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}
type AccountPublic = <Signature as Verify>::Signer;
/// Helper function to generate an account ID from seed
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}
/// Generate collator keys from seed.
///
/// This function's return type must always match the session keys of the chain in tuple format.
pub fn get_collator_keys_from_seed<AuraId: Public>(seed: &str) -> <AuraId::Pair as Pair>::Public {
get_from_seed::<AuraId>(seed)
}
/// Extracts the normalized chain id and parachain id from the input chain id.
/// (H/T to Phala for the idea)
/// E.g. "penpal-kusama-2004" yields ("penpal-kusama", Some(2004))
fn extract_parachain_id<'a>(
id: &'a str,
para_prefixes: &[&str],
) -> (&'a str, &'a str, Option<ParaId>) {
for para_prefix in para_prefixes {
if let Some(suffix) = id.strip_prefix(para_prefix) {
let para_id: u32 = suffix.parse().expect("Invalid parachain-id suffix");
return (&id[..para_prefix.len() - 1], id, Some(para_id.into()));
}
}
(id, id, None)
}
#[derive(Debug)]
pub(crate) struct ChainSpecLoader;
impl LoadSpec for ChainSpecLoader {
fn load_spec(&self, id: &str) -> Result<Box<dyn ChainSpec>, String> {
Ok(match id {
// - Default-like
"staging" => Box::new(rococo_parachain::staging_rococo_parachain_local_config()),
"tick" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/tick.json")[..],
)?),
"trick" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/trick.json")[..],
)?),
"track" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/track.json")[..],
)?),
// -- Starters
"shell" => Box::new(shell::get_shell_chain_spec()),
"seedling" => Box::new(seedling::get_seedling_chain_spec()),
// -- Asset Hub Polkadot
"asset-hub-polkadot" | "statemint" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/asset-hub-polkadot.json")[..],
)?),
// -- Asset Hub Kusama
"asset-hub-kusama" | "statemine" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/asset-hub-kusama.json")[..],
)?),
// -- Asset Hub Rococo
"asset-hub-rococo-dev" => Box::new(asset_hubs::asset_hub_rococo_development_config()),
"asset-hub-rococo-local" => Box::new(asset_hubs::asset_hub_rococo_local_config()),
// the chain spec as used for generating the upgrade genesis values
"asset-hub-rococo-genesis" => Box::new(asset_hubs::asset_hub_rococo_genesis_config()),
"asset-hub-rococo" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/asset-hub-rococo.json")[..],
)?),
// -- Asset Hub Westend
"asset-hub-westend-dev" | "westmint-dev" =>
Box::new(asset_hubs::asset_hub_westend_development_config()),
"asset-hub-westend-local" | "westmint-local" =>
Box::new(asset_hubs::asset_hub_westend_local_config()),
// the chain spec as used for generating the upgrade genesis values
"asset-hub-westend-genesis" | "westmint-genesis" =>
Box::new(asset_hubs::asset_hub_westend_config()),
// the shell-based chain spec as used for syncing
"asset-hub-westend" | "westmint" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/asset-hub-westend.json")[..],
)?),
// -- Polkadot Collectives
"collectives-polkadot" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/collectives-polkadot.json")[..],
)?),
// -- Westend Collectives
"collectives-westend-dev" =>
Box::new(collectives::collectives_westend_development_config()),
"collectives-westend-local" =>
Box::new(collectives::collectives_westend_local_config()),
"collectives-westend" => Box::new(GenericChainSpec::from_json_bytes(
&include_bytes!("../../chain-specs/collectives-westend.json")[..],
)?),
// -- BridgeHub
bridge_like_id
if bridge_like_id.starts_with(bridge_hubs::BridgeHubRuntimeType::ID_PREFIX) =>
bridge_like_id
.parse::<bridge_hubs::BridgeHubRuntimeType>()
.expect("invalid value")
.load_config()?,
// -- Coretime
coretime_like_id
if coretime_like_id.starts_with(coretime::CoretimeRuntimeType::ID_PREFIX) =>
coretime_like_id
.parse::<coretime::CoretimeRuntimeType>()
.expect("invalid value")
.load_config()?,
// -- Penpal
id if id.starts_with("penpal-rococo") => {
let (_, _, para_id) = extract_parachain_id(&id, &["penpal-rococo-"]);
Box::new(penpal::get_penpal_chain_spec(
para_id.expect("Must specify parachain id"),
"rococo-local",
))
},
id if id.starts_with("penpal-westend") => {
let (_, _, para_id) = extract_parachain_id(&id, &["penpal-westend-"]);
Box::new(penpal::get_penpal_chain_spec(
para_id.expect("Must specify parachain id"),
"westend-local",
))
},
// -- Glutton Westend
id if id.starts_with("glutton-westend-dev") => {
let (_, _, para_id) = extract_parachain_id(&id, &["glutton-westend-dev-"]);
Box::new(glutton::glutton_westend_development_config(
para_id.expect("Must specify parachain id"),
))
},
id if id.starts_with("glutton-westend-local") => {
let (_, _, para_id) = extract_parachain_id(&id, &["glutton-westend-local-"]);
Box::new(glutton::glutton_westend_local_config(
para_id.expect("Must specify parachain id"),
))
},
// the chain spec as used for generating the upgrade genesis values
id if id.starts_with("glutton-westend-genesis") => {
let (_, _, para_id) = extract_parachain_id(&id, &["glutton-westend-genesis-"]);
Box::new(glutton::glutton_westend_config(
para_id.expect("Must specify parachain id"),
))
},
// -- People
people_like_id if people_like_id.starts_with(people::PeopleRuntimeType::ID_PREFIX) =>
people_like_id
.parse::<people::PeopleRuntimeType>()
.expect("invalid value")
.load_config()?,
// -- Fallback (generic chainspec)
"" => {
log::warn!("No ChainSpec.id specified, so using default one, based on rococo-parachain runtime");
Box::new(rococo_parachain::rococo_parachain_local_config())
},
// -- Loading a specific spec from disk
path => Box::new(GenericChainSpec::from_json_file(path.into())?),
})
}
}
/// Helper enum that is used for better distinction of different parachain/runtime configuration
/// (it is based/calculated on ChainSpec's ID attribute)
#[derive(Debug, PartialEq)]
enum LegacyRuntime {
Omni,
Shell,
Seedling,
AssetHubPolkadot,
AssetHub,
Penpal,
Collectives,
Glutton,
BridgeHub(bridge_hubs::BridgeHubRuntimeType),
Coretime(coretime::CoretimeRuntimeType),
People(people::PeopleRuntimeType),
}
impl LegacyRuntime {
fn from_id(id: &str) -> LegacyRuntime {
let id = id.replace('_', "-");
if id.starts_with("shell") {
LegacyRuntime::Shell
} else if id.starts_with("seedling") {
LegacyRuntime::Seedling
} else if id.starts_with("asset-hub-polkadot") | id.starts_with("statemint") {
LegacyRuntime::AssetHubPolkadot
} else if id.starts_with("asset-hub-kusama") |
id.starts_with("statemine") |
id.starts_with("asset-hub-rococo") |
id.starts_with("rockmine") |
id.starts_with("asset-hub-westend") |
id.starts_with("westmint")
{
LegacyRuntime::AssetHub
} else if id.starts_with("penpal") {
LegacyRuntime::Penpal
} else if id.starts_with("collectives-polkadot") || id.starts_with("collectives-westend") {
LegacyRuntime::Collectives
} else if id.starts_with(bridge_hubs::BridgeHubRuntimeType::ID_PREFIX) {
LegacyRuntime::BridgeHub(
id.parse::<bridge_hubs::BridgeHubRuntimeType>().expect("Invalid value"),
)
} else if id.starts_with(coretime::CoretimeRuntimeType::ID_PREFIX) {
LegacyRuntime::Coretime(
id.parse::<coretime::CoretimeRuntimeType>().expect("Invalid value"),
)
} else if id.starts_with("glutton") {
LegacyRuntime::Glutton
} else if id.starts_with(people::PeopleRuntimeType::ID_PREFIX) {
LegacyRuntime::People(id.parse::<people::PeopleRuntimeType>().expect("Invalid value"))
} else {
log::warn!(
"No specific runtime was recognized for ChainSpec's id: '{}', \
so Runtime::Omni(Consensus::Aura) will be used",
id
);
LegacyRuntime::Omni
}
}
}
#[derive(Debug)]
pub(crate) struct RuntimeResolver;
impl RuntimeResolverT for RuntimeResolver {
fn runtime(&self, chain_spec: &dyn ChainSpec) -> sc_cli::Result<Runtime> {
let legacy_runtime = LegacyRuntime::from_id(chain_spec.id());
Ok(match legacy_runtime {
LegacyRuntime::AssetHubPolkadot =>
Runtime::Omni(BlockNumber::U32, Consensus::Aura(AuraConsensusId::Ed25519)),
LegacyRuntime::AssetHub |
LegacyRuntime::BridgeHub(_) |
LegacyRuntime::Collectives |
LegacyRuntime::Coretime(_) |
LegacyRuntime::People(_) |
LegacyRuntime::Glutton |
LegacyRuntime::Penpal |
LegacyRuntime::Omni =>
Runtime::Omni(BlockNumber::U32, Consensus::Aura(AuraConsensusId::Sr25519)),
LegacyRuntime::Shell | LegacyRuntime::Seedling => Runtime::Shell,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup, ChainType, Extension};
use serde::{Deserialize, Serialize};
use sp_core::sr25519;
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension, Default,
)]
#[serde(deny_unknown_fields)]
pub struct Extensions1 {
pub attribute1: String,
pub attribute2: u32,
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension, Default,
)]
#[serde(deny_unknown_fields)]
pub struct Extensions2 {
pub attribute_x: String,
pub attribute_y: String,
pub attribute_z: u32,
}
pub type DummyChainSpec<E> = sc_service::GenericChainSpec<E>;
pub fn create_default_with_extensions<E: Extension>(
id: &str,
extension: E,
) -> DummyChainSpec<E> {
DummyChainSpec::builder(
rococo_parachain_runtime::WASM_BINARY
.expect("WASM binary was not built, please build it!"),
extension,
)
.with_name("Dummy local testnet")
.with_id(id)
.with_chain_type(ChainType::Local)
.with_genesis_config_patch(crate::chain_spec::rococo_parachain::testnet_genesis(
get_account_id_from_seed::<sr25519::Public>("Alice"),
vec![
get_from_seed::<rococo_parachain_runtime::AuraId>("Alice"),
get_from_seed::<rococo_parachain_runtime::AuraId>("Bob"),
],
vec![get_account_id_from_seed::<sr25519::Public>("Alice")],
1000.into(),
))
.build()
}
#[test]
fn test_legacy_runtime_for_different_chain_specs() {
let chain_spec = create_default_with_extensions("shell-1", Extensions1::default());
assert_eq!(LegacyRuntime::Shell, LegacyRuntime::from_id(chain_spec.id()));
let chain_spec = create_default_with_extensions("shell-2", Extensions2::default());
assert_eq!(LegacyRuntime::Shell, LegacyRuntime::from_id(chain_spec.id()));
let chain_spec = create_default_with_extensions("seedling", Extensions2::default());
assert_eq!(LegacyRuntime::Seedling, LegacyRuntime::from_id(chain_spec.id()));
let chain_spec =
create_default_with_extensions("penpal-rococo-1000", Extensions2::default());
assert_eq!(LegacyRuntime::Penpal, LegacyRuntime::from_id(chain_spec.id()));
let chain_spec = crate::chain_spec::rococo_parachain::rococo_parachain_local_config();
assert_eq!(LegacyRuntime::Omni, LegacyRuntime::from_id(chain_spec.id()));
}
}