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 sp_runtime::traits::BadOrigin;
20
use {
21
    crate::{
22
        tests::common::*, Balances, CollatorConfiguration, ContainerRegistrar, DataPreservers,
23
        ForeignAssetsCreator, Registrar, StreamPayment,
24
    },
25
    cumulus_primitives_core::{relay_chain::HeadData, ParaId},
26
    frame_support::{assert_err, assert_noop, assert_ok, BoundedVec},
27
    pallet_registrar_runtime_api::{
28
        runtime_decl_for_registrar_api::RegistrarApi, ContainerChainGenesisData,
29
    },
30
    pallet_stream_payment::StreamConfig,
31
    sp_std::vec,
32
    starlight_runtime_constants::currency::EXISTENTIAL_DEPOSIT,
33
    tp_stream_payment_common::{
34
        AssetId as StreamPaymentAssetId, TimeUnit as StreamPaymentTimeUnit,
35
    },
36
    xcm::latest::prelude::{Junctions::X2, *},
37
};
38

            
39
#[test]
40
1
fn genesis_balances() {
41
1
    ExtBuilder::default()
42
1
        .with_balances(vec![
43
1
            // Alice gets 10k extra tokens for her mapping deposit
44
1
            (AccountId::from(ALICE), 210_000 * UNIT),
45
1
            (AccountId::from(BOB), 100_000 * UNIT),
46
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
47
1
            (AccountId::from(DAVE), 100_000 * UNIT),
48
1
        ])
49
1
        .build()
50
1
        .execute_with(|| {
51
1
            assert_eq!(
52
1
                Balances::usable_balance(AccountId::from(ALICE)) + EXISTENTIAL_DEPOSIT,
53
1
                210_000 * UNIT,
54
1
            );
55
1
            assert_eq!(
56
1
                Balances::usable_balance(AccountId::from(BOB)) + EXISTENTIAL_DEPOSIT,
57
1
                100_000 * UNIT,
58
1
            );
59
1
        });
60
1
}
61

            
62
#[test]
63
1
fn genesis_para_registrar() {
64
1
    ExtBuilder::default()
65
1
        .with_empty_parachains(vec![1001, 1002])
66
1
        .build()
67
1
        .execute_with(|| {
68
1
            assert_eq!(
69
1
                ContainerRegistrar::registered_para_ids(),
70
1
                vec![1001.into(), 1002.into()]
71
1
            );
72
1
        });
73
1
}
74

            
75
#[test]
76
1
fn genesis_para_registrar_deregister() {
77
1
    ExtBuilder::default()
78
1
        .with_empty_parachains(vec![1001, 1002])
79
1
        .build()
80
1
        .execute_with(|| {
81
1
            assert_eq!(
82
1
                ContainerRegistrar::registered_para_ids(),
83
1
                vec![1001.into(), 1002.into()]
84
1
            );
85

            
86
1
            run_to_block(2);
87
1
            assert_ok!(
88
1
                ContainerRegistrar::deregister(root_origin(), 1002.into()),
89
1
                ()
90
1
            );
91

            
92
            // Pending
93
1
            assert_eq!(
94
1
                ContainerRegistrar::pending_registered_para_ids(),
95
1
                vec![(2u32, BoundedVec::try_from(vec![1001u32.into()]).unwrap())]
96
1
            );
97

            
98
1
            run_to_session(1);
99
1
            assert_eq!(
100
1
                ContainerRegistrar::pending_registered_para_ids(),
101
1
                vec![(2u32, BoundedVec::try_from(vec![1001u32.into()]).unwrap())]
102
1
            );
103
1
            assert_eq!(
104
1
                ContainerRegistrar::registered_para_ids(),
105
1
                vec![1001.into(), 1002.into()]
106
1
            );
107

            
108
1
            run_to_session(2);
109
1
            assert_eq!(ContainerRegistrar::pending_registered_para_ids(), vec![]);
110
1
            assert_eq!(ContainerRegistrar::registered_para_ids(), vec![1001.into()]);
111
1
        });
112
1
}
113

            
114
#[test]
115
1
fn genesis_para_registrar_runtime_api() {
116
1
    ExtBuilder::default()
117
1
        .with_empty_parachains(vec![1001, 1002])
118
1
        .build()
119
1
        .execute_with(|| {
120
1
            assert_eq!(
121
1
                ContainerRegistrar::registered_para_ids(),
122
1
                vec![1001.into(), 1002.into()]
123
1
            );
124
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into(), 1002.into()]);
125

            
126
1
            run_to_block(2);
127
1
            assert_ok!(
128
1
                ContainerRegistrar::deregister(root_origin(), 1002.into()),
129
1
                ()
130
1
            );
131
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into(), 1002.into()]);
132

            
133
1
            run_to_session(1);
134
1
            assert_eq!(
135
1
                ContainerRegistrar::registered_para_ids(),
136
1
                vec![1001.into(), 1002.into()]
137
1
            );
138
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into(), 1002.into()]);
139

            
140
1
            run_to_session(2);
141
1
            assert_eq!(ContainerRegistrar::registered_para_ids(), vec![1001.into()]);
142
1
            assert_eq!(Runtime::registered_paras(), vec![1001.into()]);
143
1
        });
144
1
}
145

            
146
#[test]
147
1
fn genesis_para_registrar_container_chain_genesis_data_runtime_api() {
148
1
    let genesis_data_2001 = empty_genesis_data();
149
1
    let genesis_data_2002 = ContainerChainGenesisData {
150
1
        storage: BoundedVec::try_from(vec![(b"key".to_vec(), b"value".to_vec()).into()]).unwrap(),
151
1
        name: Default::default(),
152
1
        id: Default::default(),
153
1
        fork_id: Default::default(),
154
1
        extensions: BoundedVec::try_from(vec![]).unwrap(),
155
1
        properties: Default::default(),
156
1
    };
157
1
    ExtBuilder::default()
158
1
        .with_para_ids(vec![
159
1
            ParaRegistrationParams {
160
1
                para_id: 2001,
161
1
                genesis_data: genesis_data_2001.clone(),
162
1
                block_production_credits: u32::MAX,
163
1
                collator_assignment_credits: u32::MAX,
164
1
                parathread_params: None,
165
1
            },
166
1
            ParaRegistrationParams {
167
1
                para_id: 2002,
168
1
                genesis_data: genesis_data_2002.clone(),
169
1
                block_production_credits: u32::MAX,
170
1
                collator_assignment_credits: u32::MAX,
171
1
                parathread_params: None,
172
1
            },
173
1
        ])
174
1
        .with_next_free_para_id(2003.into())
175
1
        .build()
176
1
        .execute_with(|| {
177
1
            assert_eq!(
178
1
                ContainerRegistrar::registered_para_ids(),
179
1
                vec![2001.into(), 2002.into()]
180
1
            );
181
1
            assert_eq!(Runtime::registered_paras(), vec![2001.into(), 2002.into()]);
182

            
183
1
            assert_eq!(
184
1
                Runtime::genesis_data(2001.into()).as_ref(),
185
1
                Some(&genesis_data_2001)
186
1
            );
187
1
            assert_eq!(
188
1
                Runtime::genesis_data(2002.into()).as_ref(),
189
1
                Some(&genesis_data_2002)
190
1
            );
191
1
            assert_eq!(Runtime::genesis_data(2003.into()).as_ref(), None);
192

            
193
1
            run_to_block(2);
194
1
            assert_ok!(ContainerRegistrar::deregister(root_origin(), 2002.into()), ());
195

            
196
1
            assert_eq!(Runtime::genesis_data(2002.into()).as_ref(), Some(&genesis_data_2002), "Deregistered container chain genesis data should not be removed until after 2 sessions");
197
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
198
1
            assert_ok!(
199
1
                ContainerRegistrar::register(
200
1
                    origin_of(ALICE.into()),
201
1
                    2003.into(),
202
1
                    get_genesis_data_with_validation_code().0,
203
1
                    Some(HeadData(vec![1u8, 1u8, 1u8]))
204
1
                ),
205
1
                ()
206
1
            );
207

            
208
            // Registered container chains are inserted immediately
209
1
            assert_eq!(
210
1
                Runtime::genesis_data(2003.into()).as_ref(),
211
1
                Some(&get_genesis_data_with_validation_code().0)
212
1
            );
213

            
214
            // Deregistered container chain genesis data is removed after 2 sessions
215
1
            run_to_session(2u32);
216
1
            assert_eq!(Runtime::genesis_data(2002.into()).as_ref(), None);
217
1
        });
218
1
}
219

            
220
#[test]
221
1
fn test_configuration_on_session_change() {
222
1
    ExtBuilder::default().build().execute_with(|| {
223
1
        assert_eq!(CollatorConfiguration::config().max_collators, 100);
224
1
        assert_eq!(
225
1
            CollatorConfiguration::config().min_orchestrator_collators,
226
1
            2
227
1
        );
228
1
        assert_eq!(CollatorConfiguration::config().collators_per_container, 2);
229

            
230
1
        assert_ok!(
231
1
            CollatorConfiguration::set_max_collators(root_origin(), 50),
232
1
            ()
233
1
        );
234
1
        run_to_session(1u32);
235
1

            
236
1
        assert_ok!(
237
1
            CollatorConfiguration::set_min_orchestrator_collators(root_origin(), 20),
238
1
            ()
239
1
        );
240
1
        assert_eq!(CollatorConfiguration::config().max_collators, 100);
241
1
        assert_eq!(
242
1
            CollatorConfiguration::config().min_orchestrator_collators,
243
1
            2
244
1
        );
245
1
        assert_eq!(CollatorConfiguration::config().collators_per_container, 2);
246

            
247
1
        run_to_session(2u32);
248
1
        assert_ok!(
249
1
            CollatorConfiguration::set_collators_per_container(root_origin(), 10),
250
1
            ()
251
1
        );
252
1
        assert_eq!(CollatorConfiguration::config().max_collators, 50);
253
1
        assert_eq!(
254
1
            CollatorConfiguration::config().min_orchestrator_collators,
255
1
            2
256
1
        );
257
1
        assert_eq!(CollatorConfiguration::config().collators_per_container, 2);
258

            
259
1
        run_to_session(3u32);
260
1

            
261
1
        assert_eq!(CollatorConfiguration::config().max_collators, 50);
262
1
        assert_eq!(
263
1
            CollatorConfiguration::config().min_orchestrator_collators,
264
1
            20
265
1
        );
266
1
        assert_eq!(CollatorConfiguration::config().collators_per_container, 2);
267

            
268
1
        run_to_session(4u32);
269
1

            
270
1
        assert_eq!(CollatorConfiguration::config().max_collators, 50);
271
1
        assert_eq!(
272
1
            CollatorConfiguration::config().min_orchestrator_collators,
273
1
            20
274
1
        );
275
1
        assert_eq!(CollatorConfiguration::config().collators_per_container, 10);
276
1
    });
277
1
}
278

            
279
#[test]
280
1
fn test_cannot_mark_valid_para_with_no_bootnodes() {
281
1
    ExtBuilder::default()
282
1
        .with_balances(vec![
283
1
            // Alice gets 10k extra tokens for her mapping deposit
284
1
            (AccountId::from(ALICE), 210_000 * UNIT),
285
1
            (AccountId::from(BOB), 100_000 * UNIT),
286
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
287
1
            (AccountId::from(DAVE), 100_000 * UNIT),
288
1
        ])
289
1
        .with_collators(vec![
290
1
            (AccountId::from(ALICE), 210 * UNIT),
291
1
            (AccountId::from(BOB), 100 * UNIT),
292
1
            (AccountId::from(CHARLIE), 100 * UNIT),
293
1
            (AccountId::from(DAVE), 100 * UNIT),
294
1
        ])
295
1
        .build()
296
1
        .execute_with(|| {
297
1
            run_to_block(2);
298
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
299
1
            assert_ok!(ContainerRegistrar::register(
300
1
                origin_of(ALICE.into()),
301
1
                2000.into(),
302
1
                get_genesis_data_with_validation_code().0,
303
1
                Some(HeadData(vec![1u8, 1u8, 1u8]))
304
1
            ));
305
1
            assert_noop!(
306
1
                ContainerRegistrar::mark_valid_for_collating(root_origin(), 2000.into()),
307
1
                pallet_data_preservers::Error::<Runtime>::NoBootNodes,
308
1
            );
309
1
        });
310
1
}
311

            
312
#[test]
313
1
fn test_container_deregister_unassign_data_preserver() {
314
1
    ExtBuilder::default()
315
1
        .with_balances(vec![
316
1
            (AccountId::from(ALICE), 210_000 * UNIT),
317
1
            (AccountId::from(BOB), 100_000 * UNIT),
318
1
        ])
319
1
        .build()
320
1
        .execute_with(|| {
321
            use pallet_data_preservers::{
322
                AssignerParameterOf, ParaIdsFilter, Profile, ProfileMode, ProviderRequestOf,
323
            };
324

            
325
1
            let profile = Profile {
326
1
                url: b"test".to_vec().try_into().unwrap(),
327
1
                para_ids: ParaIdsFilter::AnyParaId,
328
1
                mode: ProfileMode::Bootnode,
329
1
                assignment_request: ProviderRequestOf::<Runtime>::Free,
330
1
            };
331
1

            
332
1
            let para_id = ParaId::from(2000);
333
1
            let profile_id = 0u64;
334
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
335
1
            assert_ok!(ContainerRegistrar::register(
336
1
                origin_of(ALICE.into()),
337
1
                para_id,
338
1
                get_genesis_data_with_validation_code().0,
339
1
                Some(HeadData(vec![1u8, 1u8, 1u8]))
340
1
            ));
341

            
342
1
            assert_ok!(DataPreservers::create_profile(
343
1
                origin_of(BOB.into()),
344
1
                profile.clone(),
345
1
            ));
346

            
347
            // Start assignment
348
1
            assert_ok!(DataPreservers::start_assignment(
349
1
                origin_of(ALICE.into()),
350
1
                profile_id,
351
1
                para_id,
352
1
                AssignerParameterOf::<Runtime>::Free
353
1
            ));
354
1
            assert!(pallet_data_preservers::Assignments::<Runtime>::get(para_id).contains(&0u64));
355

            
356
            // Deregister from Registrar
357
1
            assert_ok!(ContainerRegistrar::deregister(root_origin(), para_id), ());
358

            
359
            // Check DataPreserver assignment has been cleared
360
1
            assert!(pallet_data_preservers::Assignments::<Runtime>::get(para_id).is_empty());
361
1
        });
362
1
}
363

            
364
#[test]
365
1
fn stream_payment_works() {
366
1
    ExtBuilder::default()
367
1
        .with_balances(vec![
368
1
            (AccountId::from(ALICE), 100_000 * UNIT),
369
1
            (AccountId::from(BOB), 100_000 * UNIT),
370
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
371
1
        ])
372
1
        .with_collators(vec![
373
1
            (AccountId::from(CHARLIE), 100 * UNIT),
374
1
            (AccountId::from(DAVE), 100 * UNIT),
375
1
        ])
376
1
        .build()
377
1
        .execute_with(|| {
378
            use pallet_stream_payment::{ChangeKind, StreamConfig};
379

            
380
1
            assert_ok!(StreamPayment::open_stream(
381
1
                origin_of(ALICE.into()),
382
1
                BOB.into(),
383
1
                StreamConfig {
384
1
                    rate: 2 * UNIT,
385
1
                    asset_id: StreamPaymentAssetId::Native,
386
1
                    time_unit: StreamPaymentTimeUnit::BlockNumber,
387
1
                    minimum_request_deadline_delay: 0,
388
1
                    soft_minimum_deposit: 0,
389
1
                },
390
1
                1_000 * UNIT,
391
1
            ));
392

            
393
1
            run_block();
394
1

            
395
1
            assert_ok!(StreamPayment::perform_payment(origin_of(CHARLIE.into()), 0));
396
1
            assert_eq!(
397
1
                Balances::free_balance(AccountId::from(BOB)),
398
1
                100_000 * UNIT + 2 * UNIT
399
1
            );
400

            
401
1
            assert_ok!(StreamPayment::request_change(
402
1
                origin_of(ALICE.into()),
403
1
                0,
404
1
                ChangeKind::Suggestion,
405
1
                StreamConfig {
406
1
                    rate: 1 * UNIT,
407
1
                    asset_id: StreamPaymentAssetId::Native,
408
1
                    time_unit: StreamPaymentTimeUnit::BlockNumber,
409
1
                    minimum_request_deadline_delay: 0,
410
1
                    soft_minimum_deposit: 0,
411
1
                },
412
1
                None,
413
1
            ));
414

            
415
1
            assert_ok!(StreamPayment::accept_requested_change(
416
1
                origin_of(BOB.into()),
417
1
                0,
418
1
                1, // nonce
419
1
                None,
420
1
            ));
421

            
422
1
            run_block();
423
1

            
424
1
            assert_ok!(StreamPayment::close_stream(origin_of(BOB.into()), 0));
425

            
426
1
            assert_eq!(
427
1
                Balances::free_balance(AccountId::from(BOB)),
428
1
                100_000 * UNIT + 3 * UNIT
429
1
            );
430
1
            assert_eq!(
431
1
                Balances::free_balance(AccountId::from(ALICE)),
432
1
                100_000 * UNIT - 3 * UNIT
433
1
            );
434
1
        });
435
1
}
436

            
437
#[test]
438
1
fn test_data_preserver_with_stream_payment() {
439
1
    ExtBuilder::default()
440
1
        .with_balances(vec![
441
1
            (AccountId::from(ALICE), 210_000 * UNIT),
442
1
            (AccountId::from(BOB), 100_000 * UNIT),
443
1
        ])
444
1
        .build()
445
1
        .execute_with(|| {
446
            use pallet_data_preservers::{
447
                AssignerParameterOf, ParaIdsFilter, Profile, ProfileMode, ProviderRequestOf,
448
            };
449

            
450
1
            let profile = Profile {
451
1
                url: b"test".to_vec().try_into().unwrap(),
452
1
                para_ids: ParaIdsFilter::AnyParaId,
453
1
                mode: ProfileMode::Bootnode,
454
1
                assignment_request: ProviderRequestOf::<Runtime>::StreamPayment {
455
1
                    config: StreamConfig {
456
1
                        time_unit: StreamPaymentTimeUnit::BlockNumber,
457
1
                        asset_id: StreamPaymentAssetId::Native,
458
1
                        rate: 42,
459
1
                        minimum_request_deadline_delay: 0,
460
1
                        soft_minimum_deposit: 0,
461
1
                    },
462
1
                },
463
1
            };
464
1

            
465
1
            let para_id = ParaId::from(2000);
466
1
            let profile_id = 0u64;
467
1

            
468
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
469
1
            assert_ok!(ContainerRegistrar::register(
470
1
                origin_of(ALICE.into()),
471
1
                para_id,
472
1
                get_genesis_data_with_validation_code().0,
473
1
                Some(HeadData(vec![1u8, 1u8, 1u8]))
474
1
            ));
475

            
476
1
            assert_ok!(DataPreservers::create_profile(
477
1
                origin_of(BOB.into()),
478
1
                profile.clone(),
479
1
            ));
480

            
481
            // Start assignment
482
1
            assert_ok!(DataPreservers::start_assignment(
483
1
                origin_of(ALICE.into()),
484
1
                profile_id,
485
1
                para_id,
486
1
                AssignerParameterOf::<Runtime>::StreamPayment {
487
1
                    initial_deposit: 1_000
488
1
                }
489
1
            ));
490
1
            assert!(
491
1
                pallet_data_preservers::Assignments::<Runtime>::get(para_id).contains(&profile_id)
492
1
            );
493
1
            let profile = pallet_data_preservers::Profiles::<Runtime>::get(&profile_id)
494
1
                .expect("profile to exists");
495
1
            let (assigned_para_id, witness) = profile.assignment.expect("profile to be assigned");
496
1
            assert_eq!(assigned_para_id, para_id);
497
1
            assert_eq!(
498
1
                witness,
499
1
                tp_data_preservers_common::AssignmentWitness::StreamPayment { stream_id: 0 }
500
1
            );
501
1
        });
502
1
}
503

            
504
#[test]
505
1
fn test_data_preserver_kind_needs_to_match() {
506
1
    ExtBuilder::default()
507
1
        .with_balances(vec![
508
1
            (AccountId::from(ALICE), 210_000 * UNIT),
509
1
            (AccountId::from(BOB), 100_000 * UNIT),
510
1
        ])
511
1
        .build()
512
1
        .execute_with(|| {
513
            use pallet_data_preservers::{
514
                AssignerParameterOf, ParaIdsFilter, Profile, ProfileMode, ProviderRequestOf,
515
            };
516

            
517
1
            let profile = Profile {
518
1
                url: b"test".to_vec().try_into().unwrap(),
519
1
                para_ids: ParaIdsFilter::AnyParaId,
520
1
                mode: ProfileMode::Bootnode,
521
1
                assignment_request: ProviderRequestOf::<Runtime>::Free,
522
1
            };
523
1

            
524
1
            let para_id = ParaId::from(2000);
525
1
            let profile_id = 0u64;
526
1

            
527
1
            assert_ok!(Registrar::reserve(origin_of(ALICE.into())));
528
1
            assert_ok!(ContainerRegistrar::register(
529
1
                origin_of(ALICE.into()),
530
1
                para_id,
531
1
                get_genesis_data_with_validation_code().0,
532
1
                Some(HeadData(vec![1u8, 1u8, 1u8]))
533
1
            ));
534

            
535
1
            assert_ok!(DataPreservers::create_profile(
536
1
                origin_of(BOB.into()),
537
1
                profile.clone(),
538
1
            ));
539

            
540
            // Start assignment
541
1
            assert_err!(
542
1
                DataPreservers::start_assignment(
543
1
                    origin_of(ALICE.into()),
544
1
                    profile_id,
545
1
                    para_id,
546
1
                    AssignerParameterOf::<Runtime>::StreamPayment {
547
1
                        initial_deposit: 1_000
548
1
                    }
549
1
                ),
550
1
                pallet_data_preservers::Error::<Runtime>::AssignmentPaymentRequestParameterMismatch
551
1
            );
552
1
        });
553
1
}
554

            
555
#[test]
556
1
fn test_registrar_extrinsic_permissions() {
557
1
    ExtBuilder::default()
558
1
        .with_balances(vec![
559
1
            (AccountId::from(ALICE), 210_000 * UNIT),
560
1
            (AccountId::from(BOB), 100_000 * UNIT),
561
1
        ])
562
1
        .with_empty_parachains(vec![1001])
563
1
        .build()
564
1
        .execute_with(|| {
565
1
            let para_id = ParaId::from(1001);
566
1

            
567
1
            // Pause container chain should fail if not para manager
568
1
            assert_noop!(
569
1
                ContainerRegistrar::pause_container_chain(origin_of(BOB.into()), para_id),
570
1
                BadOrigin
571
1
            );
572

            
573
            // Set Bob as para manager
574
1
            assert_ok!(ContainerRegistrar::set_para_manager(
575
1
                root_origin(),
576
1
                para_id,
577
1
                AccountId::from(BOB)
578
1
            ));
579

            
580
            // Pause container chain should succeed if para manager
581
            // TODO: Revert later after allowed this operation to para manager
582
            // assert_ok!(
583
            //     ContainerRegistrar::pause_container_chain(origin_of(BOB.into()), para_id),
584
            //     ()
585
            // );
586
1
        });
587
1
}
588

            
589
#[test]
590
1
fn stream_payment_stored_profile_correct_size() {
591
    use crate::OPEN_STREAM_HOLD_AMOUNT;
592
    use pallet_stream_payment::{
593
        ChangeKind, ChangeRequest, DepositChange, Party, Stream, StreamConfig, StreamOf,
594
    };
595
    use parity_scale_codec::Encode;
596

            
597
1
    let stream: StreamOf<Runtime> = Stream {
598
1
        source: ALICE.into(),
599
1
        target: BOB.into(),
600
1
        config: StreamConfig {
601
1
            time_unit: StreamPaymentTimeUnit::Timestamp,
602
1
            asset_id: StreamPaymentAssetId::Native,
603
1
            rate: 41,
604
1
            minimum_request_deadline_delay: 0,
605
1
            soft_minimum_deposit: 0,
606
1
        },
607
1
        deposit: 42,
608
1
        last_time_updated: 43,
609
1
        request_nonce: 44,
610
1
        pending_request: Some(ChangeRequest {
611
1
            requester: Party::Source,
612
1
            kind: ChangeKind::Mandatory { deadline: 45 },
613
1
            new_config: StreamConfig {
614
1
                time_unit: StreamPaymentTimeUnit::BlockNumber,
615
1
                asset_id: StreamPaymentAssetId::Native,
616
1
                rate: 46,
617
1
                minimum_request_deadline_delay: 0,
618
1
                soft_minimum_deposit: 0,
619
1
            },
620
1
            deposit_change: Some(DepositChange::Absolute(47)),
621
1
        }),
622
1
        opening_deposit: 48,
623
1
    };
624
1
    let size = stream.encoded_size();
625
1
    assert_eq!(
626
        size, OPEN_STREAM_HOLD_AMOUNT as usize,
627
        "encoded len doesn't match size configured for hold"
628
    );
629
1
}
630

            
631
#[test]
632
1
fn test_register_eth_foreign_asset() {
633
1
    ExtBuilder::default()
634
1
        .with_balances(vec![
635
1
            (AccountId::from(ALICE), 210_000 * UNIT),
636
1
            (AccountId::from(BOB), 100_000 * UNIT),
637
1
        ])
638
1
        .build()
639
1
        .execute_with(|| {
640
1
            let asset_location = Location {
641
1
                parents: 0,
642
1
                interior: X2([
643
1
                    GlobalConsensus(NetworkId::Ethereum { chain_id: 1 }),
644
1
                    AccountKey20 {
645
1
                        network: Some(NetworkId::Ethereum { chain_id: 1 }),
646
1
                        key: [0; 20],
647
1
                    },
648
1
                ]
649
1
                .into()),
650
1
            };
651
1

            
652
1
            let asset_id = 42u16;
653
1

            
654
1
            assert_ok!(ForeignAssetsCreator::create_foreign_asset(
655
1
                root_origin(),
656
1
                asset_location,
657
1
                asset_id,
658
1
                AccountId::from(BOB),
659
1
                true,
660
1
                1
661
1
            ));
662
1
        });
663
1
}
664

            
665
#[test]
666
1
fn test_register_eth_foreign_asset_not_root_should_fail() {
667
1
    ExtBuilder::default()
668
1
        .with_balances(vec![
669
1
            (AccountId::from(ALICE), 210_000 * UNIT),
670
1
            (AccountId::from(BOB), 100_000 * UNIT),
671
1
        ])
672
1
        .build()
673
1
        .execute_with(|| {
674
1
            let asset_location = Location {
675
1
                parents: 0,
676
1
                interior: X2([
677
1
                    GlobalConsensus(NetworkId::Ethereum { chain_id: 1 }),
678
1
                    AccountKey20 {
679
1
                        network: Some(NetworkId::Ethereum { chain_id: 1 }),
680
1
                        key: [0; 20],
681
1
                    },
682
1
                ]
683
1
                .into()),
684
1
            };
685
1

            
686
1
            let asset_id = 42u16;
687
1

            
688
1
            assert_noop!(
689
1
                ForeignAssetsCreator::create_foreign_asset(
690
1
                    origin_of(AccountId::from(ALICE)),
691
1
                    asset_location,
692
1
                    asset_id,
693
1
                    AccountId::from(BOB),
694
1
                    true,
695
1
                    1
696
1
                ),
697
1
                BadOrigin
698
1
            );
699
1
        });
700
1
}
701

            
702
#[test]
703
1
fn test_register_same_eth_foreign_asset_twice_should_fail() {
704
1
    ExtBuilder::default()
705
1
        .with_balances(vec![
706
1
            (AccountId::from(ALICE), 210_000 * UNIT),
707
1
            (AccountId::from(BOB), 100_000 * UNIT),
708
1
        ])
709
1
        .build()
710
1
        .execute_with(|| {
711
1
            let asset_location = Location {
712
1
                parents: 0,
713
1
                interior: X2([
714
1
                    GlobalConsensus(NetworkId::Ethereum { chain_id: 1 }),
715
1
                    AccountKey20 {
716
1
                        network: Some(NetworkId::Ethereum { chain_id: 1 }),
717
1
                        key: [0; 20],
718
1
                    },
719
1
                ]
720
1
                .into()),
721
1
            };
722
1

            
723
1
            let asset_id = 42u16;
724
1

            
725
1
            assert_ok!(ForeignAssetsCreator::create_foreign_asset(
726
1
                root_origin(),
727
1
                asset_location,
728
1
                asset_id,
729
1
                AccountId::from(BOB),
730
1
                true,
731
1
                1
732
1
            ));
733

            
734
1
            let asset_location = Location {
735
1
                parents: 0,
736
1
                interior: X2([
737
1
                    GlobalConsensus(NetworkId::Ethereum { chain_id: 1 }),
738
1
                    AccountKey20 {
739
1
                        network: Some(NetworkId::Ethereum { chain_id: 1 }),
740
1
                        key: [1; 20],
741
1
                    },
742
1
                ]
743
1
                .into()),
744
1
            };
745
1

            
746
1
            assert_noop!(
747
1
                ForeignAssetsCreator::create_foreign_asset(
748
1
                    root_origin(),
749
1
                    asset_location,
750
1
                    asset_id,
751
1
                    AccountId::from(BOB),
752
1
                    true,
753
1
                    1
754
1
                ),
755
1
                pallet_foreign_asset_creator::Error::<Runtime>::AssetAlreadyExists
756
1
            );
757
1
        });
758
1
}