Skip to content

Commit

Permalink
update: staking's tests from v2.0.0-alpha.3
Browse files Browse the repository at this point in the history
clearloop committed Mar 11, 2020
1 parent 5e7a434 commit 352b8f7
Showing 5 changed files with 4,811 additions and 4,511 deletions.
1,971 changes: 1,971 additions & 0 deletions frame/staking/src/darwinia_tests.rs

Large diffs are not rendered by default.

28 changes: 18 additions & 10 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -231,10 +231,12 @@
#![feature(drain_filter)]
#![recursion_limit = "128"]

#[cfg(test)]
mod darwinia_tests;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
mod substrate_tests;

mod inflation;
mod slashing;
@@ -489,7 +491,7 @@ where
let normal_ring = *active_ring - *active_deposit_ring;
if normal_ring < *slash_ring {
let mut slash_deposit_ring = *slash_ring - (*active_ring - *active_deposit_ring);
*active_deposit_ring -= slash_deposit_ring;
*active_deposit_ring = active_deposit_ring.saturating_sub(slash_deposit_ring);

deposit_item.drain_filter(|item| {
if ts >= item.expire_time {
@@ -608,9 +610,14 @@ where
ring_balance: RingBalance,
#[codec(compact)]
kton_balance: KtonBalance,
/// Contribution of validators/nominators
power: Power,
}

/// We use `power` instead of balance value to represent the
/// contribution of validators/nominators(validators can contribute too),
/// so does the rewards.
///
/// A snapshot of the stake backing a single validator in the system.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, RuntimeDebug)]
pub struct Exposure<AccountId, RingBalance, KtonBalance>
@@ -1119,8 +1126,8 @@ decl_module! {
let promise_month = promise_month.min(36);

match max_additional {
StakingBalance::RingBalance(r) => {
let stash_balance = T::RingCurrency::usable_balance(&stash);
StakingBalance::RingBalance(r) => {
let stash_balance = T::RingCurrency::free_balance(&stash);
if let Some(extra) = stash_balance.checked_sub(&ledger.active_ring) {
let extra = extra.min(r);
let (start_time, expire_time) = Self::bond_ring(
@@ -1136,7 +1143,7 @@ decl_module! {
}
},
StakingBalance::KtonBalance(k) => {
let stash_balance = T::KtonCurrency::usable_balance(&stash);
let stash_balance = T::KtonCurrency::free_balance(&stash);
if let Some(extra) = stash_balance.checked_sub(&ledger.active_kton) {
let extra = extra.min(k);

@@ -1151,6 +1158,7 @@ decl_module! {
}

// TODO: doc
/// Deposit Ring and reward Kton.
fn deposit_extra(origin, value: RingBalance<T>, promise_month: Moment) {
let stash = ensure_signed(origin)?;
let controller = Self::bonded(&stash).ok_or(<Error<T>>::NotStash)?;
@@ -1842,6 +1850,7 @@ impl<T: Trait> Module<T> {
let reward = reward.saturating_sub(off_the_table);
let mut imbalance = <RingPositiveImbalance<T>>::zero();
let mut nominators_reward = vec![];

let validator_cut = if reward.is_zero() {
Zero::zero()
} else {
@@ -1851,7 +1860,6 @@ impl<T: Trait> Module<T> {
for i in &exposure.others {
let per_u64 = Perbill::from_rational_approximation(i.power, total);
let nominator_reward = per_u64 * reward;

imbalance.maybe_subsume(Self::make_payout(&i.who, nominator_reward));
nominators_reward.push(NominatorReward {
who: i.who.to_owned(),
@@ -1862,10 +1870,10 @@ impl<T: Trait> Module<T> {
let per_u64 = Perbill::from_rational_approximation(exposure.own_power, total);
per_u64 * reward
};

let validator_reward = validator_cut + off_the_table;

imbalance.maybe_subsume(Self::make_payout(stash, validator_reward));

(imbalance, (validator_reward, nominators_reward))
}

@@ -2086,7 +2094,7 @@ impl<T: Trait> Module<T> {
power,
});
}
total_power += power;
total_power = total_power.saturating_add(power);
},
);
let exposure = Exposure {
@@ -2389,10 +2397,10 @@ impl<T: Trait> OnDepositRedeem<T::AccountId> for Module<T> {
let start_time = start_time * 1000;
let promise_month = months.min(36);

// let stash_balance = T::Ring::free_balance(&stash);
// let stash_balance = T::Ring::free_balance(&stash);

// TODO: Lock but no kton reward because this is a deposit redeem
// let extra = extra.min(r);
// let extra = extra.min(r);

let redeemed_positive_imbalance_ring = T::RingCurrency::deposit_into_existing(&stash, amount)?;

6 changes: 4 additions & 2 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
@@ -232,7 +232,7 @@ pub struct ExtBuilder {
impl Default for ExtBuilder {
fn default() -> Self {
Self {
existential_deposit: 0,
existential_deposit: 1, // this shoule be greater than zero
validator_pool: false,
nominate: true,
validator_count: 2,
@@ -289,7 +289,9 @@ impl ExtBuilder {
pub fn build(self) -> sp_io::TestExternalities {
self.set_associated_consts();
let mut storage = system::GenesisConfig::default().build_storage::<Test>().unwrap();
let balance_factor = if self.existential_deposit > 0 { 256 } else { 1 };
// existential_deposit should always > 0, actually, the minimum value is 1,
// more info plz ckechkout https://github.com/paritytech/substrate/blob/013c1ee167354a08283fb69915fda56a62fee943/frame/staking/src/mock.rs#L290
let balance_factor = if self.existential_deposit > 1 { 256 } else { 1 };

let num_validators = self.num_validators.unwrap_or(self.validator_count);
let validators = (0..num_validators)
2,818 changes: 2,818 additions & 0 deletions frame/staking/src/substrate_tests.rs

Large diffs are not rendered by default.

4,499 changes: 0 additions & 4,499 deletions frame/staking/src/tests.rs

This file was deleted.

0 comments on commit 352b8f7

Please sign in to comment.