Skip to content

Commit

Permalink
clean up for PR
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonwells committed Oct 13, 2023
1 parent bd3b7b6 commit f4755d5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
25 changes: 14 additions & 11 deletions pallets/capacity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ pub mod pallet {
pub type EpochLength<T: Config> =
StorageValue<_, T::BlockNumber, ValueQuery, EpochLengthDefault<T>>;

/// Information about the current staking reward era.
/// Information about the current staking reward era. Checked every block.
#[pallet::storage]
#[pallet::whitelist_storage]
#[pallet::getter(fn get_current_era)]
pub type CurrentEraInfo<T: Config> =
StorageValue<_, RewardEraInfo<T::RewardEra, T::BlockNumber>, ValueQuery>;
Expand Down Expand Up @@ -767,26 +768,26 @@ impl<T: Config> Pallet<T> {

fn start_new_reward_era_if_needed(current_block: T::BlockNumber) -> Weight {
let current_era_info: RewardEraInfo<T::RewardEra, T::BlockNumber> = Self::get_current_era(); // 1r

if current_block.saturating_sub(current_era_info.started_at) >= T::EraLength::get().into() {
// 1r
let new_era_info = RewardEraInfo {
era_index: current_era_info.era_index.saturating_add(One::one()),
started_at: current_block,
};

let current_reward_pool_info =
Self::get_reward_pool_for_era(current_era_info.era_index).unwrap_or_default(); // 1r

let past_eras_max = T::StakingRewardsPastErasMax::get();
let entries: u32 = StakingRewardPool::<T>::count();
let entries: u32 = StakingRewardPool::<T>::count(); // 1r

if past_eras_max.eq(&entries.into()) {
// 2r
let current_era = Self::get_current_era().era_index;
let earliest_era = current_era.saturating_sub(past_eras_max).add(One::one());
let earliest_era =
current_era_info.era_index.saturating_sub(past_eras_max).add(One::one());
StakingRewardPool::<T>::remove(earliest_era); // 1w
}
CurrentEraInfo::<T>::set(new_era_info); // 1w

// let msa_handle = T::HandleProvider::get_handle_for_msa(msa_id);
let total_reward_pool =
T::RewardsProvider::reward_pool_size(current_reward_pool_info.total_staked_token);
let new_reward_pool = RewardPoolInfo {
Expand All @@ -797,10 +798,10 @@ impl<T: Config> Pallet<T> {
StakingRewardPool::<T>::insert(new_era_info.era_index, new_reward_pool); // 1w

T::WeightInfo::on_initialize()
.saturating_add(T::DbWeight::get().reads(5))
.saturating_add(T::DbWeight::get().reads(3))
.saturating_add(T::DbWeight::get().writes(3))
} else {
T::DbWeight::get().reads(2)
T::DbWeight::get().reads(1)
}
}

Expand All @@ -821,8 +822,7 @@ impl<T: Config> Pallet<T> {
Self::get_staking_account_for(staker).ok_or(Error::<T>::NotAStakingAccount)?;

let current_era: T::RewardEra = Self::get_current_era().era_index;
let thaw_eras = T::ChangeStakingTargetThawEras::get();
let thaw_at = current_era.saturating_add(thaw_eras);
let thaw_at = current_era.saturating_add(T::ChangeStakingTargetThawEras::get());
staking_account_details.update_stake_change_unlocking(amount, &thaw_at, &current_era)?;
Self::set_staking_account(staker, &staking_account_details);
Ok(())
Expand All @@ -842,8 +842,11 @@ impl<T: Config> Pallet<T> {
let mut to_msa_target = Self::get_target_for(staker, to_msa).unwrap_or_default();

if to_msa_target.amount.is_zero() {
// it's a new StakingTargetDetails record.
to_msa_target.staking_type = staking_type.clone();
} else {
// make sure they are not retargeting to a StakingTargetDetails with a different staking
// type, otherwise it could interfere with staking rewards.
ensure!(
to_msa_target.staking_type.eq(staking_type),
Error::<T>::CannotChangeStakingType
Expand Down
4 changes: 2 additions & 2 deletions pallets/capacity/src/tests/eras_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn start_new_era_if_needed_updates_reward_pool() {
);
system_run_to_block(8);

// TODO: Provider boost, after staking updates reward pool info
// TODO: Provider boost, after staking updates reward pool info #1699
// let staker = 10_000;
// let provider_msa: MessageSourceId = 1;
// let stake_amount = 600u64;
Expand All @@ -67,7 +67,7 @@ fn start_new_era_if_needed_updates_reward_pool() {
}
);

// TODO: after staking updates reward pool info
// TODO: after staking updates reward pool info #1699
// system_run_to_block(19);
// run_to_block(20);
});
Expand Down
2 changes: 1 addition & 1 deletion pallets/capacity/src/tests/unstaking_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sp_core::bounded::BoundedVec;
#[test]
fn unstake_happy_path() {
new_test_ext().execute_with(|| {
// TODO: ProviderBoost after unstake affects reward pool info
// TODO: ProviderBoost after unstake affects reward pool info #1699
let token_account = 200;
let target: MessageSourceId = 1;
let staking_amount = 100;
Expand Down
11 changes: 7 additions & 4 deletions pallets/capacity/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Types for the Capacity Pallet
use super::*;
use codec::{Decode, Encode, MaxEncodedLen};
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use frame_support::{
log::warn, BoundedVec, EqNoBound, PartialEqNoBound, RuntimeDebug, RuntimeDebugNoBound,
};
use scale_info::TypeInfo;
use sp_runtime::traits::{CheckedAdd, CheckedSub, Saturating, Zero};
use sp_runtime::traits::{AtLeast32BitUnsigned, CheckedAdd, CheckedSub, Saturating, Zero};

use common_primitives::capacity::StakingType;
#[cfg(any(feature = "runtime-benchmarks", test))]
Expand Down Expand Up @@ -342,7 +342,7 @@ pub trait StakingRewardsProvider<T: Config> {

/// Validate a payout claim for `accountId`, using `proof` and the provided `payload` StakingRewardClaim.
/// Returns whether the claim passes validation. Accounts must first pass `payoutEligible` test.
/// Errors::
/// Errors:
/// - NotAStakingAccount
/// - MaxUnlockingChunksExceeded
/// - All other conditions that would prevent a reward from being claimed return 'false'
Expand Down Expand Up @@ -371,7 +371,10 @@ pub trait StakingRewardsProvider<T: Config> {
TypeInfo,
MaxEncodedLen,
)]
pub struct RewardEraInfo<RewardEra, BlockNumber> {
pub struct RewardEraInfo<RewardEra, BlockNumber>
where
RewardEra: AtLeast32BitUnsigned + EncodeLike,
{
/// the index of this era
pub era_index: RewardEra,
/// the starting block of this era
Expand Down

0 comments on commit f4755d5

Please sign in to comment.