Skip to content

Commit

Permalink
storage migration
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Jul 24, 2024
1 parent 24a09ae commit 5b5affa
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub mod pallet {
use super::*;

/// The current storage version.
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(7);
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(8);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down
90 changes: 90 additions & 0 deletions pallets/dapp-staking-v3/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,102 @@ pub mod versioned_migrations {
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;

/// Migration V7 to V8 wrapped in a [`frame_support::migrations::VersionedMigration`], ensuring
/// the migration is only performed when on-chain version is 7.
pub type V7ToV8<T> = frame_support::migrations::VersionedMigration<
7,
8,
v8::VersionMigrateV7ToV8<T>,
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;
}

// TierThreshold as percentage of the total issuance
mod v8 {
use super::*;
use crate::migration::v7::TiersConfiguration as TiersConfigurationV7;

pub struct VersionMigrateV7ToV8<T>(PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for VersionMigrateV7ToV8<T> {
fn on_runtime_upgrade() -> Weight {
let _ = TierConfig::<T>::translate::<
TiersConfigurationV7<T::NumberOfTiers, T::TierSlots, T::BaseNativeCurrencyPrice>,
_,
>(|maybe_old_config| match maybe_old_config {
Some(old_config) => {
let new_tier_threshold_values = extract_threshold_values(
old_config.tier_thresholds,
T::Currency::total_issuance(),
);

Some(TiersConfiguration {
slots_per_tier: old_config.slots_per_tier,
reward_portion: old_config.reward_portion,
tier_threshold_values: new_tier_threshold_values,
_phantom: Default::default(),
})
}
_ => None,
});

T::DbWeight::get().reads_writes(1, 1)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_data: Vec<u8>) -> Result<(), TryRuntimeError> {
ensure!(
Pallet::<T>::on_chain_storage_version() >= 8,
"dapp-staking-v3::migration::v8: wrong storage version"
);
Ok(())
}
}
}

/// Translate DAppTiers to include rank rewards.
mod v7 {
use super::*;
use crate::migration::v6::DAppTierRewards as DAppTierRewardsV6;
use astar_primitives::dapp_staking::TierSlots as TierSlotsFunc;

/// Configuration of dApp tiers.
#[derive(
Encode,
Decode,
MaxEncodedLen,
RuntimeDebugNoBound,
PartialEqNoBound,
EqNoBound,
CloneNoBound,
TypeInfo,
)]
#[scale_info(skip_type_params(NT, T, P))]
pub struct TiersConfiguration<NT: Get<u32>, T: TierSlotsFunc, P: Get<FixedU128>> {
/// Total number of slots.
#[codec(compact)]
pub number_of_slots: u16,
/// Number of slots per tier.
/// First entry refers to the first tier, and so on.
pub slots_per_tier: BoundedVec<u16, NT>,
/// Reward distribution per tier, in percentage.
/// First entry refers to the first tier, and so on.
/// The sum of all values must be exactly equal to 1.
pub reward_portion: BoundedVec<Permill, NT>,
/// Requirements for entry into each tier.
/// First entry refers to the first tier, and so on.
pub tier_thresholds: BoundedVec<TierThreshold, NT>,
/// Phantom data to keep track of the tier slots function.
#[codec(skip)]
pub(crate) _phantom: PhantomData<(T, P)>,
}

pub struct VersionMigrateV6ToV7<T>(PhantomData<T>);

Expand Down
42 changes: 36 additions & 6 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use frame_support::{
parameter_types,
traits::{
AsEnsureOriginWithArg, ConstBool, ConstU32, Contains, Currency, FindAuthor, Get, Imbalance,
InstanceFilter, Nothing, OnFinalize, OnUnbalanced, Randomness, WithdrawReasons,
InstanceFilter, Nothing, OnFinalize, OnRuntimeUpgrade, OnUnbalanced, Randomness,
WithdrawReasons,
},
weights::{
constants::{
Expand All @@ -40,7 +41,7 @@ use frame_support::{
ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
},
ConsensusEngineId, PalletId,
BoundedVec, ConsensusEngineId, PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
Expand Down Expand Up @@ -1261,16 +1262,45 @@ pub type Executive = frame_executive::Executive<
Migrations,
>;

pub struct StaticTierParamsMigration;
impl OnRuntimeUpgrade for StaticTierParamsMigration {
fn on_runtime_upgrade() -> Weight {
let tier_thresholds = BoundedVec::try_from(vec![
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(35_700_000), // 3.57%
minimum_required_percentage: Perbill::from_parts(23_800_000), // 2.38%
},
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(8_900_000), // 0.89%
minimum_required_percentage: Perbill::from_parts(6_000_000), // 0.6%
},
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(23_800_000), // 2.38%
minimum_required_percentage: Perbill::from_parts(17_900_000), // 1.79%
},
TierThreshold::FixedPercentage {
required_percentage: Perbill::from_parts(6_000_000), // 0.6%
},
])
.unwrap();

pallet_dapp_staking_v3::StaticTierParams::<Runtime>::mutate(|config| {
config.tier_thresholds = tier_thresholds;
});

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
}
}

/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = (
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
// XCM V3 -> V4
pallet_xc_asset_config::migrations::versioned::V2ToV3<Runtime>,
pallet_identity::migration::versioned::V0ToV1<Runtime, 250>,
// dapp-staking dyn tier threshold migrations
pallet_dapp_staking_v3::migration::versioned_migrations::V7ToV8<Runtime>,
StaticTierParamsMigration, // runtime specific
);

type EventRecord = frame_system::EventRecord<
Expand Down
41 changes: 35 additions & 6 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use frame_support::{
tokens::{PayFromAccount, UnityAssetBalanceConversion},
AsEnsureOriginWithArg, ConstU128, ConstU32, Contains, Currency, EqualPrivilegeOnly,
FindAuthor, Get, Imbalance, InstanceFilter, LinearStoragePrice, Nothing, OnFinalize,
OnUnbalanced, WithdrawReasons,
OnRuntimeUpgrade, OnUnbalanced, WithdrawReasons,
},
weights::{
constants::{
Expand All @@ -43,7 +43,7 @@ use frame_support::{
ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
},
ConsensusEngineId, PalletId,
BoundedVec, ConsensusEngineId, PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
Expand Down Expand Up @@ -1616,17 +1616,46 @@ pub type Executive = frame_executive::Executive<
Migrations,
>;

pub struct StaticTierParamsMigration;
impl OnRuntimeUpgrade for StaticTierParamsMigration {
fn on_runtime_upgrade() -> Weight {
let tier_thresholds = BoundedVec::try_from(vec![
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(20_000), // 0.0020%
minimum_required_percentage: Perbill::from_parts(17_000), // 0.0017%
},
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(10_000), // 0.0010%
minimum_required_percentage: Perbill::from_parts(6_800), // 0.00068%
},
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(5_400), // 0.00054%
minimum_required_percentage: Perbill::from_parts(3_400), // 0.00034%
},
TierThreshold::FixedPercentage {
required_percentage: Perbill::from_parts(1_400), // 0.00014%
},
])
.unwrap();

pallet_dapp_staking_v3::StaticTierParams::<Runtime>::mutate(|config| {
config.tier_thresholds = tier_thresholds;
});

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
}
}

/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = (
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
// XCM V3 -> V4
pallet_xc_asset_config::migrations::versioned::V2ToV3<Runtime>,
pallet_identity::migration::versioned::V0ToV1<Runtime, 250>,
pallet_unified_accounts::migration::ClearCorruptedUnifiedMappings<Runtime>,
// dapp-staking dyn tier threshold migrations
pallet_dapp_staking_v3::migration::versioned_migrations::V7ToV8<Runtime>,
StaticTierParamsMigration, // runtime specific
);

type EventRecord = frame_system::EventRecord<
Expand Down
40 changes: 35 additions & 5 deletions runtime/shiden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use frame_support::{
parameter_types,
traits::{
AsEnsureOriginWithArg, ConstU32, Contains, Currency, FindAuthor, Get, Imbalance,
InstanceFilter, Nothing, OnFinalize, OnUnbalanced, WithdrawReasons,
InstanceFilter, Nothing, OnFinalize, OnRuntimeUpgrade, OnUnbalanced, WithdrawReasons,
},
weights::{
constants::{
Expand All @@ -40,7 +40,7 @@ use frame_support::{
ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
},
ConsensusEngineId, PalletId,
BoundedVec, ConsensusEngineId, PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
Expand Down Expand Up @@ -1263,16 +1263,46 @@ pub type Executive = frame_executive::Executive<
Migrations,
>;

pub struct StaticTierParamsMigration;
impl OnRuntimeUpgrade for StaticTierParamsMigration {
fn on_runtime_upgrade() -> Weight {
let tier_thresholds = BoundedVec::try_from(vec![
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(35_700_000), // 3.57%
minimum_required_percentage: Perbill::from_parts(23_800_000), // 2.38%
},
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(8_900_000), // 0.89%
minimum_required_percentage: Perbill::from_parts(6_000_000), // 0.6%
},
TierThreshold::DynamicPercentage {
current_percentage: Perbill::from_parts(23_800_000), // 2.38%
minimum_required_percentage: Perbill::from_parts(17_900_000), // 1.79%
},
TierThreshold::FixedPercentage {
required_percentage: Perbill::from_parts(6_000_000), // 0.6%
},
])
.unwrap();

pallet_dapp_staking_v3::StaticTierParams::<Runtime>::mutate(|config| {
config.tier_thresholds = tier_thresholds;
});

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
}
}

/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = (
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
// XCM V3 -> V4
pallet_xc_asset_config::migrations::versioned::V2ToV3<Runtime>,
pallet_identity::migration::versioned::V0ToV1<Runtime, 250>,
// dapp-staking dyn tier threshold migrations
pallet_dapp_staking_v3::migration::versioned_migrations::V7ToV8<Runtime>,
StaticTierParamsMigration, // runtime specific
);

type EventRecord = frame_system::EventRecord<
Expand Down

0 comments on commit 5b5affa

Please sign in to comment.