diff --git a/pallets/capacity/src/migration/mod.rs b/pallets/capacity/src/migration/mod.rs index 937d239372..0ea1e97d0a 100644 --- a/pallets/capacity/src/migration/mod.rs +++ b/pallets/capacity/src/migration/mod.rs @@ -2,3 +2,5 @@ pub mod v2; /// migrations to v3 pub mod v3; +/// initial values for ProviderBoost storage +pub mod provider_boost_init; diff --git a/pallets/capacity/src/migration/provider_boost_init.rs b/pallets/capacity/src/migration/provider_boost_init.rs new file mode 100644 index 0000000000..b3687b6fd9 --- /dev/null +++ b/pallets/capacity/src/migration/provider_boost_init.rs @@ -0,0 +1,57 @@ +use crate::{Config, CurrentEraInfo, RewardEraInfo, RewardPoolInfo, StakingRewardPool}; +use frame_support::{ + pallet_prelude::Weight, + traits::OnRuntimeUpgrade +}; +use frame_support::{ + traits::Get, +}; +use sp_runtime::TryRuntimeError; + +#[cfg(feature = "try-runtime")] +use sp_std::vec::Vec; + +/// Initialization during runtime upgrade for Provider Boost storage +pub struct ProviderBoostInit(sp_std::marker::PhantomData); + +impl OnRuntimeUpgrade for ProviderBoostInit { + fn on_runtime_upgrade() -> Weight { + let current_era_info = CurrentEraInfo::::get(); + if current_era_info.eq(&RewardEraInfo::default()) { // 1r + let current_block = frame_system::Pallet::::block_number(); // 1r + let era_index: T::RewardEra = 0u32.into(); + CurrentEraInfo::::set(RewardEraInfo { era_index, started_at: current_block, }); // 1w + StakingRewardPool::::insert(era_index, RewardPoolInfo::default()); // 1w + T::DbWeight::get().reads_writes(2, 2) + } else { + T::DbWeight::get().reads(1) + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, TryRuntimeError> { + if CurrentEraInfo::::exists() { + log::info!("CurrentEraInfo exists; initialization should be skipped."); + } else { + log::info!("CurrentEraInfo not found. Initialization should proceed."); + } + if StakingRewardPool::::iter().count() == 0usize { + log::info!("StakingRewardPool will be updated with Era 0"); + } else { + log::info!("StakingRewardPool has already been initialized.") + } + Ok(Vec::default()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { + assert!(CurrentEraInfo::::exists()); + let current_block = frame_system::Pallet::::block_number(); + let info = CurrentEraInfo::::get() ; + assert_eq!(info.started_at, current_block); + log::info!("CurrentEraInfo.started_at is set to {:?}.", info.started_at); + assert_eq!(StakingRewardPool::::iter().count(), 1); + Ok(()) + } +} + diff --git a/runtime/frequency/src/lib.rs b/runtime/frequency/src/lib.rs index 82c77e0c85..eca41db8fd 100644 --- a/runtime/frequency/src/lib.rs +++ b/runtime/frequency/src/lib.rs @@ -294,6 +294,7 @@ pub type Executive = frame_executive::Executive< Runtime, pallet_balances::Pallet, >, + pallet_capacity::migration::provider_boost_init::ProviderBoostInit, MigratePalletsCurrentStorage, ), >; @@ -555,9 +556,9 @@ impl pallet_capacity::Config for Runtime { type EpochNumber = u32; type CapacityPerToken = CapacityPerToken; type RuntimeFreezeReason = RuntimeFreezeReason; - type RewardEra = RewardEra; + type RewardEra = u32; type EraLength = ConstU32<{ 14 * DAYS }>; - type StakingRewardsPastErasMax = ConstU32<26u32>; // 1 year + type StakingRewardsPastErasMax = ConstU32<30u32>; type RewardsProvider = Capacity; type MaxRetargetsPerRewardEra = ConstU32<16>; }