From 2f8ebc3759f4c110f4b7a5371ac5460536cc004b Mon Sep 17 00:00:00 2001 From: Gav Date: Thu, 22 Dec 2022 18:32:38 +0100 Subject: [PATCH 1/4] Improve inactive fund tracking --- frame/treasury/src/lib.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/frame/treasury/src/lib.rs b/frame/treasury/src/lib.rs index 0ffc53d8b7978..c3e205e6ee736 100644 --- a/frame/treasury/src/lib.rs +++ b/frame/treasury/src/lib.rs @@ -292,6 +292,11 @@ pub mod pallet { amount: BalanceOf, beneficiary: T::AccountId, }, + /// The inactive funds of the pallet have been updated. + UpdatedInactive { + reactivated: BalanceOf, + deactivated: BalanceOf, + } } /// Error for the treasury pallet. @@ -323,11 +328,13 @@ pub mod pallet { let pot = Self::pot(); let deactivated = Inactive::::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), - } + T::Currency::reactivate(deactivated); + T::Currency::deactivate(pot); Inactive::::put(&pot); + Self::deposit_event(Event::::UpdatedInactive { + reactivated: deactivated, + deactivated: pot, + }); } // Check to see if we should spend some funds! From 9113474ab84cf706f5703c1dfb4d78827f910e40 Mon Sep 17 00:00:00 2001 From: Gav Date: Fri, 23 Dec 2022 14:27:35 +0100 Subject: [PATCH 2/4] Resetting migration --- frame/balances/src/migration.rs | 26 ++++++++++++++++++++++++++ frame/treasury/src/lib.rs | 12 +++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/frame/balances/src/migration.rs b/frame/balances/src/migration.rs index 08e1d8c7a2c74..731e95d9b9e06 100644 --- a/frame/balances/src/migration.rs +++ b/frame/balances/src/migration.rs @@ -69,3 +69,29 @@ impl, A: Get>, I: 'static> OnRuntimeUpgrade migrate_v0_to_v1::(&A::get()) } } + +pub struct ResetInactive(PhantomData<(T, I)>); +impl, I: 'static> OnRuntimeUpgrade for ResetInactive { + fn on_runtime_upgrade() -> Weight { + let onchain_version = Pallet::::on_chain_storage_version(); + + if onchain_version == 1 { + // Remove the old `StorageVersion` type. + frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix( + Pallet::::name().as_bytes(), + "StorageVersion".as_bytes(), + )); + + InactiveIssuance::::kill(); + + // Set storage version to `0`. + StorageVersion::new(0).put::>(); + + log::info!(target: "runtime::balances", "Storage to version 1"); + 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) + } + } +} diff --git a/frame/treasury/src/lib.rs b/frame/treasury/src/lib.rs index c3e205e6ee736..1bde1238191a4 100644 --- a/frame/treasury/src/lib.rs +++ b/frame/treasury/src/lib.rs @@ -225,7 +225,8 @@ pub mod pallet { /// The amount which has been reported as inactive to Currency. #[pallet::storage] - pub type Inactive, I: 'static = ()> = StorageValue<_, BalanceOf, ValueQuery>; + pub type Deactivated, I: 'static = ()> = + StorageValue<_, BalanceOf, ValueQuery>; /// Proposal indices that have been approved but not yet awarded. #[pallet::storage] @@ -293,10 +294,7 @@ pub mod pallet { beneficiary: T::AccountId, }, /// The inactive funds of the pallet have been updated. - UpdatedInactive { - reactivated: BalanceOf, - deactivated: BalanceOf, - } + UpdatedInactive { reactivated: BalanceOf, deactivated: BalanceOf }, } /// Error for the treasury pallet. @@ -326,11 +324,11 @@ pub mod pallet { /// # fn on_initialize(n: T::BlockNumber) -> Weight { let pot = Self::pot(); - let deactivated = Inactive::::get(); + let deactivated = Deactivated::::get(); if pot != deactivated { T::Currency::reactivate(deactivated); T::Currency::deactivate(pot); - Inactive::::put(&pot); + Deactivated::::put(&pot); Self::deposit_event(Event::::UpdatedInactive { reactivated: deactivated, deactivated: pot, From 9d096f0d97f2130e23d835e57e4ed94946d872d3 Mon Sep 17 00:00:00 2001 From: Gav Date: Fri, 23 Dec 2022 16:44:47 +0100 Subject: [PATCH 3/4] Fix --- bin/node/executor/tests/basic.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index c88703b929e6b..02b2a8787b5d5 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -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 { From 38cf923a3b692c05f1a2cdcbdb63b385cff2fdf9 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Fri, 23 Dec 2022 18:05:54 +0100 Subject: [PATCH 4/4] Update frame/balances/src/migration.rs --- frame/balances/src/migration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/balances/src/migration.rs b/frame/balances/src/migration.rs index 731e95d9b9e06..b660ec9fd3235 100644 --- a/frame/balances/src/migration.rs +++ b/frame/balances/src/migration.rs @@ -87,7 +87,7 @@ impl, I: 'static> OnRuntimeUpgrade for ResetInactive { // Set storage version to `0`. StorageVersion::new(0).put::>(); - log::info!(target: "runtime::balances", "Storage to version 1"); + 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");