Skip to content

Commit

Permalink
initialize storage for ProviderBoost on runtime upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonwells committed May 3, 2024
1 parent 19a2786 commit 0792a76
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pallets/capacity/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
pub mod v2;
/// migrations to v3
pub mod v3;
/// initial values for ProviderBoost storage
pub mod provider_boost_init;
57 changes: 57 additions & 0 deletions pallets/capacity/src/migration/provider_boost_init.rs
Original file line number Diff line number Diff line change
@@ -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<T: Config>(sp_std::marker::PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for ProviderBoostInit<T> {
fn on_runtime_upgrade() -> Weight {
let current_era_info = CurrentEraInfo::<T>::get();
if current_era_info.eq(&RewardEraInfo::default()) { // 1r
let current_block = frame_system::Pallet::<T>::block_number(); // 1r
let era_index: T::RewardEra = 0u32.into();
CurrentEraInfo::<T>::set(RewardEraInfo { era_index, started_at: current_block, }); // 1w
StakingRewardPool::<T>::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<Vec<u8>, TryRuntimeError> {
if CurrentEraInfo::<T>::exists() {
log::info!("CurrentEraInfo exists; initialization should be skipped.");
} else {
log::info!("CurrentEraInfo not found. Initialization should proceed.");
}
if StakingRewardPool::<T>::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<u8>) -> Result<(), TryRuntimeError> {
assert!(CurrentEraInfo::<T>::exists());
let current_block = frame_system::Pallet::<T>::block_number();
let info = CurrentEraInfo::<T>::get() ;
assert_eq!(info.started_at, current_block);
log::info!("CurrentEraInfo.started_at is set to {:?}.", info.started_at);
assert_eq!(StakingRewardPool::<T>::iter().count(), 1);
Ok(())
}
}

5 changes: 3 additions & 2 deletions runtime/frequency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ pub type Executive = frame_executive::Executive<
Runtime,
pallet_balances::Pallet<Runtime>,
>,
pallet_capacity::migration::provider_boost_init::ProviderBoostInit<Runtime>,
MigratePalletsCurrentStorage<Runtime>,
),
>;
Expand Down Expand Up @@ -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>;
}
Expand Down

0 comments on commit 0792a76

Please sign in to comment.