referrerpolicy=no-referrer-when-downgrade

pallet_society/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Society Pallet
19//!
20//! - [`Config`]
21//! - [`Call`]
22//!
23//! ## Overview
24//!
25//! The Society pallet is an economic game which incentivizes users to participate
26//! and maintain a membership society.
27//!
28//! ### User Types
29//!
30//! At any point, a user in the society can be one of a:
31//! * Bidder - A user who has submitted intention of joining the society.
32//! * Candidate - A user who will be voted on to join the society.
33//! * Member - A user who is a member of the society.
34//! * Suspended Member - A member of the society who has accumulated too many strikes
35//! or failed their membership challenge.
36//!
37//! Of the non-suspended members, there is always a:
38//! * Head - A member who is exempt from suspension.
39//! * Defender - A member whose membership is under question and voted on again.
40//!
41//! Of the non-suspended members of the society, a random set of them are chosen as
42//! "skeptics". The mechanics of skeptics is explained in the
43//! [member phase](#member-phase) below.
44//!
45//! ### Mechanics
46//!
47//! #### Rewards
48//!
49//! Members are incentivized to participate in the society through rewards paid
50//! by the Society treasury. These payments have a maturity period that the user
51//! must wait before they are able to access the funds.
52//!
53//! #### Punishments
54//!
55//! Members can be punished by slashing the reward payouts that have not been
56//! collected. Additionally, members can accumulate "strikes", and when they
57//! reach a max strike limit, they become suspended.
58//!
59//! #### Skeptics
60//!
61//! During the voting period, a random set of members are selected as "skeptics".
62//! These skeptics are expected to vote on the current candidates. If they do not vote,
63//! their skeptic status is treated as a rejection vote, the member is deemed
64//! "lazy", and are given a strike per missing vote.
65//!
66//! #### Membership Challenges
67//!
68//! Every challenge rotation period, an existing member will be randomly selected
69//! to defend their membership into society. Then, other members can vote whether
70//! this defender should stay in society. A simple majority wins vote will determine
71//! the outcome of the user. Ties are treated as a failure of the challenge, but
72//! assuming no one else votes, the defender always get a free vote on their
73//! own challenge keeping them in the society. The Head member is exempt from the
74//! negative outcome of a membership challenge.
75//!
76//! #### Society Treasury
77//!
78//! The membership society is independently funded by a treasury managed by this
79//! pallet. Some subset of this treasury is placed in a Society Pot, which is used
80//! to determine the number of accepted bids.
81//!
82//! #### Rate of Growth
83//!
84//! The membership society can grow at a rate of 10 accepted candidates per rotation period up
85//! to the max membership threshold. Once this threshold is met, candidate selections
86//! are stalled until there is space for new members to join. This can be resolved by
87//! voting out existing members through the random challenges or by using governance
88//! to increase the maximum membership count.
89//!
90//! ### User Life Cycle
91//!
92//! A user can go through the following phases:
93//!
94//! ```ignore
95//!           +------->  User  <----------+
96//!           |           +               |
97//!           |           |               |
98//! +----------------------------------------------+
99//! |         |           |               |        |
100//! |         |           v               |        |
101//! |         |        Bidder <-----------+        |
102//! |         |           +               |        |
103//! |         |           |               +        |
104//! |         |           v            Suspended   |
105//! |         |       Candidate +----> Candidate   |
106//! |         |           +               +        |
107//! |         |           |               |        |
108//! |         +           |               |        |
109//! |   Suspended +------>|               |        |
110//! |      Member         |               |        |
111//! |         ^           |               |        |
112//! |         |           v               |        |
113//! |         +-------+ Member <----------+        |
114//! |                                              |
115//! |                                              |
116//! +------------------Society---------------------+
117//! ```
118//!
119//! #### Initialization
120//!
121//! The society is initialized with a single member who is automatically chosen as the Head.
122//!
123//! #### Bid Phase
124//!
125//! New users must have a bid to join the society.
126//!
127//! A user can make a bid by reserving a deposit. Alternatively, an already existing member
128//! can create a bid on a user's behalf by "vouching" for them.
129//!
130//! A bid includes reward information that the user would like to receive for joining
131//! the society. A vouching bid can additionally request some portion of that reward as a tip
132//! to the voucher for vouching for the prospective candidate.
133//!
134//! Every rotation period, Bids are ordered by reward amount, and the pallet
135//! selects as many bids the Society Pot can support for that period.
136//!
137//! These selected bids become candidates and move on to the Candidate phase.
138//! Bids that were not selected stay in the bidder pool until they are selected or
139//! a user chooses to "unbid".
140//!
141//! #### Candidate Phase
142//!
143//! Once a bidder becomes a candidate, members vote whether to approve or reject
144//! that candidate into society. This voting process also happens during a rotation period.
145//!
146//! The approval and rejection criteria for candidates are not set on chain,
147//! and may change for different societies.
148//!
149//! At the end of the rotation period, we collect the votes for a candidate
150//! and randomly select a vote as the final outcome.
151//!
152//! ```ignore
153//!  [ a-accept, r-reject, s-skeptic ]
154//! +----------------------------------+
155//! |                                  |
156//! |  Member   |0|1|2|3|4|5|6|7|8|9|  |
157//! |  -----------------------------   |
158//! |  Vote     |a|a|a|r|s|r|a|a|s|a|  |
159//! |  -----------------------------   |
160//! |  Selected | | | |x| | | | | | |  |
161//! |                                  |
162//! +----------------------------------+
163//!
164//! Result: Rejected
165//! ```
166//!
167//! Each member that voted opposite to this randomly selected vote is punished by
168//! slashing their unclaimed payouts and increasing the number of strikes they have.
169//!
170//! These slashed funds are given to a random user who voted the same as the
171//! selected vote as a reward for participating in the vote.
172//!
173//! If the candidate wins the vote, they receive their bid reward as a future payout.
174//! If the bid was placed by a voucher, they will receive their portion of the reward,
175//! before the rest is paid to the winning candidate.
176//!
177//! One winning candidate is selected as the Head of the members. This is randomly
178//! chosen, weighted by the number of approvals the winning candidates accumulated.
179//!
180//! If the candidate loses the vote, they are suspended and it is up to the Suspension
181//! Judgement origin to determine if the candidate should go through the bidding process
182//! again, should be accepted into the membership society, or rejected and their deposit
183//! slashed.
184//!
185//! #### Member Phase
186//!
187//! Once a candidate becomes a member, their role is to participate in society.
188//!
189//! Regular participation involves voting on candidates who want to join the membership
190//! society, and by voting in the right way, a member will accumulate future payouts.
191//! When a payout matures, members are able to claim those payouts.
192//!
193//! Members can also vouch for users to join the society, and request a "tip" from
194//! the fees the new member would collect by joining the society. This vouching
195//! process is useful in situations where a user may not have enough balance to
196//! satisfy the bid deposit. A member can only vouch one user at a time.
197//!
198//! During rotation periods, a random group of members are selected as "skeptics".
199//! These skeptics are expected to vote on the current candidates. If they do not vote,
200//! their skeptic status is treated as a rejection vote, the member is deemed
201//! "lazy", and are given a strike per missing vote.
202//!
203//! There is a challenge period in parallel to the rotation period. During a challenge period,
204//! a random member is selected to defend their membership to the society. Other members
205//! make a traditional majority-wins vote to determine if the member should stay in the society.
206//! Ties are treated as a failure of the challenge.
207//!
208//! If a member accumulates too many strikes or fails their membership challenge,
209//! they will become suspended. While a member is suspended, they are unable to
210//! claim matured payouts. It is up to the Suspension Judgement origin to determine
211//! if the member should re-enter society or be removed from society with all their
212//! future payouts slashed.
213//!
214//! ## Interface
215//!
216//! ### Dispatchable Functions
217//!
218//! #### For General Users
219//!
220//! * `bid` - A user can make a bid to join the membership society by reserving a deposit.
221//! * `unbid` - A user can withdraw their bid for entry, the deposit is returned.
222//!
223//! #### For Members
224//!
225//! * `vouch` - A member can place a bid on behalf of a user to join the membership society.
226//! * `unvouch` - A member can revoke their vouch for a user.
227//! * `vote` - A member can vote to approve or reject a candidate's request to join the society.
228//! * `defender_vote` - A member can vote to approve or reject a defender's continued membership
229//! to the society.
230//! * `payout` - A member can claim their first matured payment.
231//! * `unfound` - Allow the founder to unfound the society when they are the only member.
232//!
233//! #### For Super Users
234//!
235//! * `found` - The founder origin can initiate this society. Useful for bootstrapping the Society
236//! pallet on an already running chain.
237//! * `judge_suspended_member` - The suspension judgement origin is able to make
238//! judgement on a suspended member.
239//! * `judge_suspended_candidate` - The suspension judgement origin is able to
240//! make judgement on a suspended candidate.
241//! * `set_max_membership` - The ROOT origin can update the maximum member count for the society.
242//! The max membership count must be greater than 1.
243
244// Ensure we're `no_std` when compiling for Wasm.
245#![cfg_attr(not(feature = "std"), no_std)]
246
247#[cfg(test)]
248mod mock;
249
250#[cfg(test)]
251mod tests;
252
253#[cfg(feature = "runtime-benchmarks")]
254mod benchmarking;
255
256pub mod weights;
257
258pub mod migrations;
259
260extern crate alloc;
261
262use alloc::vec::Vec;
263use frame_support::{
264	impl_ensure_origin_with_arg_ignoring_arg,
265	pallet_prelude::*,
266	storage::KeyLenOf,
267	traits::{
268		BalanceStatus, Currency, EnsureOrigin, EnsureOriginWithArg,
269		ExistenceRequirement::AllowDeath, Imbalance, OnUnbalanced, Randomness, ReservableCurrency,
270		StorageVersion,
271	},
272	PalletId,
273};
274use frame_system::pallet_prelude::{
275	ensure_signed, BlockNumberFor as SystemBlockNumberFor, OriginFor,
276};
277use rand_chacha::{
278	rand_core::{RngCore, SeedableRng},
279	ChaChaRng,
280};
281use scale_info::TypeInfo;
282use sp_runtime::{
283	traits::{
284		AccountIdConversion, CheckedAdd, CheckedSub, Hash, Saturating, StaticLookup,
285		TrailingZeroInput, Zero,
286	},
287	ArithmeticError::Overflow,
288	Percent, RuntimeDebug,
289};
290
291pub use weights::WeightInfo;
292
293pub use pallet::*;
294use sp_runtime::traits::BlockNumberProvider;
295
296pub type BlockNumberFor<T, I> =
297	<<T as Config<I>>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
298
299pub type BalanceOf<T, I> =
300	<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
301pub type NegativeImbalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<
302	<T as frame_system::Config>::AccountId,
303>>::NegativeImbalance;
304pub type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
305
306#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
307pub struct Vote {
308	pub approve: bool,
309	pub weight: u32,
310}
311
312/// A judgement by the suspension judgement origin on a suspended candidate.
313#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
314pub enum Judgement {
315	/// The suspension judgement origin takes no direct judgment
316	/// and places the candidate back into the bid pool.
317	Rebid,
318	/// The suspension judgement origin has rejected the candidate's application.
319	Reject,
320	/// The suspension judgement origin approves of the candidate's application.
321	Approve,
322}
323
324/// Details of a payout given as a per-block linear "trickle".
325#[derive(
326	Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen,
327)]
328pub struct Payout<Balance, BlockNumber> {
329	/// Total value of the payout.
330	pub value: Balance,
331	/// Block number at which the payout begins.
332	pub begin: BlockNumber,
333	/// Total number of blocks over which the payout is spread.
334	pub duration: BlockNumber,
335	/// Total value paid out so far.
336	pub paid: Balance,
337}
338
339/// Status of a vouching member.
340#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
341pub enum VouchingStatus {
342	/// Member is currently vouching for a user.
343	Vouching,
344	/// Member is banned from vouching for other members.
345	Banned,
346}
347
348/// Number of strikes that a member has against them.
349pub type StrikeCount = u32;
350
351/// A bid for entry into society.
352#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
353pub struct Bid<AccountId, Balance> {
354	/// The bidder/candidate trying to enter society
355	pub who: AccountId,
356	/// The kind of bid placed for this bidder/candidate. See `BidKind`.
357	pub kind: BidKind<AccountId, Balance>,
358	/// The reward that the bidder has requested for successfully joining the society.
359	pub value: Balance,
360}
361
362/// The index of a round of candidates.
363pub type RoundIndex = u32;
364
365/// The rank of a member.
366pub type Rank = u32;
367
368/// The number of votes.
369pub type VoteCount = u32;
370
371/// Tally of votes.
372#[derive(
373	Default, Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen,
374)]
375pub struct Tally {
376	/// The approval votes.
377	pub approvals: VoteCount,
378	/// The rejection votes.
379	pub rejections: VoteCount,
380}
381
382impl Tally {
383	fn more_approvals(&self) -> bool {
384		self.approvals > self.rejections
385	}
386
387	fn more_rejections(&self) -> bool {
388		self.rejections > self.approvals
389	}
390
391	fn clear_approval(&self) -> bool {
392		self.approvals >= (2 * self.rejections).max(1)
393	}
394
395	fn clear_rejection(&self) -> bool {
396		self.rejections >= (2 * self.approvals).max(1)
397	}
398}
399
400/// A bid for entry into society.
401#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
402pub struct Candidacy<AccountId, Balance> {
403	/// The index of the round where the candidacy began.
404	pub round: RoundIndex,
405	/// The kind of bid placed for this bidder/candidate. See `BidKind`.
406	pub kind: BidKind<AccountId, Balance>,
407	/// The reward that the bidder has requested for successfully joining the society.
408	pub bid: Balance,
409	/// The tally of votes so far.
410	pub tally: Tally,
411	/// True if the skeptic was already punished for note voting.
412	pub skeptic_struck: bool,
413}
414
415/// A vote by a member on a candidate application.
416#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
417pub enum BidKind<AccountId, Balance> {
418	/// The given deposit was paid for this bid.
419	Deposit(Balance),
420	/// A member vouched for this bid. The account should be reinstated into `Members` once the
421	/// bid is successful (or if it is rescinded prior to launch).
422	Vouch(AccountId, Balance),
423}
424
425impl<AccountId: PartialEq, Balance> BidKind<AccountId, Balance> {
426	fn is_vouch(&self, v: &AccountId) -> bool {
427		matches!(self, BidKind::Vouch(ref a, _) if a == v)
428	}
429}
430
431pub type PayoutsFor<T, I> =
432	BoundedVec<(BlockNumberFor<T, I>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>;
433
434/// Information concerning a member.
435#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
436pub struct MemberRecord {
437	pub rank: Rank,
438	pub strikes: StrikeCount,
439	pub vouching: Option<VouchingStatus>,
440	pub index: u32,
441}
442
443/// Information concerning a member.
444#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, Default, MaxEncodedLen)]
445pub struct PayoutRecord<Balance, PayoutsVec> {
446	pub paid: Balance,
447	pub payouts: PayoutsVec,
448}
449
450pub type PayoutRecordFor<T, I> = PayoutRecord<
451	BalanceOf<T, I>,
452	BoundedVec<(BlockNumberFor<T, I>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>,
453>;
454
455/// Record for an individual new member who was elevated from a candidate recently.
456#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
457pub struct IntakeRecord<AccountId, Balance> {
458	pub who: AccountId,
459	pub bid: Balance,
460	pub round: RoundIndex,
461}
462
463pub type IntakeRecordFor<T, I> =
464	IntakeRecord<<T as frame_system::Config>::AccountId, BalanceOf<T, I>>;
465
466#[derive(
467	Encode,
468	Decode,
469	DecodeWithMemTracking,
470	Copy,
471	Clone,
472	PartialEq,
473	Eq,
474	RuntimeDebug,
475	TypeInfo,
476	MaxEncodedLen,
477)]
478pub struct GroupParams<Balance> {
479	pub max_members: u32,
480	pub max_intake: u32,
481	pub max_strikes: u32,
482	pub candidate_deposit: Balance,
483}
484
485pub type GroupParamsFor<T, I> = GroupParams<BalanceOf<T, I>>;
486
487pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
488
489#[frame_support::pallet]
490pub mod pallet {
491	use super::*;
492
493	#[pallet::pallet]
494	#[pallet::storage_version(STORAGE_VERSION)]
495	pub struct Pallet<T, I = ()>(_);
496
497	#[pallet::config]
498	pub trait Config<I: 'static = ()>: frame_system::Config {
499		/// The overarching event type.
500		#[allow(deprecated)]
501		type RuntimeEvent: From<Event<Self, I>>
502			+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
503
504		/// The societies's pallet id
505		#[pallet::constant]
506		type PalletId: Get<PalletId>;
507
508		/// The currency type used for bidding.
509		type Currency: ReservableCurrency<Self::AccountId>;
510
511		/// Something that provides randomness in the runtime.
512		type Randomness: Randomness<Self::Hash, BlockNumberFor<Self, I>>;
513
514		/// The maximum number of strikes before a member gets funds slashed.
515		#[pallet::constant]
516		type GraceStrikes: Get<u32>;
517
518		/// The amount of incentive paid within each period. Doesn't include VoterTip.
519		#[pallet::constant]
520		type PeriodSpend: Get<BalanceOf<Self, I>>;
521
522		/// The number of [Config::BlockNumberProvider] blocks on which new candidates should be
523		/// voted on. Together with
524		/// `ClaimPeriod`, this sums to the number of blocks between candidate intake periods.
525		#[pallet::constant]
526		type VotingPeriod: Get<BlockNumberFor<Self, I>>;
527
528		/// The number of [Config::BlockNumberProvider] blocks on which new candidates can claim
529		/// their membership and be the named head.
530		#[pallet::constant]
531		type ClaimPeriod: Get<BlockNumberFor<Self, I>>;
532
533		/// The maximum duration of the payout lock.
534		#[pallet::constant]
535		type MaxLockDuration: Get<BlockNumberFor<Self, I>>;
536
537		/// The origin that is allowed to call `found`.
538		type FounderSetOrigin: EnsureOrigin<Self::RuntimeOrigin>;
539
540		/// The number of [Config::BlockNumberProvider] blocks between membership challenges.
541		#[pallet::constant]
542		type ChallengePeriod: Get<BlockNumberFor<Self, I>>;
543
544		/// The maximum number of payouts a member may have waiting unclaimed.
545		#[pallet::constant]
546		type MaxPayouts: Get<u32>;
547
548		/// The maximum number of bids at once.
549		#[pallet::constant]
550		type MaxBids: Get<u32>;
551
552		/// Weight information for extrinsics in this pallet.
553		type WeightInfo: WeightInfo;
554		/// Provider for the block number. Normally this is the `frame_system` pallet.
555		type BlockNumberProvider: BlockNumberProvider;
556	}
557
558	#[pallet::error]
559	pub enum Error<T, I = ()> {
560		/// User is not a member.
561		NotMember,
562		/// User is already a member.
563		AlreadyMember,
564		/// User is suspended.
565		Suspended,
566		/// User is not suspended.
567		NotSuspended,
568		/// Nothing to payout.
569		NoPayout,
570		/// Society already founded.
571		AlreadyFounded,
572		/// Not enough in pot to accept candidate.
573		InsufficientPot,
574		/// Member is already vouching or banned from vouching again.
575		AlreadyVouching,
576		/// Member is not vouching.
577		NotVouchingOnBidder,
578		/// Cannot remove the head of the chain.
579		Head,
580		/// Cannot remove the founder.
581		Founder,
582		/// User has already made a bid.
583		AlreadyBid,
584		/// User is already a candidate.
585		AlreadyCandidate,
586		/// User is not a candidate.
587		NotCandidate,
588		/// Too many members in the society.
589		MaxMembers,
590		/// The caller is not the founder.
591		NotFounder,
592		/// The caller is not the head.
593		NotHead,
594		/// The membership cannot be claimed as the candidate was not clearly approved.
595		NotApproved,
596		/// The candidate cannot be kicked as the candidate was not clearly rejected.
597		NotRejected,
598		/// The candidacy cannot be dropped as the candidate was clearly approved.
599		Approved,
600		/// The candidacy cannot be bestowed as the candidate was clearly rejected.
601		Rejected,
602		/// The candidacy cannot be concluded as the voting is still in progress.
603		InProgress,
604		/// The candidacy cannot be pruned until a full additional intake period has passed.
605		TooEarly,
606		/// The skeptic already voted.
607		Voted,
608		/// The skeptic need not vote on candidates from expired rounds.
609		Expired,
610		/// User is not a bidder.
611		NotBidder,
612		/// There is no defender currently.
613		NoDefender,
614		/// Group doesn't exist.
615		NotGroup,
616		/// The member is already elevated to this rank.
617		AlreadyElevated,
618		/// The skeptic has already been punished for this offence.
619		AlreadyPunished,
620		/// Funds are insufficient to pay off society debts.
621		InsufficientFunds,
622		/// The candidate/defender has no stale votes to remove.
623		NoVotes,
624		/// There is no deposit associated with a bid.
625		NoDeposit,
626	}
627
628	#[pallet::event]
629	#[pallet::generate_deposit(pub(super) fn deposit_event)]
630	pub enum Event<T: Config<I>, I: 'static = ()> {
631		/// The society is founded by the given identity.
632		Founded { founder: T::AccountId },
633		/// A membership bid just happened. The given account is the candidate's ID and their offer
634		/// is the second.
635		Bid { candidate_id: T::AccountId, offer: BalanceOf<T, I> },
636		/// A membership bid just happened by vouching. The given account is the candidate's ID and
637		/// their offer is the second. The vouching party is the third.
638		Vouch { candidate_id: T::AccountId, offer: BalanceOf<T, I>, vouching: T::AccountId },
639		/// A candidate was dropped (due to an excess of bids in the system).
640		AutoUnbid { candidate: T::AccountId },
641		/// A candidate was dropped (by their request).
642		Unbid { candidate: T::AccountId },
643		/// A candidate was dropped (by request of who vouched for them).
644		Unvouch { candidate: T::AccountId },
645		/// A group of candidates have been inducted. The batch's primary is the first value, the
646		/// batch in full is the second.
647		Inducted { primary: T::AccountId, candidates: Vec<T::AccountId> },
648		/// A suspended member has been judged.
649		SuspendedMemberJudgement { who: T::AccountId, judged: bool },
650		/// A candidate has been suspended
651		CandidateSuspended { candidate: T::AccountId },
652		/// A member has been suspended
653		MemberSuspended { member: T::AccountId },
654		/// A member has been challenged
655		Challenged { member: T::AccountId },
656		/// A vote has been placed
657		Vote { candidate: T::AccountId, voter: T::AccountId, vote: bool },
658		/// A vote has been placed for a defending member
659		DefenderVote { voter: T::AccountId, vote: bool },
660		/// A new set of \[params\] has been set for the group.
661		NewParams { params: GroupParamsFor<T, I> },
662		/// Society is unfounded.
663		Unfounded { founder: T::AccountId },
664		/// Some funds were deposited into the society account.
665		Deposit { value: BalanceOf<T, I> },
666		/// A \[member\] got elevated to \[rank\].
667		Elevated { member: T::AccountId, rank: Rank },
668		/// A deposit was poked / adjusted.
669		DepositPoked {
670			who: T::AccountId,
671			old_deposit: BalanceOf<T, I>,
672			new_deposit: BalanceOf<T, I>,
673		},
674	}
675
676	/// Old name generated by `decl_event`.
677	#[deprecated(note = "use `Event` instead")]
678	pub type RawEvent<T, I = ()> = Event<T, I>;
679
680	/// The max number of members for the society at one time.
681	#[pallet::storage]
682	pub type Parameters<T: Config<I>, I: 'static = ()> =
683		StorageValue<_, GroupParamsFor<T, I>, OptionQuery>;
684
685	/// Amount of our account balance that is specifically for the next round's bid(s).
686	#[pallet::storage]
687	pub type Pot<T: Config<I>, I: 'static = ()> = StorageValue<_, BalanceOf<T, I>, ValueQuery>;
688
689	/// The first member.
690	#[pallet::storage]
691	pub type Founder<T: Config<I>, I: 'static = ()> = StorageValue<_, T::AccountId>;
692
693	/// The most primary from the most recently approved rank 0 members in the society.
694	#[pallet::storage]
695	pub type Head<T: Config<I>, I: 'static = ()> = StorageValue<_, T::AccountId>;
696
697	/// A hash of the rules of this society concerning membership. Can only be set once and
698	/// only by the founder.
699	#[pallet::storage]
700	pub type Rules<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Hash>;
701
702	/// The current members and their rank. Doesn't include `SuspendedMembers`.
703	#[pallet::storage]
704	pub type Members<T: Config<I>, I: 'static = ()> =
705		StorageMap<_, Twox64Concat, T::AccountId, MemberRecord, OptionQuery>;
706
707	/// Information regarding rank-0 payouts, past and future.
708	#[pallet::storage]
709	pub type Payouts<T: Config<I>, I: 'static = ()> =
710		StorageMap<_, Twox64Concat, T::AccountId, PayoutRecordFor<T, I>, ValueQuery>;
711
712	/// The number of items in `Members` currently. (Doesn't include `SuspendedMembers`.)
713	#[pallet::storage]
714	pub type MemberCount<T: Config<I>, I: 'static = ()> = StorageValue<_, u32, ValueQuery>;
715
716	/// The current items in `Members` keyed by their unique index. Keys are densely populated
717	/// `0..MemberCount` (does not include `MemberCount`).
718	#[pallet::storage]
719	pub type MemberByIndex<T: Config<I>, I: 'static = ()> =
720		StorageMap<_, Twox64Concat, u32, T::AccountId, OptionQuery>;
721
722	/// The set of suspended members, with their old membership record.
723	#[pallet::storage]
724	pub type SuspendedMembers<T: Config<I>, I: 'static = ()> =
725		StorageMap<_, Twox64Concat, T::AccountId, MemberRecord, OptionQuery>;
726
727	/// The number of rounds which have passed.
728	#[pallet::storage]
729	pub type RoundCount<T: Config<I>, I: 'static = ()> = StorageValue<_, RoundIndex, ValueQuery>;
730
731	/// The current bids, stored ordered by the value of the bid.
732	#[pallet::storage]
733	pub type Bids<T: Config<I>, I: 'static = ()> =
734		StorageValue<_, BoundedVec<Bid<T::AccountId, BalanceOf<T, I>>, T::MaxBids>, ValueQuery>;
735
736	#[pallet::storage]
737	pub type Candidates<T: Config<I>, I: 'static = ()> = StorageMap<
738		_,
739		Blake2_128Concat,
740		T::AccountId,
741		Candidacy<T::AccountId, BalanceOf<T, I>>,
742		OptionQuery,
743	>;
744
745	/// The current skeptic.
746	#[pallet::storage]
747	pub type Skeptic<T: Config<I>, I: 'static = ()> = StorageValue<_, T::AccountId, OptionQuery>;
748
749	/// Double map from Candidate -> Voter -> (Maybe) Vote.
750	#[pallet::storage]
751	pub type Votes<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
752		_,
753		Twox64Concat,
754		T::AccountId,
755		Twox64Concat,
756		T::AccountId,
757		Vote,
758		OptionQuery,
759	>;
760
761	/// Clear-cursor for Vote, map from Candidate -> (Maybe) Cursor.
762	#[pallet::storage]
763	pub type VoteClearCursor<T: Config<I>, I: 'static = ()> =
764		StorageMap<_, Twox64Concat, T::AccountId, BoundedVec<u8, KeyLenOf<Votes<T, I>>>>;
765
766	/// At the end of the claim period, this contains the most recently approved members (along with
767	/// their bid and round ID) who is from the most recent round with the lowest bid. They will
768	/// become the new `Head`.
769	#[pallet::storage]
770	pub type NextHead<T: Config<I>, I: 'static = ()> =
771		StorageValue<_, IntakeRecordFor<T, I>, OptionQuery>;
772
773	/// The number of challenge rounds there have been. Used to identify stale DefenderVotes.
774	#[pallet::storage]
775	pub type ChallengeRoundCount<T: Config<I>, I: 'static = ()> =
776		StorageValue<_, RoundIndex, ValueQuery>;
777
778	/// The defending member currently being challenged, along with a running tally of votes.
779	#[pallet::storage]
780	pub type Defending<T: Config<I>, I: 'static = ()> =
781		StorageValue<_, (T::AccountId, T::AccountId, Tally)>;
782
783	/// Votes for the defender, keyed by challenge round.
784	#[pallet::storage]
785	pub type DefenderVotes<T: Config<I>, I: 'static = ()> =
786		StorageDoubleMap<_, Twox64Concat, RoundIndex, Twox64Concat, T::AccountId, Vote>;
787
788	/// Next intake rotation scheduled with [Config::BlockNumberProvider].
789	#[pallet::storage]
790	pub type NextIntakeAt<T: Config<I>, I: 'static = ()> = StorageValue<_, BlockNumberFor<T, I>>;
791
792	/// Next challenge rotation scheduled with [Config::BlockNumberProvider].
793	#[pallet::storage]
794	pub type NextChallengeAt<T: Config<I>, I: 'static = ()> = StorageValue<_, BlockNumberFor<T, I>>;
795
796	#[pallet::hooks]
797	impl<T: Config<I>, I: 'static> Hooks<SystemBlockNumberFor<T>> for Pallet<T, I> {
798		fn on_initialize(_n: SystemBlockNumberFor<T>) -> Weight {
799			let mut weight = Weight::zero();
800			let weights = T::BlockWeights::get();
801			let now = T::BlockNumberProvider::current_block_number();
802
803			let phrase = b"society_rotation";
804			// we'll need a random seed here.
805			// TODO: deal with randomness freshness
806			// https://github.com/paritytech/substrate/issues/8312
807			let (seed, _) = T::Randomness::random(phrase);
808			// seed needs to be guaranteed to be 32 bytes.
809			let seed = <[u8; 32]>::decode(&mut TrailingZeroInput::new(seed.as_ref()))
810				.expect("input is padded with zeroes; qed");
811			let mut rng = ChaChaRng::from_seed(seed);
812
813			// Run a candidate/membership rotation
814			let is_intake_moment = match Self::period() {
815				Period::Intake { .. } => true,
816				_ => false,
817			};
818			if is_intake_moment {
819				Self::rotate_intake(&mut rng);
820				weight.saturating_accrue(weights.max_block / 20);
821				Self::set_next_intake_at();
822			}
823
824			// Run a challenge rotation
825			if now >= Self::next_challenge_at() {
826				Self::rotate_challenge(&mut rng);
827				weight.saturating_accrue(weights.max_block / 20);
828				Self::set_next_challenge_at();
829			}
830
831			weight
832		}
833	}
834
835	#[pallet::genesis_config]
836	#[derive(frame_support::DefaultNoBound)]
837	pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
838		pub pot: BalanceOf<T, I>,
839	}
840
841	#[pallet::genesis_build]
842	impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
843		fn build(&self) {
844			Pot::<T, I>::put(self.pot);
845		}
846	}
847
848	#[pallet::call]
849	impl<T: Config<I>, I: 'static> Pallet<T, I> {
850		/// A user outside of the society can make a bid for entry.
851		///
852		/// Payment: The group's Candidate Deposit will be reserved for making a bid. It is returned
853		/// when the bid becomes a member, or if the bid calls `unbid`.
854		///
855		/// The dispatch origin for this call must be _Signed_.
856		///
857		/// Parameters:
858		/// - `value`: A one time payment the bid would like to receive when joining the society.
859		#[pallet::call_index(0)]
860		#[pallet::weight(T::WeightInfo::bid())]
861		pub fn bid(origin: OriginFor<T>, value: BalanceOf<T, I>) -> DispatchResult {
862			let who = ensure_signed(origin)?;
863
864			let mut bids = Bids::<T, I>::get();
865			ensure!(!Self::has_bid(&bids, &who), Error::<T, I>::AlreadyBid);
866			ensure!(!Candidates::<T, I>::contains_key(&who), Error::<T, I>::AlreadyCandidate);
867			ensure!(!Members::<T, I>::contains_key(&who), Error::<T, I>::AlreadyMember);
868			ensure!(!SuspendedMembers::<T, I>::contains_key(&who), Error::<T, I>::Suspended);
869
870			let params = Parameters::<T, I>::get().ok_or(Error::<T, I>::NotGroup)?;
871			let deposit = params.candidate_deposit;
872			// NOTE: Reserve must happen before `insert_bid` since that could end up unreserving.
873			T::Currency::reserve(&who, deposit)?;
874			Self::insert_bid(&mut bids, &who, value, BidKind::Deposit(deposit));
875
876			Bids::<T, I>::put(bids);
877			Self::deposit_event(Event::<T, I>::Bid { candidate_id: who, offer: value });
878			Ok(())
879		}
880
881		/// A bidder can remove their bid for entry into society.
882		/// By doing so, they will have their candidate deposit returned or
883		/// they will unvouch their voucher.
884		///
885		/// Payment: The bid deposit is unreserved if the user made a bid.
886		///
887		/// The dispatch origin for this call must be _Signed_ and a bidder.
888		#[pallet::call_index(1)]
889		#[pallet::weight(T::WeightInfo::unbid())]
890		pub fn unbid(origin: OriginFor<T>) -> DispatchResult {
891			let who = ensure_signed(origin)?;
892
893			let mut bids = Bids::<T, I>::get();
894			let pos = bids.iter().position(|bid| bid.who == who).ok_or(Error::<T, I>::NotBidder)?;
895			Self::clean_bid(&bids.remove(pos));
896			Bids::<T, I>::put(bids);
897			Self::deposit_event(Event::<T, I>::Unbid { candidate: who });
898			Ok(())
899		}
900
901		/// As a member, vouch for someone to join society by placing a bid on their behalf.
902		///
903		/// There is no deposit required to vouch for a new bid, but a member can only vouch for
904		/// one bid at a time. If the bid becomes a suspended candidate and ultimately rejected by
905		/// the suspension judgement origin, the member will be banned from vouching again.
906		///
907		/// As a vouching member, you can claim a tip if the candidate is accepted. This tip will
908		/// be paid as a portion of the reward the member will receive for joining the society.
909		///
910		/// The dispatch origin for this call must be _Signed_ and a member.
911		///
912		/// Parameters:
913		/// - `who`: The user who you would like to vouch for.
914		/// - `value`: The total reward to be paid between you and the candidate if they become
915		/// a member in the society.
916		/// - `tip`: Your cut of the total `value` payout when the candidate is inducted into
917		/// the society. Tips larger than `value` will be saturated upon payout.
918		#[pallet::call_index(2)]
919		#[pallet::weight(T::WeightInfo::vouch())]
920		pub fn vouch(
921			origin: OriginFor<T>,
922			who: AccountIdLookupOf<T>,
923			value: BalanceOf<T, I>,
924			tip: BalanceOf<T, I>,
925		) -> DispatchResult {
926			let voucher = ensure_signed(origin)?;
927			let who = T::Lookup::lookup(who)?;
928
929			// Get bids and check user is not bidding.
930			let mut bids = Bids::<T, I>::get();
931			ensure!(!Self::has_bid(&bids, &who), Error::<T, I>::AlreadyBid);
932
933			// Check user is not already a candidate, member or suspended member.
934			ensure!(!Candidates::<T, I>::contains_key(&who), Error::<T, I>::AlreadyCandidate);
935			ensure!(!Members::<T, I>::contains_key(&who), Error::<T, I>::AlreadyMember);
936			ensure!(!SuspendedMembers::<T, I>::contains_key(&who), Error::<T, I>::Suspended);
937
938			// Check sender can vouch.
939			let mut record = Members::<T, I>::get(&voucher).ok_or(Error::<T, I>::NotMember)?;
940			ensure!(record.vouching.is_none(), Error::<T, I>::AlreadyVouching);
941
942			// Update voucher record.
943			record.vouching = Some(VouchingStatus::Vouching);
944			// Update bids
945			Self::insert_bid(&mut bids, &who, value, BidKind::Vouch(voucher.clone(), tip));
946
947			// Write new state.
948			Members::<T, I>::insert(&voucher, &record);
949			Bids::<T, I>::put(bids);
950			Self::deposit_event(Event::<T, I>::Vouch {
951				candidate_id: who,
952				offer: value,
953				vouching: voucher,
954			});
955			Ok(())
956		}
957
958		/// As a vouching member, unvouch a bid. This only works while vouched user is
959		/// only a bidder (and not a candidate).
960		///
961		/// The dispatch origin for this call must be _Signed_ and a vouching member.
962		///
963		/// Parameters:
964		/// - `pos`: Position in the `Bids` vector of the bid who should be unvouched.
965		#[pallet::call_index(3)]
966		#[pallet::weight(T::WeightInfo::unvouch())]
967		pub fn unvouch(origin: OriginFor<T>) -> DispatchResult {
968			let voucher = ensure_signed(origin)?;
969
970			let mut bids = Bids::<T, I>::get();
971			let pos = bids
972				.iter()
973				.position(|bid| bid.kind.is_vouch(&voucher))
974				.ok_or(Error::<T, I>::NotVouchingOnBidder)?;
975			let bid = bids.remove(pos);
976			Self::clean_bid(&bid);
977
978			Bids::<T, I>::put(bids);
979			Self::deposit_event(Event::<T, I>::Unvouch { candidate: bid.who });
980			Ok(())
981		}
982
983		/// As a member, vote on a candidate.
984		///
985		/// The dispatch origin for this call must be _Signed_ and a member.
986		///
987		/// Parameters:
988		/// - `candidate`: The candidate that the member would like to bid on.
989		/// - `approve`: A boolean which says if the candidate should be approved (`true`) or
990		///   rejected (`false`).
991		#[pallet::call_index(4)]
992		#[pallet::weight(T::WeightInfo::vote())]
993		pub fn vote(
994			origin: OriginFor<T>,
995			candidate: AccountIdLookupOf<T>,
996			approve: bool,
997		) -> DispatchResultWithPostInfo {
998			let voter = ensure_signed(origin)?;
999			let candidate = T::Lookup::lookup(candidate)?;
1000
1001			let mut candidacy =
1002				Candidates::<T, I>::get(&candidate).ok_or(Error::<T, I>::NotCandidate)?;
1003			let record = Members::<T, I>::get(&voter).ok_or(Error::<T, I>::NotMember)?;
1004
1005			let first_time = Votes::<T, I>::mutate(&candidate, &voter, |v| {
1006				let first_time = v.is_none();
1007				*v = Some(Self::do_vote(*v, approve, record.rank, &mut candidacy.tally));
1008				first_time
1009			});
1010
1011			Candidates::<T, I>::insert(&candidate, &candidacy);
1012			Self::deposit_event(Event::<T, I>::Vote { candidate, voter, vote: approve });
1013			Ok(if first_time { Pays::No } else { Pays::Yes }.into())
1014		}
1015
1016		/// As a member, vote on the defender.
1017		///
1018		/// The dispatch origin for this call must be _Signed_ and a member.
1019		///
1020		/// Parameters:
1021		/// - `approve`: A boolean which says if the candidate should be
1022		/// approved (`true`) or rejected (`false`).
1023		#[pallet::call_index(5)]
1024		#[pallet::weight(T::WeightInfo::defender_vote())]
1025		pub fn defender_vote(origin: OriginFor<T>, approve: bool) -> DispatchResultWithPostInfo {
1026			let voter = ensure_signed(origin)?;
1027
1028			let mut defending = Defending::<T, I>::get().ok_or(Error::<T, I>::NoDefender)?;
1029			let record = Members::<T, I>::get(&voter).ok_or(Error::<T, I>::NotMember)?;
1030
1031			let round = ChallengeRoundCount::<T, I>::get();
1032			let first_time = DefenderVotes::<T, I>::mutate(round, &voter, |v| {
1033				let first_time = v.is_none();
1034				*v = Some(Self::do_vote(*v, approve, record.rank, &mut defending.2));
1035				first_time
1036			});
1037
1038			Defending::<T, I>::put(defending);
1039			Self::deposit_event(Event::<T, I>::DefenderVote { voter, vote: approve });
1040			Ok(if first_time { Pays::No } else { Pays::Yes }.into())
1041		}
1042
1043		/// Transfer the first matured payout for the sender and remove it from the records.
1044		///
1045		/// NOTE: This extrinsic needs to be called multiple times to claim multiple matured
1046		/// payouts.
1047		///
1048		/// Payment: The member will receive a payment equal to their first matured
1049		/// payout to their free balance.
1050		///
1051		/// The dispatch origin for this call must be _Signed_ and a member with
1052		/// payouts remaining.
1053		#[pallet::call_index(6)]
1054		#[pallet::weight(T::WeightInfo::payout())]
1055		pub fn payout(origin: OriginFor<T>) -> DispatchResult {
1056			let who = ensure_signed(origin)?;
1057			ensure!(
1058				Members::<T, I>::get(&who).ok_or(Error::<T, I>::NotMember)?.rank == 0,
1059				Error::<T, I>::NoPayout
1060			);
1061			let mut record = Payouts::<T, I>::get(&who);
1062			let block_number = T::BlockNumberProvider::current_block_number();
1063			if let Some((when, amount)) = record.payouts.first() {
1064				if when <= &block_number {
1065					record.paid = record.paid.checked_add(amount).ok_or(Overflow)?;
1066					T::Currency::transfer(&Self::payouts(), &who, *amount, AllowDeath)?;
1067					record.payouts.remove(0);
1068					Payouts::<T, I>::insert(&who, record);
1069					return Ok(())
1070				}
1071			}
1072			Err(Error::<T, I>::NoPayout)?
1073		}
1074
1075		/// Repay the payment previously given to the member with the signed origin, remove any
1076		/// pending payments, and elevate them from rank 0 to rank 1.
1077		#[pallet::call_index(7)]
1078		#[pallet::weight(T::WeightInfo::waive_repay())]
1079		pub fn waive_repay(origin: OriginFor<T>, amount: BalanceOf<T, I>) -> DispatchResult {
1080			let who = ensure_signed(origin)?;
1081			let mut record = Members::<T, I>::get(&who).ok_or(Error::<T, I>::NotMember)?;
1082			let mut payout_record = Payouts::<T, I>::get(&who);
1083			ensure!(record.rank == 0, Error::<T, I>::AlreadyElevated);
1084			ensure!(amount >= payout_record.paid, Error::<T, I>::InsufficientFunds);
1085
1086			T::Currency::transfer(&who, &Self::account_id(), payout_record.paid, AllowDeath)?;
1087			payout_record.paid = Zero::zero();
1088			payout_record.payouts.clear();
1089			record.rank = 1;
1090			Members::<T, I>::insert(&who, record);
1091			Payouts::<T, I>::insert(&who, payout_record);
1092			Self::deposit_event(Event::<T, I>::Elevated { member: who, rank: 1 });
1093
1094			Ok(())
1095		}
1096
1097		/// Found the society.
1098		///
1099		/// This is done as a discrete action in order to allow for the
1100		/// pallet to be included into a running chain and can only be done once.
1101		///
1102		/// The dispatch origin for this call must be from the _FounderSetOrigin_.
1103		///
1104		/// Parameters:
1105		/// - `founder` - The first member and head of the newly founded society.
1106		/// - `max_members` - The initial max number of members for the society.
1107		/// - `max_intake` - The maximum number of candidates per intake period.
1108		/// - `max_strikes`: The maximum number of strikes a member may get before they become
1109		///   suspended and may only be reinstated by the founder.
1110		/// - `candidate_deposit`: The deposit required to make a bid for membership of the group.
1111		/// - `rules` - The rules of this society concerning membership.
1112		///
1113		/// Complexity: O(1)
1114		#[pallet::call_index(8)]
1115		#[pallet::weight(T::WeightInfo::found_society())]
1116		pub fn found_society(
1117			origin: OriginFor<T>,
1118			founder: AccountIdLookupOf<T>,
1119			max_members: u32,
1120			max_intake: u32,
1121			max_strikes: u32,
1122			candidate_deposit: BalanceOf<T, I>,
1123			rules: Vec<u8>,
1124		) -> DispatchResult {
1125			T::FounderSetOrigin::ensure_origin(origin)?;
1126			let founder = T::Lookup::lookup(founder)?;
1127			ensure!(!Head::<T, I>::exists(), Error::<T, I>::AlreadyFounded);
1128			ensure!(max_members > 1, Error::<T, I>::MaxMembers);
1129			// This should never fail in the context of this function...
1130			let params = GroupParams { max_members, max_intake, max_strikes, candidate_deposit };
1131			Parameters::<T, I>::put(params);
1132			Self::insert_member(&founder, 1)?;
1133			Head::<T, I>::put(&founder);
1134			Founder::<T, I>::put(&founder);
1135			Rules::<T, I>::put(T::Hashing::hash(&rules));
1136			Self::deposit_event(Event::<T, I>::Founded { founder });
1137			Ok(())
1138		}
1139
1140		/// Dissolve the society and remove all members.
1141		///
1142		/// The dispatch origin for this call must be Signed, and the signing account must be both
1143		/// the `Founder` and the `Head`. This implies that it may only be done when there is one
1144		/// member.
1145		#[pallet::call_index(9)]
1146		#[pallet::weight(T::WeightInfo::dissolve())]
1147		pub fn dissolve(origin: OriginFor<T>) -> DispatchResult {
1148			let founder = ensure_signed(origin)?;
1149			ensure!(Founder::<T, I>::get().as_ref() == Some(&founder), Error::<T, I>::NotFounder);
1150			ensure!(MemberCount::<T, I>::get() == 1, Error::<T, I>::NotHead);
1151
1152			let _ = Members::<T, I>::clear(u32::MAX, None);
1153			MemberCount::<T, I>::kill();
1154			let _ = MemberByIndex::<T, I>::clear(u32::MAX, None);
1155			let _ = SuspendedMembers::<T, I>::clear(u32::MAX, None);
1156			let _ = Payouts::<T, I>::clear(u32::MAX, None);
1157			let _ = Votes::<T, I>::clear(u32::MAX, None);
1158			let _ = VoteClearCursor::<T, I>::clear(u32::MAX, None);
1159			Head::<T, I>::kill();
1160			NextHead::<T, I>::kill();
1161			Founder::<T, I>::kill();
1162			Rules::<T, I>::kill();
1163			Parameters::<T, I>::kill();
1164			Pot::<T, I>::kill();
1165			RoundCount::<T, I>::kill();
1166			Bids::<T, I>::kill();
1167			Skeptic::<T, I>::kill();
1168			ChallengeRoundCount::<T, I>::kill();
1169			Defending::<T, I>::kill();
1170			let _ = DefenderVotes::<T, I>::clear(u32::MAX, None);
1171			let _ = Candidates::<T, I>::clear(u32::MAX, None);
1172			Self::deposit_event(Event::<T, I>::Unfounded { founder });
1173			Ok(())
1174		}
1175
1176		/// Allow suspension judgement origin to make judgement on a suspended member.
1177		///
1178		/// If a suspended member is forgiven, we simply add them back as a member, not affecting
1179		/// any of the existing storage items for that member.
1180		///
1181		/// If a suspended member is rejected, remove all associated storage items, including
1182		/// their payouts, and remove any vouched bids they currently have.
1183		///
1184		/// The dispatch origin for this call must be Signed from the Founder.
1185		///
1186		/// Parameters:
1187		/// - `who` - The suspended member to be judged.
1188		/// - `forgive` - A boolean representing whether the suspension judgement origin forgives
1189		///   (`true`) or rejects (`false`) a suspended member.
1190		#[pallet::call_index(10)]
1191		#[pallet::weight(T::WeightInfo::judge_suspended_member())]
1192		pub fn judge_suspended_member(
1193			origin: OriginFor<T>,
1194			who: AccountIdLookupOf<T>,
1195			forgive: bool,
1196		) -> DispatchResultWithPostInfo {
1197			ensure!(
1198				Some(ensure_signed(origin)?) == Founder::<T, I>::get(),
1199				Error::<T, I>::NotFounder
1200			);
1201			let who = T::Lookup::lookup(who)?;
1202			let record = SuspendedMembers::<T, I>::get(&who).ok_or(Error::<T, I>::NotSuspended)?;
1203			if forgive {
1204				// Try to add member back to society. Can fail with `MaxMembers` limit.
1205				Self::reinstate_member(&who, record.rank)?;
1206			} else {
1207				let payout_record = Payouts::<T, I>::take(&who);
1208				let total = payout_record
1209					.payouts
1210					.into_iter()
1211					.map(|x| x.1)
1212					.fold(Zero::zero(), |acc: BalanceOf<T, I>, x| acc.saturating_add(x));
1213				Self::unreserve_payout(total);
1214			}
1215			SuspendedMembers::<T, I>::remove(&who);
1216			Self::deposit_event(Event::<T, I>::SuspendedMemberJudgement { who, judged: forgive });
1217			Ok(Pays::No.into())
1218		}
1219
1220		/// Change the maximum number of members in society and the maximum number of new candidates
1221		/// in a single intake period.
1222		///
1223		/// The dispatch origin for this call must be Signed by the Founder.
1224		///
1225		/// Parameters:
1226		/// - `max_members` - The maximum number of members for the society. This must be no less
1227		///   than the current number of members.
1228		/// - `max_intake` - The maximum number of candidates per intake period.
1229		/// - `max_strikes`: The maximum number of strikes a member may get before they become
1230		///   suspended and may only be reinstated by the founder.
1231		/// - `candidate_deposit`: The deposit required to make a bid for membership of the group.
1232		#[pallet::call_index(11)]
1233		#[pallet::weight(T::WeightInfo::set_parameters())]
1234		pub fn set_parameters(
1235			origin: OriginFor<T>,
1236			max_members: u32,
1237			max_intake: u32,
1238			max_strikes: u32,
1239			candidate_deposit: BalanceOf<T, I>,
1240		) -> DispatchResult {
1241			ensure!(
1242				Some(ensure_signed(origin)?) == Founder::<T, I>::get(),
1243				Error::<T, I>::NotFounder
1244			);
1245			ensure!(max_members >= MemberCount::<T, I>::get(), Error::<T, I>::MaxMembers);
1246			let params = GroupParams { max_members, max_intake, max_strikes, candidate_deposit };
1247			Parameters::<T, I>::put(&params);
1248			Self::deposit_event(Event::<T, I>::NewParams { params });
1249			Ok(())
1250		}
1251
1252		/// Punish the skeptic with a strike if they did not vote on a candidate. Callable by the
1253		/// candidate.
1254		#[pallet::call_index(12)]
1255		#[pallet::weight(T::WeightInfo::punish_skeptic())]
1256		pub fn punish_skeptic(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
1257			let candidate = ensure_signed(origin)?;
1258			let mut candidacy =
1259				Candidates::<T, I>::get(&candidate).ok_or(Error::<T, I>::NotCandidate)?;
1260			ensure!(!candidacy.skeptic_struck, Error::<T, I>::AlreadyPunished);
1261			ensure!(!Self::in_progress(candidacy.round), Error::<T, I>::InProgress);
1262			let punished = Self::check_skeptic(&candidate, &mut candidacy);
1263			Candidates::<T, I>::insert(&candidate, candidacy);
1264			Ok(if punished { Pays::No } else { Pays::Yes }.into())
1265		}
1266
1267		/// Transform an approved candidate into a member. Callable only by the
1268		/// the candidate, and only after the period for voting has ended.
1269		#[pallet::call_index(13)]
1270		#[pallet::weight(T::WeightInfo::claim_membership())]
1271		pub fn claim_membership(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
1272			let candidate = ensure_signed(origin)?;
1273			let candidacy =
1274				Candidates::<T, I>::get(&candidate).ok_or(Error::<T, I>::NotCandidate)?;
1275			ensure!(candidacy.tally.clear_approval(), Error::<T, I>::NotApproved);
1276			ensure!(!Self::in_progress(candidacy.round), Error::<T, I>::InProgress);
1277			Self::induct_member(candidate, candidacy, 0)?;
1278			Ok(Pays::No.into())
1279		}
1280
1281		/// Transform an approved candidate into a member. Callable only by the Signed origin of the
1282		/// Founder, only after the period for voting has ended and only when the candidate is not
1283		/// clearly rejected.
1284		#[pallet::call_index(14)]
1285		#[pallet::weight(T::WeightInfo::bestow_membership())]
1286		pub fn bestow_membership(
1287			origin: OriginFor<T>,
1288			candidate: T::AccountId,
1289		) -> DispatchResultWithPostInfo {
1290			ensure!(
1291				Some(ensure_signed(origin)?) == Founder::<T, I>::get(),
1292				Error::<T, I>::NotFounder
1293			);
1294			let candidacy =
1295				Candidates::<T, I>::get(&candidate).ok_or(Error::<T, I>::NotCandidate)?;
1296			ensure!(!candidacy.tally.clear_rejection(), Error::<T, I>::Rejected);
1297			ensure!(!Self::in_progress(candidacy.round), Error::<T, I>::InProgress);
1298			Self::induct_member(candidate, candidacy, 0)?;
1299			Ok(Pays::No.into())
1300		}
1301
1302		/// Remove the candidate's application from the society. Callable only by the Signed origin
1303		/// of the Founder, only after the period for voting has ended, and only when they do not
1304		/// have a clear approval.
1305		///
1306		/// Any bid deposit is lost and voucher is banned.
1307		#[pallet::call_index(15)]
1308		#[pallet::weight(T::WeightInfo::kick_candidate())]
1309		pub fn kick_candidate(
1310			origin: OriginFor<T>,
1311			candidate: T::AccountId,
1312		) -> DispatchResultWithPostInfo {
1313			ensure!(
1314				Some(ensure_signed(origin)?) == Founder::<T, I>::get(),
1315				Error::<T, I>::NotFounder
1316			);
1317			let mut candidacy =
1318				Candidates::<T, I>::get(&candidate).ok_or(Error::<T, I>::NotCandidate)?;
1319			ensure!(!Self::in_progress(candidacy.round), Error::<T, I>::InProgress);
1320			ensure!(!candidacy.tally.clear_approval(), Error::<T, I>::Approved);
1321			Self::check_skeptic(&candidate, &mut candidacy);
1322			Self::reject_candidate(&candidate, &candidacy.kind);
1323			Candidates::<T, I>::remove(&candidate);
1324			Ok(Pays::No.into())
1325		}
1326
1327		/// Remove the candidate's application from the society. Callable only by the candidate.
1328		///
1329		/// Any bid deposit is lost and voucher is banned.
1330		#[pallet::call_index(16)]
1331		#[pallet::weight(T::WeightInfo::resign_candidacy())]
1332		pub fn resign_candidacy(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
1333			let candidate = ensure_signed(origin)?;
1334			let mut candidacy =
1335				Candidates::<T, I>::get(&candidate).ok_or(Error::<T, I>::NotCandidate)?;
1336			if !Self::in_progress(candidacy.round) {
1337				Self::check_skeptic(&candidate, &mut candidacy);
1338			}
1339			Self::reject_candidate(&candidate, &candidacy.kind);
1340			Candidates::<T, I>::remove(&candidate);
1341			Ok(Pays::No.into())
1342		}
1343
1344		/// Remove a `candidate`'s failed application from the society. Callable by any
1345		/// signed origin but only at the end of the subsequent round and only for
1346		/// a candidate with more rejections than approvals.
1347		///
1348		/// The bid deposit is lost and the voucher is banned.
1349		#[pallet::call_index(17)]
1350		#[pallet::weight(T::WeightInfo::drop_candidate())]
1351		pub fn drop_candidate(
1352			origin: OriginFor<T>,
1353			candidate: T::AccountId,
1354		) -> DispatchResultWithPostInfo {
1355			ensure_signed(origin)?;
1356			let candidacy =
1357				Candidates::<T, I>::get(&candidate).ok_or(Error::<T, I>::NotCandidate)?;
1358			ensure!(candidacy.tally.clear_rejection(), Error::<T, I>::NotRejected);
1359			ensure!(RoundCount::<T, I>::get() > candidacy.round + 1, Error::<T, I>::TooEarly);
1360			Self::reject_candidate(&candidate, &candidacy.kind);
1361			Candidates::<T, I>::remove(&candidate);
1362			Ok(Pays::No.into())
1363		}
1364
1365		/// Remove up to `max` stale votes for the given `candidate`.
1366		///
1367		/// May be called by any Signed origin, but only after the candidate's candidacy is ended.
1368		#[pallet::call_index(18)]
1369		#[pallet::weight(T::WeightInfo::cleanup_candidacy())]
1370		pub fn cleanup_candidacy(
1371			origin: OriginFor<T>,
1372			candidate: T::AccountId,
1373			max: u32,
1374		) -> DispatchResultWithPostInfo {
1375			ensure_signed(origin)?;
1376			ensure!(!Candidates::<T, I>::contains_key(&candidate), Error::<T, I>::InProgress);
1377			let maybe_cursor = VoteClearCursor::<T, I>::get(&candidate);
1378			let r =
1379				Votes::<T, I>::clear_prefix(&candidate, max, maybe_cursor.as_ref().map(|x| &x[..]));
1380			if let Some(cursor) = r.maybe_cursor {
1381				VoteClearCursor::<T, I>::insert(&candidate, BoundedVec::truncate_from(cursor));
1382			}
1383			Ok(if r.loops == 0 { Pays::Yes } else { Pays::No }.into())
1384		}
1385
1386		/// Remove up to `max` stale votes for the defender in the given `challenge_round`.
1387		///
1388		/// May be called by any Signed origin, but only after the challenge round is ended.
1389		#[pallet::call_index(19)]
1390		#[pallet::weight(T::WeightInfo::cleanup_challenge())]
1391		pub fn cleanup_challenge(
1392			origin: OriginFor<T>,
1393			challenge_round: RoundIndex,
1394			max: u32,
1395		) -> DispatchResultWithPostInfo {
1396			ensure_signed(origin)?;
1397			ensure!(
1398				challenge_round < ChallengeRoundCount::<T, I>::get(),
1399				Error::<T, I>::InProgress
1400			);
1401			let _ = DefenderVotes::<T, I>::clear_prefix(challenge_round, max, None);
1402			// clear_prefix() v2 is always returning backend = 0, ignoring it till v3.
1403			// let (_, backend, _, _) = r.deconstruct();
1404			// if backend == 0 { return Err(Error::<T, I>::NoVotes.into()); };
1405			Ok(Pays::No.into())
1406		}
1407
1408		/// Poke the deposit reserved when bidding.
1409		///
1410		/// The dispatch origin for this call must be _Signed_ and must be the bidder.
1411		///
1412		/// The transaction fee is waived if the deposit is changed after poking/reconsideration.
1413		///
1414		/// Emits `DepositPoked` if successful.
1415		#[pallet::call_index(20)]
1416		#[pallet::weight(T::WeightInfo::poke_deposit())]
1417		pub fn poke_deposit(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
1418			let who = ensure_signed(origin)?;
1419
1420			// Get current bids and find the bidder's bid
1421			let mut bids = Bids::<T, I>::get();
1422			let bid = bids.iter_mut().find(|bid| bid.who == who).ok_or(Error::<T, I>::NotBidder)?;
1423
1424			// Only handle deposit bids
1425			let old_deposit = match &bid.kind {
1426				BidKind::Deposit(amount) => *amount,
1427				_ => return Err(Error::<T, I>::NoDeposit.into()),
1428			};
1429
1430			let params = Parameters::<T, I>::get().ok_or(Error::<T, I>::NotGroup)?;
1431			let new_deposit = params.candidate_deposit;
1432
1433			if old_deposit == new_deposit {
1434				return Ok(Pays::Yes.into());
1435			}
1436
1437			if new_deposit > old_deposit {
1438				// Need to reserve more
1439				let extra = new_deposit.saturating_sub(old_deposit);
1440				T::Currency::reserve(&who, extra)?;
1441			} else {
1442				// Need to unreserve some
1443				let excess = old_deposit.saturating_sub(new_deposit);
1444				let remaining_unreserved = T::Currency::unreserve(&who, excess);
1445				if !remaining_unreserved.is_zero() {
1446					defensive!(
1447						"Failed to unreserve for full amount for bid (Requested, Actual)",
1448						(excess, excess.saturating_sub(remaining_unreserved))
1449					);
1450				}
1451			}
1452
1453			bid.kind = BidKind::Deposit(new_deposit);
1454			Bids::<T, I>::put(bids);
1455
1456			Self::deposit_event(Event::<T, I>::DepositPoked {
1457				who: who.clone(),
1458				old_deposit,
1459				new_deposit,
1460			});
1461
1462			Ok(Pays::No.into())
1463		}
1464	}
1465}
1466
1467/// Simple ensure origin struct to filter for the founder account.
1468pub struct EnsureFounder<T>(core::marker::PhantomData<T>);
1469impl<T: Config> EnsureOrigin<<T as frame_system::Config>::RuntimeOrigin> for EnsureFounder<T> {
1470	type Success = T::AccountId;
1471	fn try_origin(o: T::RuntimeOrigin) -> Result<Self::Success, T::RuntimeOrigin> {
1472		match (o.as_signer(), Founder::<T>::get()) {
1473			(Some(who), Some(f)) if *who == f => Ok(f),
1474			_ => Err(o),
1475		}
1476	}
1477
1478	#[cfg(feature = "runtime-benchmarks")]
1479	fn try_successful_origin() -> Result<T::RuntimeOrigin, ()> {
1480		let founder = Founder::<T>::get().ok_or(())?;
1481		Ok(T::RuntimeOrigin::from(frame_system::RawOrigin::Signed(founder)))
1482	}
1483}
1484
1485impl_ensure_origin_with_arg_ignoring_arg! {
1486	impl<{ T: Config, A }>
1487		EnsureOriginWithArg<T::RuntimeOrigin, A> for EnsureFounder<T>
1488	{}
1489}
1490
1491#[derive(Debug, PartialEq, Eq)]
1492pub enum Period<BlockNumber> {
1493	Voting { elapsed: BlockNumber, more: BlockNumber },
1494	Claim { elapsed: BlockNumber, more: BlockNumber },
1495	Intake { elapsed: BlockNumber },
1496}
1497
1498impl<T: Config<I>, I: 'static> Pallet<T, I> {
1499	/// Get the period we are currently in.
1500	fn period() -> Period<BlockNumberFor<T, I>> {
1501		let claim_period = T::ClaimPeriod::get();
1502		let voting_period = T::VotingPeriod::get();
1503		let rotation_period = voting_period + claim_period;
1504		let now = T::BlockNumberProvider::current_block_number();
1505		let phase = now % rotation_period;
1506		if now >= Self::next_intake_at() {
1507			Period::Intake { elapsed: now - Self::next_intake_at() }
1508		} else if phase < voting_period {
1509			Period::Voting { elapsed: phase, more: voting_period - phase }
1510		} else {
1511			Period::Claim { elapsed: phase - voting_period, more: rotation_period - phase }
1512		}
1513	}
1514
1515	/// Next intake (candidate/membership) rotation scheduled with [Config::BlockNumberProvider].
1516	///
1517	/// Rounds the previous block number up to the next rotation period (voting + claim periods).
1518	pub fn next_intake_at() -> BlockNumberFor<T, I> {
1519		match NextIntakeAt::<T, I>::get() {
1520			Some(next) => next,
1521			None => {
1522				// executed once.
1523				let now = T::BlockNumberProvider::current_block_number();
1524				let prev_block = now.saturating_sub(BlockNumberFor::<T, I>::one());
1525				let rotation_period = T::VotingPeriod::get().saturating_add(T::ClaimPeriod::get());
1526				let elapsed = prev_block % rotation_period;
1527				let next_intake_at = prev_block + (rotation_period - elapsed);
1528				NextIntakeAt::<T, I>::put(next_intake_at);
1529				next_intake_at
1530			},
1531		}
1532	}
1533
1534	/// Set the next intake (candidate/membership) rotation.
1535	///
1536	/// This supposed to be called once the current intake is executed.
1537	fn set_next_intake_at() {
1538		let prev_next_intake_at = Self::next_intake_at();
1539		let next_intake_at = prev_next_intake_at
1540			.saturating_add(T::VotingPeriod::get().saturating_add(T::ClaimPeriod::get()));
1541		NextIntakeAt::<T, I>::put(next_intake_at);
1542	}
1543
1544	/// Returns the next challenge rotation scheduled with [Config::BlockNumberProvider].
1545	///
1546	/// Rounds the previous block number up to the next multiple of the challenge duration.
1547	pub fn next_challenge_at() -> BlockNumberFor<T, I> {
1548		match NextChallengeAt::<T, I>::get() {
1549			Some(next) => next,
1550			None => {
1551				// executed once.
1552				let now = T::BlockNumberProvider::current_block_number();
1553				let prev_block = now.saturating_sub(BlockNumberFor::<T, I>::one());
1554				let challenge_period = T::ChallengePeriod::get();
1555				let elapsed = prev_block % challenge_period;
1556				let next_challenge_at = prev_block + (challenge_period - elapsed);
1557				NextChallengeAt::<T, I>::put(next_challenge_at);
1558				next_challenge_at
1559			},
1560		}
1561	}
1562
1563	/// Set the next challenge rotation.
1564	///
1565	/// This supposed to be called once the current challenge is executed.
1566	fn set_next_challenge_at() {
1567		let prev_next_challenge_at = Self::next_challenge_at();
1568		let next_challenge_at = prev_next_challenge_at.saturating_add(T::ChallengePeriod::get());
1569		NextChallengeAt::<T, I>::put(next_challenge_at);
1570	}
1571
1572	/// Returns true if the given `target_round` is still in its initial voting phase.
1573	fn in_progress(target_round: RoundIndex) -> bool {
1574		let round = RoundCount::<T, I>::get();
1575		target_round == round && matches!(Self::period(), Period::Voting { .. })
1576	}
1577
1578	/// Returns the new vote.
1579	fn do_vote(maybe_old: Option<Vote>, approve: bool, rank: Rank, tally: &mut Tally) -> Vote {
1580		match maybe_old {
1581			Some(Vote { approve: true, weight }) => tally.approvals.saturating_reduce(weight),
1582			Some(Vote { approve: false, weight }) => tally.rejections.saturating_reduce(weight),
1583			_ => {},
1584		}
1585		let weight_root = rank + 1;
1586		let weight = weight_root * weight_root;
1587		match approve {
1588			true => tally.approvals.saturating_accrue(weight),
1589			false => tally.rejections.saturating_accrue(weight),
1590		}
1591		Vote { approve, weight }
1592	}
1593
1594	/// Returns `true` if a punishment was given.
1595	fn check_skeptic(
1596		candidate: &T::AccountId,
1597		candidacy: &mut Candidacy<T::AccountId, BalanceOf<T, I>>,
1598	) -> bool {
1599		if RoundCount::<T, I>::get() != candidacy.round || candidacy.skeptic_struck {
1600			return false
1601		}
1602		// We expect the skeptic to have voted.
1603		let skeptic = match Skeptic::<T, I>::get() {
1604			Some(s) => s,
1605			None => return false,
1606		};
1607		let maybe_vote = Votes::<T, I>::get(&candidate, &skeptic);
1608		let approved = candidacy.tally.clear_approval();
1609		let rejected = candidacy.tally.clear_rejection();
1610		match (maybe_vote, approved, rejected) {
1611			(None, _, _) |
1612			(Some(Vote { approve: true, .. }), false, true) |
1613			(Some(Vote { approve: false, .. }), true, false) => {
1614				// Can't do much if the punishment doesn't work out.
1615				if Self::strike_member(&skeptic).is_ok() {
1616					candidacy.skeptic_struck = true;
1617					true
1618				} else {
1619					false
1620				}
1621			},
1622			_ => false,
1623		}
1624	}
1625
1626	/// End the current challenge period and start a new one.
1627	fn rotate_challenge(rng: &mut impl RngCore) {
1628		let mut next_defender = None;
1629		let mut round = ChallengeRoundCount::<T, I>::get();
1630
1631		// End current defender rotation
1632		if let Some((defender, skeptic, tally)) = Defending::<T, I>::get() {
1633			// We require strictly more approvals, since the member should be voting for themselves.
1634			if !tally.more_approvals() {
1635				// Member has failed the challenge: Suspend them. This will fail if they are Head
1636				// or Founder, in which case we ignore.
1637				let _ = Self::suspend_member(&defender);
1638			}
1639
1640			// Check defender skeptic voted and that their vote was with the majority.
1641			let skeptic_vote = DefenderVotes::<T, I>::get(round, &skeptic);
1642			match (skeptic_vote, tally.more_approvals(), tally.more_rejections()) {
1643				(None, _, _) |
1644				(Some(Vote { approve: true, .. }), false, true) |
1645				(Some(Vote { approve: false, .. }), true, false) => {
1646					// Punish skeptic and challenge them next.
1647					let _ = Self::strike_member(&skeptic);
1648					let founder = Founder::<T, I>::get();
1649					let head = Head::<T, I>::get();
1650					if Some(&skeptic) != founder.as_ref() && Some(&skeptic) != head.as_ref() {
1651						next_defender = Some(skeptic);
1652					}
1653				},
1654				_ => {},
1655			}
1656			round.saturating_inc();
1657			ChallengeRoundCount::<T, I>::put(round);
1658		}
1659
1660		// Avoid challenging if there's only two members since we never challenge the Head or
1661		// the Founder.
1662		if MemberCount::<T, I>::get() > 2 {
1663			let defender = next_defender
1664				.or_else(|| Self::pick_defendant(rng))
1665				.expect("exited if members empty; qed");
1666			let skeptic =
1667				Self::pick_member_except(rng, &defender).expect("exited if members empty; qed");
1668			Self::deposit_event(Event::<T, I>::Challenged { member: defender.clone() });
1669			Defending::<T, I>::put((defender, skeptic, Tally::default()));
1670		} else {
1671			Defending::<T, I>::kill();
1672		}
1673	}
1674
1675	/// End the current intake period and begin a new one.
1676	///
1677	/// ---------------------------------------------
1678	///  #10  || #11           _              || #12
1679	///       || Voting        | Claiming     ||
1680	/// ---------------------------------------------
1681	fn rotate_intake(rng: &mut impl RngCore) {
1682		// We assume there's at least one member or this logic won't work.
1683		let member_count = MemberCount::<T, I>::get();
1684		if member_count < 1 {
1685			return
1686		}
1687		let maybe_head = NextHead::<T, I>::take();
1688		if let Some(head) = maybe_head {
1689			Head::<T, I>::put(&head.who);
1690		}
1691
1692		// Bump the pot by at most `PeriodSpend`, but less if there's not very much left in our
1693		// account.
1694		let mut pot = Pot::<T, I>::get();
1695		let unaccounted = T::Currency::free_balance(&Self::account_id()).saturating_sub(pot);
1696		pot.saturating_accrue(T::PeriodSpend::get().min(unaccounted / 2u8.into()));
1697		Pot::<T, I>::put(&pot);
1698
1699		// Bump round and create the new intake.
1700		let mut round_count = RoundCount::<T, I>::get();
1701		round_count.saturating_inc();
1702		let candidate_count = Self::select_new_candidates(round_count, member_count, pot);
1703		if candidate_count > 0 {
1704			// Select a member at random and make them the skeptic for this round.
1705			let skeptic = Self::pick_member(rng).expect("exited if members empty; qed");
1706			Skeptic::<T, I>::put(skeptic);
1707		}
1708		RoundCount::<T, I>::put(round_count);
1709	}
1710
1711	/// Remove a selection of bidding accounts such that the total bids is no greater than `Pot` and
1712	/// the number of bids would not surpass `MaxMembers` if all were accepted. At most one bid may
1713	/// be zero.
1714	///
1715	/// Candidates are inserted from each bidder.
1716	///
1717	/// The number of candidates inserted are returned.
1718	pub fn select_new_candidates(
1719		round: RoundIndex,
1720		member_count: u32,
1721		pot: BalanceOf<T, I>,
1722	) -> u32 {
1723		// Get the number of left-most bidders whose bids add up to less than `pot`.
1724		let mut bids = Bids::<T, I>::get();
1725		let params = match Parameters::<T, I>::get() {
1726			Some(params) => params,
1727			None => return 0,
1728		};
1729		let max_selections: u32 = params
1730			.max_intake
1731			.min(params.max_members.saturating_sub(member_count))
1732			.min(bids.len() as u32);
1733
1734		let mut selections = 0;
1735		// A running total of the cost to onboard these bids
1736		let mut total_cost: BalanceOf<T, I> = Zero::zero();
1737
1738		bids.retain(|bid| {
1739			// We only accept a zero bid as the first selection.
1740			total_cost.saturating_accrue(bid.value);
1741			let accept = selections < max_selections &&
1742				(!bid.value.is_zero() || selections == 0) &&
1743				total_cost <= pot;
1744			if accept {
1745				let candidacy = Candidacy {
1746					round,
1747					kind: bid.kind.clone(),
1748					bid: bid.value,
1749					tally: Default::default(),
1750					skeptic_struck: false,
1751				};
1752				Candidates::<T, I>::insert(&bid.who, candidacy);
1753				selections.saturating_inc();
1754			}
1755			!accept
1756		});
1757
1758		// No need to reset Bids if we're not taking anything.
1759		Bids::<T, I>::put(&bids);
1760		selections
1761	}
1762
1763	/// Puts a bid into storage ordered by smallest to largest value.
1764	/// Allows a maximum of 1000 bids in queue, removing largest value people first.
1765	fn insert_bid(
1766		bids: &mut BoundedVec<Bid<T::AccountId, BalanceOf<T, I>>, T::MaxBids>,
1767		who: &T::AccountId,
1768		value: BalanceOf<T, I>,
1769		bid_kind: BidKind<T::AccountId, BalanceOf<T, I>>,
1770	) {
1771		let pos = bids.iter().position(|bid| bid.value > value).unwrap_or(bids.len());
1772		let r = bids.force_insert_keep_left(pos, Bid { value, who: who.clone(), kind: bid_kind });
1773		let maybe_discarded = match r {
1774			Ok(x) => x,
1775			Err(x) => Some(x),
1776		};
1777		if let Some(discarded) = maybe_discarded {
1778			Self::clean_bid(&discarded);
1779			Self::deposit_event(Event::<T, I>::AutoUnbid { candidate: discarded.who });
1780		}
1781	}
1782
1783	/// Either unreserve the deposit or free up the vouching member.
1784	///
1785	/// In neither case can we do much if the action isn't completable, but there's
1786	/// no reason that either should fail.
1787	///
1788	/// WARNING: This alters the voucher item of `Members`. You must ensure that you do not
1789	/// accidentally overwrite it with an older value after calling this.
1790	fn clean_bid(bid: &Bid<T::AccountId, BalanceOf<T, I>>) {
1791		match &bid.kind {
1792			BidKind::Deposit(deposit) => {
1793				let err_amount = T::Currency::unreserve(&bid.who, *deposit);
1794				debug_assert!(err_amount.is_zero());
1795			},
1796			BidKind::Vouch(voucher, _) => {
1797				Members::<T, I>::mutate_extant(voucher, |record| record.vouching = None);
1798			},
1799		}
1800	}
1801
1802	/// Either repatriate the deposit into the Society account or ban the vouching member.
1803	///
1804	/// In neither case can we do much if the action isn't completable, but there's
1805	/// no reason that either should fail.
1806	///
1807	/// WARNING: This alters the voucher item of `Members`. You must ensure that you do not
1808	/// accidentally overwrite it with an older value after calling this.
1809	fn reject_candidate(who: &T::AccountId, kind: &BidKind<T::AccountId, BalanceOf<T, I>>) {
1810		match kind {
1811			BidKind::Deposit(deposit) => {
1812				let pot = Self::account_id();
1813				let free = BalanceStatus::Free;
1814				let r = T::Currency::repatriate_reserved(&who, &pot, *deposit, free);
1815				debug_assert!(r.is_ok());
1816			},
1817			BidKind::Vouch(voucher, _) => {
1818				Members::<T, I>::mutate_extant(voucher, |record| {
1819					record.vouching = Some(VouchingStatus::Banned)
1820				});
1821			},
1822		}
1823	}
1824
1825	/// Check a user has a bid.
1826	fn has_bid(bids: &Vec<Bid<T::AccountId, BalanceOf<T, I>>>, who: &T::AccountId) -> bool {
1827		// Bids are ordered by `value`, so we cannot binary search for a user.
1828		bids.iter().any(|bid| bid.who == *who)
1829	}
1830
1831	/// Add a member to the members list. If the user is already a member, do nothing. Can fail when
1832	/// `MaxMember` limit is reached, but in that case it has no side-effects.
1833	///
1834	/// Set the `payouts` for the member. NOTE: This *WILL NOT RESERVE THE FUNDS TO MAKE THE
1835	/// PAYOUT*. Only set this to be non-empty if you already have the funds reserved in the Payouts
1836	/// account.
1837	///
1838	/// NOTE: Generally you should not use this, and instead use `add_new_member` or
1839	/// `reinstate_member`, whose names clearly match the desired intention.
1840	fn insert_member(who: &T::AccountId, rank: Rank) -> DispatchResult {
1841		let params = Parameters::<T, I>::get().ok_or(Error::<T, I>::NotGroup)?;
1842		ensure!(MemberCount::<T, I>::get() < params.max_members, Error::<T, I>::MaxMembers);
1843		let index = MemberCount::<T, I>::mutate(|i| {
1844			i.saturating_accrue(1);
1845			*i - 1
1846		});
1847		let record = MemberRecord { rank, strikes: 0, vouching: None, index };
1848		Members::<T, I>::insert(who, record);
1849		MemberByIndex::<T, I>::insert(index, who);
1850		Ok(())
1851	}
1852
1853	/// Add a member back to the members list, setting their `rank` and `payouts`.
1854	///
1855	/// Can fail when `MaxMember` limit is reached, but in that case it has no side-effects.
1856	///
1857	/// The `payouts` value must be exactly as it was prior to suspension since no further funds
1858	/// will be reserved.
1859	fn reinstate_member(who: &T::AccountId, rank: Rank) -> DispatchResult {
1860		Self::insert_member(who, rank)
1861	}
1862
1863	/// Add a member to the members list. If the user is already a member, do nothing. Can fail when
1864	/// `MaxMember` limit is reached, but in that case it has no side-effects.
1865	fn add_new_member(who: &T::AccountId, rank: Rank) -> DispatchResult {
1866		Self::insert_member(who, rank)
1867	}
1868
1869	/// Induct a new member into the set.
1870	fn induct_member(
1871		candidate: T::AccountId,
1872		mut candidacy: Candidacy<T::AccountId, BalanceOf<T, I>>,
1873		rank: Rank,
1874	) -> DispatchResult {
1875		Self::add_new_member(&candidate, rank)?;
1876		Self::check_skeptic(&candidate, &mut candidacy);
1877
1878		let next_head = NextHead::<T, I>::get()
1879			.filter(|old| {
1880				old.round > candidacy.round ||
1881					old.round == candidacy.round && old.bid < candidacy.bid
1882			})
1883			.unwrap_or_else(|| IntakeRecord {
1884				who: candidate.clone(),
1885				bid: candidacy.bid,
1886				round: candidacy.round,
1887			});
1888		NextHead::<T, I>::put(next_head);
1889
1890		let now = T::BlockNumberProvider::current_block_number();
1891		let maturity = now + Self::lock_duration(MemberCount::<T, I>::get());
1892		Self::reward_bidder(&candidate, candidacy.bid, candidacy.kind, maturity);
1893
1894		Candidates::<T, I>::remove(&candidate);
1895		Ok(())
1896	}
1897
1898	fn strike_member(who: &T::AccountId) -> DispatchResult {
1899		let mut record = Members::<T, I>::get(who).ok_or(Error::<T, I>::NotMember)?;
1900		record.strikes.saturating_inc();
1901		Members::<T, I>::insert(who, &record);
1902		// ^^^ Keep the member record mutation self-contained as we might be suspending them later
1903		// in this function.
1904
1905		if record.strikes >= T::GraceStrikes::get() {
1906			// Too many strikes: slash the payout in half.
1907			let total_payout = Payouts::<T, I>::get(who)
1908				.payouts
1909				.iter()
1910				.fold(BalanceOf::<T, I>::zero(), |acc, x| acc.saturating_add(x.1));
1911			Self::slash_payout(who, total_payout / 2u32.into());
1912		}
1913
1914		let params = Parameters::<T, I>::get().ok_or(Error::<T, I>::NotGroup)?;
1915		if record.strikes >= params.max_strikes {
1916			// Way too many strikes: suspend.
1917			let _ = Self::suspend_member(who);
1918		}
1919		Ok(())
1920	}
1921
1922	/// Remove a member from the members list and return the candidacy.
1923	///
1924	/// If the member was vouching, then this will be reset. Any bidders that the member was
1925	/// vouching for will be cancelled unless they are already selected as candidates (in which case
1926	/// they will be able to stand).
1927	///
1928	/// If the member has existing payouts, they will be retained in the resultant `MemberRecord`
1929	/// and the funds will remain reserved.
1930	///
1931	/// The Head and the Founder may never be removed.
1932	pub fn remove_member(m: &T::AccountId) -> Result<MemberRecord, DispatchError> {
1933		ensure!(Head::<T, I>::get().as_ref() != Some(m), Error::<T, I>::Head);
1934		ensure!(Founder::<T, I>::get().as_ref() != Some(m), Error::<T, I>::Founder);
1935		if let Some(mut record) = Members::<T, I>::get(m) {
1936			let index = record.index;
1937			let last_index = MemberCount::<T, I>::mutate(|i| {
1938				i.saturating_reduce(1);
1939				*i
1940			});
1941			if index != last_index {
1942				// Move the member with the last index down to the index of the member to be
1943				// removed.
1944				if let Some(other) = MemberByIndex::<T, I>::get(last_index) {
1945					MemberByIndex::<T, I>::insert(index, &other);
1946					Members::<T, I>::mutate(other, |m_r| {
1947						if let Some(r) = m_r {
1948							r.index = index
1949						}
1950					});
1951				} else {
1952					debug_assert!(false, "ERROR: No member at the last index position?");
1953				}
1954			}
1955
1956			MemberByIndex::<T, I>::remove(last_index);
1957			Members::<T, I>::remove(m);
1958			// Remove their vouching status, potentially unbanning them in the future.
1959			if record.vouching.take() == Some(VouchingStatus::Vouching) {
1960				// Try to remove their bid if they are vouching.
1961				// If their vouch is already a candidate, do nothing.
1962				Bids::<T, I>::mutate(|bids|
1963					// Try to find the matching bid
1964					if let Some(pos) = bids.iter().position(|b| b.kind.is_vouch(&m)) {
1965						// Remove the bid, and emit an event
1966						let vouched = bids.remove(pos).who;
1967						Self::deposit_event(Event::<T, I>::Unvouch { candidate: vouched });
1968					}
1969				);
1970			}
1971			Ok(record)
1972		} else {
1973			Err(Error::<T, I>::NotMember.into())
1974		}
1975	}
1976
1977	/// Remove a member from the members set and add them to the suspended members.
1978	///
1979	/// If the member was vouching, then this will be reset. Any bidders that the member was
1980	/// vouching for will be cancelled unless they are already selected as candidates (in which case
1981	/// they will be able to stand).
1982	fn suspend_member(who: &T::AccountId) -> DispatchResult {
1983		let record = Self::remove_member(&who)?;
1984		SuspendedMembers::<T, I>::insert(who, record);
1985		Self::deposit_event(Event::<T, I>::MemberSuspended { member: who.clone() });
1986		Ok(())
1987	}
1988
1989	/// Select a member at random, given the RNG `rng`.
1990	///
1991	/// If no members exist (or the state is inconsistent), then `None` may be returned.
1992	fn pick_member(rng: &mut impl RngCore) -> Option<T::AccountId> {
1993		let member_count = MemberCount::<T, I>::get();
1994		if member_count == 0 {
1995			return None
1996		}
1997		let random_index = rng.next_u32() % member_count;
1998		MemberByIndex::<T, I>::get(random_index)
1999	}
2000
2001	/// Select a member at random except `exception`, given the RNG `rng`.
2002	///
2003	/// If `exception` is the only member (or the state is inconsistent), then `None` may be
2004	/// returned.
2005	fn pick_member_except(
2006		rng: &mut impl RngCore,
2007		exception: &T::AccountId,
2008	) -> Option<T::AccountId> {
2009		let member_count = MemberCount::<T, I>::get();
2010		if member_count <= 1 {
2011			return None
2012		}
2013		let random_index = rng.next_u32() % (member_count - 1);
2014		let pick = MemberByIndex::<T, I>::get(random_index);
2015		if pick.as_ref() == Some(exception) {
2016			MemberByIndex::<T, I>::get(member_count - 1)
2017		} else {
2018			pick
2019		}
2020	}
2021
2022	/// Select a member who is able to defend at random, given the RNG `rng`.
2023	///
2024	/// If only the Founder and Head members exist (or the state is inconsistent), then `None`
2025	/// may be returned.
2026	fn pick_defendant(rng: &mut impl RngCore) -> Option<T::AccountId> {
2027		let member_count = MemberCount::<T, I>::get();
2028		if member_count <= 2 {
2029			return None
2030		}
2031		// Founder is always at index 0, so we should never pick that one.
2032		// Head will typically but not always be the highest index. We assume it is for now and
2033		// fix it up later if not.
2034		let head = Head::<T, I>::get();
2035		let pickable_count = member_count - if head.is_some() { 2 } else { 1 };
2036		let random_index = rng.next_u32() % pickable_count + 1;
2037		let pick = MemberByIndex::<T, I>::get(random_index);
2038		if pick == head && head.is_some() {
2039			// Turns out that head was not the last index since we managed to pick it. Exchange our
2040			// pick for the last index.
2041			MemberByIndex::<T, I>::get(member_count - 1)
2042		} else {
2043			pick
2044		}
2045	}
2046
2047	/// Pay an accepted candidate their bid value.
2048	fn reward_bidder(
2049		candidate: &T::AccountId,
2050		value: BalanceOf<T, I>,
2051		kind: BidKind<T::AccountId, BalanceOf<T, I>>,
2052		maturity: BlockNumberFor<T, I>,
2053	) {
2054		let value = match kind {
2055			BidKind::Deposit(deposit) => {
2056				// In the case that a normal deposit bid is accepted we unreserve
2057				// the deposit.
2058				let err_amount = T::Currency::unreserve(candidate, deposit);
2059				debug_assert!(err_amount.is_zero());
2060				value
2061			},
2062			BidKind::Vouch(voucher, tip) => {
2063				// Check that the voucher is still vouching, else some other logic may have removed
2064				// their status.
2065				if let Some(mut record) = Members::<T, I>::get(&voucher) {
2066					if let Some(VouchingStatus::Vouching) = record.vouching {
2067						// In the case that a vouched-for bid is accepted we unset the
2068						// vouching status and transfer the tip over to the voucher.
2069						record.vouching = None;
2070						Self::bump_payout(&voucher, maturity, tip.min(value));
2071						Members::<T, I>::insert(&voucher, record);
2072						value.saturating_sub(tip)
2073					} else {
2074						value
2075					}
2076				} else {
2077					value
2078				}
2079			},
2080		};
2081
2082		Self::bump_payout(candidate, maturity, value);
2083	}
2084
2085	/// Bump the payout amount of `who`, to be unlocked at the given block number.
2086	///
2087	/// It is the caller's duty to ensure that `who` is already a member. This does nothing if `who`
2088	/// is not a member or if `value` is zero.
2089	fn bump_payout(who: &T::AccountId, when: BlockNumberFor<T, I>, value: BalanceOf<T, I>) {
2090		if value.is_zero() {
2091			return
2092		}
2093		if let Some(MemberRecord { rank: 0, .. }) = Members::<T, I>::get(who) {
2094			Payouts::<T, I>::mutate(who, |record| {
2095				// Members of rank 1 never get payouts.
2096				match record.payouts.binary_search_by_key(&when, |x| x.0) {
2097					Ok(index) => record.payouts[index].1.saturating_accrue(value),
2098					Err(index) => {
2099						// If they have too many pending payouts, then we take discard the payment.
2100						let _ = record.payouts.try_insert(index, (when, value));
2101					},
2102				}
2103			});
2104			Self::reserve_payout(value);
2105		}
2106	}
2107
2108	/// Attempt to slash the payout of some member. Return the total amount that was deducted.
2109	fn slash_payout(who: &T::AccountId, value: BalanceOf<T, I>) -> BalanceOf<T, I> {
2110		let mut record = Payouts::<T, I>::get(who);
2111		let mut rest = value;
2112		while !record.payouts.is_empty() {
2113			if let Some(new_rest) = rest.checked_sub(&record.payouts[0].1) {
2114				// not yet totally slashed after this one; drop it completely.
2115				rest = new_rest;
2116				record.payouts.remove(0);
2117			} else {
2118				// whole slash is accounted for.
2119				record.payouts[0].1.saturating_reduce(rest);
2120				rest = Zero::zero();
2121				break
2122			}
2123		}
2124		Payouts::<T, I>::insert(who, record);
2125		value - rest
2126	}
2127
2128	/// Transfer some `amount` from the main account into the payouts account and reduce the Pot
2129	/// by this amount.
2130	fn reserve_payout(amount: BalanceOf<T, I>) {
2131		// Transfer payout from the Pot into the payouts account.
2132		Pot::<T, I>::mutate(|pot| pot.saturating_reduce(amount));
2133
2134		// this should never fail since we ensure we can afford the payouts in a previous
2135		// block, but there's not much we can do to recover if it fails anyway.
2136		let res = T::Currency::transfer(&Self::account_id(), &Self::payouts(), amount, AllowDeath);
2137		debug_assert!(res.is_ok());
2138	}
2139
2140	/// Transfer some `amount` from the main account into the payouts account and increase the Pot
2141	/// by this amount.
2142	fn unreserve_payout(amount: BalanceOf<T, I>) {
2143		// Transfer payout from the Pot into the payouts account.
2144		Pot::<T, I>::mutate(|pot| pot.saturating_accrue(amount));
2145
2146		// this should never fail since we ensure we can afford the payouts in a previous
2147		// block, but there's not much we can do to recover if it fails anyway.
2148		let res = T::Currency::transfer(&Self::payouts(), &Self::account_id(), amount, AllowDeath);
2149		debug_assert!(res.is_ok());
2150	}
2151
2152	/// The account ID of the treasury pot.
2153	///
2154	/// This actually does computation. If you need to keep using it, then make sure you cache the
2155	/// value and only call this once.
2156	pub fn account_id() -> T::AccountId {
2157		T::PalletId::get().into_account_truncating()
2158	}
2159
2160	/// The account ID of the payouts pot. This is where payouts are made from.
2161	///
2162	/// This actually does computation. If you need to keep using it, then make sure you cache the
2163	/// value and only call this once.
2164	pub fn payouts() -> T::AccountId {
2165		T::PalletId::get().into_sub_account_truncating(b"payouts")
2166	}
2167
2168	/// Return the duration of the lock, in blocks, with the given number of members.
2169	///
2170	/// This is a rather opaque calculation based on the formula here:
2171	/// https://www.desmos.com/calculator/9itkal1tce
2172	fn lock_duration(x: u32) -> BlockNumberFor<T, I> {
2173		let lock_pc = 100 - 50_000 / (x + 500);
2174		Percent::from_percent(lock_pc as u8) * T::MaxLockDuration::get()
2175	}
2176}
2177
2178impl<T: Config<I>, I: 'static> OnUnbalanced<NegativeImbalanceOf<T, I>> for Pallet<T, I> {
2179	fn on_nonzero_unbalanced(amount: NegativeImbalanceOf<T, I>) {
2180		let numeric_amount = amount.peek();
2181
2182		// Must resolve into existing but better to be safe.
2183		let _ = T::Currency::resolve_creating(&Self::account_id(), amount);
2184
2185		Self::deposit_event(Event::<T, I>::Deposit { value: numeric_amount });
2186	}
2187}