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 Apr 22, 2024
1 parent 5dc7287 commit defdb39
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 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)

Check warning on line 25 in pallets/capacity/src/migration/provider_boost_init.rs

View check run for this annotation

Codecov / codecov/patch

pallets/capacity/src/migration/provider_boost_init.rs#L18-L25

Added lines #L18 - L25 were not covered by tests
} else {
T::DbWeight::get().reads(1)

Check warning on line 27 in pallets/capacity/src/migration/provider_boost_init.rs

View check run for this annotation

Codecov / codecov/patch

pallets/capacity/src/migration/provider_boost_init.rs#L27

Added line #L27 was not covered by tests
}
}

Check warning on line 29 in pallets/capacity/src/migration/provider_boost_init.rs

View check run for this annotation

Codecov / codecov/patch

pallets/capacity/src/migration/provider_boost_init.rs#L29

Added line #L29 was not covered by tests

#[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(())
}
}

7 changes: 4 additions & 3 deletions runtime/frequency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,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 @@ -344,7 +345,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("frequency-testnet"),
impl_name: create_runtime_str!("frequency"),
authoring_version: 1,
spec_version: 74,
spec_version: 75,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -487,9 +488,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 defdb39

Please sign in to comment.