43
43
//! ### Voting
44
44
//!
45
45
//! Voters can vote for a limited number of the candidates by providing a list of account ids,
46
- //! bounded by [`Config::MaxVotesPerVoter `]. Invalid votes (voting for non-candidates) and duplicate
47
- //! votes are ignored during election. Yet, a voter _might_ vote for a future candidate. Voters
48
- //! reserve a bond as they vote. Each vote defines a `value`. This amount is locked from the account
49
- //! of the voter and indicates the weight of the vote. Voters can update their votes at any time by
50
- //! calling `vote()` again. This can update the vote targets (which might update the deposit) or
51
- //! update the vote's stake ([`Voter::stake`]). After a round, votes are kept and might still be
52
- //! valid for further rounds. A voter is responsible for calling `remove_voter` once they are done
53
- //! to have their bond back and remove the lock.
46
+ //! bounded by [`MAXIMUM_VOTE `]. Invalid votes (voting for non-candidates) and duplicate votes are
47
+ //! ignored during election. Yet, a voter _might_ vote for a future candidate. Voters reserve a bond
48
+ //! as they vote. Each vote defines a `value`. This amount is locked from the account of the voter
49
+ //! and indicates the weight of the vote. Voters can update their votes at any time by calling
50
+ //! `vote()` again. This can update the vote targets (which might update the deposit) or update the
51
+ //! vote's stake ([`Voter::stake`]). After a round, votes are kept and might still be valid for
52
+ //! further rounds. A voter is responsible for calling `remove_voter` once they are done to have
53
+ //! their bond back and remove the lock.
54
54
//!
55
55
//! See [`Call::vote`], [`Call::remove_voter`].
56
56
//!
@@ -124,6 +124,9 @@ pub mod migrations;
124
124
125
125
const LOG_TARGET : & str = "runtime::elections-phragmen" ;
126
126
127
+ /// The maximum votes allowed per voter.
128
+ pub const MAXIMUM_VOTE : usize = 16 ;
129
+
127
130
type BalanceOf < T > =
128
131
<<T as Config >:: Currency as Currency < <T as frame_system:: Config >:: AccountId > >:: Balance ;
129
132
type NegativeImbalanceOf < T > = <<T as Config >:: Currency as Currency <
@@ -251,29 +254,19 @@ pub mod pallet {
251
254
252
255
/// The maximum number of candidates in a phragmen election.
253
256
///
254
- /// Warning: This impacts the size of the election which is run onchain. Chose wisely, and
255
- /// consider how it will impact `T::WeightInfo::election_phragmen`.
256
- ///
257
- /// When this limit is reached no more candidates are accepted in the election.
257
+ /// Warning: The election happens onchain, and this value will determine
258
+ /// the size of the election. When this limit is reached no more
259
+ /// candidates are accepted in the election.
258
260
#[ pallet:: constant]
259
261
type MaxCandidates : Get < u32 > ;
260
262
261
263
/// The maximum number of voters to allow in a phragmen election.
262
264
///
263
- /// Warning: This impacts the size of the election which is run onchain. Chose wisely, and
264
- /// consider how it will impact `T::WeightInfo::election_phragmen`.
265
- ///
265
+ /// Warning: This impacts the size of the election which is run onchain.
266
266
/// When the limit is reached the new voters are ignored.
267
267
#[ pallet:: constant]
268
268
type MaxVoters : Get < u32 > ;
269
269
270
- /// Maximum numbers of votes per voter.
271
- ///
272
- /// Warning: This impacts the size of the election which is run onchain. Chose wisely, and
273
- /// consider how it will impact `T::WeightInfo::election_phragmen`.
274
- #[ pallet:: constant]
275
- type MaxVotesPerVoter : Get < u32 > ;
276
-
277
270
/// Weight information for extrinsics in this pallet.
278
271
type WeightInfo : WeightInfo ;
279
272
}
@@ -291,41 +284,6 @@ pub mod pallet {
291
284
Weight :: zero ( )
292
285
}
293
286
}
294
-
295
- fn integrity_test ( ) {
296
- let block_weight = T :: BlockWeights :: get ( ) . max_block ;
297
- // mind the order.
298
- let election_weight = T :: WeightInfo :: election_phragmen (
299
- T :: MaxCandidates :: get ( ) ,
300
- T :: MaxVoters :: get ( ) ,
301
- T :: MaxVotesPerVoter :: get ( ) * T :: MaxVoters :: get ( ) ,
302
- ) ;
303
-
304
- let to_seconds = |w : & Weight | {
305
- w. ref_time ( ) as f32 /
306
- frame_support:: weights:: constants:: WEIGHT_REF_TIME_PER_SECOND as f32
307
- } ;
308
-
309
- frame_support:: log:: debug!(
310
- target: LOG_TARGET ,
311
- "election weight {}s ({:?}) // chain's block weight {}s ({:?})" ,
312
- to_seconds( & election_weight) ,
313
- election_weight,
314
- to_seconds( & block_weight) ,
315
- block_weight,
316
- ) ;
317
- assert ! (
318
- election_weight. all_lt( block_weight) ,
319
- "election weight {}s ({:?}) will exceed a {}s chain's block weight ({:?}) (MaxCandidates {}, MaxVoters {}, MaxVotesPerVoter {} -- tweak these parameters)" ,
320
- election_weight,
321
- to_seconds( & election_weight) ,
322
- to_seconds( & block_weight) ,
323
- block_weight,
324
- T :: MaxCandidates :: get( ) ,
325
- T :: MaxVoters :: get( ) ,
326
- T :: MaxVotesPerVoter :: get( ) ,
327
- ) ;
328
- }
329
287
}
330
288
331
289
#[ pallet:: call]
@@ -349,6 +307,10 @@ pub mod pallet {
349
307
///
350
308
/// It is the responsibility of the caller to **NOT** place all of their balance into the
351
309
/// lock and keep some for further operations.
310
+ ///
311
+ /// # <weight>
312
+ /// We assume the maximum weight among all 3 cases: vote_equal, vote_more and vote_less.
313
+ /// # </weight>
352
314
#[ pallet:: call_index( 0 ) ]
353
315
#[ pallet:: weight(
354
316
T :: WeightInfo :: vote_more( votes. len( ) as u32 )
@@ -362,10 +324,8 @@ pub mod pallet {
362
324
) -> DispatchResultWithPostInfo {
363
325
let who = ensure_signed ( origin) ?;
364
326
365
- ensure ! (
366
- votes. len( ) <= T :: MaxVotesPerVoter :: get( ) as usize ,
367
- Error :: <T >:: MaximumVotesExceeded
368
- ) ;
327
+ // votes should not be empty and more than `MAXIMUM_VOTE` in any case.
328
+ ensure ! ( votes. len( ) <= MAXIMUM_VOTE , Error :: <T >:: MaximumVotesExceeded ) ;
369
329
ensure ! ( !votes. is_empty( ) , Error :: <T >:: NoVotes ) ;
370
330
371
331
let candidates_count = <Candidates < T > >:: decode_len ( ) . unwrap_or ( 0 ) ;
@@ -1046,15 +1006,15 @@ impl<T: Config> Pallet<T> {
1046
1006
// count](https://en.wikipedia.org/wiki/Borda_count). We weigh everyone's vote for
1047
1007
// that new member by a multiplier based on the order of the votes. i.e. the
1048
1008
// first person a voter votes for gets a 16x multiplier, the next person gets a
1049
- // 15x multiplier, an so on... (assuming `T::MaxVotesPerVoter ` = 16)
1009
+ // 15x multiplier, an so on... (assuming `MAXIMUM_VOTE ` = 16)
1050
1010
let mut prime_votes = new_members_sorted_by_id
1051
1011
. iter ( )
1052
1012
. map ( |c| ( & c. 0 , BalanceOf :: < T > :: zero ( ) ) )
1053
1013
. collect :: < Vec < _ > > ( ) ;
1054
1014
for ( _, stake, votes) in voters_and_stakes. into_iter ( ) {
1055
1015
for ( vote_multiplier, who) in
1056
1016
votes. iter ( ) . enumerate ( ) . map ( |( vote_position, who) | {
1057
- ( ( T :: MaxVotesPerVoter :: get ( ) as usize - vote_position) as u32 , who)
1017
+ ( ( MAXIMUM_VOTE - vote_position) as u32 , who)
1058
1018
} ) {
1059
1019
if let Ok ( i) = prime_votes. binary_search_by_key ( & who, |k| k. 0 ) {
1060
1020
prime_votes[ i] . 1 = prime_votes[ i]
@@ -1213,9 +1173,16 @@ mod tests {
1213
1173
} ;
1214
1174
use substrate_test_utils:: assert_eq_uvec;
1215
1175
1176
+ parameter_types ! {
1177
+ pub BlockWeights : frame_system:: limits:: BlockWeights =
1178
+ frame_system:: limits:: BlockWeights :: simple_max(
1179
+ frame_support:: weights:: Weight :: from_ref_time( 1024 ) . set_proof_size( u64 :: MAX ) ,
1180
+ ) ;
1181
+ }
1182
+
1216
1183
impl frame_system:: Config for Test {
1217
1184
type BaseCallFilter = frame_support:: traits:: Everything ;
1218
- type BlockWeights = ( ) ;
1185
+ type BlockWeights = BlockWeights ;
1219
1186
type BlockLength = ( ) ;
1220
1187
type DbWeight = ( ) ;
1221
1188
type RuntimeOrigin = RuntimeOrigin ;
@@ -1330,7 +1297,6 @@ mod tests {
1330
1297
type KickedMember = ( ) ;
1331
1298
type WeightInfo = ( ) ;
1332
1299
type MaxVoters = PhragmenMaxVoters ;
1333
- type MaxVotesPerVoter = ConstU32 < 16 > ;
1334
1300
type MaxCandidates = PhragmenMaxCandidates ;
1335
1301
}
1336
1302
0 commit comments