1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16
use {
17
    cumulus_primitives_core::relay_chain::{
18
        AccountId, AssignmentId, AuthorityDiscoveryId, ValidatorId,
19
    },
20
    emulated_integration_tests_common::build_genesis_storage,
21
    polkadot_service::chain_spec::get_authority_keys_from_seed_no_beefy,
22
    sc_consensus_grandpa::AuthorityId as GrandpaId,
23
    sp_consensus_babe::AuthorityId as BabeId,
24
    sp_consensus_beefy::ecdsa_crypto::AuthorityId as BeefyId,
25
    sp_core::{sr25519, storage::Storage, Pair, Public},
26
    sp_runtime::{
27
        traits::{IdentifyAccount, Verify},
28
        MultiSignature,
29
    },
30
};
31

            
32
type AccountPublic = <MultiSignature as Verify>::Signer;
33

            
34
/// Helper function to generate a crypto pair from seed
35
3256
fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
36
3256
    TPublic::Pair::from_string(&format!("//{}", seed), None)
37
3256
        .expect("static values are valid; qed")
38
3256
        .public()
39
3256
}
40

            
41
/// Helper function to generate an account ID from seed.
42
3168
fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
43
3168
where
44
3168
    AccountPublic: From<<TPublic::Pair as Pair>::Public>,
45
3168
{
46
3168
    AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
47
3168
}
48

            
49
pub mod accounts {
50
    use super::*;
51
    pub const ALICE: &str = "Alice";
52
    pub const BOB: &str = "Bob";
53
    pub const CHARLIE: &str = "Charlie";
54
    pub const DAVE: &str = "Dave";
55
    pub const EVE: &str = "Eve";
56
    pub const FERDIE: &str = "Ferdei";
57
    pub const ALICE_STASH: &str = "Alice//stash";
58
    pub const BOB_STASH: &str = "Bob//stash";
59
    pub const CHARLIE_STASH: &str = "Charlie//stash";
60
    pub const DAVE_STASH: &str = "Dave//stash";
61
    pub const EVE_STASH: &str = "Eve//stash";
62
    pub const FERDIE_STASH: &str = "Ferdie//stash";
63
    pub const RANDOM: &str = "Random//stash";
64

            
65
264
    pub fn init_balances() -> Vec<AccountId> {
66
264
        vec![
67
264
            get_account_id_from_seed::<sr25519::Public>(ALICE),
68
264
            get_account_id_from_seed::<sr25519::Public>(BOB),
69
264
            get_account_id_from_seed::<sr25519::Public>(CHARLIE),
70
264
            get_account_id_from_seed::<sr25519::Public>(DAVE),
71
264
            get_account_id_from_seed::<sr25519::Public>(EVE),
72
264
            get_account_id_from_seed::<sr25519::Public>(FERDIE),
73
264
            get_account_id_from_seed::<sr25519::Public>(ALICE_STASH),
74
264
            get_account_id_from_seed::<sr25519::Public>(BOB_STASH),
75
264
            get_account_id_from_seed::<sr25519::Public>(CHARLIE_STASH),
76
264
            get_account_id_from_seed::<sr25519::Public>(DAVE_STASH),
77
264
            get_account_id_from_seed::<sr25519::Public>(EVE_STASH),
78
264
            get_account_id_from_seed::<sr25519::Public>(FERDIE_STASH),
79
264
        ]
80
264
    }
81
}
82

            
83
pub mod validators {
84
    use super::*;
85

            
86
280
    pub fn initial_authorities() -> Vec<(
87
280
        AccountId,
88
280
        AccountId,
89
280
        BabeId,
90
280
        GrandpaId,
91
280
        ValidatorId,
92
280
        AssignmentId,
93
280
        AuthorityDiscoveryId,
94
280
    )> {
95
280
        vec![get_authority_keys_from_seed_no_beefy("Alice")]
96
280
    }
97
}
98

            
99
// Westend
100
pub mod westend {
101
    use {
102
        super::*, cumulus_primitives_core::relay_chain::BlockNumber,
103
        polkadot_runtime_parachains::configuration::HostConfiguration, sp_runtime::Perbill,
104
        westend_runtime_constants::currency::UNITS as WND,
105
    };
106
    const ENDOWMENT: u128 = 1_000_000 * WND;
107
    const STASH: u128 = 100 * WND;
108

            
109
64
    pub fn get_host_config() -> HostConfiguration<BlockNumber> {
110
64
        HostConfiguration {
111
64
            max_upward_queue_count: 10,
112
64
            max_upward_queue_size: 51200,
113
64
            max_upward_message_size: 51200,
114
64
            max_upward_message_num_per_candidate: 10,
115
64
            max_downward_message_size: 51200,
116
64
            ..Default::default()
117
64
        }
118
64
    }
119

            
120
64
    fn session_keys(
121
64
        babe: BabeId,
122
64
        grandpa: GrandpaId,
123
64
        para_validator: ValidatorId,
124
64
        para_assignment: AssignmentId,
125
64
        authority_discovery: AuthorityDiscoveryId,
126
64
        beefy: BeefyId,
127
64
    ) -> westend_runtime::SessionKeys {
128
64
        westend_runtime::SessionKeys {
129
64
            babe,
130
64
            grandpa,
131
64
            para_validator,
132
64
            para_assignment,
133
64
            authority_discovery,
134
64
            beefy,
135
64
        }
136
64
    }
137

            
138
64
    pub fn genesis() -> Storage {
139
64
        let genesis_config = westend_runtime::RuntimeGenesisConfig {
140
64
            balances: westend_runtime::BalancesConfig {
141
64
                balances: accounts::init_balances()
142
64
                    .iter()
143
64
                    .cloned()
144
768
                    .map(|k| (k, ENDOWMENT))
145
64
                    .collect(),
146
64
            },
147
64
            session: westend_runtime::SessionConfig {
148
64
                keys: validators::initial_authorities()
149
64
                    .iter()
150
64
                    .map(|x| {
151
64
                        (
152
64
                            x.0.clone(),
153
64
                            x.0.clone(),
154
64
                            westend::session_keys(
155
64
                                x.2.clone(),
156
64
                                x.3.clone(),
157
64
                                x.4.clone(),
158
64
                                x.5.clone(),
159
64
                                x.6.clone(),
160
64
                                get_from_seed::<BeefyId>("Alice"),
161
64
                            ),
162
64
                        )
163
64
                    })
164
64
                    .collect::<Vec<_>>(),
165
64
                ..Default::default()
166
64
            },
167
64
            staking: westend_runtime::StakingConfig {
168
64
                validator_count: validators::initial_authorities().len() as u32,
169
64
                minimum_validator_count: 1,
170
64
                stakers: validators::initial_authorities()
171
64
                    .iter()
172
64
                    .map(|x| {
173
64
                        (
174
64
                            x.0.clone(),
175
64
                            x.1.clone(),
176
64
                            STASH,
177
64
                            westend_runtime::StakerStatus::Validator,
178
64
                        )
179
64
                    })
180
64
                    .collect(),
181
64
                invulnerables: validators::initial_authorities()
182
64
                    .iter()
183
64
                    .map(|x| x.0.clone())
184
64
                    .collect(),
185
64
                force_era: pallet_staking::Forcing::ForceNone,
186
64
                slash_reward_fraction: Perbill::from_percent(10),
187
64
                ..Default::default()
188
64
            },
189
64
            babe: westend_runtime::BabeConfig {
190
64
                authorities: Default::default(),
191
64
                epoch_config: westend_runtime::BABE_GENESIS_EPOCH_CONFIG,
192
64
                ..Default::default()
193
64
            },
194
64
            configuration: westend_runtime::ConfigurationConfig {
195
64
                config: get_host_config(),
196
64
            },
197
64
            ..Default::default()
198
64
        };
199
64
        build_genesis_storage(&genesis_config, westend_runtime::WASM_BINARY.unwrap())
200
64
    }
201
}
202

            
203
// Rococo
204
pub mod rococo {
205
    use {
206
        super::*,
207
        cumulus_primitives_core::relay_chain::BlockNumber,
208
        polkadot_parachain_primitives::primitives::ValidationCode,
209
        polkadot_runtime_parachains::{
210
            configuration::HostConfiguration,
211
            paras::{ParaGenesisArgs, ParaKind},
212
        },
213
        rococo_runtime_constants::currency::UNITS as ROC,
214
    };
215
    const ENDOWMENT: u128 = 1_000_000 * ROC;
216

            
217
24
    pub fn get_host_config() -> HostConfiguration<BlockNumber> {
218
24
        HostConfiguration {
219
24
            max_upward_queue_count: 10,
220
24
            max_upward_queue_size: 51200,
221
24
            max_upward_message_size: 51200,
222
24
            max_upward_message_num_per_candidate: 10,
223
24
            max_downward_message_size: 51200,
224
24
            ..Default::default()
225
24
        }
226
24
    }
227

            
228
24
    fn session_keys(
229
24
        babe: BabeId,
230
24
        grandpa: GrandpaId,
231
24
        para_validator: ValidatorId,
232
24
        para_assignment: AssignmentId,
233
24
        authority_discovery: AuthorityDiscoveryId,
234
24
        beefy: BeefyId,
235
24
    ) -> rococo_runtime::SessionKeys {
236
24
        rococo_runtime::SessionKeys {
237
24
            babe,
238
24
            grandpa,
239
24
            para_validator,
240
24
            para_assignment,
241
24
            authority_discovery,
242
24
            beefy,
243
24
        }
244
24
    }
245

            
246
24
    pub fn genesis() -> Storage {
247
24
        let genesis_config = rococo_runtime::RuntimeGenesisConfig {
248
24
            balances: rococo_runtime::BalancesConfig {
249
24
                balances: accounts::init_balances()
250
24
                    .iter()
251
24
                    .cloned()
252
288
                    .map(|k| (k, crate::tests::common::xcm::constants::rococo::ENDOWMENT))
253
24
                    .collect(),
254
24
            },
255
24
            session: rococo_runtime::SessionConfig {
256
24
                keys: validators::initial_authorities()
257
24
                    .iter()
258
24
                    .map(|x| {
259
24
                        (
260
24
                            x.0.clone(),
261
24
                            x.0.clone(),
262
24
                            crate::tests::common::xcm::constants::rococo::session_keys(
263
24
                                x.2.clone(),
264
24
                                x.3.clone(),
265
24
                                x.4.clone(),
266
24
                                x.5.clone(),
267
24
                                x.6.clone(),
268
24
                                get_from_seed::<BeefyId>("Alice"),
269
24
                            ),
270
24
                        )
271
24
                    })
272
24
                    .collect::<Vec<_>>(),
273
24
                ..Default::default()
274
24
            },
275
24
            babe: rococo_runtime::BabeConfig {
276
24
                authorities: Default::default(),
277
24
                epoch_config: rococo_runtime::BABE_GENESIS_EPOCH_CONFIG,
278
24
                ..Default::default()
279
24
            },
280
24
            configuration: rococo_runtime::ConfigurationConfig {
281
24
                config: crate::tests::common::xcm::constants::rococo::get_host_config(),
282
24
            },
283
24
            paras: rococo_runtime::ParasConfig {
284
24
                _config: Default::default(),
285
24
                paras: vec![(
286
24
                    3333.into(),
287
24
                    ParaGenesisArgs {
288
24
                        genesis_head: Default::default(),
289
24
                        validation_code: ValidationCode(vec![1, 1, 2, 3, 4]),
290
24
                        para_kind: ParaKind::Parathread,
291
24
                    },
292
24
                )],
293
24
            },
294
24
            ..Default::default()
295
24
        };
296
24
        build_genesis_storage(&genesis_config, rococo_runtime::WASM_BINARY.unwrap())
297
24
    }
298
}
299

            
300
// Frontier template
301
pub mod frontier_template {
302
    use {
303
        container_chain_template_frontier_runtime::AccountId,
304
        emulated_integration_tests_common::build_genesis_storage, hex_literal::hex,
305
    };
306
    pub const PARA_ID: u32 = 2001;
307
    pub const ORCHESTRATOR: u32 = 2000;
308

            
309
88
    pub fn genesis() -> sp_core::storage::Storage {
310
88
        let genesis_config = container_chain_template_frontier_runtime::RuntimeGenesisConfig {
311
88
            system: Default::default(),
312
88
            balances: container_chain_template_frontier_runtime::BalancesConfig {
313
88
                balances: pre_funded_accounts()
314
88
                    .iter()
315
88
                    .cloned()
316
352
                    .map(|k| (k, 1 << 80))
317
88
                    .collect(),
318
88
            },
319
88
            parachain_info: container_chain_template_frontier_runtime::ParachainInfoConfig {
320
88
                parachain_id: PARA_ID.into(),
321
88
                ..Default::default()
322
88
            },
323
88
            // EVM compatibility
324
88
            // We should change this to something different than Moonbeam
325
88
            // For now moonwall is very tailored for moonbeam so we need it for tests
326
88
            evm_chain_id: container_chain_template_frontier_runtime::EVMChainIdConfig {
327
88
                chain_id: 1281,
328
88
                ..Default::default()
329
88
            },
330
88
            sudo: container_chain_template_frontier_runtime::SudoConfig {
331
88
                key: Some(pre_funded_accounts()[0]),
332
88
            },
333
88
            authorities_noting:
334
88
                container_chain_template_frontier_runtime::AuthoritiesNotingConfig {
335
88
                    orchestrator_para_id: ORCHESTRATOR.into(),
336
88
                    ..Default::default()
337
88
                },
338
88
            ..Default::default()
339
88
        };
340
88

            
341
88
        build_genesis_storage(
342
88
            &genesis_config,
343
88
            container_chain_template_frontier_runtime::WASM_BINARY.unwrap(),
344
88
        )
345
88
    }
346
    /// Get pre-funded accounts
347
191
    pub fn pre_funded_accounts() -> Vec<AccountId> {
348
191
        // These addresses are derived from Substrate's canonical mnemonic:
349
191
        // bottom drive obey lake curtain smoke basket hold race lonely fit walk
350
191
        vec![
351
191
            AccountId::from(hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac")), // Alith
352
191
            AccountId::from(hex!("3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0")), // Baltathar
353
191
            AccountId::from(hex!("798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc")), // Charleth
354
191
            AccountId::from(hex!("773539d4Ac0e786233D90A233654ccEE26a613D9")), // Dorothy
355
191
        ]
356
191
    }
357
}
358

            
359
// Simple template
360
pub mod simple_template {
361
    use {super::*, container_chain_template_simple_runtime::UNIT as DEV};
362
    pub const PARA_ID: u32 = 2002;
363
    pub const ORCHESTRATOR: u32 = 2000;
364
    const ENDOWMENT: u128 = 1_000_000 * DEV;
365

            
366
88
    pub fn genesis() -> sp_core::storage::Storage {
367
88
        let genesis_config = container_chain_template_simple_runtime::RuntimeGenesisConfig {
368
88
            balances: container_chain_template_simple_runtime::BalancesConfig {
369
88
                balances: accounts::init_balances()
370
88
                    .iter()
371
88
                    .cloned()
372
1056
                    .map(|k| (k, ENDOWMENT))
373
88
                    .collect(),
374
88
            },
375
88
            parachain_info: container_chain_template_simple_runtime::ParachainInfoConfig {
376
88
                parachain_id: PARA_ID.into(),
377
88
                ..Default::default()
378
88
            },
379
88
            sudo: container_chain_template_simple_runtime::SudoConfig {
380
88
                key: Some(accounts::init_balances()[0].clone()),
381
88
            },
382
88
            authorities_noting: container_chain_template_simple_runtime::AuthoritiesNotingConfig {
383
88
                orchestrator_para_id: ORCHESTRATOR.into(),
384
88
                ..Default::default()
385
88
            },
386
88
            ..Default::default()
387
88
        };
388
88
        build_genesis_storage(
389
88
            &genesis_config,
390
88
            container_chain_template_simple_runtime::WASM_BINARY.unwrap(),
391
88
        )
392
88
    }
393
}