API for registering inactive funds (#12813)

* API for registering inactive funds

* Build fixes.

* Update frame/treasury/src/lib.rs

* Fix

* Fixes

* Fixes
This commit is contained in:
Gavin Wood
2022-12-03 09:14:47 +01:00
committed by GitHub
parent 3af87703de
commit d3fc468ad0
6 changed files with 151 additions and 1 deletions
+32 -1
View File
@@ -156,6 +156,7 @@
#[macro_use]
mod tests;
mod benchmarking;
pub mod migration;
mod tests_composite;
mod tests_local;
#[cfg(test)]
@@ -497,6 +498,13 @@ pub mod pallet {
#[pallet::whitelist_storage]
pub type TotalIssuance<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Balance, ValueQuery>;
/// The total units of outstanding deactivated balance in the system.
#[pallet::storage]
#[pallet::getter(fn inactive_issuance)]
#[pallet::whitelist_storage]
pub type InactiveIssuance<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::Balance, ValueQuery>;
/// The Balances pallet example of storing the balance of an account.
///
/// # Example
@@ -1067,6 +1075,9 @@ impl<T: Config<I>, I: 'static> fungible::Inspect<T::AccountId> for Pallet<T, I>
fn total_issuance() -> Self::Balance {
TotalIssuance::<T, I>::get()
}
fn active_issuance() -> Self::Balance {
TotalIssuance::<T, I>::get().saturating_sub(InactiveIssuance::<T, I>::get())
}
fn minimum_balance() -> Self::Balance {
T::ExistentialDeposit::get()
}
@@ -1145,6 +1156,14 @@ impl<T: Config<I>, I: 'static> fungible::Transfer<T::AccountId> for Pallet<T, I>
let er = if keep_alive { KeepAlive } else { AllowDeath };
<Self as Currency<T::AccountId>>::transfer(source, dest, amount, er).map(|_| amount)
}
fn deactivate(amount: Self::Balance) {
InactiveIssuance::<T, I>::mutate(|b| b.saturating_accrue(amount));
}
fn reactivate(amount: Self::Balance) {
InactiveIssuance::<T, I>::mutate(|b| b.saturating_reduce(amount));
}
}
impl<T: Config<I>, I: 'static> fungible::Unbalanced<T::AccountId> for Pallet<T, I> {
@@ -1418,7 +1437,19 @@ where
}
fn total_issuance() -> Self::Balance {
<TotalIssuance<T, I>>::get()
TotalIssuance::<T, I>::get()
}
fn active_issuance() -> Self::Balance {
<Self as fungible::Inspect<T::AccountId>>::active_issuance()
}
fn deactivate(amount: Self::Balance) {
<Self as fungible::Transfer<T::AccountId>>::deactivate(amount);
}
fn reactivate(amount: Self::Balance) {
<Self as fungible::Transfer<T::AccountId>>::reactivate(amount);
}
fn minimum_balance() -> Self::Balance {
+51
View File
@@ -0,0 +1,51 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade, weights::Weight};
// NOTE: This must be used alongside the account whose balance is expected to be inactive.
// Generally this will be used for the XCM teleport checking account.
pub struct MigrateToTrackInactive<T: Config, A: Get<T::AccountId>>(
sp_std::marker::PhantomData<(T, A)>,
);
impl<T: Config, A: Get<T::AccountId>> OnRuntimeUpgrade for MigrateToTrackInactive<T, A> {
fn on_runtime_upgrade() -> Weight {
let current_version = Pallet::<T>::current_storage_version();
let onchain_version = Pallet::<T>::on_chain_storage_version();
if onchain_version == 0 && current_version == 1 {
let b = Pallet::<T>::total_balance(&A::get());
Pallet::<T>::deactivate(b);
current_version.put::<Pallet<T>>();
log::info!(target: "runtime::balances", "Storage to version {:?}", current_version);
T::DbWeight::get().reads_writes(3, 3)
} else {
log::info!(target: "runtime::balances", "Migration did not execute. This probably should be removed");
T::DbWeight::get().reads(2)
}
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
Ok(vec![])
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(total: Vec<u8>) -> Result<(), &'static str> {
Ok(())
}
}