From 909900fd90c22f7415daf0acb887e55f71974fcb Mon Sep 17 00:00:00 2001 From: yooml Date: Mon, 27 Jan 2025 15:10:46 +0800 Subject: [PATCH] Add async migration code for pallet fee-share and system-staking (#1614) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 🎸 fee-share and system-staking * fix: 🐛 clippy --- pallets/fee-share/Cargo.toml | 2 +- pallets/fee-share/src/lib.rs | 1 + pallets/fee-share/src/migration.rs | 76 +++++++++++++++++++++++++ pallets/system-staking/src/lib.rs | 3 +- pallets/system-staking/src/migration.rs | 73 ++++++++++++++++++++++++ runtime/bifrost-kusama/src/lib.rs | 3 +- runtime/bifrost-polkadot/src/lib.rs | 4 +- 7 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 pallets/fee-share/src/migration.rs create mode 100644 pallets/system-staking/src/migration.rs diff --git a/pallets/fee-share/Cargo.toml b/pallets/fee-share/Cargo.toml index b4177eb5fa..532ff86c4b 100644 --- a/pallets/fee-share/Cargo.toml +++ b/pallets/fee-share/Cargo.toml @@ -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 } @@ -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 } diff --git a/pallets/fee-share/src/lib.rs b/pallets/fee-share/src/lib.rs index 22f4634941..7a6baa645d 100644 --- a/pallets/fee-share/src/lib.rs +++ b/pallets/fee-share/src/lib.rs @@ -27,6 +27,7 @@ mod tests; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; +pub mod migration; pub mod weights; diff --git a/pallets/fee-share/src/migration.rs b/pallets/fee-share/src/migration.rs new file mode 100644 index 0000000000..85e63566f2 --- /dev/null +++ b/pallets/fee-share/src/migration.rs @@ -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 . + +#![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() -> Weight { + let (_era_length, next_era) = AutoEra::::get(); + AutoEra::::set((14400u32.into(), next_era)); + + DollarStandardInfos::::iter().for_each(|(distribution_id, mut info)| { + info.interval = 432000u32.into(); + DollarStandardInfos::::insert(distribution_id, info); + }); + T::DbWeight::get().reads(1) + T::DbWeight::get().writes(1) +} + +pub struct FeeShareOnRuntimeUpgrade(PhantomData); +impl OnRuntimeUpgrade for FeeShareOnRuntimeUpgrade { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + #[allow(unused_imports)] + use frame_support::{migration, Identity}; + log::info!("Bifrost `pre_upgrade`..."); + + let (era_length, _next_era) = AutoEra::::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::(); + + log::info!("Bifrost `on_runtime_upgrade finished`"); + + weight + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_: sp_std::prelude::Vec) -> Result<(), sp_runtime::DispatchError> { + #[allow(unused_imports)] + use frame_support::{migration, Identity}; + log::info!("Bifrost `post_upgrade`..."); + + let (era_length, _next_era) = AutoEra::::get(); + log::info!("New era_length is {:?}", era_length); + assert_eq!(era_length, 14400u32.into()); + DollarStandardInfos::::iter().for_each(|(_distribution_id, info)| { + assert_eq!(info.interval, 432000u32.into()); + }); + + Ok(()) + } +} diff --git a/pallets/system-staking/src/lib.rs b/pallets/system-staking/src/lib.rs index 1121a184bc..0df5562794 100644 --- a/pallets/system-staking/src/lib.rs +++ b/pallets/system-staking/src/lib.rs @@ -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; @@ -321,7 +322,7 @@ pub mod pallet { let mut round = if let Some(round) = >::get() { round } else { - // BlocksPerRound == 1500 , 5 hours + // BlocksPerRound == 3000 , 5 hours RoundInfo::new(0u32, 0u32.into(), T::BlocksPerRound::get()) }; diff --git a/pallets/system-staking/src/migration.rs b/pallets/system-staking/src/migration.rs new file mode 100644 index 0000000000..478610c058 --- /dev/null +++ b/pallets/system-staking/src/migration.rs @@ -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 . + +#![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() -> Weight { + if let Some(mut round) = >::get() { + round.length = 3000u32; + >::put(round); + } + + T::DbWeight::get().reads(1) + T::DbWeight::get().writes(1) +} + +pub struct SystemStakingOnRuntimeUpgrade(PhantomData); +impl OnRuntimeUpgrade for SystemStakingOnRuntimeUpgrade { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + #[allow(unused_imports)] + use frame_support::{migration, Identity}; + log::info!("Bifrost `pre_upgrade`..."); + + if let Some(round) = >::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::(); + + log::info!("Bifrost `on_runtime_upgrade finished`"); + + weight + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_: sp_std::prelude::Vec) -> Result<(), sp_runtime::DispatchError> { + #[allow(unused_imports)] + use frame_support::{migration, Identity}; + log::info!("Bifrost `post_upgrade`..."); + + if let Some(round) = >::get() { + log::info!("New round is {:?}", round); + assert_eq!(round.length, 3000u32); + } + + Ok(()) + } +} diff --git a/runtime/bifrost-kusama/src/lib.rs b/runtime/bifrost-kusama/src/lib.rs index f925272208..d90cb2a061 100644 --- a/runtime/bifrost-kusama/src/lib.rs +++ b/runtime/bifrost-kusama/src/lib.rs @@ -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; } @@ -2029,6 +2029,7 @@ pub mod migrations { pub type Unreleased = ( // permanent migration, do not remove pallet_xcm::migration::MigrateToLatestXcmVersion, + bifrost_system_staking::migration::SystemStakingOnRuntimeUpgrade, ); } diff --git a/runtime/bifrost-polkadot/src/lib.rs b/runtime/bifrost-polkadot/src/lib.rs index d0d457a75d..bccfa55074 100644 --- a/runtime/bifrost-polkadot/src/lib.rs +++ b/runtime/bifrost-polkadot/src/lib.rs @@ -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(); @@ -1947,6 +1947,8 @@ pub mod migrations { pub type Unreleased = ( // permanent migration, do not remove pallet_xcm::migration::MigrateToLatestXcmVersion, + bifrost_system_staking::migration::SystemStakingOnRuntimeUpgrade, + bifrost_fee_share::migration::FeeShareOnRuntimeUpgrade, ); }