-
Notifications
You must be signed in to change notification settings - Fork 699
/
tests.rs
1294 lines (1201 loc) · 43.8 KB
/
tests.rs
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
// 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.
//! Tests for the module.
use super::*;
use migrations::old;
use mock::*;
use frame_support::{assert_noop, assert_ok};
use sp_core::blake2_256;
use sp_runtime::traits::BadOrigin;
use BidKind::*;
use VouchingStatus::*;
use RuntimeOrigin as Origin;
#[test]
fn migration_works() {
EnvBuilder::new().founded(false).execute(|| {
use old::Vote::*;
// Initialise the old storage items.
Founder::<Test>::put(10);
Head::<Test>::put(30);
old::Members::<Test, ()>::put(vec![10, 20, 30]);
old::Vouching::<Test, ()>::insert(30, Vouching);
old::Vouching::<Test, ()>::insert(40, Banned);
old::Strikes::<Test, ()>::insert(20, 1);
old::Strikes::<Test, ()>::insert(30, 2);
old::Strikes::<Test, ()>::insert(40, 5);
old::Payouts::<Test, ()>::insert(20, vec![(1, 1)]);
old::Payouts::<Test, ()>::insert(
30,
(0..=<Test as Config>::MaxPayouts::get())
.map(|i| (i as u64, i as u64))
.collect::<Vec<_>>(),
);
old::SuspendedMembers::<Test, ()>::insert(40, true);
old::Defender::<Test, ()>::put(20);
old::DefenderVotes::<Test, ()>::insert(10, Approve);
old::DefenderVotes::<Test, ()>::insert(20, Approve);
old::DefenderVotes::<Test, ()>::insert(30, Reject);
old::SuspendedCandidates::<Test, ()>::insert(50, (10, Deposit(100)));
old::Candidates::<Test, ()>::put(vec![
Bid { who: 60, kind: Deposit(100), value: 200 },
Bid { who: 70, kind: Vouch(30, 30), value: 100 },
]);
old::Votes::<Test, ()>::insert(60, 10, Approve);
old::Votes::<Test, ()>::insert(70, 10, Reject);
old::Votes::<Test, ()>::insert(70, 20, Approve);
old::Votes::<Test, ()>::insert(70, 30, Approve);
let bids = (0..=<Test as Config>::MaxBids::get())
.map(|i| Bid {
who: 100u128 + i as u128,
kind: Deposit(20u64 + i as u64),
value: 10u64 + i as u64,
})
.collect::<Vec<_>>();
old::Bids::<Test, ()>::put(bids);
migrations::from_original::<Test, ()>(&mut [][..]).expect("migration failed");
migrations::assert_internal_consistency::<Test, ()>();
assert_eq!(
membership(),
vec![
(10, MemberRecord { rank: 0, strikes: 0, vouching: None, index: 0 }),
(20, MemberRecord { rank: 0, strikes: 1, vouching: None, index: 1 }),
(30, MemberRecord { rank: 0, strikes: 2, vouching: Some(Vouching), index: 2 }),
]
);
assert_eq!(Payouts::<Test>::get(10), PayoutRecord::default());
let payouts = vec![(1, 1)].try_into().unwrap();
assert_eq!(Payouts::<Test>::get(20), PayoutRecord { paid: 0, payouts });
let payouts = (0..<Test as Config>::MaxPayouts::get())
.map(|i| (i as u64, i as u64))
.collect::<Vec<_>>()
.try_into()
.unwrap();
assert_eq!(Payouts::<Test>::get(30), PayoutRecord { paid: 0, payouts });
assert_eq!(
SuspendedMembers::<Test>::iter().collect::<Vec<_>>(),
vec![(40, MemberRecord { rank: 0, strikes: 5, vouching: Some(Banned), index: 0 }),]
);
let bids: BoundedVec<_, <Test as Config>::MaxBids> = (0..<Test as Config>::MaxBids::get())
.map(|i| Bid {
who: 100u128 + i as u128,
kind: Deposit(20u64 + i as u64),
value: 10u64 + i as u64,
})
.collect::<Vec<_>>()
.try_into()
.unwrap();
assert_eq!(Bids::<Test>::get(), bids);
assert_eq!(RoundCount::<Test, ()>::get(), 0);
assert_eq!(
candidacies(),
vec![
(
60,
Candidacy {
round: 0,
kind: Deposit(100),
bid: 200,
tally: Tally { approvals: 1, rejections: 0 },
skeptic_struck: false,
}
),
(
70,
Candidacy {
round: 0,
kind: Vouch(30, 30),
bid: 100,
tally: Tally { approvals: 2, rejections: 1 },
skeptic_struck: false,
}
),
]
);
assert_eq!(Votes::<Test>::get(60, 10), Some(Vote { approve: true, weight: 1 }));
assert_eq!(Votes::<Test>::get(70, 10), Some(Vote { approve: false, weight: 1 }));
assert_eq!(Votes::<Test>::get(70, 20), Some(Vote { approve: true, weight: 1 }));
assert_eq!(Votes::<Test>::get(70, 30), Some(Vote { approve: true, weight: 1 }));
});
}
#[test]
fn founding_works() {
EnvBuilder::new().founded(false).execute(|| {
// Not set up initially.
assert_eq!(Founder::<Test>::get(), None);
assert_eq!(Parameters::<Test>::get(), None);
assert_eq!(Pot::<Test>::get(), 0);
// Account 1 is set as the founder origin
// Account 5 cannot start a society
assert_noop!(
Society::found_society(Origin::signed(5), 20, 100, 10, 2, 25, vec![]),
BadOrigin
);
// Account 1 can start a society, where 10 is the founding member
assert_ok!(Society::found_society(
Origin::signed(1),
10,
100,
10,
2,
25,
b"be cool".to_vec()
));
// Society members only include 10
assert_eq!(members(), vec![10]);
// 10 is the head of the society
assert_eq!(Head::<Test>::get(), Some(10));
// ...and also the founder
assert_eq!(Founder::<Test>::get(), Some(10));
// 100 members max
assert_eq!(Parameters::<Test>::get().unwrap().max_members, 100);
// rules are correct
assert_eq!(Rules::<Test>::get(), Some(blake2_256(b"be cool").into()));
// Pot grows after first rotation period
next_intake();
assert_eq!(Pot::<Test>::get(), 1000);
// Cannot start another society
assert_noop!(
Society::found_society(Origin::signed(1), 20, 100, 10, 2, 25, vec![]),
Error::<Test>::AlreadyFounded
);
});
}
#[test]
fn unfounding_works() {
EnvBuilder::new().founded(false).execute(|| {
// Account 1 sets the founder...
assert_ok!(Society::found_society(Origin::signed(1), 10, 100, 10, 2, 25, vec![]));
// Account 2 cannot unfound it as it's not the founder.
assert_noop!(Society::dissolve(Origin::signed(2)), Error::<Test>::NotFounder);
// Account 10 can, though.
assert_ok!(Society::dissolve(Origin::signed(10)));
// 1 sets the founder to 20 this time
assert_ok!(Society::found_society(Origin::signed(1), 20, 100, 10, 2, 25, vec![]));
// Bring in a new member...
assert_ok!(Society::bid(Origin::signed(10), 0));
next_intake();
assert_ok!(Society::vote(Origin::signed(20), 10, true));
conclude_intake(true, None);
// Unfounding won't work now, even though it's from 20.
assert_noop!(Society::dissolve(Origin::signed(20)), Error::<Test>::NotHead);
});
}
#[test]
fn basic_new_member_works() {
EnvBuilder::new().execute(|| {
assert_eq!(Balances::free_balance(20), 50);
// Bid causes Candidate Deposit to be reserved.
assert_ok!(Society::bid(RuntimeOrigin::signed(20), 0));
assert_eq!(Balances::free_balance(20), 25);
assert_eq!(Balances::reserved_balance(20), 25);
// Rotate period every 4 blocks
next_intake();
// 20 is now a candidate
assert_eq!(candidacies(), vec![(20, candidacy(1, 0, Deposit(25), 0, 0))]);
// 10 (a member) can vote for the candidate
assert_ok!(Society::vote(Origin::signed(10), 20, true));
conclude_intake(true, None);
// Rotate period every 4 blocks
next_intake();
// 20 is now a member of the society
assert_eq!(members(), vec![10, 20]);
// Reserved balance is returned
assert_eq!(Balances::free_balance(20), 50);
assert_eq!(Balances::reserved_balance(20), 0);
});
}
#[test]
fn bidding_works() {
EnvBuilder::new().execute(|| {
// Users make bids of various amounts
assert_ok!(Society::bid(RuntimeOrigin::signed(60), 1900));
assert_ok!(Society::bid(RuntimeOrigin::signed(50), 500));
assert_ok!(Society::bid(RuntimeOrigin::signed(40), 400));
assert_ok!(Society::bid(RuntimeOrigin::signed(30), 300));
// Rotate period
next_intake();
// Pot is 1000 after "PeriodSpend"
assert_eq!(Pot::<Test>::get(), 1000);
assert_eq!(Balances::free_balance(Society::account_id()), 10_000);
// Choose smallest bidding users whose total is less than pot
assert_eq!(
candidacies(),
vec![
(30, candidacy(1, 300, Deposit(25), 0, 0)),
(40, candidacy(1, 400, Deposit(25), 0, 0)),
]
);
// A member votes for these candidates to join the society
assert_ok!(Society::vote(Origin::signed(10), 30, true));
assert_ok!(Society::vote(Origin::signed(10), 40, true));
conclude_intake(true, None);
next_intake();
// Candidates become members after a period rotation
assert_eq!(members(), vec![10, 30, 40]);
// Pot is increased by 1000, but pays out 700 to the members
assert_eq!(Balances::free_balance(Society::account_id()), 9_300);
assert_eq!(Pot::<Test>::get(), 1_300);
// Left over from the original bids is 50 who satisfies the condition of bid less than pot.
assert_eq!(candidacies(), vec![(50, candidacy(2, 500, Deposit(25), 0, 0))]);
// 40, now a member, can vote for 50
assert_ok!(Society::vote(Origin::signed(40), 50, true));
conclude_intake(true, None);
run_to_block(12);
// 50 is now a member
assert_eq!(members(), vec![10, 30, 40, 50]);
// Pot is increased by 1000, and 500 is paid out. Total payout so far is 1200.
assert_eq!(Pot::<Test>::get(), 1_800);
assert_eq!(Balances::free_balance(Society::account_id()), 8_800);
// No more candidates satisfy the requirements
assert_eq!(candidacies(), vec![]);
assert_ok!(Society::defender_vote(Origin::signed(10), true)); // Keep defender around
// Next period
run_to_block(16);
// Same members
assert_eq!(members(), vec![10, 30, 40, 50]);
// Pot is increased by 1000 again
assert_eq!(Pot::<Test>::get(), 2_800);
// No payouts
assert_eq!(Balances::free_balance(Society::account_id()), 8_800);
// Candidate 60 now qualifies based on the increased pot size.
assert_eq!(candidacies(), vec![(60, candidacy(4, 1900, Deposit(25), 0, 0))]);
// Candidate 60 is voted in.
assert_ok!(Society::vote(Origin::signed(50), 60, true));
conclude_intake(true, None);
run_to_block(20);
// 60 joins as a member
assert_eq!(members(), vec![10, 30, 40, 50, 60]);
// Pay them
assert_eq!(Pot::<Test>::get(), 1_900);
assert_eq!(Balances::free_balance(Society::account_id()), 6_900);
});
}
#[test]
fn unbidding_works() {
EnvBuilder::new().execute(|| {
// 20 and 30 make bids
assert_ok!(Society::bid(RuntimeOrigin::signed(20), 1000));
assert_ok!(Society::bid(RuntimeOrigin::signed(30), 0));
// Balances are reserved
assert_eq!(Balances::free_balance(30), 25);
assert_eq!(Balances::reserved_balance(30), 25);
// Can unbid themselves with the right position
assert_ok!(Society::unbid(Origin::signed(30)));
assert_noop!(Society::unbid(Origin::signed(30)), Error::<Test>::NotBidder);
// Balance is returned
assert_eq!(Balances::free_balance(30), 50);
assert_eq!(Balances::reserved_balance(30), 0);
// 20 wins candidacy
next_intake();
assert_eq!(candidacies(), vec![(20, candidacy(1, 1000, Deposit(25), 0, 0))]);
});
}
#[test]
fn payout_works() {
EnvBuilder::new().execute(|| {
// Original balance of 50
assert_eq!(Balances::free_balance(20), 50);
assert_ok!(Society::bid(Origin::signed(20), 1000));
next_intake();
assert_ok!(Society::vote(Origin::signed(10), 20, true));
conclude_intake(true, None);
// payout not ready
assert_noop!(Society::payout(Origin::signed(20)), Error::<Test>::NoPayout);
next_intake();
// payout should be here
assert_ok!(Society::payout(RuntimeOrigin::signed(20)));
assert_eq!(Balances::free_balance(20), 1050);
});
}
#[test]
fn non_voting_skeptic_is_punished() {
EnvBuilder::new().execute(|| {
assert_eq!(Members::<Test>::get(10).unwrap().strikes, 0);
assert_ok!(Society::bid(Origin::signed(20), 0));
next_intake();
assert_eq!(candidacies(), vec![(20, candidacy(1, 0, Deposit(25), 0, 0))]);
conclude_intake(true, None);
next_intake();
assert_eq!(members(), vec![10]);
assert_eq!(Members::<Test>::get(10).unwrap().strikes, 1);
});
}
#[test]
fn rejecting_skeptic_on_approved_is_punished() {
EnvBuilder::new().execute(|| {
place_members([20, 30]);
assert_ok!(Society::bid(Origin::signed(40), 0));
next_intake();
let skeptic = Skeptic::<Test>::get().unwrap();
for &i in &[10, 20, 30][..] {
assert_ok!(Society::vote(Origin::signed(i), 40, i != skeptic));
}
conclude_intake(true, None);
assert_eq!(Members::<Test>::get(10).unwrap().strikes, 0);
run_to_block(12);
assert_eq!(members(), vec![10, 20, 30, 40]);
assert_eq!(Members::<Test>::get(skeptic).unwrap().strikes, 1);
});
}
#[test]
fn basic_new_member_reject_works() {
EnvBuilder::new().execute(|| {
// Starting Balance
assert_eq!(Balances::free_balance(20), 50);
// 20 makes a bid
assert_ok!(Society::bid(RuntimeOrigin::signed(20), 0));
assert_eq!(Balances::free_balance(20), 25);
assert_eq!(Balances::reserved_balance(20), 25);
// Rotation Period
next_intake();
assert_eq!(candidacies(), vec![(20, candidacy(1, 0, Deposit(25), 0, 0))]);
// We say no
assert_ok!(Society::vote(Origin::signed(10), 20, false));
conclude_intake(true, None);
next_intake();
// User is not added as member
assert_eq!(members(), vec![10]);
// User is rejected.
assert_eq!(candidacies(), vec![]);
assert_eq!(Bids::<Test>::get().into_inner(), vec![]);
});
}
#[test]
fn slash_payout_works() {
EnvBuilder::new().execute(|| {
assert_eq!(Balances::free_balance(20), 50);
assert_ok!(Society::bid(Origin::signed(20), 1000));
next_intake();
assert_ok!(Society::vote(Origin::signed(10), 20, true));
conclude_intake(true, None);
// payout in queue
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(8, 1000)].try_into().unwrap() }
);
assert_noop!(Society::payout(Origin::signed(20)), Error::<Test>::NoPayout);
// slash payout
assert_eq!(Society::slash_payout(&20, 500), 500);
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(8, 500)].try_into().unwrap() }
);
run_to_block(8);
// payout should be here, but 500 less
assert_ok!(Society::payout(RuntimeOrigin::signed(20)));
assert_eq!(Balances::free_balance(20), 550);
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 500, payouts: Default::default() }
);
});
}
#[test]
fn slash_payout_multi_works() {
EnvBuilder::new().execute(|| {
assert_eq!(Balances::free_balance(20), 50);
place_members([20]);
// create a few payouts
Society::bump_payout(&20, 5, 100);
Society::bump_payout(&20, 10, 100);
Society::bump_payout(&20, 15, 100);
Society::bump_payout(&20, 20, 100);
// payouts in queue
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord {
paid: 0,
payouts: vec![(5, 100), (10, 100), (15, 100), (20, 100)].try_into().unwrap()
}
);
// slash payout
assert_eq!(Society::slash_payout(&20, 250), 250);
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(15, 50), (20, 100)].try_into().unwrap() }
);
// slash again
assert_eq!(Society::slash_payout(&20, 50), 50);
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(20, 100)].try_into().unwrap() }
);
});
}
#[test]
fn suspended_member_life_cycle_works() {
EnvBuilder::new().execute(|| {
// Add 20 to members, who is not the head and can be suspended/removed.
place_members([20]);
assert_eq!(members(), vec![10, 20]);
assert_eq!(Members::<Test>::get(20).unwrap().strikes, 0);
assert!(!SuspendedMembers::<Test>::contains_key(20));
// Let's suspend account 20 by giving them 2 strikes by not voting
assert_ok!(Society::bid(Origin::signed(30), 0));
assert_ok!(Society::bid(Origin::signed(40), 1));
next_intake();
conclude_intake(false, None);
// 2 strikes are accumulated, and 20 is suspended :(
assert!(SuspendedMembers::<Test>::contains_key(20));
assert_eq!(members(), vec![10]);
// Suspended members cannot get payout
Society::bump_payout(&20, 10, 100);
assert_noop!(Society::payout(Origin::signed(20)), Error::<Test>::NotMember);
// Normal people cannot make judgement
assert_noop!(
Society::judge_suspended_member(Origin::signed(20), 20, true),
Error::<Test>::NotFounder
);
// Suspension judgment origin can judge thee
// Suspension judgement origin forgives the suspended member
assert_ok!(Society::judge_suspended_member(Origin::signed(10), 20, true));
assert!(!SuspendedMembers::<Test>::contains_key(20));
assert_eq!(members(), vec![10, 20]);
// Let's suspend them again, directly
assert_ok!(Society::suspend_member(&20));
assert!(SuspendedMembers::<Test>::contains_key(20));
// Suspension judgement origin does not forgive the suspended member
assert_ok!(Society::judge_suspended_member(Origin::signed(10), 20, false));
// Cleaned up
assert!(!SuspendedMembers::<Test>::contains_key(20));
assert_eq!(members(), vec![10]);
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![].try_into().unwrap() }
);
});
}
#[test]
fn suspended_candidate_rejected_works() {
EnvBuilder::new().execute(|| {
place_members([20, 30]);
// 40, 50, 60, 70, 80 make bids
for &x in &[40u128, 50, 60, 70] {
assert_ok!(Society::bid(Origin::signed(x), 10));
assert_eq!(Balances::free_balance(x), 25);
assert_eq!(Balances::reserved_balance(x), 25);
}
// Rotation Period
next_intake();
assert_eq!(
candidacies(),
vec![
(40, candidacy(1, 10, Deposit(25), 0, 0)),
(50, candidacy(1, 10, Deposit(25), 0, 0)),
(60, candidacy(1, 10, Deposit(25), 0, 0)),
(70, candidacy(1, 10, Deposit(25), 0, 0)),
]
);
// Split vote over all.
for &x in &[40, 50, 60, 70] {
assert_ok!(Society::vote(Origin::signed(20), x, false));
assert_ok!(Society::vote(Origin::signed(30), x, true));
}
// Voting continues, as no canidate is clearly accepted yet and the founder chooses not to
// act.
conclude_intake(false, None);
assert_eq!(members(), vec![10, 20, 30]);
assert_eq!(candidates(), vec![40, 50, 60, 70]);
// 40 gets approved after founder weighs in giving it a clear approval.
// but the founder's rejection of 60 doesn't do much for now.
assert_ok!(Society::vote(Origin::signed(10), 40, true));
assert_ok!(Society::vote(Origin::signed(10), 60, false));
conclude_intake(false, None);
assert_eq!(members(), vec![10, 20, 30, 40]);
assert_eq!(candidates(), vec![50, 60, 70]);
assert_eq!(Balances::free_balance(40), 50);
assert_eq!(Balances::reserved_balance(40), 0);
assert_eq!(Balances::free_balance(Society::account_id()), 9990);
// Founder manually bestows membership on 50 and and kicks 70.
assert_ok!(Society::bestow_membership(Origin::signed(10), 50));
assert_eq!(members(), vec![10, 20, 30, 40, 50]);
assert_eq!(candidates(), vec![60, 70]);
assert_eq!(Balances::free_balance(50), 50);
assert_eq!(Balances::reserved_balance(50), 0);
assert_eq!(Balances::free_balance(Society::account_id()), 9980);
assert_eq!(Balances::free_balance(70), 25);
assert_eq!(Balances::reserved_balance(70), 25);
assert_ok!(Society::kick_candidate(Origin::signed(10), 70));
assert_eq!(members(), vec![10, 20, 30, 40, 50]);
assert_eq!(candidates(), vec![60]);
assert_eq!(Balances::free_balance(70), 25);
assert_eq!(Balances::reserved_balance(70), 0);
assert_eq!(Balances::free_balance(Society::account_id()), 10005);
// Next round doesn't make much difference.
next_intake();
conclude_intake(false, None);
assert_eq!(members(), vec![10, 20, 30, 40, 50]);
assert_eq!(candidates(), vec![60]);
assert_eq!(Balances::free_balance(Society::account_id()), 10005);
// But after two rounds, the clearly rejected 60 gets dropped and slashed.
next_intake();
conclude_intake(false, None);
assert_eq!(members(), vec![10, 20, 30, 40, 50]);
assert_eq!(candidates(), vec![]);
assert_eq!(Balances::free_balance(60), 25);
assert_eq!(Balances::reserved_balance(60), 0);
assert_eq!(Balances::free_balance(Society::account_id()), 10030);
});
}
#[test]
fn unpaid_vouch_works() {
EnvBuilder::new().execute(|| {
// 10 is the only member
assert_eq!(members(), vec![10]);
// A non-member cannot vouch
assert_noop!(Society::vouch(Origin::signed(1), 20, 1000, 100), Error::<Test>::NotMember);
// A member can though
assert_ok!(Society::vouch(Origin::signed(10), 20, 1000, 100));
assert_eq!(Members::<Test>::get(10).unwrap().vouching, Some(VouchingStatus::Vouching));
// A member cannot vouch twice at the same time
assert_noop!(
Society::vouch(Origin::signed(10), 30, 100, 0),
Error::<Test>::AlreadyVouching
);
// Vouching creates the right kind of bid
assert_eq!(Bids::<Test>::get().into_inner(), vec![bid(20, Vouch(10, 100), 1000)]);
// Vouched user can become candidate
next_intake();
assert_eq!(candidacies(), vec![(20, candidacy(1, 1000, Vouch(10, 100), 0, 0))]);
// Vote yes
assert_ok!(Society::vote(RuntimeOrigin::signed(10), 20, true));
// Vouched user can win
conclude_intake(true, None);
assert_eq!(members(), vec![10, 20]);
// Vouched user gets whatever remains after the voucher's reservation.
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(8, 900)].try_into().unwrap() }
);
// 10 is no longer vouching
assert_eq!(Members::<Test>::get(10).unwrap().vouching, None);
});
}
#[test]
fn paid_vouch_works() {
EnvBuilder::new().execute(|| {
place_members([20]);
assert_eq!(members(), vec![10, 20]);
assert_ok!(Society::vouch(Origin::signed(20), 30, 1000, 100));
assert_eq!(Members::<Test>::get(20).unwrap().vouching, Some(VouchingStatus::Vouching));
assert_eq!(Bids::<Test>::get().into_inner(), vec![bid(30, Vouch(20, 100), 1000)]);
next_intake();
assert_eq!(candidacies(), vec![(30, candidacy(1, 1000, Vouch(20, 100), 0, 0))]);
assert_ok!(Society::vote(Origin::signed(20), 30, true));
conclude_intake(true, None);
assert_eq!(members(), vec![10, 20, 30]);
// Voucher wins a portion of the payment
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(8, 100)].try_into().unwrap() }
);
// Vouched user wins the rest
assert_eq!(
Payouts::<Test>::get(30),
PayoutRecord { paid: 0, payouts: vec![(8, 900)].try_into().unwrap() }
);
// 20 is no longer vouching
assert_eq!(Members::<Test>::get(20).unwrap().vouching, None);
});
}
#[test]
fn voucher_cannot_win_more_than_bid() {
EnvBuilder::new().execute(|| {
place_members([20]);
// 20 vouches, but asks for more than the bid
assert_ok!(Society::vouch(Origin::signed(20), 30, 100, 1000));
// Vouching creates the right kind of bid
assert_eq!(Bids::<Test>::get().into_inner(), vec![bid(30, Vouch(20, 1000), 100)]);
// Vouched user can become candidate
next_intake();
assert_eq!(candidacies(), vec![(30, candidacy(1, 100, Vouch(20, 1000), 0, 0))]);
// Vote yes
assert_ok!(Society::vote(Origin::signed(20), 30, true));
// Vouched user can win
conclude_intake(true, None);
assert_eq!(members(), vec![10, 20, 30]);
// Voucher wins as much as the bid
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(8, 100)].try_into().unwrap() }
);
// Vouched user gets nothing
assert_eq!(
Payouts::<Test>::get(30),
PayoutRecord { paid: 0, payouts: vec![].try_into().unwrap() }
);
});
}
#[test]
fn unvouch_works() {
EnvBuilder::new().execute(|| {
// 10 is the only member
assert_eq!(members(), vec![10]);
// 10 vouches for 20
assert_ok!(Society::vouch(RuntimeOrigin::signed(10), 20, 100, 0));
// 20 has a bid
assert_eq!(Bids::<Test>::get().into_inner(), vec![bid(20, Vouch(10, 0), 100)]);
// 10 is vouched
assert_eq!(Members::<Test>::get(10).unwrap().vouching, Some(VouchingStatus::Vouching));
// 10 can unvouch
assert_ok!(Society::unvouch(Origin::signed(10)));
// 20 no longer has a bid
assert_eq!(Bids::<Test>::get().into_inner(), vec![]);
// 10 is no longer vouching
assert_eq!(Members::<Test>::get(10).unwrap().vouching, None);
// Cannot unvouch after they become candidate
assert_ok!(Society::vouch(Origin::signed(10), 20, 100, 0));
next_intake();
assert_eq!(candidacies(), vec![(20, candidacy(1, 100, Vouch(10, 0), 0, 0))]);
assert_noop!(Society::unvouch(Origin::signed(10)), Error::<Test>::NotVouchingOnBidder);
// 10 is still vouching until candidate is approved or rejected
assert_eq!(Members::<Test>::get(10).unwrap().vouching, Some(VouchingStatus::Vouching));
// Voucher inexplicably votes against their pick.
assert_ok!(Society::vote(Origin::signed(10), 20, false));
// But their pick doesn't resign (yet).
conclude_intake(false, None);
// Voting still happening and voucher cannot unvouch.
assert_eq!(candidacies(), vec![(20, candidacy(1, 100, Vouch(10, 0), 0, 1))]);
assert_eq!(Members::<Test>::get(10).unwrap().vouching, Some(VouchingStatus::Vouching));
// Candidate gives in and resigns.
conclude_intake(true, None);
// Vouxher (10) is banned from vouching.
assert_eq!(Members::<Test>::get(10).unwrap().vouching, Some(VouchingStatus::Banned));
assert_eq!(members(), vec![10]);
// 10 cannot vouch again
assert_noop!(
Society::vouch(Origin::signed(10), 30, 100, 0),
Error::<Test>::AlreadyVouching
);
// 10 cannot unvouch either, so they are banned forever.
assert_noop!(Society::unvouch(Origin::signed(10)), Error::<Test>::NotVouchingOnBidder);
});
}
#[test]
fn unbid_vouch_works() {
EnvBuilder::new().execute(|| {
// 10 is the only member
assert_eq!(members(), vec![10]);
// 10 vouches for 20
assert_ok!(Society::vouch(RuntimeOrigin::signed(10), 20, 100, 0));
// 20 has a bid
assert_eq!(Bids::<Test>::get().into_inner(), vec![bid(20, Vouch(10, 0), 100)]);
// 10 is vouched
assert_eq!(Members::<Test>::get(10).unwrap().vouching, Some(VouchingStatus::Vouching));
// 20 doesn't want to be a member and can unbid themselves.
assert_ok!(Society::unbid(Origin::signed(20)));
// Everything is cleaned up
assert_eq!(Members::<Test>::get(10).unwrap().vouching, None);
assert_eq!(Bids::<Test>::get().into_inner(), vec![]);
});
}
#[test]
fn founder_and_head_cannot_be_removed() {
EnvBuilder::new().execute(|| {
// 10 is the only member, founder, and head
assert_eq!(members(), vec![10]);
assert_eq!(Founder::<Test>::get(), Some(10));
assert_eq!(Head::<Test>::get(), Some(10));
// 10 can still accumulate strikes
assert_ok!(Society::bid(Origin::signed(20), 0));
next_intake();
conclude_intake(false, None);
assert_eq!(Members::<Test>::get(10).unwrap().strikes, 1);
assert_ok!(Society::bid(Origin::signed(30), 0));
next_intake();
conclude_intake(false, None);
assert_eq!(Members::<Test>::get(10).unwrap().strikes, 2);
// Awkwardly they can obtain more than MAX_STRIKES...
assert_ok!(Society::bid(Origin::signed(40), 0));
next_intake();
conclude_intake(false, None);
assert_eq!(Members::<Test>::get(10).unwrap().strikes, 3);
// Replace the head
assert_ok!(Society::bid(Origin::signed(50), 0));
next_intake();
assert_ok!(Society::vote(Origin::signed(10), 50, true));
conclude_intake(false, None);
assert_eq!(members(), vec![10, 50]);
assert_eq!(Head::<Test>::get(), Some(10));
next_intake();
assert_eq!(Head::<Test>::get(), Some(50));
// Founder is unchanged
assert_eq!(Founder::<Test>::get(), Some(10));
// 50 can still accumulate strikes
assert_ok!(Society::bid(Origin::signed(60), 0));
next_intake();
// Force 50 to be Skeptic so it gets a strike.
Skeptic::<Test>::put(50);
conclude_intake(false, None);
assert_eq!(Members::<Test>::get(50).unwrap().strikes, 1);
assert_ok!(Society::bid(Origin::signed(70), 0));
next_intake();
// Force 50 to be Skeptic so it gets a strike.
Skeptic::<Test>::put(50);
conclude_intake(false, None);
assert_eq!(Members::<Test>::get(50).unwrap().strikes, 2);
// Replace the head
assert_ok!(Society::bid(Origin::signed(80), 0));
next_intake();
assert_ok!(Society::vote(Origin::signed(10), 80, true));
assert_ok!(Society::vote(Origin::signed(50), 80, true));
conclude_intake(false, None);
next_intake();
assert_eq!(members(), vec![10, 50, 80]);
assert_eq!(Head::<Test>::get(), Some(80));
assert_eq!(Founder::<Test>::get(), Some(10));
// 50 can now be suspended for strikes
assert_ok!(Society::bid(Origin::signed(90), 0));
next_intake();
// Force 50 to be Skeptic and get it a strike.
Skeptic::<Test>::put(50);
conclude_intake(false, None);
next_intake();
assert_eq!(
SuspendedMembers::<Test>::get(50),
Some(MemberRecord { rank: 0, strikes: 3, vouching: None, index: 1 })
);
assert_eq!(members(), vec![10, 80]);
});
}
#[test]
fn challenges_work() {
EnvBuilder::new().execute(|| {
// Add some members
place_members([20, 30, 40]);
// Votes are empty
assert_eq!(DefenderVotes::<Test>::get(0, 10), None);
assert_eq!(DefenderVotes::<Test>::get(0, 20), None);
assert_eq!(DefenderVotes::<Test>::get(0, 30), None);
assert_eq!(DefenderVotes::<Test>::get(0, 40), None);
// Check starting point
assert_eq!(members(), vec![10, 20, 30, 40]);
assert_eq!(Defending::<Test>::get(), None);
// 30 will be challenged during the challenge rotation
next_challenge();
assert_eq!(Defending::<Test>::get().unwrap().0, 30);
// They can always free vote for themselves
assert_ok!(Society::defender_vote(Origin::signed(30), true));
// If no one else votes, nothing happens
next_challenge();
assert_eq!(members(), vec![10, 20, 30, 40]);
// Reset votes for last challenge
assert_ok!(Society::cleanup_challenge(Origin::signed(0), 0, 10));
// New challenge period
assert_eq!(Defending::<Test>::get().unwrap().0, 30);
// Non-member cannot vote
assert_noop!(Society::defender_vote(Origin::signed(1), true), Error::<Test>::NotMember);
// 3 people say accept, 1 reject
assert_ok!(Society::defender_vote(Origin::signed(10), true));
assert_ok!(Society::defender_vote(Origin::signed(20), true));
assert_ok!(Society::defender_vote(Origin::signed(30), true));
assert_ok!(Society::defender_vote(Origin::signed(40), false));
next_challenge();
// 30 survives
assert_eq!(members(), vec![10, 20, 30, 40]);
// Reset votes for last challenge
assert_ok!(Society::cleanup_challenge(Origin::signed(0), 1, 10));
// Votes are reset
assert_eq!(DefenderVotes::<Test>::get(0, 10), None);
assert_eq!(DefenderVotes::<Test>::get(0, 20), None);
assert_eq!(DefenderVotes::<Test>::get(0, 30), None);
assert_eq!(DefenderVotes::<Test>::get(0, 40), None);
// One more time
assert_eq!(Defending::<Test>::get().unwrap().0, 30);
// 2 people say accept, 2 reject
assert_ok!(Society::defender_vote(Origin::signed(10), true));
assert_ok!(Society::defender_vote(Origin::signed(20), true));
assert_ok!(Society::defender_vote(Origin::signed(30), false));
assert_ok!(Society::defender_vote(Origin::signed(40), false));
next_challenge();
// 30 is suspended
assert_eq!(members(), vec![10, 20, 40]);
assert_eq!(
SuspendedMembers::<Test>::get(30),
Some(MemberRecord { rank: 0, strikes: 0, vouching: None, index: 2 })
);
// Reset votes for last challenge
assert_ok!(Society::cleanup_challenge(Origin::signed(0), 2, 10));
// New defender is chosen
assert_eq!(Defending::<Test>::get().unwrap().0, 20);
// Votes are reset
assert_eq!(DefenderVotes::<Test>::get(0, 10), None);
assert_eq!(DefenderVotes::<Test>::get(0, 20), None);
assert_eq!(DefenderVotes::<Test>::get(0, 30), None);
assert_eq!(DefenderVotes::<Test>::get(0, 40), None);
});
}
#[test]
fn bad_vote_slash_works() {
EnvBuilder::new().execute(|| {
// Add some members
place_members([20, 30, 40, 50]);
assert_eq!(members(), vec![10, 20, 30, 40, 50]);
// Create some payouts
Society::bump_payout(&20, 5, 100);
Society::bump_payout(&30, 5, 100);
Society::bump_payout(&40, 5, 100);
Society::bump_payout(&50, 5, 100);
// Check starting point
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(5, 100)].try_into().unwrap() }
);
assert_eq!(
Payouts::<Test>::get(30),
PayoutRecord { paid: 0, payouts: vec![(5, 100)].try_into().unwrap() }
);
assert_eq!(
Payouts::<Test>::get(40),
PayoutRecord { paid: 0, payouts: vec![(5, 100)].try_into().unwrap() }
);
assert_eq!(
Payouts::<Test>::get(50),
PayoutRecord { paid: 0, payouts: vec![(5, 100)].try_into().unwrap() }
);
// Create a new bid
assert_ok!(Society::bid(Origin::signed(60), 1000));
next_intake();
// Force 20 to be the skeptic, and make it vote against the settled majority.
Skeptic::<Test>::put(20);
assert_ok!(Society::vote(Origin::signed(20), 60, true));
assert_ok!(Society::vote(Origin::signed(30), 60, false));
assert_ok!(Society::vote(Origin::signed(40), 60, false));
assert_ok!(Society::vote(Origin::signed(50), 60, false));
conclude_intake(false, None);
// Wrong voter gained a strike
assert_eq!(Members::<Test>::get(20).unwrap().strikes, 1);
assert_eq!(Members::<Test>::get(30).unwrap().strikes, 0);
assert_eq!(Members::<Test>::get(40).unwrap().strikes, 0);
assert_eq!(Members::<Test>::get(50).unwrap().strikes, 0);
// Their payout is slashed, a random person is rewarded
assert_eq!(
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![(5, 50)].try_into().unwrap() }
);
assert_eq!(
Payouts::<Test>::get(30),
PayoutRecord { paid: 0, payouts: vec![(5, 100)].try_into().unwrap() }
);
assert_eq!(
Payouts::<Test>::get(40),
PayoutRecord { paid: 0, payouts: vec![(5, 100)].try_into().unwrap() }
);
assert_eq!(
Payouts::<Test>::get(50),
PayoutRecord { paid: 0, payouts: vec![(5, 100)].try_into().unwrap() }
);
});
}
#[test]
fn user_cannot_bid_twice() {
EnvBuilder::new().execute(|| {
// Cannot bid twice
assert_ok!(Society::bid(Origin::signed(20), 100));
assert_noop!(Society::bid(Origin::signed(20), 100), Error::<Test>::AlreadyBid);
// Cannot bid when vouched
assert_ok!(Society::vouch(Origin::signed(10), 30, 100, 100));
assert_noop!(Society::bid(Origin::signed(30), 100), Error::<Test>::AlreadyBid);
// Cannot vouch when already bid
place_members([50]);
assert_noop!(Society::vouch(Origin::signed(50), 20, 100, 100), Error::<Test>::AlreadyBid);
});
}
#[test]
fn vouching_handles_removed_member_with_bid() {
EnvBuilder::new().execute(|| {
// Add a member
place_members([20]);
// Have that member vouch for a user
assert_ok!(Society::vouch(RuntimeOrigin::signed(20), 30, 1000, 100));
// That user is now a bid and the member is vouching
assert_eq!(Bids::<Test>::get().into_inner(), vec![bid(30, Vouch(20, 100), 1000)]);
assert_eq!(Members::<Test>::get(20).unwrap().vouching, Some(VouchingStatus::Vouching));
// Suspend that member
assert_ok!(Society::suspend_member(&20));
// Bid is removed, vouching status is removed
let r = MemberRecord { rank: 0, strikes: 0, vouching: None, index: 1 };
assert_eq!(SuspendedMembers::<Test>::get(20), Some(r));