Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init channel commission cleared_commission #1527

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions pallets/channel-commission/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub use weights::WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod migrations;
mod mock;
mod tests;
pub mod weights;
Expand Down Expand Up @@ -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<T>(PhantomData<T>);

#[pallet::hooks]
Expand Down Expand Up @@ -485,6 +489,7 @@ pub mod pallet {
if let Some(commission_token) = commission_token_op {
// set the commission token
CommissionTokens::<T>::insert(vtoken, commission_token);
PeriodClearedCommissions::<T>::insert(vtoken, BalanceOf::<T>::zero());

// set VtokenIssuanceSnapshots for the vtoken
let issuance = T::MultiCurrency::total_issuance(vtoken);
Expand Down Expand Up @@ -825,6 +830,9 @@ impl<T: Config> Pallet<T> {
// get the cleared amount from the PeriodClearedCommissions storage
let cleared_commission = PeriodClearedCommissions::<T>::get(commission_token);

// Reset the PeriodClearedCommissions of commission_token to 0.
PeriodClearedCommissions::<T>::insert(commission_token, BalanceOf::<T>::zero());

// calculate the bifrost commission amount
let bifrost_commission = total_commission.saturating_sub(cleared_commission);

Expand Down Expand Up @@ -852,18 +860,6 @@ impl<T: Config> Pallet<T> {
});
}
});

// clear PeriodClearedCommissions
let res = PeriodClearedCommissions::<T>::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::<T>::iter().count() as u32,
limit: REMOVE_TOKEN_LIMIT,
executed_num,
});
}
}

pub(crate) fn calculate_mul_div_result(
Expand Down
19 changes: 19 additions & 0 deletions pallets/channel-commission/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

pub mod v1;
83 changes: 83 additions & 0 deletions pallets/channel-commission/src/migrations/v1.rs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

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<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
// Check the storage version
let onchain_version = Pallet::<T>::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::<T>::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::<T>::insert(commission_token, BalanceOf::<T>::zero());
});

// Update the storage version
StorageVersion::new(1).put::<Pallet<T>>();

// Return the consumed weight
let count = CommissionTokens::<T>::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<Vec<u8>, TryRuntimeError> {
let cnt = PeriodClearedCommissions::<T>::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<u8>) -> Result<(), TryRuntimeError> {
let new_count = PeriodClearedCommissions::<T>::iter().count();
let should_count = CommissionTokens::<T>::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(())
}
}
1 change: 1 addition & 0 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,7 @@ pub mod migrations {
pub type Unreleased = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
bifrost_channel_commission::migrations::v1::MigrateToV1<Runtime>,
);
}

Expand Down
1 change: 1 addition & 0 deletions runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,7 @@ pub mod migrations {
pub type Unreleased = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
bifrost_channel_commission::migrations::v1::MigrateToV1<Runtime>,
);
}

Expand Down