This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathtests.rs
More file actions
2401 lines (2056 loc) · 74.8 KB
/
tests.rs
File metadata and controls
2401 lines (2056 loc) · 74.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
149
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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
469
470
471
472
473
474
475
476
477
478
479
480
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
513
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
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
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
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
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
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
use super::*;
use crate::mock::*;
use frame_support::{assert_noop, assert_ok, assert_storage_noop};
macro_rules! sub_pools_with_era {
($($k:expr => $v:expr),* $(,)?) => {{
use sp_std::iter::{Iterator, IntoIterator};
let not_bounded: BTreeMap<_, _> = Iterator::collect(IntoIterator::into_iter([$(($k, $v),)*]));
SubPoolsWithEra::try_from(not_bounded).unwrap()
}};
}
pub const DEFAULT_ROLES: PoolRoles<AccountId> =
PoolRoles { depositor: 10, root: 900, nominator: 901, state_toggler: 902 };
#[test]
fn test_setup_works() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(BondedPools::<Runtime>::count(), 1);
assert_eq!(RewardPools::<Runtime>::count(), 1);
assert_eq!(SubPoolsStorage::<Runtime>::count(), 0);
assert_eq!(Delegators::<Runtime>::count(), 1);
assert_eq!(StakingMock::bonding_duration(), 3);
let last_pool = LastPoolId::<Runtime>::get();
assert_eq!(
BondedPool::<Runtime>::get(last_pool).unwrap(),
BondedPool::<Runtime> {
id: last_pool,
inner: BondedPoolInner {
state: PoolState::Open,
points: 10,
delegator_counter: 1,
roles: DEFAULT_ROLES
},
}
);
assert_eq!(
RewardPools::<Runtime>::get(last_pool).unwrap(),
RewardPool::<Runtime> { balance: 0, points: 0.into(), total_earnings: 0 }
);
assert_eq!(
Delegators::<Runtime>::get(10).unwrap(),
Delegator::<Runtime> {
pool_id: last_pool,
points: 10,
reward_pool_total_earnings: 0,
unbonding_era: None
}
)
})
}
#[test]
fn exercise_delegator_life_cycle() {
// todo!()
// create pool
// join pool
// claim rewards
// get more rewards
//
}
mod bonded_pool {
use super::*;
#[test]
fn points_to_issue_works() {
let mut bonded_pool = BondedPool::<Runtime> {
id: 123123,
inner: BondedPoolInner {
state: PoolState::Open,
points: 100,
delegator_counter: 1,
roles: DEFAULT_ROLES,
},
};
// 1 points : 1 balance ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 100);
assert_eq!(bonded_pool.points_to_issue(10), 10);
assert_eq!(bonded_pool.points_to_issue(0), 0);
// 2 points : 1 balance ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 50);
assert_eq!(bonded_pool.points_to_issue(10), 20);
// 1 points : 2 balance ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 100);
bonded_pool.points = 50;
assert_eq!(bonded_pool.points_to_issue(10), 5);
// 100 points : 0 balance ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 0);
bonded_pool.points = 100;
assert_eq!(bonded_pool.points_to_issue(10), 100 * 10);
// 0 points : 100 balance
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 100);
bonded_pool.points = 100;
assert_eq!(bonded_pool.points_to_issue(10), 10);
// 10 points : 3 balance ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 30);
assert_eq!(bonded_pool.points_to_issue(10), 33);
// 2 points : 3 balance ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 300);
bonded_pool.points = 200;
assert_eq!(bonded_pool.points_to_issue(10), 6);
// 4 points : 9 balance ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 900);
bonded_pool.points = 400;
assert_eq!(bonded_pool.points_to_issue(90), 40);
}
#[test]
fn balance_to_unbond_works() {
// 1 balance : 1 points ratio
let mut bonded_pool = BondedPool::<Runtime> {
id: 123123,
inner: BondedPoolInner {
state: PoolState::Open,
points: 100,
delegator_counter: 1,
roles: DEFAULT_ROLES,
},
};
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 100);
assert_eq!(bonded_pool.balance_to_unbond(10), 10);
assert_eq!(bonded_pool.balance_to_unbond(0), 0);
// 2 balance : 1 points ratio
bonded_pool.points = 50;
assert_eq!(bonded_pool.balance_to_unbond(10), 20);
// 100 balance : 0 points ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 0);
bonded_pool.points = 0;
assert_eq!(bonded_pool.balance_to_unbond(10), 0);
// 0 balance : 100 points ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 0);
bonded_pool.points = 100;
assert_eq!(bonded_pool.balance_to_unbond(10), 0);
// 10 balance : 3 points ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 100);
bonded_pool.points = 30;
assert_eq!(bonded_pool.balance_to_unbond(10), 33);
// 2 balance : 3 points ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 200);
bonded_pool.points = 300;
assert_eq!(bonded_pool.balance_to_unbond(10), 6);
// 4 balance : 9 points ratio
StakingMock::set_bonded_balance(bonded_pool.bonded_account(), 400);
bonded_pool.points = 900;
assert_eq!(bonded_pool.balance_to_unbond(90), 40);
}
#[test]
fn ok_to_join_with_works() {
ExtBuilder::default().build_and_execute(|| {
let pool = BondedPool::<Runtime> {
id: 123,
inner: BondedPoolInner {
state: PoolState::Open,
points: 100,
delegator_counter: 1,
roles: DEFAULT_ROLES,
},
};
// Simulate a 100% slashed pool
StakingMock::set_bonded_balance(pool.bonded_account(), 0);
assert_noop!(pool.ok_to_join(), Error::<Runtime>::OverflowRisk);
// Simulate a 89%
StakingMock::set_bonded_balance(pool.bonded_account(), 11);
assert_ok!(pool.ok_to_join());
// Simulate a 90% slashed pool
StakingMock::set_bonded_balance(pool.bonded_account(), 10);
assert_noop!(pool.ok_to_join(), Error::<Runtime>::OverflowRisk);
StakingMock::set_bonded_balance(pool.bonded_account(), Balance::MAX / 10);
// New bonded balance would be over 1/10th of Balance type
assert_noop!(pool.ok_to_join(), Error::<Runtime>::OverflowRisk);
// and a sanity check
StakingMock::set_bonded_balance(pool.bonded_account(), Balance::MAX / 10 - 1);
assert_ok!(pool.ok_to_join());
});
}
}
mod reward_pool {}
mod unbond_pool {
use super::*;
#[test]
fn points_to_issue_works() {
// 1 points : 1 balance ratio
let unbond_pool = UnbondPool::<Runtime> { points: 100, balance: 100 };
assert_eq!(unbond_pool.points_to_issue(10), 10);
assert_eq!(unbond_pool.points_to_issue(0), 0);
// 2 points : 1 balance ratio
let unbond_pool = UnbondPool::<Runtime> { points: 100, balance: 50 };
assert_eq!(unbond_pool.points_to_issue(10), 20);
// 1 points : 2 balance ratio
let unbond_pool = UnbondPool::<Runtime> { points: 50, balance: 100 };
assert_eq!(unbond_pool.points_to_issue(10), 5);
// 100 points : 0 balance ratio
let unbond_pool = UnbondPool::<Runtime> { points: 100, balance: 0 };
assert_eq!(unbond_pool.points_to_issue(10), 100 * 10);
// 0 points : 100 balance
let unbond_pool = UnbondPool::<Runtime> { points: 0, balance: 100 };
assert_eq!(unbond_pool.points_to_issue(10), 10);
// 10 points : 3 balance ratio
let unbond_pool = UnbondPool::<Runtime> { points: 100, balance: 30 };
assert_eq!(unbond_pool.points_to_issue(10), 33);
// 2 points : 3 balance ratio
let unbond_pool = UnbondPool::<Runtime> { points: 200, balance: 300 };
assert_eq!(unbond_pool.points_to_issue(10), 6);
// 4 points : 9 balance ratio
let unbond_pool = UnbondPool::<Runtime> { points: 400, balance: 900 };
assert_eq!(unbond_pool.points_to_issue(90), 40);
}
#[test]
fn balance_to_unbond_works() {
// 1 balance : 1 points ratio
let unbond_pool = UnbondPool::<Runtime> { points: 100, balance: 100 };
assert_eq!(unbond_pool.balance_to_unbond(10), 10);
assert_eq!(unbond_pool.balance_to_unbond(0), 0);
// 1 balance : 2 points ratio
let unbond_pool = UnbondPool::<Runtime> { points: 100, balance: 50 };
assert_eq!(unbond_pool.balance_to_unbond(10), 5);
// 2 balance : 1 points ratio
let unbond_pool = UnbondPool::<Runtime> { points: 50, balance: 100 };
assert_eq!(unbond_pool.balance_to_unbond(10), 20);
// 100 balance : 0 points ratio
let unbond_pool = UnbondPool::<Runtime> { points: 0, balance: 100 };
assert_eq!(unbond_pool.balance_to_unbond(10), 0);
// 0 balance : 100 points ratio
let unbond_pool = UnbondPool::<Runtime> { points: 100, balance: 0 };
assert_eq!(unbond_pool.balance_to_unbond(10), 0);
// 10 balance : 3 points ratio
let unbond_pool = UnbondPool::<Runtime> { points: 30, balance: 100 };
assert_eq!(unbond_pool.balance_to_unbond(10), 33);
// 2 balance : 3 points ratio
let unbond_pool = UnbondPool::<Runtime> { points: 300, balance: 200 };
assert_eq!(unbond_pool.balance_to_unbond(10), 6);
// 4 balance : 9 points ratio
let unbond_pool = UnbondPool::<Runtime> { points: 900, balance: 400 };
assert_eq!(unbond_pool.balance_to_unbond(90), 40);
}
}
mod sub_pools {
use super::*;
#[test]
fn maybe_merge_pools_works() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(TotalUnbondingPools::<Runtime>::get(), 5);
// Given
let mut sub_pool_0 = SubPools::<Runtime> {
no_era: UnbondPool::<Runtime>::default(),
with_era: sub_pools_with_era! {
0 => UnbondPool::<Runtime> { points: 10, balance: 10 },
1 => UnbondPool::<Runtime> { points: 10, balance: 10 },
2 => UnbondPool::<Runtime> { points: 20, balance: 20 },
3 => UnbondPool::<Runtime> { points: 30, balance: 30 },
4 => UnbondPool::<Runtime> { points: 40, balance: 40 },
},
};
// When `current_era < TotalUnbondingPools`,
let sub_pool_1 = sub_pool_0.clone().maybe_merge_pools(3);
// Then it exits early without modifications
assert_eq!(sub_pool_1, sub_pool_0);
// When `current_era == TotalUnbondingPools`,
let sub_pool_1 = sub_pool_1.maybe_merge_pools(4);
// Then it exits early without modifications
assert_eq!(sub_pool_1, sub_pool_0);
// When `current_era - TotalUnbondingPools == 0`,
let mut sub_pool_1 = sub_pool_1.maybe_merge_pools(5);
// Then era 0 is merged into the `no_era` pool
sub_pool_0.no_era = sub_pool_0.with_era.remove(&0).unwrap();
assert_eq!(sub_pool_1, sub_pool_0);
// Given we have entries for era 1..=5
sub_pool_1
.with_era
.try_insert(5, UnbondPool::<Runtime> { points: 50, balance: 50 })
.unwrap();
sub_pool_0
.with_era
.try_insert(5, UnbondPool::<Runtime> { points: 50, balance: 50 })
.unwrap();
// When `current_era - TotalUnbondingPools == 1`
let sub_pool_2 = sub_pool_1.maybe_merge_pools(6);
let era_1_pool = sub_pool_0.with_era.remove(&1).unwrap();
// Then era 1 is merged into the `no_era` pool
sub_pool_0.no_era.points += era_1_pool.points;
sub_pool_0.no_era.balance += era_1_pool.balance;
assert_eq!(sub_pool_2, sub_pool_0);
// When `current_era - TotalUnbondingPools == 5`, so all pools with era <= 4 are removed
let sub_pool_3 = sub_pool_2.maybe_merge_pools(10);
// Then all eras <= 5 are merged into the `no_era` pool
for era in 2..=5 {
let to_merge = sub_pool_0.with_era.remove(&era).unwrap();
sub_pool_0.no_era.points += to_merge.points;
sub_pool_0.no_era.balance += to_merge.balance;
}
assert_eq!(sub_pool_3, sub_pool_0);
});
}
}
mod join {
use super::*;
#[test]
fn join_works() {
let bonded = |points, delegator_counter| BondedPool::<Runtime> {
id: 1,
inner: BondedPoolInner {
state: PoolState::Open,
points,
delegator_counter,
roles: DEFAULT_ROLES,
},
};
ExtBuilder::default().build_and_execute(|| {
// Given
Balances::make_free_balance_be(&11, ExistentialDeposit::get() + 2);
assert!(!Delegators::<Runtime>::contains_key(&11));
// When
assert_ok!(Pools::join(Origin::signed(11), 2, 1));
// then
assert_eq!(
Delegators::<Runtime>::get(&11).unwrap(),
Delegator::<Runtime> {
pool_id: 1,
points: 2,
reward_pool_total_earnings: 0,
unbonding_era: None
}
);
assert_eq!(BondedPool::<Runtime>::get(1).unwrap(), bonded(12, 2));
// Given
// The bonded balance is slashed in half
StakingMock::set_bonded_balance(Pools::create_bonded_account(1), 6);
// And
Balances::make_free_balance_be(&12, ExistentialDeposit::get() + 12);
assert!(!Delegators::<Runtime>::contains_key(&12));
// When
assert_ok!(Pools::join(Origin::signed(12), 12, 1));
// Then
assert_eq!(
Delegators::<Runtime>::get(&12).unwrap(),
Delegator::<Runtime> {
pool_id: 1,
points: 24,
reward_pool_total_earnings: 0,
unbonding_era: None
}
);
assert_eq!(BondedPool::<Runtime>::get(1).unwrap(), bonded(12 + 24, 3));
});
}
#[test]
fn join_errors_correctly() {
ExtBuilder::default().build_and_execute_no_checks(|| {
// 10 is already part of the default pool created.
assert_eq!(Delegators::<Runtime>::get(&10).unwrap().pool_id, 1);
assert_noop!(
Pools::join(Origin::signed(10), 420, 123),
Error::<Runtime>::AccountBelongsToOtherPool
);
assert_noop!(Pools::join(Origin::signed(11), 420, 123), Error::<Runtime>::PoolNotFound);
// Force the pools bonded balance to 0, simulating a 100% slash
StakingMock::set_bonded_balance(Pools::create_bonded_account(1), 0);
assert_noop!(Pools::join(Origin::signed(11), 420, 1), Error::<Runtime>::OverflowRisk);
// Given a mocked bonded pool
BondedPool::<Runtime> {
id: 123,
inner: BondedPoolInner {
delegator_counter: 1,
state: PoolState::Open,
points: 100,
roles: DEFAULT_ROLES,
},
}
.put();
// and reward pool
RewardPools::<Runtime>::insert(
123,
RewardPool::<Runtime> {
balance: Zero::zero(),
total_earnings: Zero::zero(),
points: U256::from(0u32),
},
);
// Force the points:balance ratio to 100/10
StakingMock::set_bonded_balance(Pools::create_bonded_account(123), 10);
assert_noop!(Pools::join(Origin::signed(11), 420, 123), Error::<Runtime>::OverflowRisk);
StakingMock::set_bonded_balance(Pools::create_bonded_account(123), Balance::MAX / 10);
// Balance is gt 1/10 of Balance::MAX
assert_noop!(Pools::join(Origin::signed(11), 5, 123), Error::<Runtime>::OverflowRisk);
StakingMock::set_bonded_balance(Pools::create_bonded_account(1), 10);
// Cannot join a pool that isn't open
unsafe_set_state(123, PoolState::Blocked).unwrap();
assert_noop!(Pools::join(Origin::signed(11), 10, 123), Error::<Runtime>::NotOpen);
unsafe_set_state(123, PoolState::Destroying).unwrap();
assert_noop!(Pools::join(Origin::signed(11), 10, 123), Error::<Runtime>::NotOpen);
// Given
MinJoinBond::<Runtime>::put(100);
// Then
assert_noop!(
Pools::join(Origin::signed(11), 99, 123),
Error::<Runtime>::MinimumBondNotMet
);
});
}
#[test]
#[should_panic = "Defensive failure has been triggered!"]
fn join_panics_when_reward_pool_not_found() {
// TODO: but we should fail defensively..
ExtBuilder::default().build_and_execute(|| {
StakingMock::set_bonded_balance(Pools::create_bonded_account(123), 100);
BondedPool::<Runtime> {
id: 123,
inner: BondedPoolInner {
state: PoolState::Open,
points: 100,
delegator_counter: 1,
roles: DEFAULT_ROLES,
},
}
.put();
let _ = Pools::join(Origin::signed(11), 420, 123);
});
}
#[test]
fn join_max_delegator_limits_are_respected() {
ExtBuilder::default().build_and_execute(|| {
// Given
assert_eq!(MaxDelegatorsPerPool::<Runtime>::get(), Some(3));
for i in 1..3 {
let account = i + 100;
Balances::make_free_balance_be(&account, 100 + Balances::minimum_balance());
assert_ok!(Pools::join(Origin::signed(account), 100, 1));
}
Balances::make_free_balance_be(&103, 100 + Balances::minimum_balance());
// Then
assert_noop!(Pools::join(Origin::signed(103), 100, 1), Error::<Runtime>::MaxDelegators);
// Given
assert_eq!(Delegators::<Runtime>::count(), 3);
assert_eq!(MaxDelegators::<Runtime>::get(), Some(4));
Balances::make_free_balance_be(&104, 100 + Balances::minimum_balance());
assert_ok!(Pools::create(Origin::signed(104), 100, 104, 104, 104));
let pool_account = BondedPools::<Runtime>::iter()
.find(|(_, bonded_pool)| bonded_pool.roles.depositor == 104)
.map(|(pool_account, _)| pool_account)
.unwrap();
// Then
assert_noop!(
Pools::join(Origin::signed(103), 100, pool_account),
Error::<Runtime>::MaxDelegators
);
});
}
}
mod claim_payout {
use super::*;
fn del(points: Balance, reward_pool_total_earnings: Balance) -> Delegator<Runtime> {
Delegator { pool_id: 1, points, reward_pool_total_earnings, unbonding_era: None }
}
fn rew(balance: Balance, points: u32, total_earnings: Balance) -> RewardPool<Runtime> {
RewardPool { balance, points: points.into(), total_earnings }
}
#[test]
fn claim_payout_works() {
ExtBuilder::default()
.add_delegators(vec![(40, 40), (50, 50)])
.build_and_execute(|| {
// Given each delegator currently has a free balance of
Balances::make_free_balance_be(&10, 0);
Balances::make_free_balance_be(&40, 0);
Balances::make_free_balance_be(&50, 0);
let reward_account = Pools::create_reward_account(1);
// and the reward pool has earned 100 in rewards
Balances::make_free_balance_be(&reward_account, 100);
// When
assert_ok!(Pools::claim_payout(Origin::signed(10)));
// Then
// Expect a payout of 10: (10 del virtual points / 100 pool points) * 100 pool
// balance
assert_eq!(Delegators::<Runtime>::get(10).unwrap(), del(10, 100));
assert_eq!(
RewardPools::<Runtime>::get(&1).unwrap(),
rew(90, 100 * 100 - 100 * 10, 100)
);
assert_eq!(Balances::free_balance(&10), 10);
assert_eq!(Balances::free_balance(&reward_account), 90);
// When
assert_ok!(Pools::claim_payout(Origin::signed(40)));
// Then
// Expect payout 40: (400 del virtual points / 900 pool points) * 90 pool balance
assert_eq!(Delegators::<Runtime>::get(40).unwrap(), del(40, 100));
assert_eq!(
RewardPools::<Runtime>::get(&1).unwrap(),
rew(50, 9_000 - 100 * 40, 100)
);
assert_eq!(Balances::free_balance(&40), 40);
assert_eq!(Balances::free_balance(&reward_account), 50);
// When
assert_ok!(Pools::claim_payout(Origin::signed(50)));
// Then
// Expect payout 50: (50 del virtual points / 50 pool points) * 50 pool balance
assert_eq!(Delegators::<Runtime>::get(50).unwrap(), del(50, 100));
assert_eq!(RewardPools::<Runtime>::get(&1).unwrap(), rew(0, 0, 100));
assert_eq!(Balances::free_balance(&50), 50);
assert_eq!(Balances::free_balance(&reward_account), 0);
// Given the reward pool has some new rewards
Balances::make_free_balance_be(&reward_account, 50);
// When
assert_ok!(Pools::claim_payout(Origin::signed(10)));
// Then
// Expect payout 5: (500 del virtual points / 5,000 pool points) * 50 pool balance
assert_eq!(Delegators::<Runtime>::get(10).unwrap(), del(10, 150));
assert_eq!(RewardPools::<Runtime>::get(&1).unwrap(), rew(45, 5_000 - 50 * 10, 150));
assert_eq!(Balances::free_balance(&10), 10 + 5);
assert_eq!(Balances::free_balance(&reward_account), 45);
// When
assert_ok!(Pools::claim_payout(Origin::signed(40)));
// Then
// Expect payout 20: (2,000 del virtual points / 4,500 pool points) * 45 pool
// balance
assert_eq!(Delegators::<Runtime>::get(40).unwrap(), del(40, 150));
assert_eq!(RewardPools::<Runtime>::get(&1).unwrap(), rew(25, 4_500 - 50 * 40, 150));
assert_eq!(Balances::free_balance(&40), 40 + 20);
assert_eq!(Balances::free_balance(&reward_account), 25);
// Given del 50 hasn't claimed and the reward pools has just earned 50
assert_ok!(Balances::mutate_account(&reward_account, |a| a.free += 50));
assert_eq!(Balances::free_balance(&reward_account), 75);
// When
assert_ok!(Pools::claim_payout(Origin::signed(50)));
// Then
// We expect a payout of 50: (5,000 del virtual points / 7,5000 pool points) * 75
// pool balance
assert_eq!(Delegators::<Runtime>::get(50).unwrap(), del(50, 200));
assert_eq!(
RewardPools::<Runtime>::get(&1).unwrap(),
rew(
25,
// old pool points + points from new earnings - del points.
//
// points from new earnings = new earnings(50) * bonded_pool.points(100)
// del points = delegator.points(50) * new_earnings_since_last_claim (100)
(2_500 + 50 * 100) - 50 * 100,
200,
)
);
assert_eq!(Balances::free_balance(&50), 50 + 50);
assert_eq!(Balances::free_balance(&reward_account), 25);
// When
assert_ok!(Pools::claim_payout(Origin::signed(10)));
// Then
// We expect a payout of 5
assert_eq!(Delegators::<Runtime>::get(10).unwrap(), del(10, 200));
assert_eq!(RewardPools::<Runtime>::get(&1).unwrap(), rew(20, 2_500 - 10 * 50, 200));
assert_eq!(Balances::free_balance(&10), 15 + 5);
assert_eq!(Balances::free_balance(&reward_account), 20);
// Given del 40 hasn't claimed and the reward pool has just earned 400
assert_ok!(Balances::mutate_account(&reward_account, |a| a.free += 400));
assert_eq!(Balances::free_balance(&reward_account), 420);
// When
assert_ok!(Pools::claim_payout(Origin::signed(10)));
// Then
// We expect a payout of 40
assert_eq!(Delegators::<Runtime>::get(10).unwrap(), del(10, 600));
assert_eq!(
RewardPools::<Runtime>::get(&1).unwrap(),
rew(
380,
// old pool points + points from new earnings - del points
//
// points from new earnings = new earnings(400) * bonded_pool.points(100)
// del points = delegator.points(10) * new_earnings_since_last_claim(400)
(2_000 + 400 * 100) - 10 * 400,
600
)
);
assert_eq!(Balances::free_balance(&10), 20 + 40);
assert_eq!(Balances::free_balance(&reward_account), 380);
// Given del 40 + del 50 haven't claimed and the reward pool has earned 20
assert_ok!(Balances::mutate_account(&reward_account, |a| a.free += 20));
assert_eq!(Balances::free_balance(&reward_account), 400);
// When
assert_ok!(Pools::claim_payout(Origin::signed(10)));
// Then
// Expect a payout of 2: (200 del virtual points / 38,000 pool points) * 400 pool
// balance
assert_eq!(Delegators::<Runtime>::get(10).unwrap(), del(10, 620));
assert_eq!(
RewardPools::<Runtime>::get(&1).unwrap(),
rew(398, (38_000 + 20 * 100) - 10 * 20, 620)
);
assert_eq!(Balances::free_balance(&10), 60 + 2);
assert_eq!(Balances::free_balance(&reward_account), 398);
// When
assert_ok!(Pools::claim_payout(Origin::signed(40)));
// Then
// Expect a payout of 188: (18,800 del virtual points / 39,800 pool points) * 399
// pool balance
assert_eq!(Delegators::<Runtime>::get(40).unwrap(), del(40, 620));
assert_eq!(
RewardPools::<Runtime>::get(&1).unwrap(),
rew(210, 39_800 - 40 * 470, 620)
);
assert_eq!(Balances::free_balance(&40), 60 + 188);
assert_eq!(Balances::free_balance(&reward_account), 210);
// When
assert_ok!(Pools::claim_payout(Origin::signed(50)));
// Then
// Expect payout of 210: (21,000 / 21,000) * 210
assert_eq!(Delegators::<Runtime>::get(50).unwrap(), del(50, 620));
assert_eq!(
RewardPools::<Runtime>::get(&1).unwrap(),
rew(0, 21_000 - 50 * 420, 620)
);
assert_eq!(Balances::free_balance(&50), 100 + 210);
assert_eq!(Balances::free_balance(&reward_account), 0);
});
}
#[test]
fn do_reward_payout_correctly_sets_pool_state_to_destroying() {
ExtBuilder::default().build_and_execute(|| {
let mut bonded_pool = BondedPool::<Runtime>::get(1).unwrap();
let mut reward_pool = RewardPools::<Runtime>::get(1).unwrap();
let mut delegator = Delegators::<Runtime>::get(10).unwrap();
let reward_account = Pools::create_reward_account(1);
// --- reward_pool.total_earnings saturates
// Given
Balances::make_free_balance_be(&reward_account, Balance::MAX);
// When
assert_ok!(Pools::do_reward_payout(10, &mut delegator, &mut bonded_pool));
// Then
assert!(bonded_pool.is_destroying());
// -- current_points saturates (reward_pool.points + new_earnings * bonded_pool.points)
// Given
let mut bonded_pool = BondedPool::<Runtime>::get(1).unwrap();
let mut delegator = Delegators::<Runtime>::get(10).unwrap();
// Force new_earnings * bonded_pool.points == 100
Balances::make_free_balance_be(&reward_account, 10);
assert_eq!(bonded_pool.points, 10);
// Force reward_pool.points == U256::MAX - new_earnings * bonded_pool.points
reward_pool.points = U256::MAX - U256::from(100u32);
RewardPools::<Runtime>::insert(1, reward_pool.clone());
// When
assert_ok!(Pools::do_reward_payout(10, &mut delegator, &mut bonded_pool));
// Then
assert!(bonded_pool.is_destroying());
});
}
#[test]
fn calculate_delegator_payout_errors_if_a_delegator_is_unbonding() {
ExtBuilder::default().build_and_execute(|| {
let mut bonded_pool = BondedPool::<Runtime>::get(1).unwrap();
let mut reward_pool = RewardPools::<Runtime>::get(1).unwrap();
let mut delegator = Delegators::<Runtime>::get(10).unwrap();
delegator.unbonding_era = Some(0 + 3);
assert_noop!(
Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut delegator
),
Error::<Runtime>::AlreadyUnbonding
);
});
}
#[test]
fn calculate_delegator_payout_works_with_a_pool_of_1() {
let del = |reward_pool_total_earnings| del(10, reward_pool_total_earnings);
ExtBuilder::default().build_and_execute(|| {
let mut bonded_pool = BondedPool::<Runtime>::get(1).unwrap();
let mut reward_pool = RewardPools::<Runtime>::get(1).unwrap();
let mut delegator = Delegators::<Runtime>::get(10).unwrap();
let reward_account = Pools::create_reward_account(1);
// Given no rewards have been earned
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut delegator,
)
.unwrap();
// Then
assert_eq!(payout, 0);
assert_eq!(delegator, del(0));
assert_eq!(reward_pool, rew(0, 0, 0));
// Given the pool has earned some rewards for the first time
Balances::make_free_balance_be(&reward_account, 5);
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut delegator,
)
.unwrap();
// Then
assert_eq!(payout, 5); // (10 * 5 del virtual points / 10 * 5 pool points) * 5 pool balance
assert_eq!(reward_pool, rew(0, 0, 5));
assert_eq!(delegator, del(5));
// Given the pool has earned rewards again
Balances::make_free_balance_be(&reward_account, 10);
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut delegator,
)
.unwrap();
// Then
assert_eq!(payout, 10); // (10 * 10 del virtual points / 10 pool points) * 5 pool balance
assert_eq!(reward_pool, rew(0, 0, 15));
assert_eq!(delegator, del(15));
// Given the pool has earned no new rewards
Balances::make_free_balance_be(&reward_account, 0);
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut delegator,
)
.unwrap();
// Then
assert_eq!(payout, 0);
assert_eq!(reward_pool, rew(0, 0, 15));
assert_eq!(delegator, del(15));
});
}
#[test]
fn calculate_delegator_payout_works_with_a_pool_of_3() {
ExtBuilder::default()
.add_delegators(vec![(40, 40), (50, 50)])
.build_and_execute(|| {
let mut bonded_pool = BondedPool::<Runtime>::get(1).unwrap();
let mut reward_pool = RewardPools::<Runtime>::get(1).unwrap();
// Delegator with 10 points
let mut del_10 = Delegators::<Runtime>::get(10).unwrap();
// Delegator with 40 points
let mut del_40 = Delegators::<Runtime>::get(40).unwrap();
// Delegator with 50 points
let mut del_50 = Delegators::<Runtime>::get(50).unwrap();
// Given we have a total of 100 points split among the delegators
assert_eq!(del_50.points + del_40.points + del_10.points, 100);
assert_eq!(bonded_pool.points, 100);
// and the reward pool has earned 100 in rewards
Balances::make_free_balance_be(&default_reward_account(), 100);
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut del_10,
)
.unwrap();
// Then
assert_eq!(payout, 10); // (10 del virtual points / 100 pool points) * 100 pool balance
assert_eq!(del_10, del(10, 100));
assert_eq!(reward_pool, rew(90, 100 * 100 - 100 * 10, 100));
// Mock the reward pool transferring the payout to del_10
assert_ok!(Balances::mutate_account(&default_reward_account(), |a| a.free -= 10));
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut del_40,
)
.unwrap();
// Then
assert_eq!(payout, 40); // (400 del virtual points / 900 pool points) * 90 pool balance
assert_eq!(del_40, del(40, 100));
assert_eq!(
reward_pool,
rew(
50,
// old pool points - delegator virtual points
9_000 - 100 * 40,
100
)
);
// Mock the reward pool transferring the payout to del_40
assert_ok!(Balances::mutate_account(&default_reward_account(), |a| a.free -= 40));
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut del_50,
)
.unwrap();
// Then
assert_eq!(payout, 50); // (50 del virtual points / 50 pool points) * 50 pool balance
assert_eq!(del_50, del(50, 100));
assert_eq!(reward_pool, rew(0, 0, 100));
// Mock the reward pool transferring the payout to del_50
assert_ok!(Balances::mutate_account(&default_reward_account(), |a| a.free -= 50));
// Given the reward pool has some new rewards
Balances::make_free_balance_be(&default_reward_account(), 50);
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut del_10,
)
.unwrap();
// Then
assert_eq!(payout, 5); // (500 del virtual points / 5,000 pool points) * 50 pool balance
assert_eq!(del_10, del(10, 150));
assert_eq!(reward_pool, rew(45, 5_000 - 50 * 10, 150));
// Mock the reward pool transferring the payout to del_10
assert_ok!(Balances::mutate_account(&default_reward_account(), |a| a.free -= 5));
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut del_40,
)
.unwrap();
// Then
assert_eq!(payout, 20); // (2,000 del virtual points / 4,500 pool points) * 45 pool balance
assert_eq!(del_40, del(40, 150));
assert_eq!(reward_pool, rew(25, 4_500 - 50 * 40, 150));
assert_ok!(Balances::mutate_account(&default_reward_account(), |a| a.free -= 20));
// Given del_50 hasn't claimed and the reward pools has just earned 50
assert_ok!(Balances::mutate_account(&default_reward_account(), |a| a.free += 50));
assert_eq!(Balances::free_balance(&default_reward_account()), 75);
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut del_50,
)
.unwrap();
// Then
assert_eq!(payout, 50); // (5,000 del virtual points / 7,5000 pool points) * 75 pool balance
assert_eq!(del_50, del(50, 200));
assert_eq!(
reward_pool,
rew(
25,
// old pool points + points from new earnings - del points.
//
// points from new earnings = new earnings(50) * bonded_pool.points(100)
// del points = delegator.points(50) * new_earnings_since_last_claim (100)
(2_500 + 50 * 100) - 50 * 100,
200,
)
);
// Mock the reward pool transferring the payout to del_50
assert_ok!(Balances::mutate_account(&default_reward_account(), |a| a.free -= 50));
// When
let payout = Pools::calculate_delegator_payout(
&mut bonded_pool,
&mut reward_pool,
&mut del_10,
)
.unwrap();