From 1d957bc66a8c1e69938eac7b87c80946ebd7161c Mon Sep 17 00:00:00 2001 From: SunTiebing <87381708+SunTiebing@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:34:09 +0800 Subject: [PATCH] init channel commission cleared_commission (#1527) * init channel commission cleared_commission * normalize format * fix clippy --- pallets/channel-commission/src/lib.rs | 20 ++--- .../channel-commission/src/migrations/mod.rs | 19 +++++ .../channel-commission/src/migrations/v1.rs | 83 +++++++++++++++++++ runtime/bifrost-kusama/src/lib.rs | 1 + runtime/bifrost-polkadot/src/lib.rs | 1 + 5 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 pallets/channel-commission/src/migrations/mod.rs create mode 100644 pallets/channel-commission/src/migrations/v1.rs diff --git a/pallets/channel-commission/src/lib.rs b/pallets/channel-commission/src/lib.rs index d2343f2ba..c28dc75c6 100644 --- a/pallets/channel-commission/src/lib.rs +++ b/pallets/channel-commission/src/lib.rs @@ -38,6 +38,7 @@ pub use weights::WeightInfo; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; +pub mod migrations; mod mock; mod tests; pub mod weights; @@ -289,8 +290,11 @@ pub mod pallet { ValueQuery, >; + const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + #[pallet::pallet] #[pallet::without_storage_info] + #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(PhantomData); #[pallet::hooks] @@ -485,6 +489,7 @@ pub mod pallet { if let Some(commission_token) = commission_token_op { // set the commission token CommissionTokens::::insert(vtoken, commission_token); + PeriodClearedCommissions::::insert(vtoken, BalanceOf::::zero()); // set VtokenIssuanceSnapshots for the vtoken let issuance = T::MultiCurrency::total_issuance(vtoken); @@ -825,6 +830,9 @@ impl Pallet { // get the cleared amount from the PeriodClearedCommissions storage let cleared_commission = PeriodClearedCommissions::::get(commission_token); + // Reset the PeriodClearedCommissions of commission_token to 0. + PeriodClearedCommissions::::insert(commission_token, BalanceOf::::zero()); + // calculate the bifrost commission amount let bifrost_commission = total_commission.saturating_sub(cleared_commission); @@ -852,18 +860,6 @@ impl Pallet { }); } }); - - // clear PeriodClearedCommissions - let res = PeriodClearedCommissions::::clear(REMOVE_TOKEN_LIMIT, None); - let executed_num = res.backend; - if let Err(_) = Self::check_removed_all(res) { - log::error!("The removal process was not complete; cursor is still present."); - Self::deposit_event(Event::RemovalNotCompleteError { - target_num: PeriodClearedCommissions::::iter().count() as u32, - limit: REMOVE_TOKEN_LIMIT, - executed_num, - }); - } } pub(crate) fn calculate_mul_div_result( diff --git a/pallets/channel-commission/src/migrations/mod.rs b/pallets/channel-commission/src/migrations/mod.rs new file mode 100644 index 000000000..379d174c5 --- /dev/null +++ b/pallets/channel-commission/src/migrations/mod.rs @@ -0,0 +1,19 @@ +// 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 . + +pub mod v1; diff --git a/pallets/channel-commission/src/migrations/v1.rs b/pallets/channel-commission/src/migrations/v1.rs new file mode 100644 index 000000000..6fe6681be --- /dev/null +++ b/pallets/channel-commission/src/migrations/v1.rs @@ -0,0 +1,83 @@ +// 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 . + +use crate::*; +use alloc::vec::Vec; +use frame_support::traits::OnRuntimeUpgrade; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; + +const LOG_TARGET: &str = "channel-commission::migration"; + +pub struct MigrateToV1(sp_std::marker::PhantomData); +impl OnRuntimeUpgrade for MigrateToV1 { + fn on_runtime_upgrade() -> frame_support::weights::Weight { + // Check the storage version + let onchain_version = Pallet::::on_chain_storage_version(); + if onchain_version < 1 { + // Transform storage values + // We transform the storage values from the old into the new format. + log::info!(target: LOG_TARGET, "Start to migrate PeriodClearedCommissions storage..."); + CommissionTokens::::iter_values().for_each(|commission_token| { + log::info!(target: LOG_TARGET, "Init PeriodClearedCommissions for {:?}...", commission_token); + // Init the PeriodClearedCommissions of commission_token to 0. + PeriodClearedCommissions::::insert(commission_token, BalanceOf::::zero()); + }); + + // Update the storage version + StorageVersion::new(1).put::>(); + + // Return the consumed weight + let count = CommissionTokens::::iter().count(); + Weight::from( + T::DbWeight::get().reads_writes(count as u64 + count as u64 + 1, count as u64 + 1), + ) + } else { + // We don't do anything here. + Weight::zero() + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, TryRuntimeError> { + let cnt = PeriodClearedCommissions::::iter().count(); + // print out the pre-migrate storage count + log::info!(target: LOG_TARGET, "PeriodClearedCommissions pre-migrate storage count: {:?}", cnt); + Ok((cnt as u64).encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_cnt: Vec) -> Result<(), TryRuntimeError> { + let new_count = PeriodClearedCommissions::::iter().count(); + let should_count = CommissionTokens::::iter().count(); + + // print out the post-migrate storage count + log::info!( + target: LOG_TARGET, + "PeriodClearedCommissions post-migrate storage count: {:?}", + new_count + ); + + ensure!( + new_count as u64 == should_count as u64, + "Post-migration storage count does not match pre-migration count" + ); + + Ok(()) + } +} diff --git a/runtime/bifrost-kusama/src/lib.rs b/runtime/bifrost-kusama/src/lib.rs index 092b0a06a..0199f63c0 100644 --- a/runtime/bifrost-kusama/src/lib.rs +++ b/runtime/bifrost-kusama/src/lib.rs @@ -2005,6 +2005,7 @@ pub mod migrations { pub type Unreleased = ( // permanent migration, do not remove pallet_xcm::migration::MigrateToLatestXcmVersion, + bifrost_channel_commission::migrations::v1::MigrateToV1, ); } diff --git a/runtime/bifrost-polkadot/src/lib.rs b/runtime/bifrost-polkadot/src/lib.rs index e26b248a1..72638b573 100644 --- a/runtime/bifrost-polkadot/src/lib.rs +++ b/runtime/bifrost-polkadot/src/lib.rs @@ -1902,6 +1902,7 @@ pub mod migrations { pub type Unreleased = ( // permanent migration, do not remove pallet_xcm::migration::MigrateToLatestXcmVersion, + bifrost_channel_commission::migrations::v1::MigrateToV1, ); }