election.rs 70 KB

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