Newer
Older
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg(test)]
use crate::{core_mask::*, mock::*, *};
use frame_support::{
assert_noop, assert_ok,
traits::nonfungible::{Inspect as NftInspect, Mutate, Transfer},
BoundedVec,
};
use frame_system::RawOrigin::Root;
Bastian Köcher
committed
use pretty_assertions::assert_eq;
use sp_runtime::{traits::Get, TokenError};
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use CoreAssignment::*;
use CoretimeTraceItem::*;
use Finality::*;
#[test]
fn basic_initialize_works() {
TestExt::new().execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
assert_eq!(CoretimeTrace::get(), vec![]);
assert_eq!(Broker::current_timeslice(), 0);
});
}
#[test]
fn drop_region_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, Some(1), 1001, Provisional));
advance_to(11);
assert_noop!(Broker::do_drop_region(region), Error::<Test>::StillValid);
advance_to(12);
// assignment worked.
let just_1001 = vec![(Task(1001), 57600)];
let just_pool = vec![(Pool, 57600)];
assert_eq!(
CoretimeTrace::get(),
vec![
(6, AssignCore { core: 0, begin: 8, assignment: just_1001, end_hint: None }),
(12, AssignCore { core: 0, begin: 14, assignment: just_pool, end_hint: None }),
]
);
// `region` still exists as it was never finalized.
assert_eq!(Regions::<Test>::iter().count(), 1);
assert_ok!(Broker::do_drop_region(region));
assert_eq!(Regions::<Test>::iter().count(), 0);
assert_noop!(Broker::do_drop_region(region), Error::<Test>::UnknownRegion);
});
}
#[test]
fn drop_renewal_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, Some(1), 1001, Final));
advance_to(11);
let e = Error::<Test>::StillValid;
assert_noop!(Broker::do_drop_renewal(region.core, region.begin + 3), e);
advance_to(12);
assert_eq!(AllowedRenewals::<Test>::iter().count(), 1);
assert_ok!(Broker::do_drop_renewal(region.core, region.begin + 3));
assert_eq!(AllowedRenewals::<Test>::iter().count(), 0);
let e = Error::<Test>::UnknownRenewal;
assert_noop!(Broker::do_drop_renewal(region.core, region.begin + 3), e);
});
}
#[test]
fn drop_contribution_works() {
TestExt::new().contribution_timeout(3).endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
// Place region in pool. Active in pool timeslices 4, 5, 6 = rcblocks 8, 10, 12; we
// expect the contribution record to timeout 3 timeslices following 7 = 14
//
// Due to the contribution_timeout being configured for 3 timeslices, the contribution
// can only be discarded at timeslice 10, i.e. rcblock 20.
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
assert_ok!(Broker::do_pool(region, Some(1), 1, Final));
assert_eq!(InstaPoolContribution::<Test>::iter().count(), 1);
advance_to(19);
assert_noop!(Broker::do_drop_contribution(region), Error::<Test>::StillValid);
advance_to(20);
assert_ok!(Broker::do_drop_contribution(region));
assert_eq!(InstaPoolContribution::<Test>::iter().count(), 0);
assert_noop!(Broker::do_drop_contribution(region), Error::<Test>::UnknownContribution);
});
}
#[test]
fn drop_history_works() {
TestExt::new()
.contribution_timeout(4)
.endow(1, 1000)
.endow(2, 30)
.execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let mut region = Broker::do_purchase(1, u64::max_value()).unwrap();
// Place region in pool. Active in pool timeslices 4, 5, 6 = rcblocks 8, 10, 12; we
// expect to make/receive revenue reports on blocks 10, 12, 14.
assert_ok!(Broker::do_pool(region, Some(1), 1, Final));
assert_ok!(Broker::do_purchase_credit(2, 30, 2));
advance_to(6);
// In the stable state with no pending payouts, we expect to see 3 items in
// InstaPoolHistory here since there is a latency of 1 timeslice (for generating the
// revenue report), the forward notice period (equivalent to another timeslice) and a
// block between the revenue report being requested and the response being processed.
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 3);
advance_to(7);
// One block later, the most recent report will have been processed, so the effective
// queue drops to 2 items.
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 2);
advance_to(8);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 3);
assert_ok!(TestCoretimeProvider::spend_instantaneous(2, 10));
advance_to(10);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 3);
assert_ok!(TestCoretimeProvider::spend_instantaneous(2, 10));
advance_to(12);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 4);
assert_ok!(TestCoretimeProvider::spend_instantaneous(2, 10));
advance_to(14);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 5);
advance_to(16);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 6);
advance_to(17);
assert_noop!(Broker::do_drop_history(u32::MAX), Error::<Test>::StillValid);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::StillValid);
advance_to(18);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 6);
// Block 18 is 8 blocks ()= 4 timeslices = contribution timeout) after first region.
// Its revenue should now be droppable.
assert_ok!(Broker::do_drop_history(region.begin));
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 5);
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::NoHistory);
advance_to(19);
region.begin += 1;
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::StillValid);
advance_to(20);
assert_ok!(Broker::do_drop_history(region.begin));
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 4);
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::NoHistory);
advance_to(21);
region.begin += 1;
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::StillValid);
advance_to(22);
assert_ok!(Broker::do_drop_history(region.begin));
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 3);
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::NoHistory);
});
}
#[test]
fn request_core_count_works() {
TestExt::new().execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 0));
assert_ok!(Broker::request_core_count(RuntimeOrigin::root(), 1));
advance_to(12);
let assignment = vec![(Pool, 57600)];
assert_eq!(
CoretimeTrace::get(),
vec![(12, AssignCore { core: 0, begin: 14, assignment, end_hint: None })],
);
});
}
#[test]
fn transfer_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(<Broker as Transfer<_>>::transfer(®ion.into(), &2));
assert_eq!(<Broker as NftInspect<_>>::owner(®ion.into()), Some(2));
assert_noop!(Broker::do_assign(region, Some(1), 1001, Final), Error::<Test>::NotOwner);
assert_ok!(Broker::do_assign(region, Some(2), 1002, Final));
});
}
fn mutate_operations_work() {
TestExt::new().endow(1, 1000).execute_with(|| {
let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
assert_noop!(
<Broker as Mutate<_>>::mint_into(®ion_id.into(), &2),
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region_id = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_noop!(
<Broker as Mutate<_>>::mint_into(®ion_id.into(), &2),
Error::<Test>::NotAllowed
);
assert_noop!(
<Broker as Mutate<_>>::burn(®ion_id.into(), Some(&2)),
Error::<Test>::NotOwner
);
// 'withdraw' the region from user 1:
assert_ok!(<Broker as Mutate<_>>::burn(®ion_id.into(), Some(&1)));
assert_eq!(Regions::<Test>::get(region_id).unwrap().owner, None);
// `mint_into` works after burning:
assert_ok!(<Broker as Mutate<_>>::mint_into(®ion_id.into(), &2));
assert_eq!(Regions::<Test>::get(region_id).unwrap().owner, Some(2));
// Unsupported operations:
assert_noop!(
<Broker as Mutate<_>>::set_attribute(®ion_id.into(), &[], &[]),
TokenError::Unsupported
);
assert_noop!(
<Broker as Mutate<_>>::set_typed_attribute::<u8, u8>(®ion_id.into(), &0, &0),
TokenError::Unsupported
);
});
}
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#[test]
fn permanent_is_not_reassignable() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, Some(1), 1001, Final));
assert_noop!(Broker::do_assign(region, Some(1), 1002, Final), Error::<Test>::UnknownRegion);
assert_noop!(Broker::do_pool(region, Some(1), 1002, Final), Error::<Test>::UnknownRegion);
assert_noop!(Broker::do_partition(region, Some(1), 1), Error::<Test>::UnknownRegion);
assert_noop!(
Broker::do_interlace(region, Some(1), CoreMask::from_chunk(0, 40)),
Error::<Test>::UnknownRegion
);
});
}
#[test]
fn provisional_is_reassignable() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, Some(1), 1001, Provisional));
let (region1, region) = Broker::do_partition(region, Some(1), 1).unwrap();
let (region2, region3) =
Broker::do_interlace(region, Some(1), CoreMask::from_chunk(0, 40)).unwrap();
assert_ok!(Broker::do_pool(region1, Some(1), 1, Provisional));
assert_ok!(Broker::do_assign(region2, Some(1), 1002, Provisional));
assert_ok!(Broker::do_assign(region3, Some(1), 1003, Provisional));
advance_to(8);
assert_eq!(
CoretimeTrace::get(),
vec![
(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Pool, 57600),],
end_hint: None
}
),
(
8,
AssignCore {
core: 0,
begin: 10,
assignment: vec![(Task(1002), 28800), (Task(1003), 28800),],
end_hint: None
}
),
]
);
});
}
#[test]
fn nft_metadata_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_eq!(attribute::<Timeslice>(region, b"begin"), 4);
assert_eq!(attribute::<Timeslice>(region, b"length"), 3);
assert_eq!(attribute::<Timeslice>(region, b"end"), 7);
assert_eq!(attribute::<Option<u64>>(region, b"owner"), Some(1));
assert_eq!(attribute::<CoreMask>(region, b"part"), 0xfffff_fffff_fffff_fffff.into());
assert_eq!(attribute::<CoreIndex>(region, b"core"), 0);
assert_eq!(attribute::<Option<u64>>(region, b"paid"), Some(100));
assert_ok!(Broker::do_transfer(region, None, 42));
let (_, region) = Broker::do_partition(region, None, 2).unwrap();
let (region, _) =
Broker::do_interlace(region, None, 0x00000_fffff_fffff_00000.into()).unwrap();
assert_eq!(attribute::<Timeslice>(region, b"begin"), 6);
assert_eq!(attribute::<Timeslice>(region, b"length"), 1);
assert_eq!(attribute::<Timeslice>(region, b"end"), 7);
assert_eq!(attribute::<Option<u64>>(region, b"owner"), Some(42));
assert_eq!(attribute::<CoreMask>(region, b"part"), 0x00000_fffff_fffff_00000.into());
assert_eq!(attribute::<CoreIndex>(region, b"core"), 0);
assert_eq!(attribute::<Option<u64>>(region, b"paid"), None);
});
}
#[test]
fn migration_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_set_lease(1000, 8));
Bastian Köcher
committed
assert_ok!(Broker::do_start_sales(100, 1));
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Sale is for regions from TS4..7
// Not ending in this sale period.
assert_noop!(Broker::do_renew(1, 0), Error::<Test>::NotAllowed);
advance_to(12);
// Sale is now for regions from TS10..13
// Ending in this sale period.
// Should now be renewable.
assert_ok!(Broker::do_renew(1, 0));
assert_eq!(balance(1), 900);
advance_to(18);
let just_pool = || vec![(Pool, 57600)];
let just_1000 = || vec![(Task(1000), 57600)];
assert_eq!(
CoretimeTrace::get(),
vec![
(6, AssignCore { core: 0, begin: 8, assignment: just_1000(), end_hint: None }),
(6, AssignCore { core: 1, begin: 8, assignment: just_pool(), end_hint: None }),
(12, AssignCore { core: 0, begin: 14, assignment: just_1000(), end_hint: None }),
(12, AssignCore { core: 1, begin: 14, assignment: just_pool(), end_hint: None }),
(18, AssignCore { core: 0, begin: 20, assignment: just_1000(), end_hint: None }),
(18, AssignCore { core: 1, begin: 20, assignment: just_pool(), end_hint: None }),
]
);
});
}
#[test]
fn renewal_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_eq!(balance(1), 900);
assert_ok!(Broker::do_assign(region, None, 1001, Final));
// Should now be renewable.
advance_to(6);
assert_noop!(Broker::do_purchase(1, u64::max_value()), Error::<Test>::TooEarly);
let core = Broker::do_renew(1, region.core).unwrap();
assert_eq!(balance(1), 800);
advance_to(8);
assert_noop!(Broker::do_purchase(1, u64::max_value()), Error::<Test>::SoldOut);
advance_to(12);
assert_ok!(Broker::do_renew(1, core));
assert_eq!(balance(1), 690);
});
}
#[test]
fn instapool_payouts_work() {
TestExt::new().endow(1, 1000).execute_with(|| {
let item = ScheduleItem { assignment: Pool, mask: CoreMask::complete() };
assert_ok!(Broker::do_reserve(Schedule::truncate_from(vec![item])));
Bastian Köcher
committed
assert_ok!(Broker::do_start_sales(100, 2));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_pool(region, None, 2, Final));
assert_ok!(Broker::do_purchase_credit(1, 20, 1));
advance_to(8);
assert_ok!(TestCoretimeProvider::spend_instantaneous(1, 10));
advance_to(11);
assert_eq!(pot(), 14);
assert_eq!(revenue(), 106);
// Cannot claim for 0 timeslices.
assert_noop!(Broker::do_claim_revenue(region, 0), Error::<Test>::NoClaimTimeslices);
// Revenue can be claimed.
assert_ok!(Broker::do_claim_revenue(region, 100));
assert_eq!(pot(), 10);
assert_eq!(balance(2), 4);
});
}
#[test]
fn instapool_partial_core_payouts_work() {
TestExt::new().endow(1, 1000).execute_with(|| {
let item = ScheduleItem { assignment: Pool, mask: CoreMask::complete() };
assert_ok!(Broker::do_reserve(Schedule::truncate_from(vec![item])));
Bastian Köcher
committed
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
let (region1, region2) =
Broker::do_interlace(region, None, CoreMask::from_chunk(0, 20)).unwrap();
assert_ok!(Broker::do_pool(region1, None, 2, Final));
assert_ok!(Broker::do_pool(region2, None, 3, Final));
assert_ok!(Broker::do_purchase_credit(1, 40, 1));
advance_to(8);
assert_ok!(TestCoretimeProvider::spend_instantaneous(1, 40));
advance_to(11);
assert_ok!(Broker::do_claim_revenue(region1, 100));
assert_ok!(Broker::do_claim_revenue(region2, 100));
assert_eq!(revenue(), 120);
assert_eq!(balance(2), 5);
assert_eq!(balance(3), 15);
assert_eq!(pot(), 0);
});
}
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#[test]
fn instapool_core_payouts_work_with_partitioned_region() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
let (region1, region2) = Broker::do_partition(region, None, 2).unwrap();
// `region1` duration is from rcblock 8 to rcblock 12. This means that the
// coretime purchased during this time period will be purchased from `region1`
//
// `region2` duration is from rcblock 12 to rcblock 14 and during this period
// coretime will be purchased from `region2`.
assert_ok!(Broker::do_pool(region1, None, 2, Final));
assert_ok!(Broker::do_pool(region2, None, 3, Final));
assert_ok!(Broker::do_purchase_credit(1, 20, 1));
advance_to(8);
assert_ok!(TestCoretimeProvider::spend_instantaneous(1, 10));
advance_to(11);
assert_eq!(pot(), 20);
assert_eq!(revenue(), 100);
assert_ok!(Broker::do_claim_revenue(region1, 100));
assert_eq!(pot(), 10);
assert_eq!(balance(2), 10);
advance_to(12);
assert_ok!(TestCoretimeProvider::spend_instantaneous(1, 10));
advance_to(15);
assert_eq!(pot(), 10);
assert_ok!(Broker::do_claim_revenue(region2, 100));
assert_eq!(pot(), 0);
// The balance of account `2` remains unchanged.
assert_eq!(balance(2), 10);
assert_eq!(balance(3), 10);
});
}
#[test]
fn initialize_with_system_paras_works() {
TestExt::new().execute_with(|| {
let item = ScheduleItem { assignment: Task(1u32), mask: CoreMask::complete() };
assert_ok!(Broker::do_reserve(Schedule::truncate_from(vec![item])));
let items = vec![
ScheduleItem { assignment: Task(2u32), mask: 0xfffff_fffff_00000_00000.into() },
ScheduleItem { assignment: Task(3u32), mask: 0x00000_00000_fffff_00000.into() },
ScheduleItem { assignment: Task(4u32), mask: 0x00000_00000_00000_fffff.into() },
];
assert_ok!(Broker::do_reserve(Schedule::truncate_from(items)));
Bastian Köcher
committed
assert_ok!(Broker::do_start_sales(100, 0));
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
advance_to(10);
assert_eq!(
CoretimeTrace::get(),
vec![
(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1), 57600),],
end_hint: None
}
),
(
6,
AssignCore {
core: 1,
begin: 8,
assignment: vec![(Task(2), 28800), (Task(3), 14400), (Task(4), 14400),],
end_hint: None
}
),
]
);
});
}
#[test]
fn initialize_with_leased_slots_works() {
TestExt::new().execute_with(|| {
assert_ok!(Broker::do_set_lease(1000, 6));
assert_ok!(Broker::do_set_lease(1001, 7));
Bastian Köcher
committed
assert_ok!(Broker::do_start_sales(100, 0));
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
advance_to(18);
let end_hint = None;
assert_eq!(
CoretimeTrace::get(),
vec![
(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1000), 57600),],
end_hint
}
),
(
6,
AssignCore {
core: 1,
begin: 8,
assignment: vec![(Task(1001), 57600),],
end_hint
}
),
(
12,
AssignCore {
core: 0,
begin: 14,
assignment: vec![(Task(1001), 57600),],
end_hint
}
),
(12, AssignCore { core: 1, begin: 14, assignment: vec![(Pool, 57600),], end_hint }),
(18, AssignCore { core: 0, begin: 20, assignment: vec![(Pool, 57600),], end_hint }),
(18, AssignCore { core: 1, begin: 20, assignment: vec![(Pool, 57600),], end_hint }),
]
);
});
}
#[test]
fn purchase_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, None, 1000, Final));
advance_to(6);
assert_eq!(
CoretimeTrace::get(),
vec![(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1000), 57600),],
end_hint: None
}
),]
);
});
}
#[test]
fn partition_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
let (region1, region) = Broker::do_partition(region, None, 1).unwrap();
let (region2, region3) = Broker::do_partition(region, None, 1).unwrap();
assert_ok!(Broker::do_assign(region1, None, 1001, Final));
assert_ok!(Broker::do_assign(region2, None, 1002, Final));
assert_ok!(Broker::do_assign(region3, None, 1003, Final));
advance_to(10);
assert_eq!(
CoretimeTrace::get(),
vec![
(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1001), 57600),],
end_hint: None
}
),
(
8,
AssignCore {
core: 0,
begin: 10,
assignment: vec![(Task(1002), 57600),],
end_hint: None
}
),
(
10,
AssignCore {
core: 0,
begin: 12,
assignment: vec![(Task(1003), 57600),],
end_hint: None
}
),
]
);
});
}
#[test]
fn interlace_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
let (region1, region) =
Broker::do_interlace(region, None, CoreMask::from_chunk(0, 30)).unwrap();
let (region2, region3) =
Broker::do_interlace(region, None, CoreMask::from_chunk(30, 60)).unwrap();
assert_ok!(Broker::do_assign(region1, None, 1001, Final));
assert_ok!(Broker::do_assign(region2, None, 1002, Final));
assert_ok!(Broker::do_assign(region3, None, 1003, Final));
advance_to(10);
assert_eq!(
CoretimeTrace::get(),
vec![(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1001), 21600), (Task(1002), 21600), (Task(1003), 14400),],
end_hint: None
}
),]
);
});
}
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#[test]
fn cant_assign_unowned_region() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
let (region1, region2) =
Broker::do_interlace(region, Some(1), CoreMask::from_chunk(0, 30)).unwrap();
// Transfer the interlaced region to account 2.
assert_ok!(Broker::do_transfer(region2, Some(1), 2));
// The initial owner should not be able to assign the non-interlaced region, since they have
// just transferred an interlaced part of it to account 2.
assert_noop!(Broker::do_assign(region, Some(1), 1001, Final), Error::<Test>::UnknownRegion);
// Account 1 can assign only the interlaced region that they did not transfer.
assert_ok!(Broker::do_assign(region1, Some(1), 1001, Final));
// Account 2 can assign the region they received.
assert_ok!(Broker::do_assign(region2, Some(2), 1002, Final));
advance_to(10);
assert_eq!(
CoretimeTrace::get(),
vec![(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1001), 21600), (Task(1002), 36000)],
end_hint: None
}
),]
);
});
}
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#[test]
fn interlace_then_partition_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
let (region1, region2) =
Broker::do_interlace(region, None, CoreMask::from_chunk(0, 20)).unwrap();
let (region1, region3) = Broker::do_partition(region1, None, 1).unwrap();
let (region2, region4) = Broker::do_partition(region2, None, 2).unwrap();
assert_ok!(Broker::do_assign(region1, None, 1001, Final));
assert_ok!(Broker::do_assign(region2, None, 1002, Final));
assert_ok!(Broker::do_assign(region3, None, 1003, Final));
assert_ok!(Broker::do_assign(region4, None, 1004, Final));
advance_to(10);
assert_eq!(
CoretimeTrace::get(),
vec![
(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1001), 14400), (Task(1002), 43200),],
end_hint: None
}
),
(
8,
AssignCore {
core: 0,
begin: 10,
assignment: vec![(Task(1002), 43200), (Task(1003), 14400),],
end_hint: None
}
),
(
10,
AssignCore {
core: 0,
begin: 12,
assignment: vec![(Task(1003), 14400), (Task(1004), 43200),],
end_hint: None
}
),
]
);
});
}
#[test]
fn partition_then_interlace_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
let (region1, region2) = Broker::do_partition(region, None, 1).unwrap();
let (region1, region3) =
Broker::do_interlace(region1, None, CoreMask::from_chunk(0, 20)).unwrap();
let (region2, region4) =
Broker::do_interlace(region2, None, CoreMask::from_chunk(0, 30)).unwrap();
assert_ok!(Broker::do_assign(region1, None, 1001, Final));
assert_ok!(Broker::do_assign(region2, None, 1002, Final));
assert_ok!(Broker::do_assign(region3, None, 1003, Final));
assert_ok!(Broker::do_assign(region4, None, 1004, Final));
advance_to(10);
assert_eq!(
CoretimeTrace::get(),
vec![
(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1001), 14400), (Task(1003), 43200),],
end_hint: None
}
),
(
8,
AssignCore {
core: 0,
begin: 10,
assignment: vec![(Task(1002), 21600), (Task(1004), 36000),],
end_hint: None
}
),
]
);
});
}
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#[test]
fn partitioning_after_assignment_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
// We will initially allocate a task to a purchased region, and after that
// we will proceed to partition the region.
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, None, 1001, Provisional));
let (_region, region1) = Broker::do_partition(region, None, 2).unwrap();
// After the partitioning if we assign a new task to `region` the other region
// will still be assigned to `Task(1001)`.
assert_ok!(Broker::do_assign(region1, None, 1002, Provisional));
advance_to(10);
assert_eq!(
CoretimeTrace::get(),
vec![
(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Task(1001), 57600),],
end_hint: None
}
),
(
10,
AssignCore {
core: 0,
begin: 12,
assignment: vec![(Task(1002), 57600),],
end_hint: None
}
),
]
);
});
}
#[test]
fn interlacing_after_assignment_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
// We will initially allocate a task to a purchased region, and after that
// we will proceed to interlace the region.
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, None, 1001, Provisional));
let (region1, _region) =
Broker::do_interlace(region, None, CoreMask::from_chunk(0, 40)).unwrap();
// Interlacing the region won't affect the assignment. The entire region will still
// be assigned to `Task(1001)`.
//
// However, after we assign a task to `region1` the `_region` won't be assigned
// to `Task(1001)` anymore. It will become idle.
assert_ok!(Broker::do_assign(region1, None, 1002, Provisional));
advance_to(10);
assert_eq!(
CoretimeTrace::get(),
vec![(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(Idle, 28800), (Task(1002), 28800)],
end_hint: None
}
),]
);
});
}
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#[test]
fn reservations_are_limited() {
TestExt::new().execute_with(|| {
let schedule = Schedule::truncate_from(vec![ScheduleItem {
assignment: Pool,
mask: CoreMask::complete(),
}]);
let max_cores: u32 = <Test as Config>::MaxReservedCores::get();
Reservations::<Test>::put(
BoundedVec::try_from(vec![schedule.clone(); max_cores as usize]).unwrap(),
);
assert_noop!(Broker::do_reserve(schedule), Error::<Test>::TooManyReservations);
});
}
#[test]
fn cannot_unreserve_unknown() {
TestExt::new().execute_with(|| {
let schedule = Schedule::truncate_from(vec![ScheduleItem {
assignment: Pool,
mask: CoreMask::complete(),
}]);
Reservations::<Test>::put(BoundedVec::try_from(vec![schedule.clone(); 1usize]).unwrap());
assert_noop!(Broker::do_unreserve(2), Error::<Test>::UnknownReservation);
});
}
#[test]
fn cannot_set_expired_lease() {
TestExt::new().execute_with(|| {
advance_to(2);
let current_timeslice = Broker::current_timeslice();
assert_noop!(
Broker::do_set_lease(1000, current_timeslice.saturating_sub(1)),
Error::<Test>::AlreadyExpired
);
});
}
Dónal Murray
committed
#[test]
fn short_leases_are_cleaned() {
TestExt::new().region_length(3).execute_with(|| {
assert_ok!(Broker::do_start_sales(200, 1));
advance_to(2);
// New leases are allowed to expire within this region given expiry > `current_timeslice`.
assert_noop!(
Broker::do_set_lease(1000, Broker::current_timeslice()),
Error::<Test>::AlreadyExpired
);
assert_eq!(Leases::<Test>::get().len(), 0);
assert_ok!(Broker::do_set_lease(1000, Broker::current_timeslice().saturating_add(1)));
assert_eq!(Leases::<Test>::get().len(), 1);
// But are cleaned up in the next rotate_sale.
let config = Configuration::<Test>::get().unwrap();
let timeslice_period: u64 = <Test as Config>::TimeslicePeriod::get();
advance_to(timeslice_period.saturating_mul(config.region_length.into()));
assert_eq!(Leases::<Test>::get().len(), 0);
});
}
#[test]
fn leases_can_be_renewed() {
TestExt::new().endow(1, 1000).execute_with(|| {
// Timeslice period is 2.
//
// Sale 1 starts at block 7, Sale 2 starts at 13.
// Set lease to expire in sale 1 and start sales.
assert_ok!(Broker::do_set_lease(2001, 9));
assert_eq!(Leases::<Test>::get().len(), 1);
// Start the sales with only one core for this lease.
Bastian Köcher
committed
assert_ok!(Broker::do_start_sales(100, 0));
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Advance to sale period 1, we should get an AllowedRenewal for task 2001 for the next
// sale.
advance_sale_period();
assert_eq!(
AllowedRenewals::<Test>::get(AllowedRenewalId { core: 0, when: 10 }),
Some(AllowedRenewalRecord {
price: 100,
completion: CompletionStatus::Complete(
vec![ScheduleItem { mask: CoreMask::complete(), assignment: Task(2001) }]
.try_into()
.unwrap()
)
})
);
// And the lease has been removed from storage.
assert_eq!(Leases::<Test>::get().len(), 0);
// Advance to sale period 2, where we can renew.
advance_sale_period();
assert_ok!(Broker::do_renew(1, 0));
// We renew for the base price of the previous sale period.
assert_eq!(balance(1), 900);
// We just renewed for this period.
advance_sale_period();
// Now we are off core and the core is pooled.
advance_sale_period();
// Check the trace agrees.
assert_eq!(
CoretimeTrace::get(),
vec![
// Period 0 gets no assign core, but leases are on-core.
// Period 1:
(
6,
AssignCore {
core: 0,
begin: 8,
assignment: vec![(CoreAssignment::Task(2001), 57600)],
end_hint: None,
},
),
// Period 2 - expiring at the end of this period, so we called renew.
(
12,
AssignCore {
core: 0,
begin: 14,
assignment: vec![(CoreAssignment::Task(2001), 57600)],
end_hint: None,
},
),
// Period 3 - we get assigned a core because we called renew in period 2.
(
18,
AssignCore {
core: 0,
begin: 20,
assignment: vec![(CoreAssignment::Task(2001), 57600)],
end_hint: None,
},
),
// Period 4 - we don't get a core as we didn't call renew again.
// This core is recycled into the pool.
(
24,
AssignCore {
core: 0,
begin: 26,
assignment: vec![(CoreAssignment::Pool, 57600)],
end_hint: None,