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 Jul 18, 2024
1 parent 7359a51 commit 5e6b298
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pallets/capacity/src/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

/// 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(())
}
}

8 changes: 5 additions & 3 deletions runtime/frequency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(pallet_schemas::migration::v4::MigrateToV4<Runtime>,),
(pallet_schemas::migration::v4::MigrateToV4<Runtime>,
pallet_capacity::migration::provider_boost_init::ProviderBoostInit<Runtime>,
),
>;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
Expand Down Expand Up @@ -515,9 +517,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 5e6b298

Please sign in to comment.