-
Notifications
You must be signed in to change notification settings - Fork 49
/
lib.rs
2089 lines (1740 loc) · 71.7 KB
/
lib.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
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! # Phragmen Election Module.
//!
//! An election module based on sequential phragmen.
//!
//! ### Term and Round
//!
//! The election happens in _rounds_: every `N` blocks, all previous members are retired and a new
//! set is elected (which may or may not have an intersection with the previous set). Each round
//! lasts for some number of blocks defined by `TermDuration` storage item. The words _term_ and
//! _round_ can be used interchangeably in this context.
//!
//! `TermDuration` might change during a round. This can shorten or extend the length of the round.
//! The next election round's block number is never stored but rather always checked on the fly.
//! Based on the current block number and `TermDuration`, the condition `BlockNumber % TermDuration
//! == 0` being satisfied will always trigger a new election round.
//!
//! ### Voting
//!
//! Voters can vote for any set of the candidates by providing a list of account ids. Invalid votes
//! (voting for non-candidates) are ignored during election. Yet, a voter _might_ vote for a future
//! candidate. Voters reserve a bond as they vote. Each vote defines a `value`. This amount is
//! locked from the account of the voter and indicates the weight of the vote. Voters can update
//! their votes at any time by calling `vote()` again. This keeps the bond untouched but can
//! optionally change the locked `value`. After a round, votes are kept and might still be valid for
//! further rounds. A voter is responsible for calling `remove_voter` once they are done to have
//! their bond back and remove the lock.
//!
//! Voters also report other voters as being defunct to earn their bond. A voter is defunct once all
//! of the candidates that they have voted for are neither a valid candidate anymore nor a member.
//! Upon reporting, if the target voter is actually defunct, the reporter will be rewarded by the
//! voting bond of the target. The target will lose their bond and get removed. If the target is not
//! defunct, the reporter is slashed and removed. To prevent being reported, voters should manually
//! submit a `remove_voter()` as soon as they are in the defunct state.
//!
//! ### Candidacy and Members
//!
//! Candidates also reserve a bond as they submit candidacy. A candidate cannot take their candidacy
//! back. A candidate can end up in one of the below situations:
//! - **Winner**: A winner is kept as a _member_. They must still have a bond in reserve and they
//! are automatically counted as a candidate for the next election.
//! - **Runner-up**: Runners-up are the best candidates immediately after the winners. The number
//! of runners_up to keep is configurable. Runners-up are used, in order that they are elected,
//! as replacements when a candidate is kicked by `[remove_member]`, or when an active member
//! renounces their candidacy. Runners are automatically counted as a candidate for the next
//! election.
//! - **Loser**: Any of the candidate who are not a winner are left as losers. A loser might be an
//! _outgoing member or runner_, meaning that they are an active member who failed to keep their
//! spot. An outgoing will always lose their bond.
//!
//! ##### Renouncing candidacy.
//!
//! All candidates, elected or not, can renounce their candidacy. A call to [`renounce_candidacy`]
//! will always cause the candidacy bond to be refunded.
//!
//! Note that with the members being the default candidates for the next round and votes persisting
//! in storage, the election system is entirely stable given no further input. This means that if
//! the system has a particular set of candidates `C` and voters `V` that lead to a set of members
//! `M` being elected, as long as `V` and `C` don't remove their candidacy and votes, `M` will keep
//! being re-elected at the end of each round.
//!
//! ### Module Information
//!
//! - [`election_sp_phragmen::Trait`](./trait.Trait.html)
//! - [`Call`](./enum.Call.html)
//! - [`Module`](./struct.Module.html)
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::{
decl_error, decl_event, decl_module, decl_storage, ensure,
traits::{ChangeMembers, Currency, Get, LockIdentifier, OnUnbalanced, ReservableCurrency},
weights::SimpleDispatchInfo,
};
use frame_system::{self as system, ensure_root, ensure_signed};
use sp_phragmen::ExtendedBalance;
use sp_runtime::{
print,
traits::{Bounded, Convert, StaticLookup, Zero},
DispatchError, DispatchResult,
};
use sp_std::prelude::*;
use darwinia_support::{LockableCurrency, NormalLock, WithdrawLock, WithdrawReason, WithdrawReasons};
const MODULE_ID: LockIdentifier = *b"phrelect";
/// The maximum votes allowed per voter.
pub const MAXIMUM_VOTE: usize = 16;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
type NegativeImbalanceOf<T> =
<<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
pub trait Trait: frame_system::Trait {
/// The overarching event type.c
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
/// The currency that people are electing with.
type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber> + ReservableCurrency<Self::AccountId>;
/// What to do when the members change.
type ChangeMembers: ChangeMembers<Self::AccountId>;
/// Convert a balance into a number used for election calculation.
/// This must fit into a `u64` but is allowed to be sensibly lossy.
type CurrencyToVote: Convert<BalanceOf<Self>, u64> + Convert<u128, BalanceOf<Self>>;
/// How much should be locked up in order to submit one's candidacy.
type CandidacyBond: Get<BalanceOf<Self>>;
/// How much should be locked up in order to be able to submit votes.
type VotingBond: Get<BalanceOf<Self>>;
/// Handler for the unbalanced reduction when a candidate has lost (and is not a runner-up)
type LoserCandidate: OnUnbalanced<NegativeImbalanceOf<Self>>;
/// Handler for the unbalanced reduction when a reporter has submitted a bad defunct report.
type BadReport: OnUnbalanced<NegativeImbalanceOf<Self>>;
/// Handler for the unbalanced reduction when a member has been kicked.
type KickedMember: OnUnbalanced<NegativeImbalanceOf<Self>>;
/// Number of members to elect.
type DesiredMembers: Get<u32>;
/// Number of runners_up to keep.
type DesiredRunnersUp: Get<u32>;
/// How long each seat is kept. This defines the next block number at which an election
/// round will happen. If set to zero, no elections are ever triggered and the module will
/// be in passive mode.
type TermDuration: Get<Self::BlockNumber>;
}
decl_storage! {
trait Store for Module<T: Trait> as PhragmenElection {
// ---- State
/// The current elected membership. Sorted based on account id.
pub Members get(fn members): Vec<(T::AccountId, BalanceOf<T>)>;
/// The current runners_up. Sorted based on low to high merit (worse to best runner).
pub RunnersUp get(fn runners_up): Vec<(T::AccountId, BalanceOf<T>)>;
/// The total number of vote rounds that have happened, excluding the upcoming one.
pub ElectionRounds get(fn election_rounds): u32 = Zero::zero();
/// Votes of a particular voter, with the round index of the votes.
pub VotesOf get(fn votes_of): linked_map T::AccountId => Vec<T::AccountId>;
/// Locked stake of a voter.
pub StakeOf get(fn stake_of): map T::AccountId => BalanceOf<T>;
/// The present candidate list. Sorted based on account id. A current member can never enter
/// this vector and is always implicitly assumed to be a candidate.
pub Candidates get(fn candidates): Vec<T::AccountId>;
}
}
decl_error! {
/// Error for the elections-phragmen module.
pub enum Error for Module<T: Trait> {
/// Cannot vote when no candidates or members exist.
UnableToVote,
/// Must vote for at least one candidate.
NoVotes,
/// Cannot vote more than candidates.
TooManyVotes,
/// Cannot vote more than maximum allowed.
MaximumVotesExceeded,
/// Cannot vote with stake less than minimum balance.
LowBalance,
/// Voter can not pay voting bond.
UnableToPayBond,
/// Must be a voter.
MustBeVoter,
/// Cannot report self.
ReportSelf,
/// Duplicated candidate submission.
DuplicatedCandidate,
/// Member cannot re-submit candidacy.
MemberSubmit,
/// Runner cannot re-submit candidacy.
RunnerSubmit,
/// Candidate does not have enough funds.
InsufficientCandidateFunds,
/// Origin is not a candidate, member or a runner up.
InvalidOrigin,
/// Not a member.
NotMember,
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
fn deposit_event() = default;
const CandidacyBond: BalanceOf<T> = T::CandidacyBond::get();
const VotingBond: BalanceOf<T> = T::VotingBond::get();
const DesiredMembers: u32 = T::DesiredMembers::get();
const DesiredRunnersUp: u32 = T::DesiredRunnersUp::get();
const TermDuration: T::BlockNumber = T::TermDuration::get();
/// Vote for a set of candidates for the upcoming round of election.
///
/// The `votes` should:
/// - not be empty.
/// - be less than the number of candidates.
///
/// Upon voting, `value` units of `who`'s balance is locked and a bond amount is reserved.
/// It is the responsibility of the caller to not place all of their balance into the lock
/// and keep some for further transactions.
///
/// # <weight>
/// #### State
/// Reads: O(1)
/// Writes: O(V) given `V` votes. V is bounded by 16.
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(100_000)]
fn vote(origin, votes: Vec<T::AccountId>, #[compact] value: BalanceOf<T>) {
let who = ensure_signed(origin)?;
let candidates_count = <Candidates<T>>::decode_len().unwrap_or(0) as usize;
let members_count = <Members<T>>::decode_len().unwrap_or(0) as usize;
// addition is valid: candidates and members never overlap.
let allowed_votes = candidates_count + members_count;
ensure!(!allowed_votes.is_zero(), Error::<T>::UnableToVote);
ensure!(votes.len() <= allowed_votes, Error::<T>::TooManyVotes);
ensure!(votes.len() <= MAXIMUM_VOTE, Error::<T>::MaximumVotesExceeded);
ensure!(!votes.is_empty(), Error::<T>::NoVotes);
ensure!(
value > T::Currency::minimum_balance(),
Error::<T>::LowBalance,
);
if !Self::is_voter(&who) {
// first time voter. Reserve bond.
T::Currency::reserve(&who, T::VotingBond::get())
.map_err(|_| Error::<T>::UnableToPayBond)?;
}
// Amount to be locked up.
let locked_balance = value.min(T::Currency::total_balance(&who));
// lock
T::Currency::set_lock(
MODULE_ID,
&who,
WithdrawLock::Normal(NormalLock {
amount: locked_balance,
until: T::BlockNumber::max_value(),
}),
WithdrawReasons::except(WithdrawReason::TransactionPayment),
);
<StakeOf<T>>::insert(&who, locked_balance);
<VotesOf<T>>::insert(&who, votes);
}
/// Remove `origin` as a voter. This removes the lock and returns the bond.
///
/// # <weight>
/// #### State
/// Reads: O(1)
/// Writes: O(1)
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
fn remove_voter(origin) {
let who = ensure_signed(origin)?;
ensure!(Self::is_voter(&who), Error::<T>::MustBeVoter);
Self::do_remove_voter(&who, true);
}
/// Report `target` for being an defunct voter. In case of a valid report, the reporter is
/// rewarded by the bond amount of `target`. Otherwise, the reporter itself is removed and
/// their bond is slashed.
///
/// A defunct voter is defined to be:
/// - a voter whose current submitted votes are all invalid. i.e. all of them are no
/// longer a candidate nor an active member.
///
/// # <weight>
/// #### State
/// Reads: O(NLogM) given M current candidates and N votes for `target`.
/// Writes: O(1)
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(1_000_000)]
fn report_defunct_voter(origin, target: <T::Lookup as StaticLookup>::Source) {
let reporter = ensure_signed(origin)?;
let target = T::Lookup::lookup(target)?;
ensure!(reporter != target, Error::<T>::ReportSelf);
ensure!(Self::is_voter(&reporter), Error::<T>::MustBeVoter);
// Checking if someone is a candidate and a member here is O(LogN), making the whole
// function O(MLonN) with N candidates in total and M of them being voted by `target`.
// We could easily add another mapping to be able to check if someone is a candidate in
// `O(1)` but that would make the process of removing candidates at the end of each
// round slightly harder. Note that for now we have a bound of number of votes (`N`).
let valid = Self::is_defunct_voter(&target);
if valid {
// reporter will get the voting bond of the target
T::Currency::repatriate_reserved(&target, &reporter, T::VotingBond::get())?;
// remove the target. They are defunct.
Self::do_remove_voter(&target, false);
} else {
// slash the bond of the reporter.
let imbalance = T::Currency::slash_reserved(&reporter, T::VotingBond::get()).0;
T::BadReport::on_unbalanced(imbalance);
// remove the reporter.
Self::do_remove_voter(&reporter, false);
}
Self::deposit_event(RawEvent::VoterReported(target, reporter, valid));
}
/// Submit oneself for candidacy.
///
/// A candidate will either:
/// - Lose at the end of the term and forfeit their deposit.
/// - Win and become a member. Members will eventually get their stash back.
/// - Become a runner-up. Runners-ups are reserved members in case one gets forcefully
/// removed.
///
/// # <weight>
/// #### State
/// Reads: O(LogN) Given N candidates.
/// Writes: O(1)
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(500_000)]
fn submit_candidacy(origin) {
let who = ensure_signed(origin)?;
let is_candidate = Self::is_candidate(&who);
ensure!(is_candidate.is_err(), Error::<T>::DuplicatedCandidate);
// assured to be an error, error always contains the index.
let index = is_candidate.unwrap_err();
ensure!(!Self::is_member(&who), Error::<T>::MemberSubmit);
ensure!(!Self::is_runner(&who), Error::<T>::RunnerSubmit);
T::Currency::reserve(&who, T::CandidacyBond::get())
.map_err(|_| Error::<T>::InsufficientCandidateFunds)?;
<Candidates<T>>::mutate(|c| c.insert(index, who));
}
/// Renounce one's intention to be a candidate for the next election round. 3 potential
/// outcomes exist:
/// - `origin` is a candidate and not elected in any set. In this case, the bond is
/// unreserved, returned and origin is removed as a candidate.
/// - `origin` is a current runner up. In this case, the bond is unreserved, returned and
/// origin is removed as a runner.
/// - `origin` is a current member. In this case, the bond is unreserved and origin is
/// removed as a member, consequently not being a candidate for the next round anymore.
/// Similar to [`remove_voter`], if replacement runners exists, they are immediately used.
#[weight = SimpleDispatchInfo::FixedOperational(2_000_000)]
fn renounce_candidacy(origin) {
let who = ensure_signed(origin)?;
// NOTE: this function attempts the 3 conditions (being a candidate, member, runner) and
// fails if none are matched. Unlike other Palette functions and modules where checks
// happen first and then execution happens, this function is written the other way
// around. The main intention is that reading all of the candidates, members and runners
// from storage is expensive. Furthermore, we know (soft proof) that they are always
// mutually exclusive. Hence, we try one, and only then decode more storage.
if let Ok(_replacement) = Self::remove_and_replace_member(&who) {
T::Currency::unreserve(&who, T::CandidacyBond::get());
Self::deposit_event(RawEvent::MemberRenounced(who.clone()));
// safety guard to make sure we do only one arm. Better to read runners later.
return Ok(());
}
let mut runners_up_with_stake = Self::runners_up();
if let Some(index) = runners_up_with_stake.iter()
.position(|(ref r, ref _s)| r == &who)
{
runners_up_with_stake.remove(index);
// unreserve the bond
T::Currency::unreserve(&who, T::CandidacyBond::get());
// update storage.
<RunnersUp<T>>::put(runners_up_with_stake);
// safety guard to make sure we do only one arm. Better to read runners later.
return Ok(());
}
let mut candidates = Self::candidates();
if let Ok(index) = candidates.binary_search(&who) {
candidates.remove(index);
// unreserve the bond
T::Currency::unreserve(&who, T::CandidacyBond::get());
// update storage.
<Candidates<T>>::put(candidates);
// safety guard to make sure we do only one arm. Better to read runners later.
return Ok(());
}
Err(Error::<T>::InvalidOrigin)?
}
/// Remove a particular member from the set. This is effective immediately and the bond of
/// the outgoing member is slashed.
///
/// If a runner-up is available, then the best runner-up will be removed and replaces the
/// outgoing member. Otherwise, a new phragmen round is started.
///
/// Note that this does not affect the designated block number of the next election.
///
/// # <weight>
/// #### State
/// Reads: O(do_phragmen)
/// Writes: O(do_phragmen)
/// # </weight>
#[weight = SimpleDispatchInfo::FixedOperational(2_000_000)]
fn remove_member(origin, who: <T::Lookup as StaticLookup>::Source) -> DispatchResult {
ensure_root(origin)?;
let who = T::Lookup::lookup(who)?;
Self::remove_and_replace_member(&who).map(|had_replacement| {
let (imbalance, _) = T::Currency::slash_reserved(&who, T::CandidacyBond::get());
T::KickedMember::on_unbalanced(imbalance);
Self::deposit_event(RawEvent::MemberKicked(who.clone()));
if !had_replacement {
Self::do_phragmen();
}
})
}
/// What to do at the end of each block. Checks if an election needs to happen or not.
fn on_initialize(n: T::BlockNumber) {
if let Err(e) = Self::end_block(n) {
print("Guru meditation");
print(e);
}
}
}
}
decl_event!(
pub enum Event<T> where
Balance = BalanceOf<T>,
<T as frame_system::Trait>::AccountId,
{
/// A new term with new members. This indicates that enough candidates existed, not that
/// enough have has been elected. The inner value must be examined for this purpose.
NewTerm(Vec<(AccountId, Balance)>),
/// No (or not enough) candidates existed for this round.
EmptyTerm,
/// A member has been removed. This should always be followed by either `NewTerm` ot
/// `EmptyTerm`.
MemberKicked(AccountId),
/// A member has renounced their candidacy.
MemberRenounced(AccountId),
/// A voter (first element) was reported (byt the second element) with the the report being
/// successful or not (third element).
VoterReported(AccountId, AccountId, bool),
}
);
impl<T: Trait> Module<T> {
/// Attempts to remove a member `who`. If a runner up exists, it is used as the replacement.
/// Otherwise, `Ok(false)` is returned to signal the caller.
///
/// In both cases, [`Members`], [`ElectionRounds`] and [`RunnersUp`] storage are updated
/// accordingly. Furthermore, the membership change is reported.
///
/// O(phragmen) in the worse case.
fn remove_and_replace_member(who: &T::AccountId) -> Result<bool, DispatchError> {
let mut members_with_stake = Self::members();
if let Ok(index) = members_with_stake.binary_search_by(|(ref m, ref _s)| m.cmp(who)) {
members_with_stake.remove(index);
let next_up = <RunnersUp<T>>::mutate(|runners_up| runners_up.pop());
let maybe_replacement = next_up.and_then(|(replacement, stake)| {
members_with_stake
.binary_search_by(|(ref m, ref _s)| m.cmp(&replacement))
.err()
.map(|index| {
members_with_stake.insert(index, (replacement.clone(), stake));
replacement
})
});
<Members<T>>::put(&members_with_stake);
let members = members_with_stake.into_iter().map(|m| m.0).collect::<Vec<_>>();
let result = Ok(maybe_replacement.is_some());
let old = [who.clone()];
match maybe_replacement {
Some(new) => T::ChangeMembers::change_members_sorted(&[new], &old, &members),
None => T::ChangeMembers::change_members_sorted(&[], &old, &members),
}
result
} else {
Err(Error::<T>::NotMember)?
}
}
/// Check if `who` is a candidate. It returns the insert index if the element does not exists as
/// an error.
///
/// State: O(LogN) given N candidates.
fn is_candidate(who: &T::AccountId) -> Result<(), usize> {
Self::candidates().binary_search(who).map(|_| ())
}
/// Check if `who` is a voter. It may or may not be a _current_ one.
///
/// State: O(1).
fn is_voter(who: &T::AccountId) -> bool {
<StakeOf<T>>::exists(who)
}
/// Check if `who` is currently an active member.
///
/// Limited number of members. Binary search. Constant time factor. O(1)
fn is_member(who: &T::AccountId) -> bool {
Self::members().binary_search_by(|(a, _b)| a.cmp(who)).is_ok()
}
/// Check if `who` is currently an active runner.
///
/// Limited number of runners-up. Binary search. Constant time factor. O(1)
fn is_runner(who: &T::AccountId) -> bool {
Self::runners_up().binary_search_by(|(a, _b)| a.cmp(who)).is_ok()
}
/// Returns number of desired members.
fn desired_members() -> u32 {
T::DesiredMembers::get()
}
/// Returns number of desired runners up.
fn desired_runners_up() -> u32 {
T::DesiredRunnersUp::get()
}
/// Returns the term duration
fn term_duration() -> T::BlockNumber {
T::TermDuration::get()
}
/// Get the members' account ids.
fn members_ids() -> Vec<T::AccountId> {
Self::members()
.into_iter()
.map(|(m, _)| m)
.collect::<Vec<T::AccountId>>()
}
/// The the runners' up account ids.
fn runners_up_ids() -> Vec<T::AccountId> {
Self::runners_up()
.into_iter()
.map(|(r, _)| r)
.collect::<Vec<T::AccountId>>()
}
/// Check if `who` is a defunct voter.
///
/// Note that false is returned if `who` is not a voter at all.
///
/// O(NLogM) with M candidates and `who` having voted for `N` of them.
fn is_defunct_voter(who: &T::AccountId) -> bool {
if Self::is_voter(who) {
Self::votes_of(who)
.iter()
.all(|v| !Self::is_member(v) && !Self::is_runner(v) && !Self::is_candidate(v).is_ok())
} else {
false
}
}
/// Remove a certain someone as a voter.
///
/// This will clean always clean the storage associated with the voter, and remove the balance
/// lock. Optionally, it would also return the reserved voting bond if indicated by `unreserve`.
fn do_remove_voter(who: &T::AccountId, unreserve: bool) {
// remove storage and lock.
<VotesOf<T>>::remove(who);
<StakeOf<T>>::remove(who);
T::Currency::remove_lock(MODULE_ID, who);
if unreserve {
T::Currency::unreserve(who, T::VotingBond::get());
}
}
/// The locked stake of a voter.
fn locked_stake_of(who: &T::AccountId) -> BalanceOf<T> {
Self::stake_of(who)
}
/// Check there's nothing to do this block.
///
/// Runs phragmen election and cleans all the previous candidate state. The voter state is NOT
/// cleaned and voters must themselves submit a transaction to retract.
fn end_block(block_number: T::BlockNumber) -> DispatchResult {
if !Self::term_duration().is_zero() {
if (block_number % Self::term_duration()).is_zero() {
Self::do_phragmen();
}
}
Ok(())
}
/// Run the phragmen election with all required side processes and state updates.
///
/// Calls the appropriate `ChangeMembers` function variant internally.
///
/// # <weight>
/// #### State
/// Reads: O(C + V*E) where C = candidates, V voters and E votes per voter exits.
/// Writes: O(M + R) with M desired members and R runners_up.
/// # </weight>
fn do_phragmen() {
let desired_seats = Self::desired_members() as usize;
let desired_runners_up = Self::desired_runners_up() as usize;
let num_to_elect = desired_runners_up + desired_seats;
let mut candidates = Self::candidates();
// candidates who explicitly called `submit_candidacy`. Only these folks are at the risk of
// losing their bond.
let exposed_candidates = candidates.clone();
// current members are always a candidate for the next round as well.
// this is guaranteed to not create any duplicates.
candidates.append(&mut Self::members_ids());
// previous runners_up are also always candidates for the next round.
candidates.append(&mut Self::runners_up_ids());
let voters_and_votes = <VotesOf<T>>::enumerate()
.map(|(v, i)| (v, i))
.collect::<Vec<(T::AccountId, Vec<T::AccountId>)>>();
let maybe_phragmen_result = sp_phragmen::elect::<_, _, _, T::CurrencyToVote>(
num_to_elect,
0,
candidates,
voters_and_votes,
Self::locked_stake_of,
);
if let Some(phragmen_result) = maybe_phragmen_result {
let old_members_ids = <Members<T>>::take()
.into_iter()
.map(|(m, _)| m)
.collect::<Vec<T::AccountId>>();
let old_runners_up_ids = <RunnersUp<T>>::take()
.into_iter()
.map(|(r, _)| r)
.collect::<Vec<T::AccountId>>();
// filter out those who had literally no votes at all.
// AUDIT/NOTE: the need to do this is because all candidates, even those who have no
// vote are still considered by phragmen and when good candidates are scarce, then these
// cheap ones might get elected. We might actually want to remove the filter and allow
// zero-voted candidates to also make it to the membership set.
let new_set_with_approval = phragmen_result.winners;
let new_set = new_set_with_approval
.into_iter()
.filter_map(|(m, a)| if a.is_zero() { None } else { Some(m) })
.collect::<Vec<T::AccountId>>();
let support_map = sp_phragmen::build_support_map::<_, _, _, T::CurrencyToVote>(
&new_set,
&phragmen_result.assignments,
Self::locked_stake_of,
);
let to_balance =
|e: ExtendedBalance| <T::CurrencyToVote as Convert<ExtendedBalance, BalanceOf<T>>>::convert(e);
let new_set_with_stake = new_set
.into_iter()
.map(|ref m| {
let support = support_map.get(m).expect(
"entire new_set was given to build_support_map; en entry must be \
created for each item; qed",
);
(m.clone(), to_balance(support.total))
})
.collect::<Vec<(T::AccountId, BalanceOf<T>)>>();
// split new set into winners and runners up.
let split_point = desired_seats.min(new_set_with_stake.len());
let mut new_members = (&new_set_with_stake[..split_point]).to_vec();
// save the runners up as-is. They are sorted based on desirability.
// sort and save the members.
new_members.sort_by(|i, j| i.0.cmp(&j.0));
// new_members_ids is sorted by account id.
let new_members_ids = new_members
.iter()
.map(|(m, _)| m.clone())
.collect::<Vec<T::AccountId>>();
let new_runners_up = &new_set_with_stake[split_point..]
.into_iter()
.cloned()
.rev()
.collect::<Vec<(T::AccountId, BalanceOf<T>)>>();
// new_runners_up remains sorted by desirability.
let new_runners_up_ids = new_runners_up
.iter()
.map(|(r, _)| r.clone())
.collect::<Vec<T::AccountId>>();
// report member changes. We compute diff because we need the outgoing list.
let (incoming, outgoing) = T::ChangeMembers::compute_members_diff(&new_members_ids, &old_members_ids);
T::ChangeMembers::change_members_sorted(&incoming, &outgoing.clone(), &new_members_ids);
// outgoing candidates lose their bond.
let mut to_burn_bond = outgoing.to_vec();
// compute the outgoing of runners up as well and append them to the `to_burn_bond`
{
let (_, outgoing) = T::ChangeMembers::compute_members_diff(&new_runners_up_ids, &old_runners_up_ids);
to_burn_bond.extend(outgoing);
}
// Burn loser bond. members list is sorted. O(NLogM) (N candidates, M members)
// runner up list is not sorted. O(K*N) given K runner ups. Overall: O(NLogM + N*K)
// both the member and runner counts are bounded.
exposed_candidates.into_iter().for_each(|c| {
// any candidate who is not a member and not a runner up.
if new_members.binary_search_by_key(&c, |(m, _)| m.clone()).is_err() && !new_runners_up_ids.contains(&c)
{
let (imbalance, _) = T::Currency::slash_reserved(&c, T::CandidacyBond::get());
T::LoserCandidate::on_unbalanced(imbalance);
}
});
// Burn outgoing bonds
to_burn_bond.into_iter().for_each(|x| {
let (imbalance, _) = T::Currency::slash_reserved(&x, T::CandidacyBond::get());
T::LoserCandidate::on_unbalanced(imbalance);
});
<Members<T>>::put(&new_members);
<RunnersUp<T>>::put(new_runners_up);
Self::deposit_event(RawEvent::NewTerm(new_members.clone().to_vec()));
} else {
Self::deposit_event(RawEvent::EmptyTerm);
}
// clean candidates.
<Candidates<T>>::kill();
ElectionRounds::mutate(|v| *v += 1);
}
}
#[cfg(test)]
mod tests {
use frame_support::{assert_noop, assert_ok, parameter_types, weights::Weight};
use frame_system as system;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, Block as BlockT, IdentityLookup},
BuildStorage, Perbill,
};
use std::cell::RefCell;
use substrate_test_utils::assert_eq_uvec;
use crate as elections;
use darwinia_support::{NormalLock, WithdrawLock};
use elections::*;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl frame_system::Trait for Test {
type Origin = Origin;
type Call = ();
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
pub const TransferFee: u64 = 0;
pub const CreationFee: u64 = 0;
}
impl pallet_balances::Trait for Test {
type Balance = u64;
type OnFreeBalanceZero = ();
type OnNewAccount = ();
type TransferPayment = ();
type DustRemoval = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type CreationFee = CreationFee;
}
parameter_types! {
pub const CandidacyBond: u64 = 3;
}
thread_local! {
static VOTING_BOND: RefCell<u64> = RefCell::new(2);
static DESIRED_MEMBERS: RefCell<u32> = RefCell::new(2);
static DESIRED_RUNNERS_UP: RefCell<u32> = RefCell::new(2);
static TERM_DURATION: RefCell<u64> = RefCell::new(5);
}
pub struct VotingBond;
impl Get<u64> for VotingBond {
fn get() -> u64 {
VOTING_BOND.with(|v| *v.borrow())
}
}
pub struct DesiredMembers;
impl Get<u32> for DesiredMembers {
fn get() -> u32 {
DESIRED_MEMBERS.with(|v| *v.borrow())
}
}
pub struct DesiredRunnersUp;
impl Get<u32> for DesiredRunnersUp {
fn get() -> u32 {
DESIRED_RUNNERS_UP.with(|v| *v.borrow())
}
}
pub struct TermDuration;
impl Get<u64> for TermDuration {
fn get() -> u64 {
TERM_DURATION.with(|v| *v.borrow())
}
}
thread_local! {
pub static MEMBERS: RefCell<Vec<u64>> = RefCell::new(vec![]);
}
pub struct TestChangeMembers;
impl ChangeMembers<u64> for TestChangeMembers {
fn change_members_sorted(incoming: &[u64], outgoing: &[u64], new: &[u64]) {
// new, incoming, outgoing must be sorted.
let mut new_sorted = new.to_vec();
new_sorted.sort();
assert_eq!(new, &new_sorted[..]);
let mut incoming_sorted = incoming.to_vec();
incoming_sorted.sort();
assert_eq!(incoming, &incoming_sorted[..]);
let mut outgoing_sorted = outgoing.to_vec();
outgoing_sorted.sort();
assert_eq!(outgoing, &outgoing_sorted[..]);
// incoming and outgoing must be disjoint
for x in incoming.iter() {
assert!(outgoing.binary_search(x).is_err());
}
let mut old_plus_incoming = MEMBERS.with(|m| m.borrow().to_vec());
old_plus_incoming.extend_from_slice(incoming);
old_plus_incoming.sort();
let mut new_plus_outgoing = new.to_vec();
new_plus_outgoing.extend_from_slice(outgoing);
new_plus_outgoing.sort();
assert_eq!(old_plus_incoming, new_plus_outgoing);
MEMBERS.with(|m| *m.borrow_mut() = new.to_vec());
}
}
/// Simple structure that exposes how u64 currency can be represented as... u64.
pub struct CurrencyToVoteHandler;
impl Convert<u64, u64> for CurrencyToVoteHandler {
fn convert(x: u64) -> u64 {
x
}
}
impl Convert<u128, u64> for CurrencyToVoteHandler {
fn convert(x: u128) -> u64 {
x as u64
}
}
impl Trait for Test {
type Event = Event;
type Currency = Balances;
type ChangeMembers = TestChangeMembers;
type CurrencyToVote = CurrencyToVoteHandler;
type CandidacyBond = CandidacyBond;
type VotingBond = VotingBond;
type LoserCandidate = ();
type BadReport = ();
type KickedMember = ();
type DesiredMembers = DesiredMembers;
type DesiredRunnersUp = DesiredRunnersUp;
type TermDuration = TermDuration;
}
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, u64, Call, ()>;
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{Module, Call, Event},
Balances: pallet_balances::{Module, Call, Event<T>, Config<T>},
Elections: elections::{Module, Call, Event<T>},
}
);
pub struct ExtBuilder {
balance_factor: u64,
voter_bond: u64,
term_duration: u64,
desired_runners_up: u32,
}
impl Default for ExtBuilder {
fn default() -> Self {
Self {
balance_factor: 1,
voter_bond: 2,
desired_runners_up: 0,
term_duration: 5,
}
}
}
impl ExtBuilder {
pub fn voter_bond(mut self, fee: u64) -> Self {
self.voter_bond = fee;
self
}
pub fn desired_runners_up(mut self, count: u32) -> Self {
self.desired_runners_up = count;
self
}
pub fn term_duration(mut self, duration: u64) -> Self {
self.term_duration = duration;
self
}
pub fn build(self) -> sp_io::TestExternalities {
VOTING_BOND.with(|v| *v.borrow_mut() = self.voter_bond);
TERM_DURATION.with(|v| *v.borrow_mut() = self.term_duration);
DESIRED_RUNNERS_UP.with(|v| *v.borrow_mut() = self.desired_runners_up);
GenesisConfig {
pallet_balances: Some(pallet_balances::GenesisConfig::<Test> {
balances: vec![
(1, 10 * self.balance_factor),
(2, 20 * self.balance_factor),
(3, 30 * self.balance_factor),
(4, 40 * self.balance_factor),
(5, 50 * self.balance_factor),
(6, 60 * self.balance_factor),
],
vesting: vec![],
}),
}