election.rs 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  1. //! Council Elections Manager
  2. //!
  3. //! # Election Parameters:
  4. //! We don't currently handle zero periods, zero council term, zero council size and candidacy
  5. //! limit in any special way. The behaviour in such cases:
  6. //!
  7. //! - Setting any period to 0 will mean the election getting stuck in that stage, until force changing
  8. //! the state.
  9. //!
  10. //! - Council Size of 0 - no limit to size of council, all applicants that move beyond
  11. //! announcing stage would become council members, so effectively the candidacy limit will
  12. //! be the size of the council, voting and revealing have no impact on final results.
  13. //!
  14. //! - If candidacy limit is zero and council size > 0, council_size number of applicants will reach the voting stage.
  15. //! and become council members, voting will have no impact on final results.
  16. //!
  17. //! - If both candidacy limit and council size are zero then all applicant become council members
  18. //! since no filtering occurs at end of announcing stage.
  19. //!
  20. //! We only guard against these edge cases in the [`set_election_parameters`] call.
  21. //!
  22. //! [`set_election_parameters`]: struct.Module.html#method.set_election_parameters
  23. // Clippy linter warning
  24. #![allow(clippy::type_complexity)]
  25. // disable it because of possible frontend API break
  26. // TODO: remove post-Constaninople
  27. // Clippy linter warning
  28. #![allow(clippy::redundant_closure_call)] // disable it because of the substrate lib design
  29. use rstd::prelude::*;
  30. use srml_support::traits::{Currency, ReservableCurrency};
  31. use srml_support::{decl_event, decl_module, decl_storage, dispatch::Result, ensure};
  32. use system::{self, ensure_root, ensure_signed};
  33. use codec::{Decode, Encode};
  34. use rstd::collections::btree_map::BTreeMap;
  35. use rstd::ops::Add;
  36. #[cfg(feature = "std")]
  37. use serde::{Deserialize, Serialize};
  38. use sr_primitives::traits::{Hash, Zero};
  39. use super::sealed_vote::SealedVote;
  40. use super::stake::Stake;
  41. use super::council;
  42. use crate::election_params::ElectionParameters;
  43. pub use common::currency::{BalanceOf, GovernanceCurrency};
  44. pub trait Trait:
  45. system::Trait + council::Trait + GovernanceCurrency + membership::members::Trait
  46. {
  47. type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
  48. type CouncilElected: CouncilElected<Seats<Self::AccountId, BalanceOf<Self>>, Self::BlockNumber>;
  49. }
  50. pub static MSG_CANNOT_CHANGE_PARAMS_DURING_ELECTION: &str = "CannotChangeParamsDuringElection";
  51. #[derive(Clone, Copy, Encode, Decode)]
  52. pub enum ElectionStage<BlockNumber> {
  53. Announcing(BlockNumber),
  54. Voting(BlockNumber),
  55. Revealing(BlockNumber),
  56. }
  57. #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
  58. #[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
  59. pub struct Seat<AccountId, Balance> {
  60. pub member: AccountId,
  61. pub stake: Balance,
  62. pub backers: Vec<Backer<AccountId, Balance>>,
  63. }
  64. impl<AccountId, Balance> Seat<AccountId, Balance>
  65. where
  66. Balance: Add<Output = Balance> + Copy,
  67. {
  68. pub fn calc_total_stake(&self) -> Balance {
  69. self.backers
  70. .iter()
  71. .fold(self.stake, |acc, backer| acc + backer.stake)
  72. }
  73. }
  74. #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
  75. #[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
  76. pub struct Backer<AccountId, Balance> {
  77. pub member: AccountId,
  78. pub stake: Balance,
  79. }
  80. pub type Seats<AccountId, Balance> = Vec<Seat<AccountId, Balance>>;
  81. // Hook for setting a new council when it is elected
  82. pub trait CouncilElected<Elected, Term> {
  83. fn council_elected(new_council: Elected, term: Term);
  84. }
  85. impl<Elected, Term> CouncilElected<Elected, Term> for () {
  86. fn council_elected(_new_council: Elected, _term: Term) {}
  87. }
  88. impl<Elected, Term, X: CouncilElected<Elected, Term>> CouncilElected<Elected, Term> for (X,) {
  89. fn council_elected(new_council: Elected, term: Term) {
  90. X::council_elected(new_council, term);
  91. }
  92. }
  93. // Chain of handlers.
  94. impl<
  95. Elected: Clone,
  96. Term: Clone,
  97. X: CouncilElected<Elected, Term>,
  98. Y: CouncilElected<Elected, Term>,
  99. > CouncilElected<Elected, Term> for (X, Y)
  100. {
  101. fn council_elected(new_council: Elected, term: Term) {
  102. X::council_elected(new_council.clone(), term.clone());
  103. Y::council_elected(new_council, term);
  104. }
  105. }
  106. #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
  107. #[derive(Clone, Copy, Encode, Decode, Default)]
  108. pub struct TransferableStake<Balance> {
  109. seat: Balance,
  110. backing: Balance,
  111. }
  112. // can we use a type alias to overcome name clashes of public types with other modules?
  113. pub type ElectionStake<T> = Stake<BalanceOf<T>>;
  114. decl_storage! {
  115. trait Store for Module<T: Trait> as CouncilElection {
  116. // Flag for wether to automatically start an election after a council term ends
  117. AutoStart get(auto_start) config() : bool = true;
  118. // Current stage if there is an election running
  119. Stage get(stage): Option<ElectionStage<T::BlockNumber>>;
  120. // The election round
  121. Round get(round): u32;
  122. ExistingStakeHolders get(existing_stake_holders): Vec<T::AccountId>;
  123. TransferableStakes get(transferable_stakes): map T::AccountId => TransferableStake<BalanceOf<T>>;
  124. Applicants get(applicants): Vec<T::AccountId>;
  125. ApplicantStakes get(applicant_stakes): map T::AccountId => ElectionStake<T>;
  126. Commitments get(commitments): Vec<T::Hash>;
  127. // TODO value type of this map looks scary, is there any way to simplify the notation?
  128. Votes get(votes): map T::Hash => SealedVote<T::AccountId, ElectionStake<T>, T::Hash, T::AccountId>;
  129. // Current Election Parameters.
  130. // Should we replace all the individual values with a single ElectionParameters type?
  131. // Having them individually makes it more flexible to add and remove new parameters in future
  132. // without dealing with migration issues.
  133. AnnouncingPeriod get(announcing_period): T::BlockNumber;
  134. VotingPeriod get(voting_period): T::BlockNumber;
  135. RevealingPeriod get(revealing_period): T::BlockNumber;
  136. CouncilSize get(council_size): u32;
  137. CandidacyLimit get (candidacy_limit): u32;
  138. MinCouncilStake get(min_council_stake): BalanceOf<T>;
  139. NewTermDuration get(new_term_duration): T::BlockNumber;
  140. MinVotingStake get(min_voting_stake): BalanceOf<T>;
  141. }
  142. add_extra_genesis {
  143. config(election_parameters): ElectionParameters<BalanceOf<T>, T::BlockNumber>;
  144. build(|config: &GenesisConfig<T>| {
  145. config.election_parameters.ensure_valid().expect("Invalid Election Parameters");
  146. Module::<T>::set_verified_election_parameters(config.election_parameters);
  147. });
  148. }
  149. }
  150. // Event for this module.
  151. decl_event!(
  152. pub enum Event<T> where
  153. <T as system::Trait>::BlockNumber,
  154. <T as system::Trait>::AccountId,
  155. <T as system::Trait>::Hash {
  156. /// A new election started
  157. ElectionStarted(),
  158. AnnouncingStarted(u32),
  159. AnnouncingEnded(),
  160. VotingStarted(),
  161. VotingEnded(),
  162. RevealingStarted(),
  163. RevealingEnded(),
  164. CouncilElected(BlockNumber),
  165. Applied(AccountId),
  166. Voted(AccountId, Hash),
  167. Revealed(AccountId, Hash, AccountId),
  168. }
  169. );
  170. impl<T: Trait> Module<T> {
  171. // HELPERS - IMMUTABLES
  172. fn council_size_usize() -> usize {
  173. Self::council_size() as usize
  174. }
  175. fn candidacy_limit_usize() -> usize {
  176. Self::candidacy_limit() as usize
  177. }
  178. fn current_block_number_plus(length: T::BlockNumber) -> T::BlockNumber {
  179. <system::Module<T>>::block_number() + length
  180. }
  181. fn can_participate(sender: &T::AccountId) -> bool {
  182. !<T as GovernanceCurrency>::Currency::free_balance(sender).is_zero()
  183. && <membership::members::Module<T>>::is_member_account(sender)
  184. }
  185. // PUBLIC IMMUTABLES
  186. /// Returns true if an election is running
  187. pub fn is_election_running() -> bool {
  188. Self::stage().is_some()
  189. }
  190. /// Returns block number at which current stage will end if an election is running.
  191. pub fn stage_ends_at() -> Option<T::BlockNumber> {
  192. if let Some(stage) = Self::stage() {
  193. match stage {
  194. ElectionStage::Announcing(ends) => Some(ends),
  195. ElectionStage::Voting(ends) => Some(ends),
  196. ElectionStage::Revealing(ends) => Some(ends),
  197. }
  198. } else {
  199. None
  200. }
  201. }
  202. // PRIVATE MUTABLES
  203. /// Starts an election. Will fail if an election is already running
  204. /// Initializes transferable stakes. Assumes election parameters have already been set.
  205. fn start_election(current_council: Seats<T::AccountId, BalanceOf<T>>) -> Result {
  206. ensure!(!Self::is_election_running(), "election already in progress");
  207. ensure!(
  208. Self::existing_stake_holders().is_empty(),
  209. "stake holders must be empty"
  210. );
  211. ensure!(Self::applicants().is_empty(), "applicants must be empty");
  212. ensure!(Self::commitments().is_empty(), "commitments must be empty");
  213. // Take snapshot of seat and backing stakes of an existing council
  214. // Its important to note that the election system takes ownership of these stakes, and is responsible
  215. // to return any unused stake to original owners at the end of the election.
  216. Self::initialize_transferable_stakes(current_council);
  217. Self::deposit_event(RawEvent::ElectionStarted());
  218. Self::move_to_announcing_stage();
  219. Ok(())
  220. }
  221. /// Sets announcing stage. Can be called from any stage and assumes all preparatory work
  222. /// for entering the stage has been performed.
  223. /// Bumps the election round.
  224. fn move_to_announcing_stage() {
  225. let next_round = Round::mutate(|n| {
  226. *n += 1;
  227. *n
  228. });
  229. let new_stage_ends_at = Self::current_block_number_plus(Self::announcing_period());
  230. <Stage<T>>::put(ElectionStage::Announcing(new_stage_ends_at));
  231. Self::deposit_event(RawEvent::AnnouncingStarted(next_round));
  232. }
  233. /// Sets announcing stage. Can be called from any stage and assumes all preparatory work
  234. /// for entering the stage has been performed.
  235. fn move_to_voting_stage() {
  236. let new_stage_ends_at = Self::current_block_number_plus(Self::voting_period());
  237. <Stage<T>>::put(ElectionStage::Voting(new_stage_ends_at));
  238. Self::deposit_event(RawEvent::VotingStarted());
  239. }
  240. /// Sets announcing stage. Can be called from any stage and assumes all preparatory work
  241. /// for entering the stage has been performed.
  242. fn move_to_revealing_stage() {
  243. let new_stage_ends_at = Self::current_block_number_plus(Self::revealing_period());
  244. <Stage<T>>::put(ElectionStage::Revealing(new_stage_ends_at));
  245. Self::deposit_event(RawEvent::RevealingStarted());
  246. }
  247. /// Sorts applicants by stake, and returns slice of applicants with least stake. Applicants not
  248. /// returned in the slice are the top `len` highest staked.
  249. fn find_least_staked_applicants(
  250. applicants: &mut Vec<T::AccountId>,
  251. len: usize,
  252. ) -> &[T::AccountId] {
  253. if len >= applicants.len() {
  254. &[]
  255. } else {
  256. #[allow(clippy::redundant_closure)] // disable incorrect Clippy linter warning
  257. applicants.sort_by_key(|applicant| Self::applicant_stakes(applicant));
  258. &applicants[0..applicants.len() - len]
  259. }
  260. }
  261. fn on_announcing_ended() {
  262. let mut applicants = Self::applicants();
  263. if applicants.len() < Self::council_size_usize() {
  264. // Not enough applicants announced candidacy
  265. Self::move_to_announcing_stage();
  266. } else {
  267. // upper limit on applicants that will move to voting stage
  268. let limit = rstd::cmp::max(Self::council_size_usize(), Self::candidacy_limit_usize());
  269. let applicants_to_drop = Self::find_least_staked_applicants(&mut applicants, limit);
  270. Self::drop_applicants(applicants_to_drop);
  271. Self::move_to_voting_stage();
  272. }
  273. }
  274. fn on_voting_ended() {
  275. Self::move_to_revealing_stage();
  276. }
  277. fn on_revealing_ended() {
  278. // tally the revealed votes
  279. let mut votes = Vec::new();
  280. for commitment in Self::commitments().iter() {
  281. votes.push(Self::votes(commitment));
  282. }
  283. let mut new_council = Self::tally_votes(&votes);
  284. // Note here that applicants with zero votes dont appear in the tally.
  285. // Is an applicant with some votes but less total stake than another applicant with zero votes
  286. // more qualified to be on the council?
  287. // Consider implications - if a council can be formed purely by staking are we fine with that?
  288. for applicant in Self::applicants().iter() {
  289. if !new_council.contains_key(applicant) {
  290. new_council.insert(
  291. applicant.clone(),
  292. Seat {
  293. member: applicant.clone(),
  294. stake: Self::applicant_stakes(applicant).total(),
  295. backers: Vec::new(),
  296. },
  297. );
  298. }
  299. }
  300. match new_council.len() {
  301. ncl if ncl == Self::council_size_usize() => {
  302. // all applicants in the tally will form the new council
  303. }
  304. ncl if ncl > Self::council_size_usize() => {
  305. // we have more than enough applicants to form the new council.
  306. // select top staked
  307. Self::filter_top_staked(&mut new_council, Self::council_size_usize());
  308. }
  309. _ => {
  310. // Not enough applicants with votes to form a council.
  311. // This may happen if we didn't add applicants with zero votes to the tally,
  312. // or in future if we allow applicants to withdraw candidacy during voting or revealing stages.
  313. // or council size was increased during voting, revealing stages.
  314. }
  315. }
  316. // unless we want to add more filtering criteria to what is considered a successful election
  317. // other than just the minimum stake for candidacy, we have a new council!
  318. Self::teardown_election(
  319. &votes,
  320. &new_council,
  321. true, /* unlock transferable stakes */
  322. );
  323. let new_council = new_council.into_iter().map(|(_, seat)| seat).collect();
  324. T::CouncilElected::council_elected(new_council, Self::new_term_duration());
  325. Self::deposit_event(RawEvent::CouncilElected(<system::Module<T>>::block_number()));
  326. }
  327. fn teardown_election(
  328. votes: &[SealedVote<T::AccountId, Stake<BalanceOf<T>>, T::Hash, T::AccountId>],
  329. new_council: &BTreeMap<T::AccountId, Seat<T::AccountId, BalanceOf<T>>>,
  330. unlock_ts: bool,
  331. ) {
  332. Self::refund_voting_stakes(&votes, &new_council);
  333. Self::clear_votes();
  334. Self::drop_unelected_applicants(&new_council);
  335. Self::clear_applicants();
  336. if unlock_ts {
  337. Self::unlock_transferable_stakes();
  338. }
  339. Self::clear_transferable_stakes();
  340. <Stage<T>>::kill();
  341. }
  342. fn unlock_transferable_stakes() {
  343. // move stakes back to account holder's free balance
  344. for stakeholder in Self::existing_stake_holders().iter() {
  345. let stake = Self::transferable_stakes(stakeholder);
  346. if !stake.seat.is_zero() || !stake.backing.is_zero() {
  347. <T as GovernanceCurrency>::Currency::unreserve(
  348. stakeholder,
  349. stake.seat + stake.backing,
  350. );
  351. }
  352. }
  353. }
  354. fn clear_transferable_stakes() {
  355. for stakeholder in Self::existing_stake_holders() {
  356. <TransferableStakes<T>>::remove(stakeholder);
  357. }
  358. <ExistingStakeHolders<T>>::kill();
  359. }
  360. fn clear_applicants() {
  361. for applicant in Self::applicants() {
  362. <ApplicantStakes<T>>::remove(applicant);
  363. }
  364. <Applicants<T>>::kill();
  365. }
  366. fn refund_applicant(applicant: &T::AccountId) {
  367. let stake = <ApplicantStakes<T>>::get(applicant);
  368. // return new stake to account's free balance
  369. if !stake.new.is_zero() {
  370. <T as GovernanceCurrency>::Currency::unreserve(applicant, stake.new);
  371. }
  372. // return unused transferable stake
  373. if !stake.transferred.is_zero() {
  374. <TransferableStakes<T>>::mutate(applicant, |transferable| {
  375. (*transferable).seat += stake.transferred
  376. });
  377. }
  378. }
  379. fn drop_applicants(drop: &[T::AccountId]) {
  380. let not_dropped: Vec<T::AccountId> = Self::applicants()
  381. .into_iter()
  382. .filter(|id| !drop.iter().any(|x| *x == *id))
  383. .collect();
  384. for applicant in drop {
  385. Self::refund_applicant(applicant);
  386. <ApplicantStakes<T>>::remove(applicant);
  387. }
  388. <Applicants<T>>::put(not_dropped);
  389. }
  390. fn drop_unelected_applicants(
  391. new_council: &BTreeMap<T::AccountId, Seat<T::AccountId, BalanceOf<T>>>,
  392. ) {
  393. let applicants_to_drop: Vec<T::AccountId> = Self::applicants()
  394. .into_iter()
  395. .filter(|applicant| !new_council.contains_key(&applicant))
  396. .collect();
  397. Self::drop_applicants(&applicants_to_drop[..]);
  398. }
  399. fn refund_voting_stakes(
  400. sealed_votes: &[SealedVote<T::AccountId, Stake<BalanceOf<T>>, T::Hash, T::AccountId>],
  401. new_council: &BTreeMap<T::AccountId, Seat<T::AccountId, BalanceOf<T>>>,
  402. ) {
  403. for sealed_vote in sealed_votes.iter() {
  404. // Do a refund if commitment was not revealed, or the vote was for applicant that did
  405. // not get elected to the council
  406. // TODO critical: shouldn't we slash the stake in such a case? This is the whole idea behid staking on something: people need to decide carefully and be responsible for their bahavior because they can loose their stake
  407. // See https://github.com/Joystream/substrate-node-joystream/issues/4
  408. let do_refund = match sealed_vote.get_vote() {
  409. Some(applicant) => !new_council.contains_key(&applicant),
  410. None => true,
  411. };
  412. if do_refund {
  413. // return new stake to account's free balance
  414. let SealedVote { voter, stake, .. } = sealed_vote;
  415. if !stake.new.is_zero() {
  416. <T as GovernanceCurrency>::Currency::unreserve(voter, stake.new);
  417. }
  418. // return unused transferable stake
  419. if !stake.transferred.is_zero() {
  420. <TransferableStakes<T>>::mutate(voter, |transferable| {
  421. (*transferable).backing += stake.transferred
  422. });
  423. }
  424. }
  425. }
  426. }
  427. fn clear_votes() {
  428. for commitment in Self::commitments() {
  429. <Votes<T>>::remove(commitment);
  430. }
  431. <Commitments<T>>::kill();
  432. }
  433. fn tally_votes(
  434. sealed_votes: &[SealedVote<T::AccountId, Stake<BalanceOf<T>>, T::Hash, T::AccountId>],
  435. ) -> BTreeMap<T::AccountId, Seat<T::AccountId, BalanceOf<T>>> {
  436. let mut tally: BTreeMap<T::AccountId, Seat<T::AccountId, BalanceOf<T>>> = BTreeMap::new();
  437. for sealed_vote in sealed_votes.iter() {
  438. if let Some(applicant) = sealed_vote.get_vote() {
  439. if !tally.contains_key(&applicant) {
  440. // Add new seat
  441. tally.insert(
  442. applicant.clone(),
  443. Seat {
  444. member: applicant.clone(),
  445. stake: Self::applicant_stakes(applicant).total(),
  446. backers: vec![],
  447. },
  448. );
  449. }
  450. if let Some(seat) = tally.get_mut(&applicant) {
  451. // Add backer to existing seat
  452. seat.backers.push(Backer {
  453. member: sealed_vote.voter.clone(),
  454. stake: sealed_vote.stake.total(),
  455. });
  456. }
  457. }
  458. }
  459. tally
  460. }
  461. fn filter_top_staked(
  462. tally: &mut BTreeMap<T::AccountId, Seat<T::AccountId, BalanceOf<T>>>,
  463. limit: usize,
  464. ) {
  465. if limit >= tally.len() {
  466. return;
  467. }
  468. // use ordering in the applicants vector (not ordering resulting from btreemap iteration)
  469. let mut seats: Vec<T::AccountId> = Self::applicants()
  470. .into_iter()
  471. .filter(|id| tally.contains_key(id))
  472. .collect();
  473. // ensure_eq!(seats.len(), tally.len());
  474. if limit >= seats.len() {
  475. // Tally is inconsistent with list of applicants!
  476. return;
  477. }
  478. // TODO: order by number of votes, then number of backers
  479. seats.sort_by_key(|applicant| {
  480. tally
  481. .get(&applicant)
  482. .map_or(Zero::zero(), |seat| seat.calc_total_stake())
  483. });
  484. // seats at bottom of list
  485. let filtered_out_seats = &seats[0..seats.len() - limit];
  486. for id in filtered_out_seats {
  487. tally.remove(id);
  488. }
  489. }
  490. /// Checks if the current election stage has ended and calls the stage ended handler
  491. fn check_if_stage_is_ending(now: T::BlockNumber) {
  492. if let Some(stage) = Self::stage() {
  493. match stage {
  494. ElectionStage::Announcing(ends) => {
  495. if ends == now {
  496. Self::deposit_event(RawEvent::AnnouncingEnded());
  497. Self::on_announcing_ended();
  498. }
  499. }
  500. ElectionStage::Voting(ends) => {
  501. if ends == now {
  502. Self::deposit_event(RawEvent::VotingEnded());
  503. Self::on_voting_ended();
  504. }
  505. }
  506. ElectionStage::Revealing(ends) => {
  507. if ends == now {
  508. Self::deposit_event(RawEvent::RevealingEnded());
  509. Self::on_revealing_ended();
  510. }
  511. }
  512. }
  513. }
  514. }
  515. /// Takes a snapshot of the stakes from the current council
  516. fn initialize_transferable_stakes(current_council: Seats<T::AccountId, BalanceOf<T>>) {
  517. let mut stakeholder_accounts: Vec<T::AccountId> = Vec::new();
  518. for seat in current_council.into_iter() {
  519. let Seat { member, stake, .. } = seat;
  520. if <TransferableStakes<T>>::exists(&member) {
  521. <TransferableStakes<T>>::mutate(&member, |transferbale_stake| {
  522. *transferbale_stake = TransferableStake {
  523. seat: transferbale_stake.seat + stake,
  524. backing: transferbale_stake.backing,
  525. }
  526. });
  527. } else {
  528. <TransferableStakes<T>>::insert(
  529. &member,
  530. TransferableStake {
  531. seat: stake,
  532. backing: BalanceOf::<T>::zero(),
  533. },
  534. );
  535. stakeholder_accounts.push(member);
  536. }
  537. for backer in seat.backers.into_iter() {
  538. let Backer { member, stake, .. } = backer;
  539. if <TransferableStakes<T>>::exists(&member) {
  540. <TransferableStakes<T>>::mutate(&member, |transferbale_stake| {
  541. *transferbale_stake = TransferableStake {
  542. seat: transferbale_stake.seat,
  543. backing: transferbale_stake.backing + stake,
  544. }
  545. });
  546. } else {
  547. <TransferableStakes<T>>::insert(
  548. &member,
  549. TransferableStake {
  550. seat: BalanceOf::<T>::zero(),
  551. backing: stake,
  552. },
  553. );
  554. stakeholder_accounts.push(member);
  555. }
  556. }
  557. }
  558. <ExistingStakeHolders<T>>::put(stakeholder_accounts);
  559. }
  560. fn new_stake_reusing_transferable(
  561. transferable: &mut BalanceOf<T>,
  562. new_stake: BalanceOf<T>,
  563. ) -> Stake<BalanceOf<T>> {
  564. let transferred = if *transferable >= new_stake {
  565. new_stake
  566. } else {
  567. *transferable
  568. };
  569. *transferable -= transferred;
  570. Stake {
  571. new: new_stake - transferred,
  572. transferred,
  573. }
  574. }
  575. fn try_add_applicant(applicant: T::AccountId, stake: BalanceOf<T>) -> Result {
  576. let mut transferable_stake = <TransferableStakes<T>>::get(&applicant);
  577. let new_stake = Self::new_stake_reusing_transferable(&mut transferable_stake.seat, stake);
  578. ensure!(
  579. <T as GovernanceCurrency>::Currency::can_reserve(&applicant, new_stake.new),
  580. "not enough free balance to reserve"
  581. );
  582. ensure!(
  583. <T as GovernanceCurrency>::Currency::reserve(&applicant, new_stake.new).is_ok(),
  584. "failed to reserve applicant stake!"
  585. );
  586. let applicant_stake = <ApplicantStakes<T>>::get(&applicant);
  587. let total_stake = applicant_stake.add(&new_stake);
  588. if <TransferableStakes<T>>::exists(&applicant) {
  589. <TransferableStakes<T>>::insert(&applicant, transferable_stake);
  590. }
  591. if !<ApplicantStakes<T>>::exists(&applicant) {
  592. // insert element at the begining, this gives priority to early applicants
  593. // when ordering applicants by stake if stakes are equal
  594. <Applicants<T>>::mutate(|applicants| applicants.insert(0, applicant.clone()));
  595. }
  596. <ApplicantStakes<T>>::insert(applicant, total_stake);
  597. Ok(())
  598. }
  599. fn try_add_vote(voter: T::AccountId, stake: BalanceOf<T>, commitment: T::Hash) -> Result {
  600. ensure!(!<Votes<T>>::exists(commitment), "duplicate commitment");
  601. let mut transferable_stake = <TransferableStakes<T>>::get(&voter);
  602. let vote_stake =
  603. Self::new_stake_reusing_transferable(&mut transferable_stake.backing, stake);
  604. ensure!(
  605. <T as GovernanceCurrency>::Currency::can_reserve(&voter, vote_stake.new),
  606. "not enough free balance to reserve"
  607. );
  608. ensure!(
  609. <T as GovernanceCurrency>::Currency::reserve(&voter, vote_stake.new).is_ok(),
  610. "failed to reserve voting stake!"
  611. );
  612. <Commitments<T>>::mutate(|commitments| commitments.push(commitment));
  613. <Votes<T>>::insert(
  614. commitment,
  615. SealedVote::new(voter.clone(), vote_stake, commitment),
  616. );
  617. if <TransferableStakes<T>>::exists(&voter) {
  618. <TransferableStakes<T>>::insert(&voter, transferable_stake);
  619. }
  620. Ok(())
  621. }
  622. fn try_reveal_vote(
  623. voter: T::AccountId,
  624. commitment: T::Hash,
  625. vote_for: T::AccountId,
  626. salt: Vec<u8>,
  627. ) -> Result {
  628. ensure!(<Votes<T>>::exists(&commitment), "commitment not found");
  629. let mut sealed_vote = <Votes<T>>::get(&commitment);
  630. ensure!(sealed_vote.is_not_revealed(), "vote already revealed");
  631. // only voter can reveal their own votes
  632. ensure!(sealed_vote.is_owned_by(voter), "only voter can reveal vote");
  633. ensure!(
  634. <ApplicantStakes<T>>::exists(&vote_for),
  635. "vote for non-applicant not allowed"
  636. );
  637. let mut salt = salt;
  638. // Tries to unseal, if salt is invalid will return error
  639. sealed_vote.unseal(vote_for, &mut salt, <T as system::Trait>::Hashing::hash)?;
  640. // Update the revealed vote
  641. <Votes<T>>::insert(commitment, sealed_vote);
  642. Ok(())
  643. }
  644. fn set_verified_election_parameters(params: ElectionParameters<BalanceOf<T>, T::BlockNumber>) {
  645. <AnnouncingPeriod<T>>::put(params.announcing_period);
  646. <VotingPeriod<T>>::put(params.voting_period);
  647. <RevealingPeriod<T>>::put(params.revealing_period);
  648. <MinCouncilStake<T>>::put(params.min_council_stake);
  649. <NewTermDuration<T>>::put(params.new_term_duration);
  650. CouncilSize::put(params.council_size);
  651. CandidacyLimit::put(params.candidacy_limit);
  652. <MinVotingStake<T>>::put(params.min_voting_stake);
  653. }
  654. }
  655. decl_module! {
  656. pub struct Module<T: Trait> for enum Call where origin: T::Origin {
  657. fn deposit_event() = default;
  658. // No origin so this is a priviledged call
  659. fn on_finalize(now: T::BlockNumber) {
  660. Self::check_if_stage_is_ending(now);
  661. }
  662. // Member can apply during announcing stage only. On first call a minimum stake will need to be provided.
  663. // Member can make subsequent calls during announcing stage to increase their stake.
  664. fn apply(origin, stake: BalanceOf<T>) {
  665. let sender = ensure_signed(origin)?;
  666. ensure!(Self::can_participate(&sender), "Only members can apply to be on council");
  667. let stage = Self::stage();
  668. ensure!(Self::stage().is_some(), "election not running");
  669. let is_announcing = match stage.unwrap() {
  670. ElectionStage::Announcing(_) => true,
  671. _ => false
  672. };
  673. ensure!(is_announcing, "election not in announcing stage");
  674. // minimum stake on first attempt to apply
  675. if !<ApplicantStakes<T>>::exists(&sender) {
  676. ensure!(stake >= Self::min_council_stake(), "minimum stake must be provided");
  677. }
  678. Self::try_add_applicant(sender.clone(), stake)?;
  679. Self::deposit_event(RawEvent::Applied(sender));
  680. }
  681. fn vote(origin, commitment: T::Hash, stake: BalanceOf<T>) {
  682. let sender = ensure_signed(origin)?;
  683. ensure!(Self::can_participate(&sender), "Only members can vote for an applicant");
  684. let stage = Self::stage();
  685. ensure!(Self::stage().is_some(), "election not running");
  686. let is_voting = match stage.unwrap() {
  687. ElectionStage::Voting(_) => true,
  688. _ => false
  689. };
  690. ensure!(is_voting, "election not in voting stage");
  691. ensure!(stake >= Self::min_voting_stake(), "voting stake too low");
  692. Self::try_add_vote(sender.clone(), stake, commitment)?;
  693. Self::deposit_event(RawEvent::Voted(sender, commitment));
  694. }
  695. fn reveal(origin, commitment: T::Hash, vote: T::AccountId, salt: Vec<u8>) {
  696. let sender = ensure_signed(origin)?;
  697. ensure!(salt.len() <= 32, "salt too large"); // at most 256 bits salt
  698. let stage = Self::stage();
  699. ensure!(Self::stage().is_some(), "election not running");
  700. let is_revealing = match stage.unwrap() {
  701. ElectionStage::Revealing(_) => true,
  702. _ => false
  703. };
  704. ensure!(is_revealing, "election not in revealing stage");
  705. Self::try_reveal_vote(sender.clone(), commitment, vote.clone(), salt)?;
  706. Self::deposit_event(RawEvent::Revealed(sender, commitment, vote));
  707. }
  708. fn set_stage_announcing(origin, ends_at: T::BlockNumber) {
  709. ensure_root(origin)?;
  710. ensure!(ends_at > <system::Module<T>>::block_number(), "must end at future block number");
  711. <Stage<T>>::put(ElectionStage::Announcing(ends_at));
  712. }
  713. fn set_stage_revealing(origin, ends_at: T::BlockNumber) {
  714. ensure_root(origin)?;
  715. ensure!(ends_at > <system::Module<T>>::block_number(), "must end at future block number");
  716. <Stage<T>>::put(ElectionStage::Revealing(ends_at));
  717. }
  718. fn set_stage_voting(origin, ends_at: T::BlockNumber) {
  719. ensure_root(origin)?;
  720. ensure!(ends_at > <system::Module<T>>::block_number(), "must end at future block number");
  721. <Stage<T>>::put(ElectionStage::Voting(ends_at));
  722. }
  723. /// Sets new election parameters. Some combination of parameters that are not desirable, so
  724. /// the parameters are checked for validity.
  725. /// The call will fail if an election is in progress. If a council is not being elected for some
  726. /// reaon after multiple rounds, force_stop_election() can be called to stop elections and followed by
  727. /// set_election_parameters().
  728. pub fn set_election_parameters(origin, params: ElectionParameters<BalanceOf<T>, T::BlockNumber>) {
  729. ensure_root(origin)?;
  730. ensure!(!Self::is_election_running(), MSG_CANNOT_CHANGE_PARAMS_DURING_ELECTION);
  731. params.ensure_valid()?;
  732. Self::set_verified_election_parameters(params);
  733. }
  734. fn force_stop_election(origin) {
  735. ensure_root(origin)?;
  736. ensure!(Self::is_election_running(), "only running election can be stopped");
  737. let mut votes = Vec::new();
  738. for commitment in Self::commitments() {
  739. votes.push(Self::votes(commitment));
  740. }
  741. // no council gets elected
  742. let empty_council = BTreeMap::new();
  743. Self::teardown_election (
  744. &votes,
  745. &empty_council,
  746. false /* do not unlock transferable stakes */
  747. );
  748. }
  749. fn force_start_election(origin) {
  750. ensure_root(origin)?;
  751. Self::start_election(<council::Module<T>>::active_council())?;
  752. }
  753. fn set_auto_start (origin, flag: bool) {
  754. ensure_root(origin)?;
  755. AutoStart::put(flag);
  756. }
  757. }
  758. }
  759. impl<T: Trait> council::CouncilTermEnded for Module<T> {
  760. fn council_term_ended() {
  761. if Self::auto_start() {
  762. let _ = Self::start_election(<council::Module<T>>::active_council());
  763. }
  764. }
  765. }
  766. #[cfg(test)]
  767. mod tests {
  768. use super::*;
  769. use crate::mock::*;
  770. use codec::Encode;
  771. use srml_support::*;
  772. #[test]
  773. fn election_starts_when_council_term_ends() {
  774. initial_test_ext().execute_with(|| {
  775. System::set_block_number(1);
  776. assert!(Council::is_term_ended());
  777. assert!(Election::stage().is_none());
  778. <Election as council::CouncilTermEnded>::council_term_ended();
  779. assert!(Election::stage().is_some());
  780. });
  781. }
  782. #[test]
  783. fn new_stake_reusing_transferable_works() {
  784. {
  785. let mut transferable = 0;
  786. let additional = 100;
  787. let new_stake = Election::new_stake_reusing_transferable(&mut transferable, additional);
  788. assert_eq!(new_stake.new, 100);
  789. assert_eq!(new_stake.transferred, 0);
  790. }
  791. {
  792. let mut transferable = 40;
  793. let additional = 60;
  794. let new_stake = Election::new_stake_reusing_transferable(&mut transferable, additional);
  795. assert_eq!(new_stake.new, 20);
  796. assert_eq!(new_stake.transferred, 40);
  797. assert_eq!(transferable, 0);
  798. }
  799. {
  800. let mut transferable = 1000;
  801. let additional = 100;
  802. let new_stake = Election::new_stake_reusing_transferable(&mut transferable, additional);
  803. assert_eq!(new_stake.new, 0);
  804. assert_eq!(new_stake.transferred, 100);
  805. assert_eq!(transferable, 900);
  806. }
  807. }
  808. #[test]
  809. fn check_default_params() {
  810. // TODO missing test implementation?
  811. }
  812. #[test]
  813. fn should_not_start_new_election_if_already_started() {
  814. initial_test_ext().execute_with(|| {
  815. assert_ok!(Election::start_election(vec![]));
  816. assert_err!(
  817. Election::start_election(vec![]),
  818. "election already in progress"
  819. );
  820. });
  821. }
  822. fn assert_announcing_period(expected_period: <Test as system::Trait>::BlockNumber) {
  823. assert!(
  824. Election::is_election_running(),
  825. "Election Stage was not set"
  826. );
  827. let election_stage = Election::stage().unwrap();
  828. match election_stage {
  829. election::ElectionStage::Announcing(period) => {
  830. assert_eq!(period, expected_period, "Election period not set correctly")
  831. }
  832. _ => assert!(false, "Election Stage was not correctly set to Announcing"),
  833. }
  834. }
  835. #[test]
  836. fn start_election_should_work() {
  837. initial_test_ext().execute_with(|| {
  838. System::set_block_number(1);
  839. <AnnouncingPeriod<Test>>::put(20);
  840. let prev_round = Election::round();
  841. assert_ok!(Election::start_election(vec![]));
  842. // election round is bumped
  843. assert_eq!(Election::round(), prev_round + 1);
  844. // we enter the announcing stage for a specified period
  845. assert_announcing_period(1 + Election::announcing_period());
  846. });
  847. }
  848. #[test]
  849. fn init_transferable_stake_should_work() {
  850. initial_test_ext().execute_with(|| {
  851. let existing_council = vec![
  852. Seat {
  853. member: 1,
  854. stake: 100,
  855. backers: vec![
  856. Backer {
  857. member: 2,
  858. stake: 50,
  859. },
  860. Backer {
  861. member: 3,
  862. stake: 40,
  863. },
  864. Backer {
  865. member: 10,
  866. stake: 10,
  867. },
  868. ],
  869. },
  870. Seat {
  871. member: 2,
  872. stake: 200,
  873. backers: vec![
  874. Backer {
  875. member: 1,
  876. stake: 10,
  877. },
  878. Backer {
  879. member: 3,
  880. stake: 60,
  881. },
  882. Backer {
  883. member: 20,
  884. stake: 20,
  885. },
  886. ],
  887. },
  888. Seat {
  889. member: 3,
  890. stake: 300,
  891. backers: vec![
  892. Backer {
  893. member: 1,
  894. stake: 20,
  895. },
  896. Backer {
  897. member: 2,
  898. stake: 40,
  899. },
  900. ],
  901. },
  902. ];
  903. Election::initialize_transferable_stakes(existing_council);
  904. let mut existing_stake_holders = Election::existing_stake_holders();
  905. existing_stake_holders.sort();
  906. assert_eq!(existing_stake_holders, vec![1, 2, 3, 10, 20]);
  907. assert_eq!(Election::transferable_stakes(&1).seat, 100);
  908. assert_eq!(Election::transferable_stakes(&1).backing, 30);
  909. assert_eq!(Election::transferable_stakes(&2).seat, 200);
  910. assert_eq!(Election::transferable_stakes(&2).backing, 90);
  911. assert_eq!(Election::transferable_stakes(&3).seat, 300);
  912. assert_eq!(Election::transferable_stakes(&3).backing, 100);
  913. assert_eq!(Election::transferable_stakes(&10).seat, 0);
  914. assert_eq!(Election::transferable_stakes(&10).backing, 10);
  915. assert_eq!(Election::transferable_stakes(&20).seat, 0);
  916. assert_eq!(Election::transferable_stakes(&20).backing, 20);
  917. });
  918. }
  919. #[test]
  920. fn try_add_applicant_should_work() {
  921. initial_test_ext().execute_with(|| {
  922. assert!(Election::applicants().len() == 0);
  923. let applicant = 20 as u64;
  924. let starting_balance = 1000 as u64;
  925. let _ = Balances::deposit_creating(&applicant, starting_balance);
  926. let stake = 100 as u64;
  927. assert!(Election::try_add_applicant(applicant, stake).is_ok());
  928. assert_eq!(Election::applicants(), vec![applicant]);
  929. assert_eq!(Election::applicant_stakes(applicant).new, stake);
  930. assert_eq!(Election::applicant_stakes(applicant).transferred, 0);
  931. assert_eq!(Balances::free_balance(&applicant), starting_balance - stake);
  932. });
  933. }
  934. #[test]
  935. fn increasing_applicant_stake_should_work() {
  936. initial_test_ext().execute_with(|| {
  937. let applicant = 20 as u64;
  938. let starting_stake = 100 as u64;
  939. <Applicants<Test>>::put(vec![applicant]);
  940. <ApplicantStakes<Test>>::insert(
  941. applicant,
  942. Stake {
  943. new: starting_stake,
  944. transferred: 0,
  945. },
  946. );
  947. let additional_stake = 100 as u64;
  948. let _ = Balances::deposit_creating(&applicant, additional_stake);
  949. assert!(Election::try_add_applicant(applicant, additional_stake).is_ok());
  950. assert_eq!(
  951. Election::applicant_stakes(applicant).new,
  952. starting_stake + additional_stake
  953. );
  954. assert_eq!(Election::applicant_stakes(applicant).transferred, 0)
  955. });
  956. }
  957. #[test]
  958. fn using_transferable_seat_stake_should_work() {
  959. initial_test_ext().execute_with(|| {
  960. let applicant = 20 as u64;
  961. let _ = Balances::deposit_creating(&applicant, 5000);
  962. <ExistingStakeHolders<Test>>::put(vec![applicant]);
  963. save_transferable_stake(
  964. applicant,
  965. TransferableStake {
  966. seat: 1000,
  967. backing: 0,
  968. },
  969. );
  970. <Applicants<Test>>::put(vec![applicant]);
  971. let starting_stake = Stake {
  972. new: 100,
  973. transferred: 0,
  974. };
  975. <ApplicantStakes<Test>>::insert(applicant, starting_stake);
  976. // transferable stake covers new stake
  977. assert!(Election::try_add_applicant(applicant, 600).is_ok());
  978. assert_eq!(
  979. Election::applicant_stakes(applicant).new,
  980. starting_stake.new
  981. );
  982. assert_eq!(Election::applicant_stakes(applicant).transferred, 600);
  983. assert_eq!(Election::transferable_stakes(applicant).seat, 400);
  984. assert_eq!(Balances::free_balance(applicant), 5000);
  985. // all remaining transferable stake is consumed and free balance covers remaining stake
  986. assert!(Election::try_add_applicant(applicant, 1000).is_ok());
  987. assert_eq!(
  988. Election::applicant_stakes(applicant).new,
  989. starting_stake.new + 600
  990. );
  991. assert_eq!(Election::applicant_stakes(applicant).transferred, 1000);
  992. assert_eq!(Election::transferable_stakes(applicant).seat, 0);
  993. assert_eq!(Balances::free_balance(applicant), 4400);
  994. });
  995. }
  996. #[test]
  997. fn moving_to_voting_without_enough_applicants_should_not_work() {
  998. initial_test_ext().execute_with(|| {
  999. System::set_block_number(1);
  1000. <AnnouncingPeriod<Test>>::put(20);
  1001. CouncilSize::put(10);
  1002. Election::move_to_announcing_stage();
  1003. let round = Election::round();
  1004. // add applicants
  1005. <Applicants<Test>>::put(vec![10, 20, 30]);
  1006. let stake = Stake {
  1007. new: 10,
  1008. transferred: 0,
  1009. };
  1010. let applicants = Election::applicants();
  1011. for applicant in applicants.iter() {
  1012. <ApplicantStakes<Test>>::insert(applicant, stake);
  1013. }
  1014. // make sure we are testing the condition that we don't have enough applicants
  1015. assert!(Election::council_size_usize() > applicants.len());
  1016. // try to move to voting stage
  1017. let ann_ends = Election::stage_ends_at().unwrap();
  1018. System::set_block_number(ann_ends);
  1019. Election::on_announcing_ended();
  1020. // A new round should have been started
  1021. assert_eq!(Election::round(), round + 1);
  1022. // A new announcing period started
  1023. assert_announcing_period(ann_ends + Election::announcing_period());
  1024. // applicants list should be unchanged..
  1025. assert_eq!(Election::applicants(), applicants);
  1026. });
  1027. }
  1028. #[test]
  1029. fn top_applicants_move_to_voting_stage() {
  1030. initial_test_ext().execute_with(|| {
  1031. <Applicants<Test>>::put(vec![10, 20, 30, 40]);
  1032. let mut applicants = Election::applicants();
  1033. for (i, applicant) in applicants.iter().enumerate() {
  1034. <ApplicantStakes<Test>>::insert(
  1035. applicant,
  1036. Stake {
  1037. new: (i * 10) as u64,
  1038. transferred: 0,
  1039. },
  1040. );
  1041. }
  1042. let rejected = Election::find_least_staked_applicants(&mut applicants, 3);
  1043. assert_eq!(rejected.to_vec(), vec![10]);
  1044. <Applicants<Test>>::put(vec![40, 30, 20, 10]);
  1045. let mut applicants = Election::applicants();
  1046. for applicant in applicants.iter() {
  1047. <ApplicantStakes<Test>>::insert(
  1048. applicant,
  1049. Stake {
  1050. new: 20,
  1051. transferred: 0,
  1052. },
  1053. );
  1054. }
  1055. // stable sort is preserving order when two elements are equivalent
  1056. let rejected = Election::find_least_staked_applicants(&mut applicants, 3);
  1057. assert_eq!(rejected.to_vec(), vec![40]);
  1058. });
  1059. }
  1060. #[test]
  1061. fn refunding_applicant_stakes_should_work() {
  1062. initial_test_ext().execute_with(|| {
  1063. let _ = Balances::deposit_creating(&1, 1000);
  1064. let _ = Balances::deposit_creating(&2, 7000);
  1065. let _ = Balances::reserve(&2, 5000);
  1066. let _ = Balances::deposit_creating(&3, 8000);
  1067. let _ = Balances::reserve(&3, 5000);
  1068. <Applicants<Test>>::put(vec![1, 2, 3]);
  1069. save_transferable_stake(
  1070. 1,
  1071. TransferableStake {
  1072. seat: 50,
  1073. backing: 0,
  1074. },
  1075. );
  1076. save_transferable_stake(
  1077. 2,
  1078. TransferableStake {
  1079. seat: 0,
  1080. backing: 0,
  1081. },
  1082. );
  1083. save_transferable_stake(
  1084. 3,
  1085. TransferableStake {
  1086. seat: 0,
  1087. backing: 0,
  1088. },
  1089. );
  1090. <ApplicantStakes<Test>>::insert(
  1091. 1,
  1092. Stake {
  1093. new: 100,
  1094. transferred: 200,
  1095. },
  1096. );
  1097. <ApplicantStakes<Test>>::insert(
  1098. 2,
  1099. Stake {
  1100. new: 300,
  1101. transferred: 400,
  1102. },
  1103. );
  1104. <ApplicantStakes<Test>>::insert(
  1105. 3,
  1106. Stake {
  1107. new: 500,
  1108. transferred: 600,
  1109. },
  1110. );
  1111. Election::drop_applicants(&vec![2, 3][..]);
  1112. assert_eq!(Election::applicants(), vec![1]);
  1113. assert_eq!(Election::applicant_stakes(1).new, 100);
  1114. assert_eq!(Election::applicant_stakes(1).transferred, 200);
  1115. assert_eq!(Election::transferable_stakes(1).seat, 50);
  1116. assert_eq!(Balances::free_balance(&1), 1000);
  1117. //assert_eq!(Election::applicant_stakes(2), Default::default());
  1118. assert!(!<ApplicantStakes<Test>>::exists(2));
  1119. assert_eq!(Election::transferable_stakes(2).seat, 400);
  1120. assert_eq!(Balances::free_balance(&2), 2300);
  1121. //assert_eq!(Election::applicant_stakes(3), Default::default());
  1122. assert!(!<ApplicantStakes<Test>>::exists(3));
  1123. assert_eq!(Election::transferable_stakes(3).seat, 600);
  1124. assert_eq!(Balances::free_balance(&3), 3500);
  1125. });
  1126. }
  1127. #[test]
  1128. fn voting_should_work() {
  1129. initial_test_ext().execute_with(|| {
  1130. let _ = Balances::deposit_creating(&20, 1000);
  1131. let payload = vec![10u8];
  1132. let commitment = <Test as system::Trait>::Hashing::hash(&payload[..]);
  1133. assert!(Election::try_add_vote(20, 100, commitment).is_ok());
  1134. assert_eq!(Election::commitments(), vec![commitment]);
  1135. assert_eq!(Election::votes(commitment).voter, 20);
  1136. assert_eq!(Election::votes(commitment).commitment, commitment);
  1137. assert_eq!(
  1138. Election::votes(commitment).stake,
  1139. Stake {
  1140. new: 100,
  1141. transferred: 0,
  1142. }
  1143. );
  1144. assert_eq!(Balances::free_balance(&20), 900);
  1145. });
  1146. }
  1147. fn save_transferable_stake(id: u64, stake: TransferableStake<u64>) {
  1148. <TransferableStakes<Test>>::insert(id, stake);
  1149. }
  1150. #[test]
  1151. fn votes_can_be_covered_by_transferable_stake() {
  1152. initial_test_ext().execute_with(|| {
  1153. let _ = Balances::deposit_creating(&20, 1000);
  1154. save_transferable_stake(
  1155. 20,
  1156. TransferableStake {
  1157. seat: 0,
  1158. backing: 500,
  1159. },
  1160. );
  1161. let payload = vec![10u8];
  1162. let commitment = <Test as system::Trait>::Hashing::hash(&payload[..]);
  1163. assert!(Election::try_add_vote(20, 100, commitment).is_ok());
  1164. assert_eq!(Election::commitments(), vec![commitment]);
  1165. assert_eq!(Election::votes(commitment).voter, 20);
  1166. assert_eq!(Election::votes(commitment).commitment, commitment);
  1167. assert_eq!(
  1168. Election::votes(commitment).stake,
  1169. Stake {
  1170. new: 0,
  1171. transferred: 100,
  1172. }
  1173. );
  1174. assert_eq!(Balances::free_balance(&20), 1000);
  1175. });
  1176. }
  1177. #[test]
  1178. fn voting_without_enough_balance_should_not_work() {
  1179. initial_test_ext().execute_with(|| {
  1180. let _ = Balances::deposit_creating(&20, 100);
  1181. save_transferable_stake(
  1182. 20,
  1183. TransferableStake {
  1184. seat: 0,
  1185. backing: 500,
  1186. },
  1187. );
  1188. let payload = vec![10u8];
  1189. let commitment = <Test as system::Trait>::Hashing::hash(&payload[..]);
  1190. assert!(Election::try_add_vote(20, 1000, commitment).is_err());
  1191. assert_eq!(Election::commitments(), vec![]);
  1192. assert!(!<Votes<Test>>::exists(commitment));
  1193. assert_eq!(Balances::free_balance(&20), 100);
  1194. });
  1195. }
  1196. #[test]
  1197. fn voting_with_existing_commitment_should_not_work() {
  1198. initial_test_ext().execute_with(|| {
  1199. let _ = Balances::deposit_creating(&20, 1000);
  1200. save_transferable_stake(
  1201. 20,
  1202. TransferableStake {
  1203. seat: 0,
  1204. backing: 500,
  1205. },
  1206. );
  1207. let payload = vec![10u8];
  1208. let commitment = <Test as system::Trait>::Hashing::hash(&payload[..]);
  1209. assert!(Election::try_add_vote(20, 100, commitment).is_ok());
  1210. assert_eq!(Election::commitments(), vec![commitment]);
  1211. assert_eq!(Election::votes(commitment).voter, 20);
  1212. assert_eq!(Election::votes(commitment).commitment, commitment);
  1213. assert_eq!(
  1214. Election::votes(commitment).stake,
  1215. Stake {
  1216. new: 0,
  1217. transferred: 100,
  1218. }
  1219. );
  1220. assert_eq!(Balances::free_balance(&20), 1000);
  1221. assert!(Election::try_add_vote(30, 100, commitment).is_err());
  1222. });
  1223. }
  1224. fn make_commitment_for_applicant(
  1225. applicant: <Test as system::Trait>::AccountId,
  1226. salt: &mut Vec<u8>,
  1227. ) -> <Test as system::Trait>::Hash {
  1228. let mut payload = applicant.encode();
  1229. payload.append(salt);
  1230. <Test as system::Trait>::Hashing::hash(&payload[..])
  1231. }
  1232. #[test]
  1233. fn revealing_vote_works() {
  1234. initial_test_ext().execute_with(|| {
  1235. let applicant = 20 as u64;
  1236. let salt = vec![128u8];
  1237. let commitment = make_commitment_for_applicant(applicant, &mut salt.clone());
  1238. let voter = 10 as u64;
  1239. <ApplicantStakes<Test>>::insert(
  1240. &applicant,
  1241. Stake {
  1242. new: 0,
  1243. transferred: 0,
  1244. },
  1245. );
  1246. <Votes<Test>>::insert(
  1247. &commitment,
  1248. SealedVote::new(
  1249. voter,
  1250. Stake {
  1251. new: 100,
  1252. transferred: 0,
  1253. },
  1254. commitment,
  1255. ),
  1256. );
  1257. assert!(<Votes<Test>>::get(commitment).is_not_revealed());
  1258. assert!(Election::try_reveal_vote(voter, commitment, applicant, salt).is_ok());
  1259. assert_eq!(
  1260. <Votes<Test>>::get(commitment).get_vote().unwrap(),
  1261. applicant
  1262. );
  1263. });
  1264. }
  1265. #[test]
  1266. fn revealing_with_bad_salt_should_not_work() {
  1267. initial_test_ext().execute_with(|| {
  1268. let applicant = 20 as u64;
  1269. let salt = vec![128u8];
  1270. let commitment = make_commitment_for_applicant(applicant, &mut salt.clone());
  1271. let voter = 10 as u64;
  1272. <ApplicantStakes<Test>>::insert(
  1273. &applicant,
  1274. Stake {
  1275. new: 0,
  1276. transferred: 0,
  1277. },
  1278. );
  1279. <Votes<Test>>::insert(
  1280. &commitment,
  1281. SealedVote::new(
  1282. voter,
  1283. Stake {
  1284. new: 100,
  1285. transferred: 0,
  1286. },
  1287. commitment,
  1288. ),
  1289. );
  1290. assert!(<Votes<Test>>::get(commitment).is_not_revealed());
  1291. assert!(Election::try_reveal_vote(voter, commitment, applicant, vec![]).is_err());
  1292. assert!(<Votes<Test>>::get(commitment).is_not_revealed());
  1293. });
  1294. }
  1295. #[test]
  1296. fn revealing_non_matching_commitment_should_not_work() {
  1297. initial_test_ext().execute_with(|| {
  1298. let applicant = 20 as u64;
  1299. let salt = vec![128u8];
  1300. let commitment = make_commitment_for_applicant(100, &mut salt.clone());
  1301. let voter = 10 as u64;
  1302. <ApplicantStakes<Test>>::insert(
  1303. &applicant,
  1304. Stake {
  1305. new: 0,
  1306. transferred: 0,
  1307. },
  1308. );
  1309. assert!(Election::try_reveal_vote(voter, commitment, applicant, vec![]).is_err());
  1310. });
  1311. }
  1312. #[test]
  1313. fn revealing_for_non_applicant_should_not_work() {
  1314. initial_test_ext().execute_with(|| {
  1315. let applicant = 20 as u64;
  1316. let salt = vec![128u8];
  1317. let commitment = make_commitment_for_applicant(applicant, &mut salt.clone());
  1318. let voter = 10 as u64;
  1319. <Votes<Test>>::insert(
  1320. &commitment,
  1321. SealedVote::new(
  1322. voter,
  1323. Stake {
  1324. new: 100,
  1325. transferred: 0,
  1326. },
  1327. commitment,
  1328. ),
  1329. );
  1330. assert!(<Votes<Test>>::get(commitment).is_not_revealed());
  1331. assert!(Election::try_reveal_vote(voter, commitment, applicant, vec![]).is_err());
  1332. assert!(<Votes<Test>>::get(commitment).is_not_revealed());
  1333. });
  1334. }
  1335. #[test]
  1336. fn revealing_by_non_committer_should_not_work() {
  1337. initial_test_ext().execute_with(|| {
  1338. let applicant = 20 as u64;
  1339. let salt = vec![128u8];
  1340. let commitment = make_commitment_for_applicant(applicant, &mut salt.clone());
  1341. let voter = 10 as u64;
  1342. let not_voter = 100 as u64;
  1343. <ApplicantStakes<Test>>::insert(
  1344. &applicant,
  1345. Stake {
  1346. new: 0,
  1347. transferred: 0,
  1348. },
  1349. );
  1350. <Votes<Test>>::insert(
  1351. &commitment,
  1352. SealedVote::new(
  1353. voter,
  1354. Stake {
  1355. new: 100,
  1356. transferred: 0,
  1357. },
  1358. commitment,
  1359. ),
  1360. );
  1361. assert!(<Votes<Test>>::get(commitment).is_not_revealed());
  1362. assert!(Election::try_reveal_vote(not_voter, commitment, applicant, salt).is_err());
  1363. assert!(<Votes<Test>>::get(commitment).is_not_revealed());
  1364. });
  1365. }
  1366. pub fn mock_votes(
  1367. mock: Vec<(u64, u64, u64, u64)>,
  1368. ) -> Vec<SealedVote<u64, Stake<u64>, primitives::H256, u64>> {
  1369. let commitment = make_commitment_for_applicant(1, &mut vec![0u8]);
  1370. mock.into_iter()
  1371. .map(|(voter, stake_ref, stake_tran, applicant)| {
  1372. SealedVote::new_unsealed(
  1373. voter as u64,
  1374. Stake {
  1375. new: stake_ref,
  1376. transferred: stake_tran,
  1377. },
  1378. commitment,
  1379. applicant as u64,
  1380. )
  1381. })
  1382. .collect()
  1383. }
  1384. #[test]
  1385. fn vote_tallying_should_work() {
  1386. initial_test_ext().execute_with(|| {
  1387. let votes = mock_votes(vec![
  1388. // (voter, stake[new], stake[transferred], applicant)
  1389. (10, 100, 0, 100),
  1390. (10, 150, 0, 100),
  1391. (10, 500, 0, 200),
  1392. (20, 200, 0, 200),
  1393. (30, 300, 0, 300),
  1394. (30, 400, 0, 300),
  1395. ]);
  1396. let tally = Election::tally_votes(&votes);
  1397. assert_eq!(tally.len(), 3);
  1398. assert_eq!(tally.get(&100).unwrap().member, 100);
  1399. assert_eq!(
  1400. tally.get(&100).unwrap().backers,
  1401. vec![
  1402. Backer {
  1403. member: 10 as u64,
  1404. stake: 100 as u64,
  1405. },
  1406. Backer {
  1407. member: 10 as u64,
  1408. stake: 150 as u64,
  1409. },
  1410. ]
  1411. );
  1412. assert_eq!(tally.get(&200).unwrap().member, 200);
  1413. assert_eq!(
  1414. tally.get(&200).unwrap().backers,
  1415. vec![
  1416. Backer {
  1417. member: 10 as u64,
  1418. stake: 500 as u64,
  1419. },
  1420. Backer {
  1421. member: 20 as u64,
  1422. stake: 200 as u64,
  1423. }
  1424. ]
  1425. );
  1426. assert_eq!(tally.get(&300).unwrap().member, 300);
  1427. assert_eq!(
  1428. tally.get(&300).unwrap().backers,
  1429. vec![
  1430. Backer {
  1431. member: 30 as u64,
  1432. stake: 300 as u64,
  1433. },
  1434. Backer {
  1435. member: 30 as u64,
  1436. stake: 400 as u64,
  1437. }
  1438. ]
  1439. );
  1440. });
  1441. }
  1442. #[test]
  1443. fn filter_top_staked_applicants_should_work() {
  1444. initial_test_ext().execute_with(|| {
  1445. // filter_top_staked depends on order of applicants
  1446. <Applicants<Test>>::put(vec![100, 200, 300]);
  1447. {
  1448. let votes = mock_votes(vec![
  1449. // (voter, stake[new], stake[transferred], applicant)
  1450. (10, 100, 0, 100),
  1451. (10, 150, 0, 100),
  1452. (10, 500, 0, 200),
  1453. (20, 200, 0, 200),
  1454. (30, 300, 0, 300),
  1455. (30, 400, 0, 300),
  1456. ]);
  1457. let mut tally = Election::tally_votes(&votes);
  1458. assert_eq!(tally.len(), 3);
  1459. Election::filter_top_staked(&mut tally, 3);
  1460. assert_eq!(tally.len(), 3);
  1461. }
  1462. {
  1463. let votes = mock_votes(vec![
  1464. // (voter, stake[new], stake[transferred], applicant)
  1465. (10, 100, 0, 100),
  1466. (10, 150, 0, 100),
  1467. (10, 500, 0, 200),
  1468. (20, 200, 0, 200),
  1469. (30, 300, 0, 300),
  1470. (30, 400, 0, 300),
  1471. ]);
  1472. let mut tally = Election::tally_votes(&votes);
  1473. assert_eq!(tally.len(), 3);
  1474. Election::filter_top_staked(&mut tally, 2);
  1475. assert_eq!(tally.len(), 2);
  1476. assert!(tally.get(&200).is_some());
  1477. assert!(tally.get(&300).is_some());
  1478. }
  1479. });
  1480. }
  1481. #[test]
  1482. fn drop_unelected_applicants_should_work() {
  1483. initial_test_ext().execute_with(|| {
  1484. <Applicants<Test>>::put(vec![100, 200, 300]);
  1485. let _ = Balances::deposit_creating(&100, 2000);
  1486. let _ = Balances::reserve(&100, 1000);
  1487. <ApplicantStakes<Test>>::insert(
  1488. 100,
  1489. Stake {
  1490. new: 20 as u64,
  1491. transferred: 50 as u64,
  1492. },
  1493. );
  1494. save_transferable_stake(
  1495. 100,
  1496. TransferableStake {
  1497. seat: 100,
  1498. backing: 0,
  1499. },
  1500. );
  1501. let mut new_council: BTreeMap<u64, Seat<u64, u64>> = BTreeMap::new();
  1502. new_council.insert(
  1503. 200 as u64,
  1504. Seat {
  1505. member: 200 as u64,
  1506. stake: 0 as u64,
  1507. backers: vec![],
  1508. },
  1509. );
  1510. new_council.insert(
  1511. 300 as u64,
  1512. Seat {
  1513. member: 300 as u64,
  1514. stake: 0 as u64,
  1515. backers: vec![],
  1516. },
  1517. );
  1518. Election::drop_unelected_applicants(&new_council);
  1519. // applicant dropped
  1520. assert_eq!(Election::applicants(), vec![200, 300]);
  1521. assert!(!<ApplicantStakes<Test>>::exists(100));
  1522. // and refunded
  1523. assert_eq!(Election::transferable_stakes(100).seat, 150);
  1524. assert_eq!(Balances::free_balance(&100), 1020);
  1525. assert_eq!(Balances::reserved_balance(&100), 980);
  1526. });
  1527. }
  1528. #[test]
  1529. fn refunding_voting_stakes_should_work() {
  1530. initial_test_ext().execute_with(|| {
  1531. // voters' balances
  1532. let _ = Balances::deposit_creating(&10, 6000);
  1533. let _ = Balances::reserve(&10, 5000);
  1534. let _ = Balances::deposit_creating(&20, 7000);
  1535. let _ = Balances::reserve(&20, 5000);
  1536. let _ = Balances::deposit_creating(&30, 8000);
  1537. let _ = Balances::reserve(&30, 5000);
  1538. save_transferable_stake(
  1539. 10,
  1540. TransferableStake {
  1541. seat: 0,
  1542. backing: 100,
  1543. },
  1544. );
  1545. save_transferable_stake(
  1546. 20,
  1547. TransferableStake {
  1548. seat: 0,
  1549. backing: 200,
  1550. },
  1551. );
  1552. save_transferable_stake(
  1553. 30,
  1554. TransferableStake {
  1555. seat: 0,
  1556. backing: 300,
  1557. },
  1558. );
  1559. let votes = mock_votes(vec![
  1560. // (voter, stake[new], stake[transferred], applicant)
  1561. (10, 100, 20, 100),
  1562. (20, 200, 40, 100),
  1563. (30, 300, 60, 100),
  1564. (10, 500, 70, 200),
  1565. (20, 600, 80, 200),
  1566. (30, 700, 90, 200),
  1567. (10, 800, 100, 300),
  1568. (20, 900, 120, 300),
  1569. (30, 1000, 140, 300),
  1570. ]);
  1571. let mut new_council: BTreeMap<u64, Seat<u64, u64>> = BTreeMap::new();
  1572. new_council.insert(
  1573. 200 as u64,
  1574. Seat {
  1575. member: 200 as u64,
  1576. stake: 0 as u64,
  1577. backers: vec![],
  1578. },
  1579. );
  1580. new_council.insert(
  1581. 300 as u64,
  1582. Seat {
  1583. member: 300 as u64,
  1584. stake: 0 as u64,
  1585. backers: vec![],
  1586. },
  1587. );
  1588. Election::refund_voting_stakes(&votes, &new_council);
  1589. assert_eq!(Balances::free_balance(&10), 1100);
  1590. assert_eq!(Balances::reserved_balance(&10), 4900);
  1591. assert_eq!(Balances::free_balance(&20), 2200);
  1592. assert_eq!(Balances::reserved_balance(&20), 4800);
  1593. assert_eq!(Balances::free_balance(&30), 3300);
  1594. assert_eq!(Balances::reserved_balance(&30), 4700);
  1595. assert_eq!(Election::transferable_stakes(10).backing, 120);
  1596. assert_eq!(Election::transferable_stakes(20).backing, 240);
  1597. assert_eq!(Election::transferable_stakes(30).backing, 360);
  1598. });
  1599. }
  1600. #[test]
  1601. fn unlock_transferable_stakes_should_work() {
  1602. initial_test_ext().execute_with(|| {
  1603. <ExistingStakeHolders<Test>>::put(vec![10, 20, 30]);
  1604. let _ = Balances::deposit_creating(&10, 6000);
  1605. let _ = Balances::reserve(&10, 5000);
  1606. save_transferable_stake(
  1607. 10,
  1608. TransferableStake {
  1609. seat: 50,
  1610. backing: 100,
  1611. },
  1612. );
  1613. let _ = Balances::deposit_creating(&20, 7000);
  1614. let _ = Balances::reserve(&20, 5000);
  1615. save_transferable_stake(
  1616. 20,
  1617. TransferableStake {
  1618. seat: 60,
  1619. backing: 200,
  1620. },
  1621. );
  1622. let _ = Balances::deposit_creating(&30, 8000);
  1623. let _ = Balances::reserve(&30, 5000);
  1624. save_transferable_stake(
  1625. 30,
  1626. TransferableStake {
  1627. seat: 70,
  1628. backing: 300,
  1629. },
  1630. );
  1631. Election::unlock_transferable_stakes();
  1632. assert_eq!(Balances::free_balance(&10), 1150);
  1633. assert_eq!(Balances::free_balance(&20), 2260);
  1634. assert_eq!(Balances::free_balance(&30), 3370);
  1635. });
  1636. }
  1637. #[test]
  1638. fn council_elected_hook_should_work() {
  1639. initial_test_ext().execute_with(|| {
  1640. let mut new_council: BTreeMap<u64, Seat<u64, u64>> = BTreeMap::new();
  1641. new_council.insert(
  1642. 200 as u64,
  1643. Seat {
  1644. member: 200 as u64,
  1645. stake: 10 as u64,
  1646. backers: vec![],
  1647. },
  1648. );
  1649. new_council.insert(
  1650. 300 as u64,
  1651. Seat {
  1652. member: 300 as u64,
  1653. stake: 20 as u64,
  1654. backers: vec![],
  1655. },
  1656. );
  1657. assert_eq!(Council::active_council().len(), 0);
  1658. let new_council = new_council
  1659. .into_iter()
  1660. .map(|(_, seat)| seat.clone())
  1661. .collect();
  1662. <Test as election::Trait>::CouncilElected::council_elected(new_council, 10);
  1663. assert_eq!(Council::active_council().len(), 2);
  1664. });
  1665. }
  1666. #[test]
  1667. fn simulation() {
  1668. initial_test_ext().execute_with(|| {
  1669. assert_eq!(Council::active_council().len(), 0);
  1670. assert!(Election::stage().is_none());
  1671. CouncilSize::put(10);
  1672. <MinCouncilStake<Test>>::put(50);
  1673. <AnnouncingPeriod<Test>>::put(10);
  1674. <VotingPeriod<Test>>::put(10);
  1675. <RevealingPeriod<Test>>::put(10);
  1676. CandidacyLimit::put(20);
  1677. <NewTermDuration<Test>>::put(100);
  1678. <MinVotingStake<Test>>::put(10);
  1679. for i in 1..30 {
  1680. let _ = Balances::deposit_creating(&(i as u64), 50000);
  1681. }
  1682. System::set_block_number(1);
  1683. assert_ok!(Election::start_election(vec![]));
  1684. for i in 1..20 {
  1685. if i < 21 {
  1686. assert!(Election::apply(Origin::signed(i), 150).is_ok());
  1687. } else {
  1688. assert!(Election::apply(Origin::signed(i + 1000), 150).is_err()); // not enough free balance
  1689. assert!(Election::apply(Origin::signed(i), 20).is_err()); // not enough minimum stake
  1690. }
  1691. }
  1692. let n = 1 + Election::announcing_period();
  1693. System::set_block_number(n);
  1694. let _ = Election::on_finalize(n);
  1695. for i in 1..20 {
  1696. assert!(Election::vote(
  1697. Origin::signed(i),
  1698. make_commitment_for_applicant(i, &mut vec![40u8]),
  1699. 100
  1700. )
  1701. .is_ok());
  1702. assert!(Election::vote(
  1703. Origin::signed(i),
  1704. make_commitment_for_applicant(i, &mut vec![41u8]),
  1705. 100
  1706. )
  1707. .is_ok());
  1708. assert!(Election::vote(
  1709. Origin::signed(i),
  1710. make_commitment_for_applicant(i + 1000, &mut vec![42u8]),
  1711. 100
  1712. )
  1713. .is_ok());
  1714. }
  1715. let n = n + Election::voting_period();
  1716. System::set_block_number(n);
  1717. let _ = Election::on_finalize(n);
  1718. for i in 1..20 {
  1719. assert!(Election::reveal(
  1720. Origin::signed(i),
  1721. make_commitment_for_applicant(i, &mut vec![40u8]),
  1722. i,
  1723. vec![40u8]
  1724. )
  1725. .is_ok());
  1726. //wrong salt
  1727. assert!(Election::reveal(
  1728. Origin::signed(i),
  1729. make_commitment_for_applicant(i, &mut vec![41u8]),
  1730. i,
  1731. vec![]
  1732. )
  1733. .is_err());
  1734. //vote not for valid applicant
  1735. assert!(Election::reveal(
  1736. Origin::signed(i),
  1737. make_commitment_for_applicant(i + 1000, &mut vec![42u8]),
  1738. i + 1000,
  1739. vec![42u8]
  1740. )
  1741. .is_err());
  1742. }
  1743. let n = n + Election::revealing_period();
  1744. System::set_block_number(n);
  1745. let _ = Election::on_finalize(n);
  1746. assert_eq!(
  1747. Council::active_council().len(),
  1748. Election::council_size_usize()
  1749. );
  1750. for (i, seat) in Council::active_council().iter().enumerate() {
  1751. assert_eq!(seat.member, (i + 1) as u64);
  1752. }
  1753. assert!(Election::stage().is_none());
  1754. // When council term ends.. start a new election.
  1755. assert_ok!(Election::start_election(vec![]));
  1756. });
  1757. }
  1758. #[test]
  1759. fn setting_election_parameters() {
  1760. initial_test_ext().execute_with(|| {
  1761. let default_parameters: ElectionParameters<u64, u64> = ElectionParameters::default();
  1762. // default all zeros is invalid
  1763. assert!(default_parameters.ensure_valid().is_err());
  1764. let new_parameters = ElectionParameters {
  1765. announcing_period: 1,
  1766. voting_period: 2,
  1767. revealing_period: 3,
  1768. council_size: 4,
  1769. candidacy_limit: 5,
  1770. min_voting_stake: 6,
  1771. min_council_stake: 7,
  1772. new_term_duration: 8,
  1773. };
  1774. assert_ok!(Election::set_election_parameters(
  1775. Origin::ROOT,
  1776. new_parameters
  1777. ));
  1778. assert_eq!(
  1779. <AnnouncingPeriod<Test>>::get(),
  1780. new_parameters.announcing_period
  1781. );
  1782. assert_eq!(<VotingPeriod<Test>>::get(), new_parameters.voting_period);
  1783. assert_eq!(
  1784. <RevealingPeriod<Test>>::get(),
  1785. new_parameters.revealing_period
  1786. );
  1787. assert_eq!(
  1788. <MinCouncilStake<Test>>::get(),
  1789. new_parameters.min_council_stake
  1790. );
  1791. assert_eq!(
  1792. <NewTermDuration<Test>>::get(),
  1793. new_parameters.new_term_duration
  1794. );
  1795. assert_eq!(CouncilSize::get(), new_parameters.council_size);
  1796. assert_eq!(CandidacyLimit::get(), new_parameters.candidacy_limit);
  1797. assert_eq!(
  1798. <MinVotingStake<Test>>::get(),
  1799. new_parameters.min_voting_stake
  1800. );
  1801. });
  1802. }
  1803. }