mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 02:51:08 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
+146
-113
@@ -251,25 +251,37 @@ mod mock;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use rand_chacha::{rand_core::{RngCore, SeedableRng}, ChaChaRng};
|
||||
use sp_std::prelude::*;
|
||||
use codec::{Encode, Decode};
|
||||
use sp_runtime::{Percent, RuntimeDebug,
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::{
|
||||
decl_error, decl_event, decl_module, decl_storage,
|
||||
dispatch::DispatchResult,
|
||||
ensure,
|
||||
traits::{
|
||||
StaticLookup, AccountIdConversion, Saturating, Zero, IntegerSquareRoot, Hash,
|
||||
TrailingZeroInput, CheckedSub
|
||||
}
|
||||
BalanceStatus, ChangeMembers, Currency, EnsureOrigin, ExistenceRequirement::AllowDeath,
|
||||
Get, Imbalance, OnUnbalanced, Randomness, ReservableCurrency,
|
||||
},
|
||||
weights::Weight,
|
||||
PalletId,
|
||||
};
|
||||
use frame_support::{decl_error, decl_module, decl_storage, decl_event, ensure, dispatch::DispatchResult, PalletId};
|
||||
use frame_support::weights::Weight;
|
||||
use frame_support::traits::{
|
||||
Currency, ReservableCurrency, Randomness, Get, ChangeMembers, BalanceStatus,
|
||||
ExistenceRequirement::AllowDeath, EnsureOrigin, OnUnbalanced, Imbalance
|
||||
use frame_system::{self as system, ensure_root, ensure_signed};
|
||||
use rand_chacha::{
|
||||
rand_core::{RngCore, SeedableRng},
|
||||
ChaChaRng,
|
||||
};
|
||||
use frame_system::{self as system, ensure_signed, ensure_root};
|
||||
use sp_runtime::{
|
||||
traits::{
|
||||
AccountIdConversion, CheckedSub, Hash, IntegerSquareRoot, Saturating, StaticLookup,
|
||||
TrailingZeroInput, Zero,
|
||||
},
|
||||
Percent, RuntimeDebug,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
type BalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<<T as system::Config>::AccountId>>::Balance;
|
||||
type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;
|
||||
type BalanceOf<T, I> =
|
||||
<<T as Config<I>>::Currency as Currency<<T as system::Config>::AccountId>>::Balance;
|
||||
type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
|
||||
<T as frame_system::Config>::AccountId,
|
||||
>>::NegativeImbalance;
|
||||
|
||||
/// The module's configuration trait.
|
||||
pub trait Config<I = DefaultInstance>: system::Config {
|
||||
@@ -370,7 +382,7 @@ pub enum VouchingStatus {
|
||||
pub type StrikeCount = u32;
|
||||
|
||||
/// A bid for entry into society.
|
||||
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug,)]
|
||||
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug)]
|
||||
pub struct Bid<AccountId, Balance> {
|
||||
/// The bidder/candidate trying to enter society
|
||||
who: AccountId,
|
||||
@@ -1187,7 +1199,6 @@ fn pick_item<'a, R: RngCore, T>(rng: &mut R, items: &'a [T]) -> Option<&'a T> {
|
||||
|
||||
/// Pick a new PRN, in the range [0, `max`] (inclusive).
|
||||
fn pick_usize<'a, R: RngCore>(rng: &mut R, max: usize) -> usize {
|
||||
|
||||
(rng.next_u32() % (max as u32 + 1)) as usize
|
||||
}
|
||||
|
||||
@@ -1198,7 +1209,7 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
mut bids: Vec<Bid<T::AccountId, BalanceOf<T, I>>>,
|
||||
who: &T::AccountId,
|
||||
value: BalanceOf<T, I>,
|
||||
bid_kind: BidKind<T::AccountId, BalanceOf<T, I>>
|
||||
bid_kind: BidKind<T::AccountId, BalanceOf<T, I>>,
|
||||
) {
|
||||
const MAX_BID_COUNT: usize = 1000;
|
||||
|
||||
@@ -1206,7 +1217,8 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
// Insert new elements after the existing ones. This ensures new bids
|
||||
// with the same bid value are further down the list than existing ones.
|
||||
Ok(pos) => {
|
||||
let different_bid = bids.iter()
|
||||
let different_bid = bids
|
||||
.iter()
|
||||
// Easily extract the index we are on
|
||||
.enumerate()
|
||||
// Skip ahead to the suggested position
|
||||
@@ -1218,25 +1230,13 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
// If the element is not at the end of the list, insert the new element
|
||||
// in the spot.
|
||||
if let Some((p, _)) = different_bid {
|
||||
bids.insert(p, Bid {
|
||||
value,
|
||||
who: who.clone(),
|
||||
kind: bid_kind,
|
||||
});
|
||||
bids.insert(p, Bid { value, who: who.clone(), kind: bid_kind });
|
||||
// If the element is at the end of the list, push the element on the end.
|
||||
} else {
|
||||
bids.push(Bid {
|
||||
value,
|
||||
who: who.clone(),
|
||||
kind: bid_kind,
|
||||
});
|
||||
bids.push(Bid { value, who: who.clone(), kind: bid_kind });
|
||||
}
|
||||
},
|
||||
Err(pos) => bids.insert(pos, Bid {
|
||||
value,
|
||||
who: who.clone(),
|
||||
kind: bid_kind,
|
||||
}),
|
||||
Err(pos) => bids.insert(pos, Bid { value, who: who.clone(), kind: bid_kind }),
|
||||
}
|
||||
// Keep it reasonably small.
|
||||
if bids.len() > MAX_BID_COUNT {
|
||||
@@ -1245,10 +1245,10 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
BidKind::Deposit(deposit) => {
|
||||
let err_amount = T::Currency::unreserve(&popped, deposit);
|
||||
debug_assert!(err_amount.is_zero());
|
||||
}
|
||||
},
|
||||
BidKind::Vouch(voucher, _) => {
|
||||
<Vouching<T, I>>::remove(&voucher);
|
||||
}
|
||||
},
|
||||
}
|
||||
Self::deposit_event(RawEvent::AutoUnbid(popped));
|
||||
}
|
||||
@@ -1263,7 +1263,10 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
}
|
||||
|
||||
/// Check a user is a candidate.
|
||||
fn is_candidate(candidates: &Vec<Bid<T::AccountId, BalanceOf<T, I>>>, who: &T::AccountId) -> bool {
|
||||
fn is_candidate(
|
||||
candidates: &Vec<Bid<T::AccountId, BalanceOf<T, I>>>,
|
||||
who: &T::AccountId,
|
||||
) -> bool {
|
||||
// Looking up a candidate is the same as looking up a bid
|
||||
Self::is_bid(candidates, who)
|
||||
}
|
||||
@@ -1307,7 +1310,7 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
T::MembershipChanged::change_members_sorted(&[], &[m.clone()], &members[..]);
|
||||
<Members<T, I>>::put(members);
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1333,73 +1336,87 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
// critical issues or side-effects. This is auto-correcting as members fall out of society.
|
||||
members.reserve(candidates.len());
|
||||
|
||||
let maturity = <system::Pallet<T>>::block_number()
|
||||
+ Self::lock_duration(members.len() as u32);
|
||||
let maturity =
|
||||
<system::Pallet<T>>::block_number() + Self::lock_duration(members.len() as u32);
|
||||
|
||||
let mut rewardees = Vec::new();
|
||||
let mut total_approvals = 0;
|
||||
let mut total_slash = <BalanceOf<T, I>>::zero();
|
||||
let mut total_payouts = <BalanceOf<T, I>>::zero();
|
||||
|
||||
let accepted = candidates.into_iter().filter_map(|Bid {value, who: candidate, kind }| {
|
||||
let mut approval_count = 0;
|
||||
let accepted = candidates
|
||||
.into_iter()
|
||||
.filter_map(|Bid { value, who: candidate, kind }| {
|
||||
let mut approval_count = 0;
|
||||
|
||||
// Creates a vector of (vote, member) for the given candidate
|
||||
// and tallies total number of approve votes for that candidate.
|
||||
let votes = members.iter()
|
||||
.filter_map(|m| <Votes<T, I>>::take(&candidate, m).map(|v| (v, m)))
|
||||
.inspect(|&(v, _)| if v == Vote::Approve { approval_count += 1 })
|
||||
.collect::<Vec<_>>();
|
||||
// Creates a vector of (vote, member) for the given candidate
|
||||
// and tallies total number of approve votes for that candidate.
|
||||
let votes = members
|
||||
.iter()
|
||||
.filter_map(|m| <Votes<T, I>>::take(&candidate, m).map(|v| (v, m)))
|
||||
.inspect(|&(v, _)| {
|
||||
if v == Vote::Approve {
|
||||
approval_count += 1
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Select one of the votes at random.
|
||||
// Note that `Vote::Skeptical` and `Vote::Reject` both reject the candidate.
|
||||
let is_accepted = pick_item(&mut rng, &votes).map(|x| x.0) == Some(Vote::Approve);
|
||||
// Select one of the votes at random.
|
||||
// Note that `Vote::Skeptical` and `Vote::Reject` both reject the candidate.
|
||||
let is_accepted =
|
||||
pick_item(&mut rng, &votes).map(|x| x.0) == Some(Vote::Approve);
|
||||
|
||||
let matching_vote = if is_accepted { Vote::Approve } else { Vote::Reject };
|
||||
let matching_vote = if is_accepted { Vote::Approve } else { Vote::Reject };
|
||||
|
||||
let bad_vote = |m: &T::AccountId| {
|
||||
// Voter voted wrong way (or was just a lazy skeptic) then reduce their payout
|
||||
// and increase their strikes. after MaxStrikes then they go into suspension.
|
||||
let amount = Self::slash_payout(m, T::WrongSideDeduction::get());
|
||||
let bad_vote = |m: &T::AccountId| {
|
||||
// Voter voted wrong way (or was just a lazy skeptic) then reduce their payout
|
||||
// and increase their strikes. after MaxStrikes then they go into suspension.
|
||||
let amount = Self::slash_payout(m, T::WrongSideDeduction::get());
|
||||
|
||||
let strikes = <Strikes<T, I>>::mutate(m, |s| {
|
||||
*s += 1;
|
||||
*s
|
||||
});
|
||||
if strikes >= T::MaxStrikes::get() {
|
||||
Self::suspend_member(m);
|
||||
}
|
||||
amount
|
||||
};
|
||||
|
||||
// Collect the voters who had a matching vote.
|
||||
rewardees.extend(votes.into_iter()
|
||||
.filter_map(|(v, m)|
|
||||
if v == matching_vote { Some(m) } else {
|
||||
total_slash += bad_vote(m);
|
||||
None
|
||||
let strikes = <Strikes<T, I>>::mutate(m, |s| {
|
||||
*s += 1;
|
||||
*s
|
||||
});
|
||||
if strikes >= T::MaxStrikes::get() {
|
||||
Self::suspend_member(m);
|
||||
}
|
||||
).cloned()
|
||||
);
|
||||
amount
|
||||
};
|
||||
|
||||
if is_accepted {
|
||||
total_approvals += approval_count;
|
||||
total_payouts += value;
|
||||
members.push(candidate.clone());
|
||||
// Collect the voters who had a matching vote.
|
||||
rewardees.extend(
|
||||
votes
|
||||
.into_iter()
|
||||
.filter_map(|(v, m)| {
|
||||
if v == matching_vote {
|
||||
Some(m)
|
||||
} else {
|
||||
total_slash += bad_vote(m);
|
||||
None
|
||||
}
|
||||
})
|
||||
.cloned(),
|
||||
);
|
||||
|
||||
Self::pay_accepted_candidate(&candidate, value, kind, maturity);
|
||||
if is_accepted {
|
||||
total_approvals += approval_count;
|
||||
total_payouts += value;
|
||||
members.push(candidate.clone());
|
||||
|
||||
// We track here the total_approvals so that every candidate has a unique range
|
||||
// of numbers from 0 to `total_approvals` with length `approval_count` so each
|
||||
// candidate is proportionally represented when selecting a "primary" below.
|
||||
Some((candidate, total_approvals, value))
|
||||
} else {
|
||||
// Suspend Candidate
|
||||
<SuspendedCandidates<T, I>>::insert(&candidate, (value, kind));
|
||||
Self::deposit_event(RawEvent::CandidateSuspended(candidate));
|
||||
None
|
||||
}
|
||||
}).collect::<Vec<_>>();
|
||||
Self::pay_accepted_candidate(&candidate, value, kind, maturity);
|
||||
|
||||
// We track here the total_approvals so that every candidate has a unique range
|
||||
// of numbers from 0 to `total_approvals` with length `approval_count` so each
|
||||
// candidate is proportionally represented when selecting a "primary" below.
|
||||
Some((candidate, total_approvals, value))
|
||||
} else {
|
||||
// Suspend Candidate
|
||||
<SuspendedCandidates<T, I>>::insert(&candidate, (value, kind));
|
||||
Self::deposit_event(RawEvent::CandidateSuspended(candidate));
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Clean up all votes.
|
||||
<Votes<T, I>>::remove_all(None);
|
||||
@@ -1411,7 +1428,12 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
Self::bump_payout(winner, maturity, total_slash);
|
||||
} else {
|
||||
// Move the slashed amount back from payouts account to local treasury.
|
||||
let res = T::Currency::transfer(&Self::payouts(), &Self::account_id(), total_slash, AllowDeath);
|
||||
let res = T::Currency::transfer(
|
||||
&Self::payouts(),
|
||||
&Self::account_id(),
|
||||
total_slash,
|
||||
AllowDeath,
|
||||
);
|
||||
debug_assert!(res.is_ok());
|
||||
}
|
||||
}
|
||||
@@ -1423,7 +1445,12 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
|
||||
// this should never fail since we ensure we can afford the payouts in a previous
|
||||
// block, but there's not much we can do to recover if it fails anyway.
|
||||
let res = T::Currency::transfer(&Self::account_id(), &Self::payouts(), total_payouts, AllowDeath);
|
||||
let res = T::Currency::transfer(
|
||||
&Self::account_id(),
|
||||
&Self::payouts(),
|
||||
total_payouts,
|
||||
AllowDeath,
|
||||
);
|
||||
debug_assert!(res.is_ok());
|
||||
}
|
||||
|
||||
@@ -1433,10 +1460,15 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
// Choose a random number between 0 and `total_approvals`
|
||||
let primary_point = pick_usize(&mut rng, total_approvals - 1);
|
||||
// Find the zero bid or the user who falls on that point
|
||||
let primary = accepted.iter().find(|e| e.2.is_zero() || e.1 > primary_point)
|
||||
.expect("e.1 of final item == total_approvals; \
|
||||
worst case find will always return that item; qed")
|
||||
.0.clone();
|
||||
let primary = accepted
|
||||
.iter()
|
||||
.find(|e| e.2.is_zero() || e.1 > primary_point)
|
||||
.expect(
|
||||
"e.1 of final item == total_approvals; \
|
||||
worst case find will always return that item; qed",
|
||||
)
|
||||
.0
|
||||
.clone();
|
||||
|
||||
let accounts = accepted.into_iter().map(|x| x.0).collect::<Vec<_>>();
|
||||
|
||||
@@ -1464,9 +1496,10 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
<Candidates<T, I>>::put(&candidates);
|
||||
|
||||
// Select sqrt(n) random members from the society and make them skeptics.
|
||||
let pick_member = |_| pick_item(&mut rng, &members[..]).expect("exited if members empty; qed");
|
||||
let pick_member =
|
||||
|_| pick_item(&mut rng, &members[..]).expect("exited if members empty; qed");
|
||||
for skeptic in (0..members.len().integer_sqrt()).map(pick_member) {
|
||||
for Bid{ who: c, .. } in candidates.iter() {
|
||||
for Bid { who: c, .. } in candidates.iter() {
|
||||
<Votes<T, I>>::insert(c, skeptic, Vote::Skeptic);
|
||||
}
|
||||
}
|
||||
@@ -1487,7 +1520,7 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
// whole slash is accounted for.
|
||||
*amount -= rest;
|
||||
rest = Zero::zero();
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
<Payouts<T, I>>::insert(who, &payouts[dropped..]);
|
||||
@@ -1497,10 +1530,12 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
|
||||
/// Bump the payout amount of `who`, to be unlocked at the given block number.
|
||||
fn bump_payout(who: &T::AccountId, when: T::BlockNumber, value: BalanceOf<T, I>) {
|
||||
if !value.is_zero(){
|
||||
<Payouts<T, I>>::mutate(who, |payouts| match payouts.binary_search_by_key(&when, |x| x.0) {
|
||||
Ok(index) => payouts[index].1 += value,
|
||||
Err(index) => payouts.insert(index, (when, value)),
|
||||
if !value.is_zero() {
|
||||
<Payouts<T, I>>::mutate(who, |payouts| {
|
||||
match payouts.binary_search_by_key(&when, |x| x.0) {
|
||||
Ok(index) => payouts[index].1 += value,
|
||||
Err(index) => payouts.insert(index, (when, value)),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1528,7 +1563,7 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
let err_amount = T::Currency::unreserve(candidate, deposit);
|
||||
debug_assert!(err_amount.is_zero());
|
||||
value
|
||||
}
|
||||
},
|
||||
BidKind::Vouch(voucher, tip) => {
|
||||
// Check that the voucher is still vouching, else some other logic may have removed their status.
|
||||
if <Vouching<T, I>>::take(&voucher) == Some(VouchingStatus::Vouching) {
|
||||
@@ -1539,7 +1574,7 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
} else {
|
||||
value
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Self::bump_payout(candidate, maturity, value);
|
||||
@@ -1554,14 +1589,12 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
|
||||
let mut approval_count = 0;
|
||||
let mut rejection_count = 0;
|
||||
// Tallies total number of approve and reject votes for the defender.
|
||||
members.iter()
|
||||
.filter_map(|m| <DefenderVotes<T, I>>::take(m))
|
||||
.for_each(|v| {
|
||||
match v {
|
||||
Vote::Approve => approval_count += 1,
|
||||
_ => rejection_count += 1,
|
||||
}
|
||||
});
|
||||
members.iter().filter_map(|m| <DefenderVotes<T, I>>::take(m)).for_each(
|
||||
|v| match v {
|
||||
Vote::Approve => approval_count += 1,
|
||||
_ => rejection_count += 1,
|
||||
},
|
||||
);
|
||||
|
||||
if approval_count <= rejection_count {
|
||||
// User has failed the challenge
|
||||
|
||||
@@ -21,16 +21,16 @@ use super::*;
|
||||
use crate as pallet_society;
|
||||
|
||||
use frame_support::{
|
||||
parameter_types, ord_parameter_types,
|
||||
traits::{OnInitialize, OnFinalize},
|
||||
ord_parameter_types, parameter_types,
|
||||
traits::{OnFinalize, OnInitialize},
|
||||
};
|
||||
use frame_support_test::TestRandomness;
|
||||
use frame_system::EnsureSignedBy;
|
||||
use sp_core::H256;
|
||||
use sp_runtime::{
|
||||
testing::Header,
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
};
|
||||
use frame_system::EnsureSignedBy;
|
||||
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
@@ -156,14 +156,16 @@ impl EnvBuilder {
|
||||
pub fn execute<R, F: FnOnce() -> R>(mut self, f: F) -> R {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
self.balances.push((Society::account_id(), self.balance.max(self.pot)));
|
||||
pallet_balances::GenesisConfig::<Test> {
|
||||
balances: self.balances,
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
pallet_society::GenesisConfig::<Test>{
|
||||
pallet_balances::GenesisConfig::<Test> { balances: self.balances }
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
pallet_society::GenesisConfig::<Test> {
|
||||
members: self.members,
|
||||
pot: self.pot,
|
||||
max_members: self.max_members,
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
let mut ext: sp_io::TestExternalities = t.into();
|
||||
ext.execute_with(f)
|
||||
}
|
||||
@@ -210,12 +212,7 @@ pub fn run_to_block(n: u64) {
|
||||
pub fn create_bid<AccountId, Balance>(
|
||||
value: Balance,
|
||||
who: AccountId,
|
||||
kind: BidKind<AccountId, Balance>
|
||||
) -> Bid<AccountId, Balance>
|
||||
{
|
||||
Bid {
|
||||
who,
|
||||
kind,
|
||||
value
|
||||
}
|
||||
kind: BidKind<AccountId, Balance>,
|
||||
) -> Bid<AccountId, Balance> {
|
||||
Bid { who, kind, value }
|
||||
}
|
||||
|
||||
@@ -20,9 +20,9 @@
|
||||
use super::*;
|
||||
use mock::*;
|
||||
|
||||
use frame_support::{assert_ok, assert_noop};
|
||||
use sp_runtime::traits::BadOrigin;
|
||||
use frame_support::{assert_noop, assert_ok};
|
||||
use sp_core::blake2_256;
|
||||
use sp_runtime::traits::BadOrigin;
|
||||
|
||||
#[test]
|
||||
fn founding_works() {
|
||||
@@ -118,10 +118,13 @@ fn bidding_works() {
|
||||
assert_eq!(Society::pot(), 1000);
|
||||
assert_eq!(Balances::free_balance(Society::account_id()), 10_000);
|
||||
// Choose smallest bidding users whose total is less than pot
|
||||
assert_eq!(Society::candidates(), vec![
|
||||
create_bid(300, 30, BidKind::Deposit(25)),
|
||||
create_bid(400, 40, BidKind::Deposit(25)),
|
||||
]);
|
||||
assert_eq!(
|
||||
Society::candidates(),
|
||||
vec![
|
||||
create_bid(300, 30, BidKind::Deposit(25)),
|
||||
create_bid(400, 40, BidKind::Deposit(25)),
|
||||
]
|
||||
);
|
||||
// A member votes for these candidates to join the society
|
||||
assert_ok!(Society::vote(Origin::signed(10), 30, true));
|
||||
assert_ok!(Society::vote(Origin::signed(10), 40, true));
|
||||
@@ -132,7 +135,7 @@ fn bidding_works() {
|
||||
assert_eq!(Balances::free_balance(Society::account_id()), 9_300);
|
||||
assert_eq!(Society::pot(), 1_300);
|
||||
// Left over from the original bids is 50 who satisfies the condition of bid less than pot.
|
||||
assert_eq!(Society::candidates(), vec![ create_bid(500, 50, BidKind::Deposit(25)) ]);
|
||||
assert_eq!(Society::candidates(), vec![create_bid(500, 50, BidKind::Deposit(25))]);
|
||||
// 40, now a member, can vote for 50
|
||||
assert_ok!(Society::vote(Origin::signed(40), 50, true));
|
||||
run_to_block(12);
|
||||
@@ -144,7 +147,7 @@ fn bidding_works() {
|
||||
// No more candidates satisfy the requirements
|
||||
assert_eq!(Society::candidates(), vec![]);
|
||||
assert_ok!(Society::defender_vote(Origin::signed(10), true)); // Keep defender around
|
||||
// Next period
|
||||
// Next period
|
||||
run_to_block(16);
|
||||
// Same members
|
||||
assert_eq!(Society::members(), vec![10, 30, 40, 50]);
|
||||
@@ -153,7 +156,7 @@ fn bidding_works() {
|
||||
// No payouts
|
||||
assert_eq!(Balances::free_balance(Society::account_id()), 8_800);
|
||||
// Candidate 60 now qualifies based on the increased pot size.
|
||||
assert_eq!(Society::candidates(), vec![ create_bid(1900, 60, BidKind::Deposit(25)) ]);
|
||||
assert_eq!(Society::candidates(), vec![create_bid(1900, 60, BidKind::Deposit(25))]);
|
||||
// Candidate 60 is voted in.
|
||||
assert_ok!(Society::vote(Origin::signed(50), 60, true));
|
||||
run_to_block(20);
|
||||
@@ -183,7 +186,7 @@ fn unbidding_works() {
|
||||
assert_eq!(Balances::reserved_balance(30), 0);
|
||||
// 20 wins candidacy
|
||||
run_to_block(4);
|
||||
assert_eq!(Society::candidates(), vec![ create_bid(1000, 20, BidKind::Deposit(25)) ]);
|
||||
assert_eq!(Society::candidates(), vec![create_bid(1000, 20, BidKind::Deposit(25))]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -350,7 +353,10 @@ fn suspended_candidate_rejected_works() {
|
||||
assert_eq!(Society::suspended_candidate(20).is_some(), true);
|
||||
|
||||
// Normal user cannot make judgement on suspended candidate
|
||||
assert_noop!(Society::judge_suspended_candidate(Origin::signed(20), 20, Judgement::Approve), BadOrigin);
|
||||
assert_noop!(
|
||||
Society::judge_suspended_candidate(Origin::signed(20), 20, Judgement::Approve),
|
||||
BadOrigin
|
||||
);
|
||||
|
||||
// Suspension judgement origin makes no direct judgement
|
||||
assert_ok!(Society::judge_suspended_candidate(Origin::signed(2), 20, Judgement::Rebid));
|
||||
@@ -391,7 +397,10 @@ fn vouch_works() {
|
||||
assert_ok!(Society::vouch(Origin::signed(10), 20, 1000, 100));
|
||||
assert_eq!(<Vouching<Test>>::get(10), Some(VouchingStatus::Vouching));
|
||||
// A member cannot vouch twice at the same time
|
||||
assert_noop!(Society::vouch(Origin::signed(10), 30, 100, 0), Error::<Test, _>::AlreadyVouching);
|
||||
assert_noop!(
|
||||
Society::vouch(Origin::signed(10), 30, 100, 0),
|
||||
Error::<Test, _>::AlreadyVouching
|
||||
);
|
||||
// Vouching creates the right kind of bid
|
||||
assert_eq!(<Bids<Test>>::get(), vec![create_bid(1000, 20, BidKind::Vouch(10, 100))]);
|
||||
// Vouched user can become candidate
|
||||
@@ -475,7 +484,10 @@ fn unvouch_works() {
|
||||
assert_eq!(Society::members(), vec![10]);
|
||||
|
||||
// 10 cannot vouch again
|
||||
assert_noop!(Society::vouch(Origin::signed(10), 30, 100, 0), Error::<Test, _>::AlreadyVouching);
|
||||
assert_noop!(
|
||||
Society::vouch(Origin::signed(10), 30, 100, 0),
|
||||
Error::<Test, _>::AlreadyVouching
|
||||
);
|
||||
// 10 cannot unvouch either, so they are banned forever.
|
||||
assert_noop!(Society::unvouch(Origin::signed(10), 0), Error::<Test, _>::NotVouching);
|
||||
});
|
||||
@@ -654,7 +666,7 @@ fn bad_vote_slash_works() {
|
||||
assert_eq!(<Strikes<Test>>::get(30), 0);
|
||||
assert_eq!(<Strikes<Test>>::get(40), 0);
|
||||
// Their payout is slashed, a random person is rewarded
|
||||
assert_eq!(<Payouts<Test>>::get(10), vec![(5, 100), (9,2)]);
|
||||
assert_eq!(<Payouts<Test>>::get(10), vec![(5, 100), (9, 2)]);
|
||||
assert_eq!(<Payouts<Test>>::get(20), vec![(5, 98)]);
|
||||
assert_eq!(<Payouts<Test>>::get(30), vec![(5, 100)]);
|
||||
assert_eq!(<Payouts<Test>>::get(40), vec![(5, 100)]);
|
||||
@@ -672,7 +684,10 @@ fn user_cannot_bid_twice() {
|
||||
assert_noop!(Society::bid(Origin::signed(30), 100), Error::<Test, _>::AlreadyBid);
|
||||
// Cannot vouch when already bid
|
||||
assert_ok!(Society::add_member(&50));
|
||||
assert_noop!(Society::vouch(Origin::signed(50), 20, 100, 100), Error::<Test, _>::AlreadyBid);
|
||||
assert_noop!(
|
||||
Society::vouch(Origin::signed(50), 20, 100, 100),
|
||||
Error::<Test, _>::AlreadyBid
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -794,7 +809,11 @@ fn max_limits_work() {
|
||||
assert_eq!(Society::candidates().len(), 4);
|
||||
// Fill up members with suspended candidates from the first rotation
|
||||
for i in 100..104 {
|
||||
assert_ok!(Society::judge_suspended_candidate(Origin::signed(2), i, Judgement::Approve));
|
||||
assert_ok!(Society::judge_suspended_candidate(
|
||||
Origin::signed(2),
|
||||
i,
|
||||
Judgement::Approve
|
||||
));
|
||||
}
|
||||
assert_eq!(Society::members().len(), 100);
|
||||
// Can't add any more members
|
||||
@@ -840,15 +859,18 @@ fn zero_bid_works() {
|
||||
assert_eq!(Society::pot(), 1000);
|
||||
assert_eq!(Balances::free_balance(Society::account_id()), 10_000);
|
||||
// Choose smallest bidding users whose total is less than pot, with only one zero bid.
|
||||
assert_eq!(Society::candidates(), vec![
|
||||
create_bid(0, 30, BidKind::Deposit(25)),
|
||||
create_bid(300, 50, BidKind::Deposit(25)),
|
||||
create_bid(400, 60, BidKind::Deposit(25)),
|
||||
]);
|
||||
assert_eq!(<Bids<Test>>::get(), vec![
|
||||
create_bid(0, 20, BidKind::Deposit(25)),
|
||||
create_bid(0, 40, BidKind::Deposit(25)),
|
||||
]);
|
||||
assert_eq!(
|
||||
Society::candidates(),
|
||||
vec![
|
||||
create_bid(0, 30, BidKind::Deposit(25)),
|
||||
create_bid(300, 50, BidKind::Deposit(25)),
|
||||
create_bid(400, 60, BidKind::Deposit(25)),
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
<Bids<Test>>::get(),
|
||||
vec![create_bid(0, 20, BidKind::Deposit(25)), create_bid(0, 40, BidKind::Deposit(25)),]
|
||||
);
|
||||
// A member votes for these candidates to join the society
|
||||
assert_ok!(Society::vote(Origin::signed(10), 30, true));
|
||||
assert_ok!(Society::vote(Origin::signed(10), 50, true));
|
||||
@@ -878,7 +900,7 @@ fn bids_ordered_correctly() {
|
||||
|
||||
for j in 0..5 {
|
||||
for i in 0..5 {
|
||||
final_list.push(create_bid(j, 100 + (i * 5 + j) as u128, BidKind::Deposit(25)));
|
||||
final_list.push(create_bid(j, 100 + (i * 5 + j) as u128, BidKind::Deposit(25)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user