Skip to content

Commit

Permalink
Add async migration code for pallet fee-share and system-staking (#1614)
Browse files Browse the repository at this point in the history
* feat: 🎸 fee-share and system-staking

* fix: 🐛 clippy
  • Loading branch information
yooml authored Jan 27, 2025
1 parent 2f288f0 commit 909900f
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pallets/fee-share/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ parity-scale-codec = { workspace = true, features = ["derive"] }
scale-info = { workspace = true, features = ["derive"] }
sp-arithmetic = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
xcm = { workspace = true }
zenlink-protocol = { workspace = true }
Expand All @@ -40,7 +41,6 @@ pallet-prices = { workspace = true }
pallet-xcm = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
xcm-builder = { workspace = true }
xcm-executor = { workspace = true }

Expand Down
1 change: 1 addition & 0 deletions pallets/fee-share/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod migration;

pub mod weights;

Expand Down
76 changes: 76 additions & 0 deletions pallets/fee-share/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// This file is part of Bifrost.

// Copyright (C) Liebi Technologies PTE. LTD.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

use super::{AutoEra, Config, DollarStandardInfos, Weight};
use frame_support::traits::{Get, OnRuntimeUpgrade};
use sp_std::marker::PhantomData;

pub fn update_for_async<T: Config>() -> Weight {
let (_era_length, next_era) = AutoEra::<T>::get();
AutoEra::<T>::set((14400u32.into(), next_era));

DollarStandardInfos::<T>::iter().for_each(|(distribution_id, mut info)| {
info.interval = 432000u32.into();
DollarStandardInfos::<T>::insert(distribution_id, info);
});
T::DbWeight::get().reads(1) + T::DbWeight::get().writes(1)
}

pub struct FeeShareOnRuntimeUpgrade<T>(PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for FeeShareOnRuntimeUpgrade<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::prelude::Vec<u8>, sp_runtime::DispatchError> {
#[allow(unused_imports)]
use frame_support::{migration, Identity};
log::info!("Bifrost `pre_upgrade`...");

let (era_length, _next_era) = AutoEra::<T>::get();
log::info!("Old era_length is {:?}", era_length);
assert_eq!(era_length, 7200u32.into());

Ok(sp_std::prelude::Vec::new())
}

fn on_runtime_upgrade() -> Weight {
log::info!("Bifrost `on_runtime_upgrade`...");

let weight = update_for_async::<T>();

log::info!("Bifrost `on_runtime_upgrade finished`");

weight
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: sp_std::prelude::Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
#[allow(unused_imports)]
use frame_support::{migration, Identity};
log::info!("Bifrost `post_upgrade`...");

let (era_length, _next_era) = AutoEra::<T>::get();
log::info!("New era_length is {:?}", era_length);
assert_eq!(era_length, 14400u32.into());
DollarStandardInfos::<T>::iter().for_each(|(_distribution_id, info)| {
assert_eq!(info.interval, 432000u32.into());
});

Ok(())
}
}
3 changes: 2 additions & 1 deletion pallets/system-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
pub mod types;
pub mod weights;
pub use weights::WeightInfo;
pub mod migration;

use bifrost_primitives::{CurrencyId, FarmingInfo, PoolId, VtokenMintingInterface};
pub use frame_support::weights::Weight;
Expand Down Expand Up @@ -321,7 +322,7 @@ pub mod pallet {
let mut round = if let Some(round) = <Round<T>>::get() {
round
} else {
// BlocksPerRound == 1500 , 5 hours
// BlocksPerRound == 3000 , 5 hours
RoundInfo::new(0u32, 0u32.into(), T::BlocksPerRound::get())
};

Expand Down
73 changes: 73 additions & 0 deletions pallets/system-staking/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// This file is part of Bifrost.

// Copyright (C) Liebi Technologies PTE. LTD.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

use super::{Config, Round, Weight};
use frame_support::traits::{Get, OnRuntimeUpgrade};
use sp_std::marker::PhantomData;

pub fn update_for_async<T: Config>() -> Weight {
if let Some(mut round) = <Round<T>>::get() {
round.length = 3000u32;
<Round<T>>::put(round);
}

T::DbWeight::get().reads(1) + T::DbWeight::get().writes(1)
}

pub struct SystemStakingOnRuntimeUpgrade<T>(PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for SystemStakingOnRuntimeUpgrade<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::prelude::Vec<u8>, sp_runtime::DispatchError> {
#[allow(unused_imports)]
use frame_support::{migration, Identity};
log::info!("Bifrost `pre_upgrade`...");

if let Some(round) = <Round<T>>::get() {
log::info!("Old round is {:?}", round);
assert_eq!(round.length, 1500u32);
}

Ok(sp_std::prelude::Vec::new())
}

fn on_runtime_upgrade() -> Weight {
log::info!("Bifrost `on_runtime_upgrade`...");

let weight = update_for_async::<T>();

log::info!("Bifrost `on_runtime_upgrade finished`");

weight
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: sp_std::prelude::Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
#[allow(unused_imports)]
use frame_support::{migration, Identity};
log::info!("Bifrost `post_upgrade`...");

if let Some(round) = <Round<T>>::get() {
log::info!("New round is {:?}", round);
assert_eq!(round.length, 3000u32);
}

Ok(())
}
}
3 changes: 2 additions & 1 deletion runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ impl bifrost_farming::Config for Runtime {
}

parameter_types! {
pub const BlocksPerRound: u32 = prod_or_fast!(1500, 50);
pub const BlocksPerRound: u32 = prod_or_fast!(3000, 50);
pub const MaxTokenLen: u32 = 500;
pub const MaxFarmingPoolIdLen: u32 = 100;
}
Expand Down Expand Up @@ -2029,6 +2029,7 @@ pub mod migrations {
pub type Unreleased = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
bifrost_system_staking::migration::SystemStakingOnRuntimeUpgrade<Runtime>,
);
}

Expand Down
4 changes: 3 additions & 1 deletion runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ impl bifrost_farming::Config for Runtime {
}

parameter_types! {
pub const BlocksPerRound: u32 = prod_or_fast!(1500, 50);
pub const BlocksPerRound: u32 = prod_or_fast!(3000, 50);
pub const MaxTokenLen: u32 = 500;
pub const MaxFarmingPoolIdLen: u32 = 100;
pub BenefitReceivingAccount: AccountId = FeeSharePalletId::get().into_account_truncating();
Expand Down Expand Up @@ -1947,6 +1947,8 @@ pub mod migrations {
pub type Unreleased = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
bifrost_system_staking::migration::SystemStakingOnRuntimeUpgrade<Runtime>,
bifrost_fee_share::migration::FeeShareOnRuntimeUpgrade<Runtime>,
);
}

Expand Down

0 comments on commit 909900f

Please sign in to comment.