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

            
17
use {
18
    crate::{AuthorInherent, BlockProductionCost, CollatorAssignmentCost, RuntimeOrigin},
19
    cumulus_primitives_core::{ParaId, PersistedValidationData},
20
    cumulus_primitives_parachain_inherent::ParachainInherentData,
21
    dp_consensus::runtime_decl_for_tanssi_authority_assignment_api::TanssiAuthorityAssignmentApi,
22
    frame_support::{
23
        assert_ok,
24
        traits::{OnFinalize, OnInitialize},
25
    },
26
    nimbus_primitives::{NimbusId, NIMBUS_ENGINE_ID},
27
    pallet_collator_assignment_runtime_api::runtime_decl_for_collator_assignment_api::CollatorAssignmentApi,
28
    pallet_registrar_runtime_api::ContainerChainGenesisData,
29
    pallet_services_payment::{ProvideBlockProductionCost, ProvideCollatorAssignmentCost},
30
    parity_scale_codec::{Decode, Encode, MaxEncodedLen},
31
    polkadot_parachain_primitives::primitives::HeadData,
32
    sp_consensus_aura::AURA_ENGINE_ID,
33
    sp_consensus_slots::Slot,
34
    sp_core::{Get, Pair},
35
    sp_runtime::{traits::Dispatchable, BuildStorage, Digest, DigestItem},
36
    sp_std::collections::btree_map::BTreeMap,
37
    test_relay_sproof_builder::ParaHeaderSproofBuilder,
38
};
39

            
40
pub use crate::{
41
    AccountId, AssetRate, AuthorNoting, AuthorityAssignment, AuthorityMapping, Balance, Balances,
42
    CollatorAssignment, Configuration, DataPreservers, ForeignAssets, ForeignAssetsCreator,
43
    InactivityTracking, InflationRewards, Initializer, Invulnerables, MinimumSelfDelegation,
44
    ParachainInfo, PooledStaking, Proxy, ProxyType, Registrar, RewardsPortion, Runtime,
45
    RuntimeCall, ServicesPayment, Session, System, TransactionPayment,
46
};
47

            
48
pub const UNIT: Balance = 1_000_000_000_000;
49

            
50
119
pub fn session_to_block(n: u32) -> u32 {
51
119
    let block_number = crate::Period::get() * n;
52
119

            
53
119
    // Add 1 because the block that emits the NewSession event cannot contain any extrinsics,
54
119
    // so this is the first block of the new session that can actually be used
55
119
    block_number + 1
56
119
}
57

            
58
#[derive(Debug, Clone, Eq, PartialEq)]
59
pub struct RunSummary {
60
    pub author_id: AccountId,
61
    pub inflation: Balance,
62
}
63

            
64
116
pub fn run_to_session(n: u32) {
65
116
    run_to_block(session_to_block(n));
66
116
}
67

            
68
/// Utility function that advances the chain to the desired block number.
69
///
70
/// After this function returns, the current block number will be `n`, and the block will be "open",
71
/// meaning that on_initialize has been executed, but on_finalize has not. To execute on_finalize as
72
/// well, for example to test a runtime api, manually call `end_block` after this, run the test, and
73
/// call `start_block` to ensure that this function keeps working as expected.
74
/// Extrinsics should always be executed before on_finalize.
75
191
pub fn run_to_block(n: u32) -> BTreeMap<u32, RunSummary> {
76
191
    let current_block_number = System::block_number();
77
191
    assert!(
78
191
        current_block_number < n,
79
        "run_to_block called with block {} when current block is {}",
80
        n,
81
        current_block_number
82
    );
83
191
    let mut summaries = BTreeMap::new();
84

            
85
2306
    while System::block_number() < n {
86
2115
        let summary = run_block();
87
2115
        let block_number = System::block_number();
88
2115
        summaries.insert(block_number, summary);
89
2115
    }
90

            
91
191
    summaries
92
191
}
93

            
94
2263
pub fn insert_authorities_and_slot_digests(slot: u64) {
95
2263
    let authorities =
96
2263
        Runtime::para_id_authorities(ParachainInfo::get()).expect("authorities should be set");
97
2263

            
98
2263
    let authority: NimbusId = authorities[slot as usize % authorities.len()].clone();
99
2263

            
100
2263
    let pre_digest = Digest {
101
2263
        logs: vec![
102
2263
            DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode()),
103
2263
            DigestItem::PreRuntime(NIMBUS_ENGINE_ID, authority.encode()),
104
2263
        ],
105
2263
    };
106
2263

            
107
2263
    System::reset_events();
108
2263
    System::initialize(
109
2263
        &(System::block_number() + 1),
110
2263
        &System::parent_hash(),
111
2263
        &pre_digest,
112
2263
    );
113
2263
}
114

            
115
// Used to create the next block inherent data
116
#[derive(Clone, Encode, Decode, Default, PartialEq, Debug, scale_info::TypeInfo, MaxEncodedLen)]
117
pub struct MockInherentData {
118
    pub random_seed: Option<[u8; 32]>,
119
}
120

            
121
2263
fn take_new_inherent_data() -> Option<MockInherentData> {
122
2263
    let data: Option<MockInherentData> =
123
2263
        frame_support::storage::unhashed::take(b"__mock_new_inherent_data");
124
2263

            
125
2263
    data
126
2263
}
127

            
128
2
fn set_new_inherent_data(data: MockInherentData) {
129
2
    frame_support::storage::unhashed::put(b"__mock_new_inherent_data", &Some(data));
130
2
}
131

            
132
#[derive(Clone, Encode, Decode, PartialEq, Debug, scale_info::TypeInfo, MaxEncodedLen)]
133
enum RunBlockState {
134
2154
    Start(u32),
135
2153
    End(u32),
136
}
137

            
138
impl RunBlockState {
139
4417
    fn assert_can_advance(&self, new_state: &RunBlockState) {
140
4417
        match self {
141
2154
            RunBlockState::Start(n) => {
142
2154
                assert_eq!(
143
2154
                    new_state,
144
2154
                    &RunBlockState::End(*n),
145
                    "expected a call to end_block({}), but user called {:?}",
146
                    *n,
147
                    new_state
148
                );
149
            }
150
2263
            RunBlockState::End(n) => {
151
2263
                assert_eq!(
152
2263
                    new_state,
153
2263
                    &RunBlockState::Start(*n + 1),
154
                    "expected a call to start_block({}), but user called {:?}",
155
                    *n + 1,
156
                    new_state
157
                )
158
            }
159
        }
160
4417
    }
161
}
162

            
163
4417
fn advance_block_state_machine(new_state: RunBlockState) {
164
4417
    if frame_support::storage::unhashed::exists(b"__mock_is_xcm_test") {
165
        // Disable this check in XCM tests, because the XCM emulator runs on_initialize and
166
        // on_finalize automatically
167
        return;
168
4417
    }
169
4417
    let old_state: RunBlockState =
170
4417
        frame_support::storage::unhashed::get(b"__mock_debug_block_state").unwrap_or(
171
4417
            // Initial state is expecting a call to start() with block number 1, so old state should be
172
4417
            // end of block 0
173
4417
            RunBlockState::End(0),
174
4417
        );
175
4417
    old_state.assert_can_advance(&new_state);
176
4417
    frame_support::storage::unhashed::put(b"__mock_debug_block_state", &new_state);
177
4417
}
178

            
179
2263
pub fn start_block() -> RunSummary {
180
2263
    let block_number = System::block_number();
181
2263
    advance_block_state_machine(RunBlockState::Start(block_number + 1));
182
2263
    let mut slot = current_slot() + 1;
183
2263
    if block_number == 0 {
184
110
        // Hack to avoid breaking all tests. When the current block is 1, the slot number should be
185
110
        // 1. But all of our tests assume it will be 0. So use slot number = block_number - 1.
186
110
        slot = 0;
187
2153
    }
188

            
189
2263
    let maybe_mock_inherent = take_new_inherent_data();
190

            
191
2263
    if let Some(mock_inherent_data) = maybe_mock_inherent {
192
2
        set_parachain_inherent_data(mock_inherent_data);
193
2261
    }
194

            
195
2263
    insert_authorities_and_slot_digests(slot);
196
2263

            
197
2263
    // Initialize the new block
198
2263
    CollatorAssignment::on_initialize(System::block_number());
199
2263
    Session::on_initialize(System::block_number());
200
2263
    Initializer::on_initialize(System::block_number());
201
2263
    AuthorInherent::on_initialize(System::block_number());
202
2263

            
203
2263
    // `Initializer::on_finalize` needs to run at least one to have
204
2263
    // author mapping setup.
205
2263
    let author_id = current_author();
206
2263

            
207
2263
    let current_issuance = Balances::total_issuance();
208
2263
    InflationRewards::on_initialize(System::block_number());
209
2263
    let new_issuance = Balances::total_issuance();
210
2263

            
211
2263
    frame_support::storage::unhashed::put(
212
2263
        &frame_support::storage::storage_prefix(b"AsyncBacking", b"SlotInfo"),
213
2263
        &(Slot::from(slot), 1),
214
2263
    );
215
2263

            
216
2263
    pallet_author_inherent::Pallet::<Runtime>::kick_off_authorship_validation(None.into())
217
2263
        .expect("author inherent to dispatch correctly");
218
2263

            
219
2263
    InactivityTracking::on_initialize(System::block_number());
220
2263

            
221
2263
    RunSummary {
222
2263
        author_id,
223
2263
        inflation: new_issuance - current_issuance,
224
2263
    }
225
2263
}
226

            
227
2154
pub fn end_block() {
228
2154
    let block_number = System::block_number();
229
2154
    advance_block_state_machine(RunBlockState::End(block_number));
230
2154
    // Finalize the block
231
2154
    CollatorAssignment::on_finalize(System::block_number());
232
2154
    Session::on_finalize(System::block_number());
233
2154
    Initializer::on_finalize(System::block_number());
234
2154
    AuthorInherent::on_finalize(System::block_number());
235
2154
    TransactionPayment::on_finalize(System::block_number());
236
2154
    InactivityTracking::on_finalize(System::block_number());
237
2154
}
238

            
239
2151
pub fn run_block() -> RunSummary {
240
2151
    end_block();
241
2151

            
242
2151
    start_block()
243
2151
}
244

            
245
/// Mock the inherent that sets validation data in ParachainSystem, which
246
/// contains the `relay_chain_block_number`, which is used in `collator-assignment` as a
247
/// source of randomness.
248
112
pub fn set_parachain_inherent_data(mock_inherent_data: MockInherentData) {
249
    use {
250
        cumulus_primitives_core::relay_chain::well_known_keys,
251
        cumulus_test_relay_sproof_builder::RelayStateSproofBuilder,
252
    };
253

            
254
112
    let relay_sproof = RelayStateSproofBuilder {
255
112
        para_id: 100u32.into(),
256
112
        included_para_head: Some(HeadData(vec![1, 2, 3])),
257
112
        current_slot: (current_slot()).into(),
258
112
        additional_key_values: if mock_inherent_data.random_seed.is_some() {
259
2
            vec![(
260
2
                well_known_keys::CURRENT_BLOCK_RANDOMNESS.to_vec(),
261
2
                Some(mock_inherent_data.random_seed).encode(),
262
2
            )]
263
        } else {
264
110
            vec![]
265
        },
266
112
        ..Default::default()
267
112
    };
268
112

            
269
112
    let (relay_parent_storage_root, relay_chain_state) = relay_sproof.into_state_root_and_proof();
270
112
    let vfp = PersistedValidationData {
271
112
        relay_parent_number: 1u32,
272
112
        relay_parent_storage_root,
273
112
        ..Default::default()
274
112
    };
275
112
    let parachain_inherent_data = ParachainInherentData {
276
112
        validation_data: vfp,
277
112
        relay_chain_state,
278
112
        downward_messages: Default::default(),
279
112
        horizontal_messages: Default::default(),
280
112
    };
281
112

            
282
112
    // Delete existing flag to avoid error
283
112
    // 'ValidationData must be updated only once in a block'
284
112
    // TODO: this is a hack
285
112
    frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
286
112
        b"ParachainSystem",
287
112
        b"ValidationData",
288
112
    ));
289
112

            
290
112
    assert_ok!(RuntimeCall::ParachainSystem(
291
112
        cumulus_pallet_parachain_system::Call::<Runtime>::set_validation_data {
292
112
            data: parachain_inherent_data
293
112
        }
294
112
    )
295
112
    .dispatch(inherent_origin()));
296
112
}
297

            
298
2
pub fn set_parachain_inherent_data_random_seed(random_seed: [u8; 32]) {
299
2
    set_new_inherent_data(MockInherentData {
300
2
        random_seed: Some(random_seed),
301
2
    });
302
2
}
303

            
304
#[derive(Default, Clone)]
305
pub struct ParaRegistrationParams {
306
    pub para_id: u32,
307
    pub genesis_data: ContainerChainGenesisData,
308
    pub block_production_credits: u32,
309
    pub collator_assignment_credits: u32,
310
    pub parathread_params: Option<tp_traits::ParathreadParams>,
311
}
312

            
313
110
pub fn default_config() -> pallet_configuration::HostConfiguration {
314
110
    pallet_configuration::HostConfiguration {
315
110
        max_collators: 100,
316
110
        min_orchestrator_collators: 2,
317
110
        max_orchestrator_collators: 2,
318
110
        collators_per_container: 2,
319
110
        full_rotation_period: 24,
320
110
        ..Default::default()
321
110
    }
322
110
}
323

            
324
pub struct ExtBuilder {
325
    // endowed accounts with balances
326
    balances: Vec<(AccountId, Balance)>,
327
    // [collator, amount]
328
    collators: Vec<(AccountId, Balance)>,
329
    // sudo key
330
    sudo: Option<AccountId>,
331
    // list of registered para ids: para_id, genesis_data, boot_nodes, block_credits, session_credits
332
    para_ids: Vec<ParaRegistrationParams>,
333
    // configuration to apply
334
    config: pallet_configuration::HostConfiguration,
335
    safe_xcm_version: Option<u32>,
336
    own_para_id: Option<ParaId>,
337
}
338

            
339
impl Default for ExtBuilder {
340
110
    fn default() -> Self {
341
110
        Self {
342
110
            balances: vec![
343
110
                // Alice gets 10k extra tokens for her mapping deposit
344
110
                (AccountId::from(ALICE), 210_000 * UNIT),
345
110
                (AccountId::from(BOB), 100_000 * UNIT),
346
110
            ],
347
110
            collators: vec![
348
110
                (AccountId::from(ALICE), 210 * UNIT),
349
110
                (AccountId::from(BOB), 100 * UNIT),
350
110
            ],
351
110
            sudo: Default::default(),
352
110
            para_ids: Default::default(),
353
110
            config: default_config(),
354
110
            safe_xcm_version: Default::default(),
355
110
            own_para_id: Default::default(),
356
110
        }
357
110
    }
358
}
359

            
360
impl ExtBuilder {
361
93
    pub fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
362
93
        self.balances = balances;
363
93
        self
364
93
    }
365

            
366
82
    pub fn with_collators(mut self, collators: Vec<(AccountId, Balance)>) -> Self {
367
82
        self.collators = collators;
368
82
        self
369
82
    }
370

            
371
8
    pub fn with_sudo(mut self, sudo: AccountId) -> Self {
372
8
        self.sudo = Some(sudo);
373
8
        self
374
8
    }
375

            
376
2
    pub fn with_para_ids(mut self, para_ids: Vec<ParaRegistrationParams>) -> Self {
377
2
        self.para_ids = para_ids;
378
2
        self
379
2
    }
380

            
381
    /// Helper function like `with_para_ids` but registering parachains with an empty genesis data,
382
    /// and max amount of credits.
383
57
    pub fn with_empty_parachains(mut self, para_ids: Vec<u32>) -> Self {
384
57
        self.para_ids = para_ids
385
57
            .into_iter()
386
115
            .map(|para_id| ParaRegistrationParams {
387
115
                para_id,
388
115
                genesis_data: empty_genesis_data(),
389
115
                block_production_credits: u32::MAX,
390
115
                collator_assignment_credits: u32::MAX,
391
115
                parathread_params: None,
392
115
            })
393
57
            .collect();
394
57
        self
395
57
    }
396

            
397
6
    pub fn with_config(mut self, config: pallet_configuration::HostConfiguration) -> Self {
398
6
        self.config = config;
399
6
        self
400
6
    }
401

            
402
110
    pub fn build_storage(self) -> sp_core::storage::Storage {
403
110
        let mut t = frame_system::GenesisConfig::<Runtime>::default()
404
110
            .build_storage()
405
110
            .unwrap();
406
110

            
407
110
        pallet_balances::GenesisConfig::<Runtime> {
408
110
            balances: self.balances,
409
110
        }
410
110
        .assimilate_storage(&mut t)
411
110
        .unwrap();
412
110

            
413
110
        // We need to initialize these pallets first. When initializing pallet-session,
414
110
        // these values will be taken into account for collator-assignment.
415
110

            
416
110
        pallet_registrar::GenesisConfig::<Runtime> {
417
110
            para_ids: self
418
110
                .para_ids
419
110
                .iter()
420
110
                .cloned()
421
118
                .map(|registered_para| {
422
118
                    (
423
118
                        registered_para.para_id.into(),
424
118
                        registered_para.genesis_data,
425
118
                        registered_para.parathread_params,
426
118
                    )
427
118
                })
428
110
                .collect(),
429
110
            phantom: Default::default(),
430
110
        }
431
110
        .assimilate_storage(&mut t)
432
110
        .unwrap();
433
110

            
434
110
        pallet_services_payment::GenesisConfig::<Runtime> {
435
110
            para_id_credits: self
436
110
                .para_ids
437
110
                .clone()
438
110
                .into_iter()
439
118
                .map(|registered_para| {
440
118
                    (
441
118
                        registered_para.para_id.into(),
442
118
                        registered_para.block_production_credits,
443
118
                        registered_para.collator_assignment_credits,
444
118
                    )
445
118
                        .into()
446
118
                })
447
110
                .collect(),
448
110
        }
449
110
        .assimilate_storage(&mut t)
450
110
        .unwrap();
451
110

            
452
110
        pallet_configuration::GenesisConfig::<Runtime> {
453
110
            config: self.config,
454
110
            ..Default::default()
455
110
        }
456
110
        .assimilate_storage(&mut t)
457
110
        .unwrap();
458
110

            
459
110
        pallet_xcm::GenesisConfig::<Runtime> {
460
110
            safe_xcm_version: self.safe_xcm_version,
461
110
            ..Default::default()
462
110
        }
463
110
        .assimilate_storage(&mut t)
464
110
        .unwrap();
465

            
466
110
        if let Some(own_para_id) = self.own_para_id {
467
            parachain_info::GenesisConfig::<Runtime> {
468
                parachain_id: own_para_id,
469
                ..Default::default()
470
            }
471
            .assimilate_storage(&mut t)
472
            .unwrap();
473
110
        }
474

            
475
110
        if !self.collators.is_empty() {
476
110
            // We set invulnerables in pallet_invulnerables
477
110
            let invulnerables: Vec<AccountId> = self
478
110
                .collators
479
110
                .clone()
480
110
                .into_iter()
481
361
                .map(|(account, _balance)| account)
482
110
                .collect();
483
110

            
484
110
            pallet_invulnerables::GenesisConfig::<Runtime> {
485
110
                invulnerables: invulnerables.clone(),
486
110
            }
487
110
            .assimilate_storage(&mut t)
488
110
            .unwrap();
489
110

            
490
110
            // But we also initialize their keys in the session pallet
491
110
            let keys: Vec<_> = self
492
110
                .collators
493
110
                .into_iter()
494
361
                .map(|(account, _balance)| {
495
361
                    let nimbus_id = get_aura_id_from_seed(&account.to_string());
496
361
                    (
497
361
                        account.clone(),
498
361
                        account,
499
361
                        crate::SessionKeys { nimbus: nimbus_id },
500
361
                    )
501
361
                })
502
110
                .collect();
503
110
            pallet_session::GenesisConfig::<Runtime> {
504
110
                keys,
505
110
                ..Default::default()
506
110
            }
507
110
            .assimilate_storage(&mut t)
508
110
            .unwrap();
509
110
        }
510
110
        pallet_sudo::GenesisConfig::<Runtime> { key: self.sudo }
511
110
            .assimilate_storage(&mut t)
512
110
            .unwrap();
513
110

            
514
110
        if self.safe_xcm_version.is_some() {
515
            // Disable run_block checks in XCM tests, because the XCM emulator runs on_initialize and
516
            // on_finalize automatically
517
            t.top.insert(b"__mock_is_xcm_test".to_vec(), b"1".to_vec());
518
110
        }
519

            
520
110
        t
521
110
    }
522

            
523
110
    pub fn build(self) -> sp_io::TestExternalities {
524
110
        let t = self.build_storage();
525
110
        let mut ext = sp_io::TestExternalities::new(t);
526
110

            
527
110
        ext.execute_with(|| {
528
110
            // Start block 1
529
110
            start_block();
530
110
            set_parachain_inherent_data(Default::default());
531
110
        });
532
110
        ext
533
110
    }
534
}
535

            
536
87
pub fn root_origin() -> <Runtime as frame_system::Config>::RuntimeOrigin {
537
87
    <Runtime as frame_system::Config>::RuntimeOrigin::root()
538
87
}
539

            
540
205
pub fn origin_of(account_id: AccountId) -> <Runtime as frame_system::Config>::RuntimeOrigin {
541
205
    <Runtime as frame_system::Config>::RuntimeOrigin::signed(account_id)
542
205
}
543

            
544
121
pub fn inherent_origin() -> <Runtime as frame_system::Config>::RuntimeOrigin {
545
121
    <Runtime as frame_system::Config>::RuntimeOrigin::none()
546
121
}
547

            
548
/// Helper function to generate a crypto pair from seed
549
442
pub fn get_aura_id_from_seed(seed: &str) -> NimbusId {
550
442
    sp_core::sr25519::Pair::from_string(&format!("//{}", seed), None)
551
442
        .expect("static values are valid; qed")
552
442
        .public()
553
442
        .into()
554
442
}
555

            
556
4
pub fn get_orchestrator_current_author() -> Option<AccountId> {
557
4
    let slot: u64 = current_slot();
558
4
    let orchestrator_collators = Runtime::parachain_collators(ParachainInfo::get())?;
559
4
    let author_index = slot % orchestrator_collators.len() as u64;
560
4
    let account = orchestrator_collators.get(author_index as usize)?;
561
4
    Some(account.clone())
562
4
}
563
/// Mocks the author noting inherent to insert the data we
564
9
pub fn set_author_noting_inherent_data(builder: ParaHeaderSproofBuilder) {
565
9
    let (relay_storage_root, relay_storage_proof) = builder.into_state_root_and_proof();
566
9

            
567
9
    // For now we directly touch parachain_system storage to set the relay state root.
568
9
    // TODO: Properly set the parachain_system inherent, which require a sproof builder combining
569
9
    // what is required by parachain_system and author_noting.
570
9
    frame_support::storage::unhashed::put(
571
9
        &frame_support::storage::storage_prefix(b"ParachainSystem", b"ValidationData"),
572
9
        &PersistedValidationData {
573
9
            parent_head: HeadData(Default::default()),
574
9
            relay_parent_number: 0u32,
575
9
            relay_parent_storage_root: relay_storage_root,
576
9
            max_pov_size: 0u32,
577
9
        },
578
9
    );
579
9

            
580
9
    // But we also need to store the new proof submitted
581
9
    frame_support::storage::unhashed::put(
582
9
        &frame_support::storage::storage_prefix(b"ParachainSystem", b"RelayStateProof"),
583
9
        &relay_storage_proof,
584
9
    );
585
9

            
586
9
    assert_ok!(RuntimeCall::AuthorNoting(
587
9
        pallet_author_noting::Call::<Runtime>::set_latest_author_data {
588
9
            data: tp_author_noting_inherent::OwnParachainInherentData {
589
9
                relay_storage_proof,
590
9
            }
591
9
        }
592
9
    )
593
9
    .dispatch(inherent_origin()));
594
9
}
595

            
596
144
pub fn empty_genesis_data() -> ContainerChainGenesisData {
597
144
    ContainerChainGenesisData {
598
144
        storage: Default::default(),
599
144
        name: Default::default(),
600
144
        id: Default::default(),
601
144
        fork_id: Default::default(),
602
144
        extensions: Default::default(),
603
144
        properties: Default::default(),
604
144
    }
605
144
}
606

            
607
2402
pub fn current_slot() -> u64 {
608
2402
    u64::from(
609
2402
        pallet_async_backing::SlotInfo::<Runtime>::get()
610
2402
            .unwrap_or_default()
611
2402
            .0,
612
2402
    )
613
2402
}
614

            
615
31
pub fn authorities() -> Vec<NimbusId> {
616
31
    let session_index = Session::current_index();
617
31

            
618
31
    AuthorityAssignment::collator_container_chain(session_index)
619
31
        .expect("authorities should be set")
620
31
        .orchestrator_chain
621
31
}
622

            
623
2290
pub fn current_author() -> AccountId {
624
2290
    let current_session = Session::current_index();
625
2290
    let mapping =
626
2290
        pallet_authority_mapping::Pallet::<Runtime>::authority_id_mapping(current_session)
627
2290
            .expect("there is a mapping for the current session");
628
2290

            
629
2290
    let author = pallet_author_inherent::Author::<Runtime>::get()
630
2290
        .expect("there should be a registered author");
631
2290

            
632
2290
    mapping
633
2290
        .get(&author)
634
2290
        .expect("there is a mapping for the current author")
635
2290
        .clone()
636
2290
}
637

            
638
15
pub fn block_credits_to_required_balance(number_of_blocks: u32, para_id: ParaId) -> Balance {
639
15
    let block_cost = BlockProductionCost::block_cost(&para_id).0;
640
15
    u128::from(number_of_blocks).saturating_mul(block_cost)
641
15
}
642

            
643
4
pub fn collator_assignment_credits_to_required_balance(
644
4
    number_of_sessions: u32,
645
4
    para_id: ParaId,
646
4
) -> Balance {
647
4
    let collator_assignment_cost = CollatorAssignmentCost::collator_assignment_cost(&para_id).0;
648
4
    u128::from(number_of_sessions).saturating_mul(collator_assignment_cost)
649
4
}
650

            
651
pub const ALICE: [u8; 32] = [4u8; 32];
652
pub const BOB: [u8; 32] = [5u8; 32];
653
pub const CHARLIE: [u8; 32] = [6u8; 32];
654
pub const DAVE: [u8; 32] = [7u8; 32];
655
pub const EVE: [u8; 32] = [8u8; 32];
656
pub const FERDIE: [u8; 32] = [9u8; 32];
657

            
658
21
pub fn set_dummy_boot_node(para_manager: RuntimeOrigin, para_id: ParaId) {
659
    use {
660
        pallet_data_preservers::{ParaIdsFilter, Profile, ProfileMode},
661
        tp_data_preservers_common::{AssignerExtra, ProviderRequest},
662
    };
663

            
664
21
    let profile = Profile {
665
21
        url:
666
21
            b"/ip4/127.0.0.1/tcp/33049/ws/p2p/12D3KooWHVMhQDHBpj9vQmssgyfspYecgV6e3hH1dQVDUkUbCYC9"
667
21
                .to_vec()
668
21
                .try_into()
669
21
                .expect("to fit in BoundedVec"),
670
21
        para_ids: ParaIdsFilter::AnyParaId,
671
21
        mode: ProfileMode::Bootnode,
672
21
        assignment_request: ProviderRequest::Free,
673
21
    };
674
21

            
675
21
    let profile_id = pallet_data_preservers::NextProfileId::<Runtime>::get();
676
21
    let profile_owner = AccountId::new([1u8; 32]);
677
21
    DataPreservers::force_create_profile(RuntimeOrigin::root(), profile, profile_owner)
678
21
        .expect("profile create to succeed");
679
21

            
680
21
    DataPreservers::start_assignment(para_manager, profile_id, para_id, AssignerExtra::Free)
681
21
        .expect("assignement to work");
682
21

            
683
21
    assert!(
684
21
        pallet_data_preservers::Assignments::<Runtime>::get(para_id).contains(&profile_id),
685
        "profile should be correctly assigned"
686
    );
687
21
}