Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
ea30833
make pool roles optional
kianenigma May 13, 2022
00e8c87
undo lock file changes?
kianenigma May 13, 2022
fa5e95a
add migration
kianenigma May 13, 2022
f2df79e
add the ability for pools to chill themselves
kianenigma May 14, 2022
56cf997
boilerplate of tests
kianenigma May 14, 2022
508bc0a
new system works, tests need fixing
kianenigma May 15, 2022
f83944a
somewhat stable, but I think I found another bug as well
kianenigma May 15, 2022
ac48c37
Fix it all
kianenigma May 16, 2022
cbeb9fb
Master.into()
kianenigma May 16, 2022
031040f
Add more more sophisticated test + capture one more bug.
kianenigma May 16, 2022
efc7b4f
Update frame/staking/src/lib.rs
kianenigma May 16, 2022
60d42f1
reduce the diff a little bit
kianenigma May 17, 2022
02aa7d4
add some test for the slashing bug
kianenigma May 19, 2022
90db26e
cleanup
kianenigma May 19, 2022
5bf6d9c
Merge branch 'kiz-pool-chill' of github.com:paritytech/substrate into…
kianenigma May 19, 2022
1d8c940
Master.into()
kianenigma May 19, 2022
2ec4857
Merge branch 'master' of github.com:paritytech/substrate into kiz-poo…
kianenigma May 20, 2022
a51e408
fix lock file?
kianenigma May 20, 2022
28c8852
Fix
kianenigma May 20, 2022
c9413a2
fmt
kianenigma May 20, 2022
433476d
Merge branch 'master' of github.com:paritytech/substrate into kiz-poo…
kianenigma May 23, 2022
c34b655
Update frame/nomination-pools/src/lib.rs
kianenigma May 23, 2022
d0d75a1
Update frame/nomination-pools/src/lib.rs
kianenigma May 23, 2022
a6afb06
Update frame/nomination-pools/src/lib.rs
kianenigma May 23, 2022
a3a43e7
Update frame/nomination-pools/src/mock.rs
kianenigma May 23, 2022
4b7b0c7
Fix build
kianenigma May 23, 2022
3f66688
Merge branch 'kiz-pool-chill' of github.com:paritytech/substrate into…
kianenigma May 23, 2022
640ec31
fix some fishy tests..
kianenigma May 23, 2022
398ddfe
add one last integrity check for MinCreateBond
kianenigma May 24, 2022
d5dc697
remove bad assertion -- needs to be dealt with later
kianenigma May 24, 2022
05fb517
Merge branch 'master' of github.com:paritytech/substrate into kiz-poo…
kianenigma May 24, 2022
f4dbd0a
nits
shawntabrizi May 25, 2022
0a79c80
Master.into()
kianenigma Jun 8, 2022
78c0310
Merge branch 'kiz-pool-chill' of github.com:paritytech/substrate into…
kianenigma Jun 9, 2022
0fb1125
fix tests and add benchmarks for chill
kianenigma Jun 9, 2022
12773bb
remove stuff
kianenigma Jun 9, 2022
ca475df
fix benchmarks
kianenigma Jun 10, 2022
44a2722
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jun 10, 2022
f027faf
cargo run --quiet --profile=production --features=runtime-benchmarks…
Jun 10, 2022
33b581c
Master.into()
kianenigma Jun 11, 2022
c77613f
Merge branch 'kiz-pool-chill' of github.com:paritytech/substrate into…
kianenigma Jun 11, 2022
d69af2c
remove defensive
kianenigma Jun 11, 2022
d318197
Merge branch 'master' of github.com:paritytech/substrate into kiz-poo…
kianenigma Jun 13, 2022
486a0e9
first working version
kianenigma Jun 13, 2022
9b2113f
Master.into()
kianenigma Jun 14, 2022
36cb484
bring back all tests
kianenigma Jun 14, 2022
723574b
ALL new tests work now
kianenigma Jun 14, 2022
624abe9
Merge branch 'master' of github.com:paritytech/substrate into kiz-fix…
kianenigma Jun 14, 2022
82287b0
cleanup
kianenigma Jun 14, 2022
e403fb1
make sure benchmarks and all work
kianenigma Jun 15, 2022
4cad93a
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jun 15, 2022
221369b
cargo run --quiet --profile=production --features=runtime-benchmarks…
Jun 15, 2022
696a55e
round of self-review, make arithmetic safe
kianenigma Jun 15, 2022
0689b58
Merge branch 'kiz-fix-reward-pools-2' of github.com:paritytech/substr…
kianenigma Jun 15, 2022
bcb413c
fix warn
kianenigma Jun 15, 2022
bce40f7
add migration code
kianenigma Jun 16, 2022
8fc25ac
Merge branch 'master' of github.com:paritytech/substrate into kiz-fix…
kianenigma Jun 16, 2022
fc3ad18
Fix doc
kianenigma Jun 17, 2022
03107f3
Merge branches 'kiz-fix-reward-pools-2' and 'master' of github.com:pa…
kianenigma Jun 21, 2022
9f875a9
add precision notes
kianenigma Jun 21, 2022
0513284
make arithmetic fallible
kianenigma Jun 21, 2022
1c43f09
fix node runtime
kianenigma Jun 21, 2022
ef56db6
a lot of precision tests and notes and stuff
kianenigma Jun 22, 2022
3da2364
document MaxPOintsToBalance better
kianenigma Jun 22, 2022
ed5083f
Merge branch 'master' of github.com:paritytech/substrate into kiz-fix…
kianenigma Jun 23, 2022
3690489
:round of self-review
kianenigma Jun 23, 2022
62d35c8
fmt
kianenigma Jun 23, 2022
1c840b2
fix some comments
kianenigma Jun 23, 2022
7d9d403
Merge branch 'master' of github.com:paritytech/substrate into kiz-fix…
kianenigma Jun 26, 2022
78b79f2
Master.into()
kianenigma Jul 1, 2022
a8ccd71
Fix proportional slashing logic
kianenigma Jul 5, 2022
60b7641
Update frame/nomination-pools/src/tests.rs
kianenigma Jul 6, 2022
a2082cd
Update frame/nomination-pools/src/tests.rs
kianenigma Jul 6, 2022
ecb7890
Update frame/nomination-pools/src/lib.rs
kianenigma Jul 6, 2022
7e56e1a
track poinst in migration
kianenigma Jul 6, 2022
80b31c0
Merge branch 'kiz-fix-reward-pools-2' of github.com:paritytech/substr…
kianenigma Jul 6, 2022
579da37
fix
kianenigma Jul 6, 2022
51c1608
fmt
kianenigma Jul 6, 2022
3779081
fix migration
kianenigma Jul 6, 2022
9171a13
remove event read
kianenigma Jul 6, 2022
b9ab747
Apply suggestions from code review
kianenigma Jul 7, 2022
d4f45e7
Update frame/staking/src/lib.rs
kianenigma Jul 9, 2022
ca47b05
Update frame/nomination-pools/src/lib.rs
kianenigma Jul 9, 2022
f3e10a9
Update frame/nomination-pools/src/lib.rs
kianenigma Jul 9, 2022
463ddfd
Merge branch 'master' of github.com:paritytech/substrate into kiz-fix…
kianenigma Jul 10, 2022
51873c7
Merge branch 'kiz-fix-reward-pools-2' of github.com:paritytech/substr…
kianenigma Jul 10, 2022
c205ac3
update
kianenigma Jul 10, 2022
69d1f9c
fmt
kianenigma Jul 10, 2022
3efe9f5
fmt
kianenigma Jul 10, 2022
84a2639
add one last test
kianenigma Jul 10, 2022
e7673f8
fmt
kianenigma Jul 12, 2022
ed2c225
Merge branch 'master' of github.com:paritytech/substrate into kiz-fix…
kianenigma Jul 12, 2022
de9886d
merged
kianenigma Jul 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
a lot of precision tests and notes and stuff
  • Loading branch information
kianenigma committed Jun 22, 2022
commit ef56db6e5422dec23c187e7c5703cdbcdf0e03da
63 changes: 45 additions & 18 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ use frame_support::{
use scale_info::TypeInfo;
use sp_core::U256;
use sp_runtime::{
traits::{AccountIdConversion, Bounded, CheckedSub, Convert, Saturating, Zero, CheckedAdd},
traits::{AccountIdConversion, Bounded, CheckedAdd, CheckedSub, Convert, Saturating, Zero},
FixedPointNumber, FixedPointOperand,
};
use sp_staking::{EraIndex, OnStakerSlash, StakingInterface};
Expand Down Expand Up @@ -412,8 +412,11 @@ pub struct PoolMember<T: Config> {

impl<T: Config> PoolMember<T> {
/// The pending rewards of this member.
fn pending_rewards(&self, current_reward_counter: T::RewardCounter) -> Result<BalanceOf<T>, Error::<T>> {
// Precision note: Reward counters are fixedU128 with base of 10^18. Multiplying it with
fn pending_rewards(
&self,
current_reward_counter: T::RewardCounter,
) -> Result<BalanceOf<T>, Error<T>> {
// accuracy note: Reward counters are fixedU128 with base of 10^18. Multiplying it with
// points, which have the same granularity as with balance should almost always be fine for
// Polkadot and Kusama since: The total issuance of both are
//
Expand Down Expand Up @@ -449,7 +452,8 @@ impl<T: Config> PoolMember<T> {
// way earlier than the few decades we anticipate due to severe slashing, no operations can
// happen in that pool.
(current_reward_counter.defensive_saturating_sub(self.last_recorded_reward_counter))
.checked_mul_int(self.active_points()).ok_or(Error::<T>::OverflowRisk)
.checked_mul_int(self.active_points())
.ok_or(Error::<T>::OverflowRisk)
}

/// Active balance of the member.
Expand Down Expand Up @@ -708,7 +712,7 @@ impl<T: Config> BondedPool<T> {
MaxPoolMembers::<T>::get().map_or(true, |max| PoolMembers::<T>::count() < max),
Error::<T>::MaxPoolMembers
);
self.member_counter = self.member_counter.defensive_saturating_add(1);
self.member_counter = self.member_counter.checked_add(1).ok_or(Error::<T>::OverflowRisk)?;
Ok(())
}

Expand Down Expand Up @@ -991,12 +995,14 @@ impl<T: Config> RewardPool<T> {
fn update_records(&mut self, id: PoolId, bonded_points: BalanceOf<T>) -> Result<(), Error<T>> {
let balance = Self::current_balance(id);
self.last_recorded_reward_counter = self.current_reward_counter(id, bonded_points)?;
self.last_recorded_total_payouts =
balance.checked_add(&self.total_rewards_claimed).ok_or(Error::<T>::OverflowRisk)?;
self.last_recorded_total_payouts = balance
.checked_add(&self.total_rewards_claimed)
.ok_or(Error::<T>::OverflowRisk)?;
Ok(())
}

/// Get the current reward counter, based on the given `bonded_points`.
/// Get the current reward counter, based on the given `bonded_points` being the state of the
/// bonded pool at this time.
fn current_reward_counter(
&self,
id: PoolId,
Expand All @@ -1007,8 +1013,9 @@ impl<T: Config> RewardPool<T> {
.saturating_add(self.total_rewards_claimed)
.saturating_sub(self.last_recorded_total_payouts);

// accuracy notes: `payouts_since_last_record` is a subset of the total_issuance at the very
// worse. Bonded_points are similarly, in a non-slashed pool, have the same granularity as
// * accuracy notes regarding the multiplication in `checked_from_rational`:
// `payouts_since_last_record` is a subset of the total_issuance at the very
// worse. `bonded_points` are similarly, in a non-slashed pool, have the same granularity as
// balance, and are thus below within the range of total_issuance. In the worse case
// scenario, for `saturating_from_rational`, we have:
//
Expand All @@ -1024,11 +1031,24 @@ impl<T: Config> RewardPool<T> {
// Polkadot. The important note here is that `reward_pool.last_recorded_reward_counter` only
// ever accumulates, but its semantics imply that it is less than total_issuance, when
// represented as `FixedU128`, which means it is less than `total_issuance * 10^18`.
//
// * accuracy notes regarding `checked_from_rational` collapsing to zero, meaning that no
// reward can be claimed:
//
// largest `bonded_points`, such that the reward counter is non-zero, with `FixedU128`
// will be when the payout is being computed. This essentially means `payout/bonded_points`
// needs to be more than 1/1^18. Thus, assuming that `bonded_points` will always be less
// than `10 * dot_total_issuance`, if the reward_counter is the smallest possible value,
// the value of the reward being calculated is:
//
// x / 10^20 = 1/ 10^18
//
// x = 100
//
// which is basically 10^-8 DOTs. See `smallest_claimable_reward` for an example of this.
T::RewardCounter::checked_from_rational(payouts_since_last_record, bonded_points)
.and_then(|ref r| self.last_recorded_reward_counter.checked_add(r))
.ok_or(Error::<T>::OverflowRisk)

// TODO: test for arithmetic failing + micro reward payment in a mega-pool.
}

/// Current free balance of the reward pool.
Expand Down Expand Up @@ -1165,7 +1185,7 @@ pub mod pallet {
use super::*;
use frame_support::traits::StorageVersion;
use frame_system::{ensure_signed, pallet_prelude::*};
use sp_runtime::traits::CheckedAdd;
use sp_runtime::traits::CheckedAdd;

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
Expand Down Expand Up @@ -1984,8 +2004,14 @@ use sp_runtime::traits::CheckedAdd;

/// Set a new state for the pool.
///
/// The dispatch origin of this call must be signed by the state toggler, or the root role
/// of the pool.
/// If a pool is already in the `Destroying` state, then under no condition can its state
/// change again.
///
/// The dispatch origin of this call must be either:
///
/// 1. signed by the state toggler, or the root role of the pool,
/// 2. if the pool conditions to be open are NOT met (as described by `ok_to_be_open`), and
/// then the state of the pool can be permissionlessly changed to `Destroying`.
#[pallet::weight(T::WeightInfo::set_state())]
pub fn set_state(
origin: OriginFor<T>,
Expand Down Expand Up @@ -2308,13 +2334,14 @@ impl<T: Config> Pallet<T> {
reward_pool.current_reward_counter(bonded_pool.id, bonded_pool.points)?;
let pending_rewards = member.pending_rewards(current_reward_counter)?;

member.last_recorded_reward_counter = current_reward_counter;
reward_pool.register_claimed_reward(pending_rewards);

if pending_rewards.is_zero() {
return Ok(pending_rewards)
}

// IFF the reward is non-zero alter the member and reward pool info.
member.last_recorded_reward_counter = current_reward_counter;
reward_pool.register_claimed_reward(pending_rewards);

// Transfer payout to the member.
T::Currency::transfer(
&bonded_pool.reward_account(),
Expand Down
13 changes: 11 additions & 2 deletions frame/nomination-pools/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use super::*;
use crate::{self as pools};
use frame_support::{assert_ok, parameter_types, PalletId};
use frame_system::RawOrigin;
use sp_runtime::{FixedU128};
use sp_runtime::FixedU128;

pub type AccountId = u128;
pub type Balance = u128;
pub type RewardCounter = FixedU128;
// This sneaky little hack allows us to write code exactly as we would do in the pallet in the tests
// as well, e.g. `StorageItem::<T>::get()`.
pub type T = Runtime;

// Ext builder creates a pool with id 1.
pub fn default_bonded_account() -> AccountId {
Expand All @@ -25,6 +28,7 @@ parameter_types! {
pub storage UnbondingBalanceMap: BTreeMap<AccountId, Balance> = Default::default();
#[derive(Clone, PartialEq)]
pub static MaxUnbonding: u32 = 8;
pub static StakingMinBond: Balance = 10;
pub storage Nominations: Option<Vec<AccountId>> = None;
}

Expand All @@ -42,7 +46,7 @@ impl sp_staking::StakingInterface for StakingMock {
type AccountId = AccountId;

fn minimum_bond() -> Self::Balance {
10
StakingMinBond::get()
}

fn current_era() -> EraIndex {
Expand Down Expand Up @@ -229,6 +233,11 @@ impl ExtBuilder {
self
}

pub(crate) fn min_bond(self, min: Balance) -> Self {
StakingMinBond::set(min);
self
}

pub(crate) fn with_check(self, level: u8) -> Self {
CheckLevel::set(level);
self
Expand Down
Loading