From af643832141a199da977b870d41a1c5eed7d7eb7 Mon Sep 17 00:00:00 2001 From: remzrn Date: Thu, 9 Dec 2021 08:05:48 +0300 Subject: [PATCH] Fixed logic of the storage migration to triple reference counting. (#10337) * Fixed logic of the storage migration to triple reference counting. The previous behaviour made it impossible for any chain not already upgraded to dual reference counting to upgrade the runtime. * +Removed the on_runtime_upgrade() function from frame-system. +Removed the specific migration .anciallaries from the frame-system pallet level. +Introducted a new module that hosts self-contained ancillary functions and logic to perform the storage migration. The current logic attempts to infer the state of the storage based on whether or not a given migration can be conducted. * Formatting. * + Removed specific AccountData struct. AccountData must now be provided during the runtime implementation of the trait V2ToV3. + Removed apply function. + Made the individual translation function self-sufficient. * + Removed unused decorators. --- substrate/frame/system/src/lib.rs | 51 +------ substrate/frame/system/src/migrations/mod.rs | 139 +++++++++++++++++++ 2 files changed, 141 insertions(+), 49 deletions(-) create mode 100644 substrate/frame/system/src/migrations/mod.rs diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 7603fe6541..7825c9ec07 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -114,6 +114,8 @@ pub mod mocking; mod tests; pub mod weights; +pub mod migrations; + pub use extensions::{ check_genesis::CheckGenesis, check_mortality::CheckMortality, check_non_zero_sender::CheckNonZeroSender, check_nonce::CheckNonce, @@ -315,15 +317,6 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - if !UpgradedToTripleRefCount::::get() { - UpgradedToTripleRefCount::::put(true); - migrations::migrate_to_triple_ref_count::() - } else { - 0 - } - } - fn integrity_test() { T::BlockWeights::get().validate().expect("The weights are invalid."); } @@ -634,46 +627,6 @@ pub mod pallet { } } -pub mod migrations { - use super::*; - - #[allow(dead_code)] - /// Migrate from unique `u8` reference counting to triple `u32` reference counting. - pub fn migrate_all() -> frame_support::weights::Weight { - Account::::translate::<(T::Index, u8, T::AccountData), _>(|_key, (nonce, rc, data)| { - Some(AccountInfo { - nonce, - consumers: rc as RefCount, - providers: 1, - sufficients: 0, - data, - }) - }); - T::BlockWeights::get().max_block - } - - #[allow(dead_code)] - /// Migrate from unique `u32` reference counting to triple `u32` reference counting. - pub fn migrate_to_dual_ref_count() -> frame_support::weights::Weight { - Account::::translate::<(T::Index, RefCount, T::AccountData), _>( - |_key, (nonce, consumers, data)| { - Some(AccountInfo { nonce, consumers, providers: 1, sufficients: 0, data }) - }, - ); - T::BlockWeights::get().max_block - } - - /// Migrate from dual `u32` reference counting to triple `u32` reference counting. - pub fn migrate_to_triple_ref_count() -> frame_support::weights::Weight { - Account::::translate::<(T::Index, RefCount, RefCount, T::AccountData), _>( - |_key, (nonce, consumers, providers, data)| { - Some(AccountInfo { nonce, consumers, providers, sufficients: 0, data }) - }, - ); - T::BlockWeights::get().max_block - } -} - #[cfg(feature = "std")] impl GenesisConfig { /// Direct implementation of `GenesisBuild::build_storage`. diff --git a/substrate/frame/system/src/migrations/mod.rs b/substrate/frame/system/src/migrations/mod.rs new file mode 100644 index 0000000000..03547d200f --- /dev/null +++ b/substrate/frame/system/src/migrations/mod.rs @@ -0,0 +1,139 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Migrate the reference counting state. + +use codec::{Decode, Encode, FullCodec}; +use frame_support::{ + pallet_prelude::ValueQuery, traits::PalletInfoAccess, weights::Weight, Blake2_128Concat, + RuntimeDebug, +}; +use sp_std::prelude::*; + +/// Type used to encode the number of references an account has. +type RefCount = u32; + +/// Information of an account. +#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)] +struct AccountInfo { + nonce: Index, + consumers: RefCount, + providers: RefCount, + sufficients: RefCount, + data: AccountData, +} + +/// Trait to implement to give information about types used for migration +pub trait V2ToV3 { + /// The system pallet. + type Pallet: 'static + PalletInfoAccess; + + /// System config account id + type AccountId: 'static + FullCodec; + + /// System config index + type Index: 'static + FullCodec + Copy; + + /// System config account data + type AccountData: 'static + FullCodec; +} + +// ### Warning +// +// The call below is only valid because the name System is enforced +// at runtime construction level for the system pallet. +frame_support::generate_storage_alias!( + System, UpgradedToU32RefCount => Value< + bool, + ValueQuery + > +); + +// ### Warning +// +// The call below is only valid because the name System is enforced +// at runtime construction level for the system pallet. +frame_support::generate_storage_alias!( + System, UpgradedToTripleRefCount => Value< + bool, + ValueQuery + > +); + +// ### Warning +// +// The call below is only valid because the name System is enforced +// at runtime construction level for the system pallet. +frame_support::generate_storage_alias!( + System, Account => Map< + (Blake2_128Concat, T::AccountId), + AccountInfo + > +); + +/// Migrate from unique `u8` reference counting to triple `u32` reference counting. +pub fn migrate_from_single_u8_to_triple_ref_count() -> Weight { + let mut translated: usize = 0; + >::translate::<(T::Index, u8, T::AccountData), _>(|_key, (nonce, rc, data)| { + translated = translated + 1; + Some(AccountInfo { nonce, consumers: rc as RefCount, providers: 1, sufficients: 0, data }) + }); + log::info!( + target: "runtime::system", + "Applied migration from single u8 to triple reference counting to {:?} elements.", + translated + ); + ::put(true); + ::put(true); + Weight::max_value() +} + +/// Migrate from unique `u32` reference counting to triple `u32` reference counting. +pub fn migrate_from_single_to_triple_ref_count() -> Weight { + let mut translated: usize = 0; + >::translate::<(T::Index, RefCount, T::AccountData), _>( + |_key, (nonce, consumers, data)| { + translated = translated + 1; + Some(AccountInfo { nonce, consumers, providers: 1, sufficients: 0, data }) + }, + ); + log::info!( + target: "runtime::system", + "Applied migration from single to triple reference counting to {:?} elements.", + translated + ); + ::put(true); + Weight::max_value() +} + +/// Migrate from dual `u32` reference counting to triple `u32` reference counting. +pub fn migrate_from_dual_to_triple_ref_count() -> Weight { + let mut translated: usize = 0; + >::translate::<(T::Index, RefCount, RefCount, T::AccountData), _>( + |_key, (nonce, consumers, providers, data)| { + translated = translated + 1; + Some(AccountInfo { nonce, consumers, providers, sufficients: 0, data }) + }, + ); + log::info!( + target: "runtime::system", + "Applied migration from dual to triple reference counting to {:?} elements.", + translated + ); + ::put(true); + Weight::max_value() +}