mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 22:21:07 +00:00
Improve inactive fund tracking (#13009)
* Improve inactive fund tracking * Resetting migration * Fix * Update frame/balances/src/migration.rs
This commit is contained in:
@@ -30,7 +30,7 @@ use sp_runtime::{
|
||||
use kitchensink_runtime::{
|
||||
constants::{currency::*, time::SLOT_DURATION},
|
||||
Balances, CheckedExtrinsic, Header, Runtime, RuntimeCall, RuntimeEvent, System,
|
||||
TransactionPayment, UncheckedExtrinsic,
|
||||
TransactionPayment, Treasury, UncheckedExtrinsic,
|
||||
};
|
||||
use node_primitives::{Balance, Hash};
|
||||
use node_testing::keyring::*;
|
||||
@@ -398,6 +398,7 @@ fn full_native_block_import_works() {
|
||||
});
|
||||
|
||||
fees = t.execute_with(|| transfer_fee(&xt()));
|
||||
let pot = t.execute_with(|| Treasury::pot());
|
||||
|
||||
executor_call(&mut t, "Core_execute_block", &block2.0, true).0.unwrap();
|
||||
|
||||
@@ -408,6 +409,14 @@ fn full_native_block_import_works() {
|
||||
);
|
||||
assert_eq!(Balances::total_balance(&bob()), 179 * DOLLARS - fees);
|
||||
let events = vec![
|
||||
EventRecord {
|
||||
phase: Phase::Initialization,
|
||||
event: RuntimeEvent::Treasury(pallet_treasury::Event::UpdatedInactive {
|
||||
reactivated: 0,
|
||||
deactivated: pot,
|
||||
}),
|
||||
topics: vec![],
|
||||
},
|
||||
EventRecord {
|
||||
phase: Phase::ApplyExtrinsic(0),
|
||||
event: RuntimeEvent::System(frame_system::Event::ExtrinsicSuccess {
|
||||
|
||||
@@ -69,3 +69,29 @@ impl<T: Config<I>, A: Get<Vec<T::AccountId>>, I: 'static> OnRuntimeUpgrade
|
||||
migrate_v0_to_v1::<T, I>(&A::get())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ResetInactive<T, I = ()>(PhantomData<(T, I)>);
|
||||
impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for ResetInactive<T, I> {
|
||||
fn on_runtime_upgrade() -> Weight {
|
||||
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
|
||||
|
||||
if onchain_version == 1 {
|
||||
// Remove the old `StorageVersion` type.
|
||||
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
|
||||
Pallet::<T, I>::name().as_bytes(),
|
||||
"StorageVersion".as_bytes(),
|
||||
));
|
||||
|
||||
InactiveIssuance::<T, I>::kill();
|
||||
|
||||
// Set storage version to `0`.
|
||||
StorageVersion::new(0).put::<Pallet<T, I>>();
|
||||
|
||||
log::info!(target: "runtime::balances", "Storage to version 0");
|
||||
T::DbWeight::get().reads_writes(1, 2)
|
||||
} else {
|
||||
log::info!(target: "runtime::balances", "Migration did not execute. This probably should be removed");
|
||||
T::DbWeight::get().reads(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,7 +225,8 @@ pub mod pallet {
|
||||
|
||||
/// The amount which has been reported as inactive to Currency.
|
||||
#[pallet::storage]
|
||||
pub type Inactive<T: Config<I>, I: 'static = ()> = StorageValue<_, BalanceOf<T, I>, ValueQuery>;
|
||||
pub type Deactivated<T: Config<I>, I: 'static = ()> =
|
||||
StorageValue<_, BalanceOf<T, I>, ValueQuery>;
|
||||
|
||||
/// Proposal indices that have been approved but not yet awarded.
|
||||
#[pallet::storage]
|
||||
@@ -292,6 +293,8 @@ pub mod pallet {
|
||||
amount: BalanceOf<T, I>,
|
||||
beneficiary: T::AccountId,
|
||||
},
|
||||
/// The inactive funds of the pallet have been updated.
|
||||
UpdatedInactive { reactivated: BalanceOf<T, I>, deactivated: BalanceOf<T, I> },
|
||||
}
|
||||
|
||||
/// Error for the treasury pallet.
|
||||
@@ -321,13 +324,15 @@ pub mod pallet {
|
||||
/// # </weight>
|
||||
fn on_initialize(n: T::BlockNumber) -> Weight {
|
||||
let pot = Self::pot();
|
||||
let deactivated = Inactive::<T, I>::get();
|
||||
let deactivated = Deactivated::<T, I>::get();
|
||||
if pot != deactivated {
|
||||
match (pot > deactivated, pot.max(deactivated) - pot.min(deactivated)) {
|
||||
(true, delta) => T::Currency::deactivate(delta),
|
||||
(false, delta) => T::Currency::reactivate(delta),
|
||||
}
|
||||
Inactive::<T, I>::put(&pot);
|
||||
T::Currency::reactivate(deactivated);
|
||||
T::Currency::deactivate(pot);
|
||||
Deactivated::<T, I>::put(&pot);
|
||||
Self::deposit_event(Event::<T, I>::UpdatedInactive {
|
||||
reactivated: deactivated,
|
||||
deactivated: pot,
|
||||
});
|
||||
}
|
||||
|
||||
// Check to see if we should spend some funds!
|
||||
|
||||
Reference in New Issue
Block a user