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
#![cfg(test)]
18

            
19
use {
20
    crate::{
21
        tests::common::*, ContainerRegistrar, DataDepositPerByte, ParaDeposit, Paras, Registrar,
22
        RuntimeCall, SlotFrequency, System,
23
    },
24
    alloc::vec,
25
    cumulus_primitives_core::relay_chain::HeadData,
26
    frame_support::{assert_noop, assert_ok, BoundedVec},
27
    pallet_registrar::Event as ContainerRegistrarEvent,
28
    pallet_registrar_runtime_api::{
29
        runtime_decl_for_registrar_api::RegistrarApi, ContainerChainGenesisData,
30
    },
31
    runtime_common::paras_registrar,
32
    runtime_parachains::configuration as parachains_configuration,
33
    sp_runtime::traits::Dispatchable,
34
    tp_traits::ParaId,
35
};
36

            
37
#[test]
38
1
fn registrar_needs_a_reserved_para_id() {
39
1
    ExtBuilder::default()
40
1
        .with_balances(vec![
41
1
            // Alice gets 10k extra tokens for her mapping deposit
42
1
            (AccountId::from(ALICE), 210_000 * UNIT),
43
1
            (AccountId::from(BOB), 100_000 * UNIT),
44
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
45
1
            (AccountId::from(DAVE), 100_000 * UNIT),
46
1
        ])
47
1
        .build()
48
1
        .execute_with(|| {
49
1
            run_to_block(2);
50
1
            let validation_code =
51
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize];
52
1
            let genesis_data_2000 = ContainerChainGenesisData {
53
1
                storage: BoundedVec::try_from(vec![
54
1
                    (b":code".to_vec(), validation_code.clone()).into()
55
1
                ])
56
1
                .unwrap(),
57
1
                name: Default::default(),
58
1
                id: Default::default(),
59
1
                fork_id: Default::default(),
60
1
                extensions: BoundedVec::try_from(vec![]).unwrap(),
61
1
                properties: Default::default(),
62
1
            };
63
1
            let genesis_data_no_code = ContainerChainGenesisData {
64
1
                storage: BoundedVec::try_from(vec![(b":code".to_vec(), vec![1u8]).into()]).unwrap(),
65
1
                name: Default::default(),
66
1
                id: Default::default(),
67
1
                fork_id: Default::default(),
68
1
                extensions: BoundedVec::try_from(vec![]).unwrap(),
69
1
                properties: Default::default(),
70
1
            };
71

            
72
1
            let next_para_id = ParaId::from(2000);
73
1
            assert_noop!(
74
1
                ContainerRegistrar::register(
75
1
                    origin_of(ALICE.into()),
76
1
                    next_para_id,
77
1
                    genesis_data_2000.clone(),
78
1
                    Some(HeadData(vec![1u8, 1u8, 1u8])),
79
                ),
80
1
                paras_registrar::Error::<Runtime>::NotReserved
81
            );
82

            
83
            // After a reservation, we can register
84
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
85
1
            assert_noop!(
86
1
                ContainerRegistrar::register(
87
1
                    origin_of(ALICE.into()),
88
1
                    next_para_id,
89
1
                    genesis_data_no_code,
90
1
                    Some(HeadData(vec![1u8, 1u8, 1u8])),
91
                ),
92
1
                paras_registrar::Error::<Runtime>::InvalidCode
93
            );
94

            
95
1
            let validation_code: cumulus_primitives_core::relay_chain::ValidationCode =
96
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize].into();
97
1
            assert_ok!(ContainerRegistrar::register(
98
1
                origin_of(ALICE.into()),
99
1
                next_para_id,
100
1
                genesis_data_2000.clone(),
101
1
                Some(HeadData(vec![1u8, 1u8, 1u8])),
102
            ));
103

            
104
1
            assert!(Paras::lifecycle(next_para_id)
105
1
                .expect("para should be onboarding")
106
1
                .is_onboarding());
107
            // Two sessions later the para should be a parathread
108
            // But only if the pvf is accepted! which we havent done
109
1
            run_to_session(2);
110
1
            assert!(Paras::lifecycle(next_para_id)
111
1
                .expect("para should be onboarding")
112
1
                .is_onboarding());
113

            
114
            // Now let's accept the pvf, so that after 2 sesssions we have the chain onboarded
115
1
            assert_ok!(Paras::add_trusted_validation_code(
116
1
                root_origin(),
117
1
                validation_code
118
            ));
119
1
            run_to_session(4);
120

            
121
            // PVF accepted and the para should be a parathread
122
1
            assert!(Paras::lifecycle(next_para_id)
123
1
                .expect("para should be parathread")
124
1
                .is_parathread());
125
1
        });
126
1
}
127

            
128
#[test]
129
1
fn register_para_via_container_registrar() {
130
1
    ExtBuilder::default()
131
1
        .with_para_ids(vec![
132
1
            (1001, empty_genesis_data(), u32::MAX, u32::MAX).into(),
133
1
            (1002, empty_genesis_data(), u32::MAX, u32::MAX).into(),
134
1
        ])
135
1
        .build()
136
1
        .execute_with(|| {
137
            // In this test we're gonna register a para via ContainerRegistrar,
138
            // which will internally use the InnerRegistrar type to also register the para
139
            // in the relay Registrar pallet.
140

            
141
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into(), 1002.into()]);
142
1
            assert_eq!(Runtime::genesis_data(2000.into()).as_ref(), None);
143
1
            assert_eq!(
144
1
                parachains_configuration::ActiveConfig::<Runtime>::get().max_head_data_size,
145
                20500
146
            );
147

            
148
1
            let validation_code =
149
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize];
150
1
            let genesis_data_2000 = ContainerChainGenesisData {
151
1
                storage: BoundedVec::try_from(vec![
152
1
                    (b":code".to_vec(), validation_code.clone()).into()
153
1
                ])
154
1
                .unwrap(),
155
1
                name: Default::default(),
156
1
                id: Default::default(),
157
1
                fork_id: Default::default(),
158
1
                extensions: BoundedVec::try_from(vec![]).unwrap(),
159
1
                properties: Default::default(),
160
1
            };
161

            
162
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
163
1
            assert_ok!(
164
1
                ContainerRegistrar::register(
165
1
                    origin_of(ALICE.into()),
166
1
                    2000.into(),
167
1
                    genesis_data_2000.clone(),
168
1
                    Some(HeadData(vec![1u8, 1u8, 1u8]))
169
                ),
170
                ()
171
            );
172

            
173
            // Now let's check if the para was preoperly registered in the relay.
174
            // Run to next session.
175
1
            run_to_session(1);
176
1
            assert!(Paras::lifecycle(2000.into())
177
1
                .expect("para should be onboarding")
178
1
                .is_onboarding());
179

            
180
            // We need to accept the validation code, so that the para is onboarded after 2 sessions.
181
1
            assert_ok!(Paras::add_trusted_validation_code(
182
1
                root_origin(),
183
1
                validation_code.into()
184
            ));
185
1
            run_to_session(3);
186

            
187
            // Now the para should be a parathread.
188
1
            assert!(Paras::lifecycle(2000.into())
189
1
                .expect("para should be parathread")
190
1
                .is_parathread());
191
1
        });
192
1
}
193

            
194
#[test]
195
1
fn cannot_register_para_twice_in_relay() {
196
1
    ExtBuilder::default()
197
1
        .with_para_ids(vec![
198
1
            (1001, empty_genesis_data(), u32::MAX, u32::MAX).into(),
199
1
            (1002, empty_genesis_data(), u32::MAX, u32::MAX).into(),
200
1
        ])
201
1
        .build()
202
1
        .execute_with(|| {
203
            // In this test we're gonna confirm that a para cannot be registered in the relay
204
            // after being already registered through ContainerRegistrar.
205

            
206
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into(), 1002.into()]);
207
1
            assert_eq!(Runtime::genesis_data(2000.into()).as_ref(), None);
208
1
            assert_eq!(
209
1
                parachains_configuration::ActiveConfig::<Runtime>::get().max_head_data_size,
210
                20500
211
            );
212

            
213
1
            let validation_code =
214
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize];
215
1
            let genesis_data_2000 = ContainerChainGenesisData {
216
1
                storage: BoundedVec::try_from(vec![
217
1
                    (b":code".to_vec(), validation_code.clone()).into()
218
1
                ])
219
1
                .unwrap(),
220
1
                name: Default::default(),
221
1
                id: Default::default(),
222
1
                fork_id: Default::default(),
223
1
                extensions: BoundedVec::try_from(vec![]).unwrap(),
224
1
                properties: Default::default(),
225
1
            };
226

            
227
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
228
1
            assert_ok!(
229
1
                ContainerRegistrar::register(
230
1
                    origin_of(ALICE.into()),
231
1
                    2000.into(),
232
1
                    genesis_data_2000.clone(),
233
1
                    Some(HeadData(vec![1u8, 1u8, 1u8]))
234
                ),
235
                ()
236
            );
237

            
238
            // Now let's check if the para was preoperly registered in the relay.
239
            // Run to next session.
240
1
            run_to_session(1);
241
1
            assert!(Paras::lifecycle(2000.into())
242
1
                .expect("para should be onboarding")
243
1
                .is_onboarding());
244

            
245
            // We need to accept the validation code, so that the para is onboarded after 2 sessions.
246
1
            assert_ok!(Paras::add_trusted_validation_code(
247
1
                root_origin(),
248
1
                validation_code.clone().into()
249
            ));
250
1
            run_to_session(3);
251

            
252
            // Now the para should be a parathread.
253
1
            assert!(Paras::lifecycle(2000.into())
254
1
                .expect("para should be parathread")
255
1
                .is_parathread());
256

            
257
            // We shouldn't be able to register the para again
258
1
            assert_noop!(
259
1
                ContainerRegistrar::register(
260
1
                    origin_of(ALICE.into()),
261
1
                    2000.into(),
262
1
                    genesis_data_2000.clone(),
263
1
                    Some(HeadData(vec![1u8, 1u8, 1u8]))
264
                ),
265
1
                pallet_registrar::Error::<Runtime>::ParaIdAlreadyRegistered,
266
            );
267
1
        });
268
1
}
269

            
270
#[test]
271
1
fn mark_valid_for_collating_converts_to_parachain() {
272
1
    ExtBuilder::default()
273
1
        .with_para_ids(vec![
274
1
            (1001, empty_genesis_data(), u32::MAX, u32::MAX).into(),
275
1
            (1002, empty_genesis_data(), u32::MAX, u32::MAX).into(),
276
1
        ])
277
1
        .build()
278
1
        .execute_with(|| {
279
            // In this test we're gonna check that inside mark_valid_for_collating(),
280
            // if we are passing a parathread, this one will be upgraded to a parachain
281
            // at the end of the execution.
282

            
283
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into(), 1002.into()]);
284
1
            assert_eq!(Runtime::genesis_data(2000.into()).as_ref(), None);
285
1
            assert_eq!(
286
1
                parachains_configuration::ActiveConfig::<Runtime>::get().max_head_data_size,
287
                20500
288
            );
289

            
290
1
            let validation_code =
291
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize];
292
1
            let genesis_data_2000 = ContainerChainGenesisData {
293
1
                storage: BoundedVec::try_from(vec![
294
1
                    (b":code".to_vec(), validation_code.clone()).into()
295
1
                ])
296
1
                .unwrap(),
297
1
                name: Default::default(),
298
1
                id: Default::default(),
299
1
                fork_id: Default::default(),
300
1
                extensions: BoundedVec::try_from(vec![]).unwrap(),
301
1
                properties: Default::default(),
302
1
            };
303

            
304
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
305
1
            assert_ok!(
306
1
                ContainerRegistrar::register(
307
1
                    origin_of(ALICE.into()),
308
1
                    2000.into(),
309
1
                    genesis_data_2000.clone(),
310
1
                    Some(HeadData(vec![1u8, 1u8, 1u8]))
311
                ),
312
                ()
313
            );
314

            
315
            // Now let's check if the para was preoperly registered in the relay.
316
            // Run to next session.
317
1
            run_to_session(1);
318
1
            assert!(Paras::lifecycle(2000.into())
319
1
                .expect("para should be onboarding")
320
1
                .is_onboarding());
321

            
322
            // We need to accept the validation code, so that the para is onboarded after 2 sessions.
323
1
            assert_ok!(Paras::add_trusted_validation_code(
324
1
                root_origin(),
325
1
                validation_code.into()
326
            ));
327
1
            run_to_session(3);
328

            
329
            // Now the para should be a parathread.
330
1
            assert!(Paras::lifecycle(2000.into())
331
1
                .expect("para should be parathread")
332
1
                .is_parathread());
333

            
334
1
            set_dummy_boot_node(origin_of(ALICE.into()), 2000.into());
335

            
336
            // Call mark_valid_for_collating.
337
1
            assert_ok!(
338
1
                ContainerRegistrar::mark_valid_for_collating(root_origin(), 2000.into()),
339
                ()
340
            );
341

            
342
            // The change should be applied after 2 sessions.
343
1
            run_to_session(5);
344
1
            assert!(Paras::lifecycle(2000.into())
345
1
                .expect("para should be parachain")
346
1
                .is_parachain());
347
1
        });
348
1
}
349

            
350
#[test]
351
1
fn deregister_calls_schedule_para_cleanup() {
352
1
    ExtBuilder::default()
353
1
        .with_para_ids(vec![
354
1
            (1001, empty_genesis_data(), u32::MAX, u32::MAX).into(),
355
1
            (1002, empty_genesis_data(), u32::MAX, u32::MAX).into(),
356
1
        ])
357
1
        .build()
358
1
        .execute_with(|| {
359
            // In this test we're gonna check that when calling ContainerRegistrar::deregister(),
360
            // the para is also offboarded from the relay.
361

            
362
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into(), 1002.into()]);
363
1
            assert_eq!(Runtime::genesis_data(2000.into()).as_ref(), None);
364
1
            assert_eq!(
365
1
                parachains_configuration::ActiveConfig::<Runtime>::get().max_head_data_size,
366
                20500
367
            );
368

            
369
1
            let validation_code =
370
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize];
371
1
            let genesis_data_2000 = ContainerChainGenesisData {
372
1
                storage: BoundedVec::try_from(vec![
373
1
                    (b":code".to_vec(), validation_code.clone()).into()
374
1
                ])
375
1
                .unwrap(),
376
1
                name: Default::default(),
377
1
                id: Default::default(),
378
1
                fork_id: Default::default(),
379
1
                extensions: BoundedVec::try_from(vec![]).unwrap(),
380
1
                properties: Default::default(),
381
1
            };
382

            
383
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
384
1
            assert_ok!(
385
1
                ContainerRegistrar::register(
386
1
                    origin_of(ALICE.into()),
387
1
                    2000.into(),
388
1
                    genesis_data_2000.clone(),
389
1
                    Some(HeadData(vec![1u8, 1u8, 1u8]))
390
                ),
391
                ()
392
            );
393

            
394
            // Now let's check if the para was preoperly registered in the relay.
395
            // Run to next session.
396
1
            run_to_session(1);
397
1
            assert!(Paras::lifecycle(2000.into())
398
1
                .expect("para should be onboarding")
399
1
                .is_onboarding());
400

            
401
            // We need to accept the validation code, so that the para is onboarded after 2 sessions.
402
1
            assert_ok!(Paras::add_trusted_validation_code(
403
1
                root_origin(),
404
1
                validation_code.into()
405
            ));
406

            
407
1
            run_to_session(3);
408

            
409
            // Now the para should be a parathread.
410
1
            assert!(Paras::lifecycle(2000.into())
411
1
                .expect("para should be parathread")
412
1
                .is_parathread());
413

            
414
1
            set_dummy_boot_node(origin_of(ALICE.into()), 2000.into());
415

            
416
            // Call mark_valid_for_collating.
417
1
            assert_ok!(
418
1
                ContainerRegistrar::mark_valid_for_collating(root_origin(), 2000.into()),
419
                ()
420
            );
421

            
422
            // The change should be applied after 2 sessions.
423
1
            run_to_session(5);
424
1
            assert!(Paras::lifecycle(2000.into())
425
1
                .expect("para should be parachain")
426
1
                .is_parachain());
427

            
428
1
            assert_eq!(
429
1
                Runtime::genesis_data(2000.into()).as_ref(),
430
1
                Some(&genesis_data_2000)
431
            );
432

            
433
1
            assert_ok!(ContainerRegistrar::deregister(root_origin(), 2000.into()));
434

            
435
            // Assert that the ParaIdDeregistered event was properly deposited
436
1
            System::assert_last_event(
437
1
                ContainerRegistrarEvent::ParaIdDeregistered {
438
1
                    para_id: 2000.into(),
439
1
                }
440
1
                .into(),
441
            );
442

            
443
1
            run_to_session(7);
444
1
            end_block();
445

            
446
1
            assert_eq!(Runtime::genesis_data(2000.into()).as_ref(), None);
447

            
448
            // Para should be offboarding after 2 sessions.
449
            // we need one more block to trigger the on_initialize of containerRegistrar
450
            // which should fully clean everything
451
1
            start_block();
452
1
            assert!(Paras::lifecycle(2000.into())
453
1
                .expect("para should be offboarding")
454
1
                .is_offboarding());
455
1
        });
456
1
}
457

            
458
#[test]
459
1
fn deregister_two_paras_in_the_same_block() {
460
1
    ExtBuilder::default()
461
1
        .with_para_ids(vec![
462
1
            (1001, empty_genesis_data(), u32::MAX, u32::MAX).into(),
463
1
            (1002, empty_genesis_data(), u32::MAX, u32::MAX).into(),
464
1
        ])
465
1
        .build()
466
1
        .execute_with(|| {
467
            // In this test we're gonna check that when calling ContainerRegistrar::deregister(),
468
            // two paraIds are properly offboarded from the relay.
469

            
470
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into(), 1002.into()]);
471
1
            assert_eq!(Runtime::genesis_data(2000.into()).as_ref(), None);
472
1
            assert_eq!(Runtime::genesis_data(2001.into()).as_ref(), None);
473
1
            assert_eq!(
474
1
                parachains_configuration::ActiveConfig::<Runtime>::get().max_head_data_size,
475
                20500
476
            );
477

            
478
1
            let validation_code =
479
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize];
480
1
            let genesis_data_2000_and_2001 = ContainerChainGenesisData {
481
1
                storage: BoundedVec::try_from(vec![
482
1
                    (b":code".to_vec(), validation_code.clone()).into()
483
1
                ])
484
1
                .unwrap(),
485
1
                name: Default::default(),
486
1
                id: Default::default(),
487
1
                fork_id: Default::default(),
488
1
                extensions: BoundedVec::try_from(vec![]).unwrap(),
489
1
                properties: Default::default(),
490
1
            };
491

            
492
            // Register paraId 2000
493
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
494
1
            assert_ok!(
495
1
                ContainerRegistrar::register(
496
1
                    origin_of(ALICE.into()),
497
1
                    2000.into(),
498
1
                    genesis_data_2000_and_2001.clone(),
499
1
                    Some(HeadData(vec![1u8, 1u8, 1u8]))
500
                ),
501
                ()
502
            );
503

            
504
            // Register paraId 2001
505
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
506
1
            assert_ok!(
507
1
                ContainerRegistrar::register(
508
1
                    origin_of(ALICE.into()),
509
1
                    2001.into(),
510
1
                    genesis_data_2000_and_2001.clone(),
511
1
                    Some(HeadData(vec![1u8, 1u8, 1u8]))
512
                ),
513
                ()
514
            );
515

            
516
            // Now let's check if the paras were preoperly registered in the relay.
517
            // Run to next session.
518
1
            run_to_session(1);
519
1
            assert!(Paras::lifecycle(2000.into())
520
1
                .expect("para should be onboarding")
521
1
                .is_onboarding());
522

            
523
1
            assert!(Paras::lifecycle(2001.into())
524
1
                .expect("para should be onboarding")
525
1
                .is_onboarding());
526

            
527
            // We need to accept the validation code, so that paras are onboarded after 2 sessions.
528
1
            assert_ok!(Paras::add_trusted_validation_code(
529
1
                root_origin(),
530
1
                validation_code.into()
531
            ));
532

            
533
1
            run_to_session(3);
534

            
535
            // Now paras should be parathreads.
536
1
            assert!(Paras::lifecycle(2000.into())
537
1
                .expect("para should be parathread")
538
1
                .is_parathread());
539
1
            assert!(Paras::lifecycle(2001.into())
540
1
                .expect("para should be parathread")
541
1
                .is_parathread());
542

            
543
1
            set_dummy_boot_node(origin_of(ALICE.into()), 2000.into());
544
1
            set_dummy_boot_node(origin_of(ALICE.into()), 2001.into());
545

            
546
            // Call mark_valid_for_collating.
547
1
            assert_ok!(
548
1
                ContainerRegistrar::mark_valid_for_collating(root_origin(), 2000.into()),
549
                ()
550
            );
551

            
552
1
            assert_ok!(
553
1
                ContainerRegistrar::mark_valid_for_collating(root_origin(), 2001.into()),
554
                ()
555
            );
556

            
557
            // The change should be applied after 2 sessions.
558
1
            run_to_session(5);
559
1
            assert!(Paras::lifecycle(2000.into())
560
1
                .expect("para should be parachain")
561
1
                .is_parachain());
562

            
563
1
            assert!(Paras::lifecycle(2001.into())
564
1
                .expect("para should be parachain")
565
1
                .is_parachain());
566

            
567
1
            assert_eq!(
568
1
                Runtime::genesis_data(2000.into()).as_ref(),
569
1
                Some(&genesis_data_2000_and_2001)
570
            );
571

            
572
1
            assert_eq!(
573
1
                Runtime::genesis_data(2001.into()).as_ref(),
574
1
                Some(&genesis_data_2000_and_2001)
575
            );
576

            
577
1
            assert_ok!(ContainerRegistrar::deregister(root_origin(), 2000.into()));
578

            
579
            // Assert that the ParaIdDeregistered event was properly deposited
580
1
            System::assert_last_event(
581
1
                ContainerRegistrarEvent::ParaIdDeregistered {
582
1
                    para_id: 2000.into(),
583
1
                }
584
1
                .into(),
585
            );
586

            
587
1
            assert_ok!(ContainerRegistrar::deregister(root_origin(), 2001.into()));
588
1
            System::assert_last_event(
589
1
                ContainerRegistrarEvent::ParaIdDeregistered {
590
1
                    para_id: 2001.into(),
591
1
                }
592
1
                .into(),
593
            );
594

            
595
1
            run_to_session(7);
596
1
            end_block();
597

            
598
1
            assert_eq!(Runtime::genesis_data(2000.into()).as_ref(), None);
599
1
            assert_eq!(Runtime::genesis_data(2001.into()).as_ref(), None);
600

            
601
1
            start_block();
602
            // Paras should be offboarding after 2 sessions.
603
1
            assert!(Paras::lifecycle(2000.into())
604
1
                .expect("para should be offboarding")
605
1
                .is_offboarding());
606

            
607
1
            assert!(Paras::lifecycle(2001.into())
608
1
                .expect("para should be offboarding")
609
1
                .is_offboarding());
610
1
        });
611
1
}
612

            
613
#[test]
614
1
fn test_register_parathread_not_allowed() {
615
1
    ExtBuilder::default()
616
1
        .with_balances(vec![
617
1
            // Alice gets 10k extra tokens for her mapping deposit
618
1
            (AccountId::from(ALICE), 210_000 * UNIT),
619
1
            (AccountId::from(BOB), 100_000 * UNIT),
620
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
621
1
            (AccountId::from(DAVE), 100_000 * UNIT),
622
1
        ])
623
1
        .with_collators(vec![
624
1
            (AccountId::from(ALICE), 210 * UNIT),
625
1
            (AccountId::from(BOB), 100 * UNIT),
626
1
            (AccountId::from(CHARLIE), 100 * UNIT),
627
1
            (AccountId::from(DAVE), 100 * UNIT),
628
1
        ])
629
1
        .build()
630
1
        .execute_with(|| {
631
1
            run_to_block(2);
632

            
633
1
            assert_noop!(
634
1
                RuntimeCall::ContainerRegistrar(
635
1
                    pallet_registrar::Call::<Runtime>::register_parathread {
636
1
                        para_id: 3001.into(),
637
1
                        slot_frequency: SlotFrequency { min: 1, max: 1 },
638
1
                        genesis_data: empty_genesis_data(),
639
1
                        head_data: None
640
1
                    }
641
1
                )
642
1
                .dispatch(
643
1
                    <Runtime as frame_system::Config>::RuntimeOrigin::signed(AccountId::from(
644
                        ALICE
645
                    ))
646
                ),
647
1
                frame_system::Error::<Runtime>::CallFiltered
648
            );
649
1
        });
650
1
}
651

            
652
#[test]
653
1
fn test_relay_registrar_through_extrinsic_not_allowed() {
654
1
    ExtBuilder::default()
655
1
        .with_balances(vec![
656
1
            // Alice gets 10k extra tokens for her mapping deposit
657
1
            (AccountId::from(ALICE), 210_000 * UNIT),
658
1
            (AccountId::from(BOB), 100_000 * UNIT),
659
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
660
1
            (AccountId::from(DAVE), 100_000 * UNIT),
661
1
        ])
662
1
        .with_collators(vec![
663
1
            (AccountId::from(ALICE), 210 * UNIT),
664
1
            (AccountId::from(BOB), 100 * UNIT),
665
1
            (AccountId::from(CHARLIE), 100 * UNIT),
666
1
            (AccountId::from(DAVE), 100 * UNIT),
667
1
        ])
668
1
        .build()
669
1
        .execute_with(|| {
670
1
            run_to_block(2);
671

            
672
1
            let validation_code =
673
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize];
674

            
675
1
            assert_noop!(
676
1
                RuntimeCall::Registrar(paras_registrar::Call::<Runtime>::register {
677
1
                    id: 3001.into(),
678
1
                    validation_code: cumulus_primitives_core::relay_chain::ValidationCode(
679
1
                        validation_code
680
1
                    ),
681
1
                    genesis_head: HeadData(vec![1u8, 1u8, 1u8]),
682
1
                })
683
1
                .dispatch(
684
1
                    <Runtime as frame_system::Config>::RuntimeOrigin::signed(AccountId::from(
685
                        ALICE
686
                    ))
687
                ),
688
1
                frame_system::Error::<Runtime>::CallFiltered
689
            );
690
1
        });
691
1
}
692

            
693
#[test]
694
1
fn test_relay_registrar_deregister_through_extrinsic_not_allowed() {
695
1
    ExtBuilder::default()
696
1
        .with_balances(vec![
697
1
            // Alice gets 10k extra tokens for her mapping deposit
698
1
            (AccountId::from(ALICE), 210_000 * UNIT),
699
1
            (AccountId::from(BOB), 100_000 * UNIT),
700
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
701
1
            (AccountId::from(DAVE), 100_000 * UNIT),
702
1
        ])
703
1
        .with_collators(vec![
704
1
            (AccountId::from(ALICE), 210 * UNIT),
705
1
            (AccountId::from(BOB), 100 * UNIT),
706
1
            (AccountId::from(CHARLIE), 100 * UNIT),
707
1
            (AccountId::from(DAVE), 100 * UNIT),
708
1
        ])
709
1
        .build()
710
1
        .execute_with(|| {
711
1
            run_to_block(2);
712

            
713
1
            assert_noop!(
714
1
                RuntimeCall::Registrar(paras_registrar::Call::<Runtime>::deregister {
715
1
                    id: 3001.into()
716
1
                })
717
1
                .dispatch(
718
1
                    <Runtime as frame_system::Config>::RuntimeOrigin::signed(AccountId::from(
719
                        ALICE
720
                    ))
721
                ),
722
1
                frame_system::Error::<Runtime>::CallFiltered
723
            );
724
1
        });
725
1
}
726

            
727
#[test]
728
1
fn test_container_registrar_register_is_allowed() {
729
1
    ExtBuilder::default()
730
1
        .with_balances(vec![
731
1
            // Alice gets 10k extra tokens for her mapping deposit
732
1
            (AccountId::from(ALICE), 210_000 * UNIT),
733
1
            (AccountId::from(BOB), 100_000 * UNIT),
734
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
735
1
            (AccountId::from(DAVE), 100_000 * UNIT),
736
1
        ])
737
1
        .with_collators(vec![
738
1
            (AccountId::from(ALICE), 210 * UNIT),
739
1
            (AccountId::from(BOB), 100 * UNIT),
740
1
            (AccountId::from(CHARLIE), 100 * UNIT),
741
1
            (AccountId::from(DAVE), 100 * UNIT),
742
1
        ])
743
1
        .build()
744
1
        .execute_with(|| {
745
1
            run_to_block(2);
746

            
747
1
            assert_ok!(
748
1
                RuntimeCall::Registrar(paras_registrar::Call::<Runtime>::reserve {}).dispatch(
749
1
                    <Runtime as frame_system::Config>::RuntimeOrigin::signed(AccountId::from(
750
                        ALICE
751
                    ))
752
                )
753
            );
754
1
        });
755
1
}
756

            
757
#[test]
758
1
fn test_relay_registrar_reserve_is_allowed() {
759
1
    ExtBuilder::default()
760
1
        .with_balances(vec![
761
1
            // Alice gets 10k extra tokens for her mapping deposit
762
1
            (AccountId::from(ALICE), 210_000 * UNIT),
763
1
            (AccountId::from(BOB), 100_000 * UNIT),
764
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
765
1
            (AccountId::from(DAVE), 100_000 * UNIT),
766
1
        ])
767
1
        .with_collators(vec![
768
1
            (AccountId::from(ALICE), 210 * UNIT),
769
1
            (AccountId::from(BOB), 100 * UNIT),
770
1
            (AccountId::from(CHARLIE), 100 * UNIT),
771
1
            (AccountId::from(DAVE), 100 * UNIT),
772
1
        ])
773
1
        .build()
774
1
        .execute_with(|| {
775
1
            run_to_block(2);
776

            
777
1
            let validation_code =
778
1
                vec![1u8; cumulus_primitives_core::relay_chain::MIN_CODE_SIZE as usize];
779
1
            let genesis_data_2000 = ContainerChainGenesisData {
780
1
                storage: BoundedVec::try_from(vec![
781
1
                    (b":code".to_vec(), validation_code.clone()).into()
782
1
                ])
783
1
                .unwrap(),
784
1
                name: Default::default(),
785
1
                id: Default::default(),
786
1
                fork_id: Default::default(),
787
1
                extensions: BoundedVec::try_from(vec![]).unwrap(),
788
1
                properties: Default::default(),
789
1
            };
790

            
791
1
            let balance_reserved_before = Balances::reserved_balance(&AccountId::from(ALICE));
792

            
793
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
794

            
795
1
            let genesis_head = HeadData(vec![1u8, 1u8, 1u8]);
796

            
797
1
            assert_ok!(RuntimeCall::ContainerRegistrar(
798
1
                pallet_registrar::Call::<Runtime>::register {
799
1
                    para_id: 2000.into(),
800
1
                    genesis_data: genesis_data_2000.clone(),
801
1
                    head_data: Some(genesis_head.clone())
802
1
                }
803
1
            )
804
1
            .dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::signed(
805
1
                AccountId::from(ALICE)
806
            )));
807
1
            let balance_reserved_after = Balances::reserved_balance(&AccountId::from(ALICE));
808

            
809
1
            let base_deposit = ParaDeposit::get();
810
1
            let deposit_per_byte = DataDepositPerByte::get();
811
1
            let total_byte_deposit = (deposit_per_byte
812
1
                * parachains_configuration::ActiveConfig::<Runtime>::get().max_code_size as u128)
813
1
                .saturating_add(deposit_per_byte * genesis_head.0.len() as u128);
814

            
815
            // I am not able to match this exactly, I guess I am missing something. but for now I guess it's ok at least
816
1
            assert!(
817
1
                balance_reserved_after.saturating_sub(balance_reserved_before)
818
1
                    > total_byte_deposit.saturating_add(base_deposit)
819
            );
820
1
        });
821
1
}