diff --git a/frame/indices/src/lib.rs b/frame/indices/src/lib.rs index e58112403f628..4131a254711dd 100644 --- a/frame/indices/src/lib.rs +++ b/frame/indices/src/lib.rs @@ -28,7 +28,7 @@ use sp_runtime::traits::{ use frame_support::{Parameter, decl_module, decl_error, decl_event, decl_storage, ensure}; use frame_support::dispatch::DispatchResult; use frame_support::traits::{Currency, ReservableCurrency, Get, BalanceStatus::Reserved}; -use frame_support::weights::constants::WEIGHT_PER_MICROS; +use frame_support::weights::{constants::WEIGHT_PER_MICROS, Weight}; use frame_system::{ensure_signed, ensure_root}; use self::address::Address as RawAddress; @@ -36,6 +36,7 @@ mod mock; pub mod address; mod tests; mod benchmarking; +pub mod migration; pub type Address = RawAddress<::AccountId, ::AccountIndex>; type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -104,6 +105,10 @@ decl_module! { pub struct Module for enum Call where origin: T::Origin, system = frame_system { fn deposit_event() = default; + fn on_runtime_upgrade() -> Weight { + migration::migrate_enum_set::() + } + /// Assign an previously unassigned index. /// /// Payment: `Deposit` is reserved from the sender account. diff --git a/frame/indices/src/migration.rs b/frame/indices/src/migration.rs new file mode 100644 index 0000000000000..c941e5d7c246d --- /dev/null +++ b/frame/indices/src/migration.rs @@ -0,0 +1,51 @@ +use super::*; +use frame_support::weights::Weight; +use sp_runtime::traits::One; + +mod deprecated { + use crate::Trait; + use frame_support::{decl_module, decl_storage}; + use sp_std::prelude::*; + + decl_storage! { + trait Store for Module as Indices { + /// The next free enumeration set. + pub NextEnumSet get(fn next_enum_set): T::AccountIndex; + + /// The enumeration sets. + pub EnumSet get(fn enum_set): map hasher(opaque_blake2_256) T::AccountIndex => Vec; + } + } + decl_module! { + pub struct Module for enum Call where origin: T::Origin { } + } +} + +// Taken from a migration removal PR [here](https://github.com/paritytech/substrate/pull/5870/files#diff-12b2ce0dfddc1915cd81a902d368c2e7L246) +pub fn migrate_enum_set() -> Weight { + if deprecated::NextEnumSet::::exists() { + // migrations need doing. + let set_count = deprecated::NextEnumSet::::take(); + let set_size: T::AccountIndex = 64.into(); + + let mut set_index: T::AccountIndex = Zero::zero(); + while set_index < set_count { + if !deprecated::EnumSet::::contains_key(&set_index) { + break; + } + let accounts = deprecated::EnumSet::::take(&set_index); + for (item_index, target) in accounts.into_iter().enumerate() { + if target != T::AccountId::default() && !T::Currency::total_balance(&target).is_zero() { + let index = set_index * set_size + T::AccountIndex::from(item_index as u32); + // We need to add `false` to indicate the index is not frozen. + // See the [frozen indices PR](https://github.com/paritytech/substrate/pull/6307/) + Accounts::::insert(index, (target, BalanceOf::::zero(), false)); + } + } + set_index += One::one(); + } + T::MaximumBlockWeight::get() + } else { + 0 + } +} \ No newline at end of file diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index acb331ebe08f6..630a2802eb6b9 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -127,7 +127,7 @@ use frame_support::{ }, weights::{ Weight, RuntimeDbWeight, DispatchInfo, PostDispatchInfo, DispatchClass, - extract_actual_weight, Pays, FunctionOf, + extract_actual_weight, }, dispatch::DispatchResultWithPostInfo, }; @@ -758,11 +758,9 @@ decl_module! { Self::kill_account(&who); } - #[weight = FunctionOf( - |(accounts,): (&Vec,)| accounts.len() as Weight * 10_000, - DispatchClass::Normal, - Pays::Yes, - )] + // TODO: Update the weight here to avoid DoS attacks. Alternatively: remove it quickly in + // another upgrade. + #[weight = accounts.len() as Weight * 10_000] fn migrate_accounts(origin, accounts: Vec) { let _ = ensure_signed(origin)?; for a in &accounts { diff --git a/frame/transaction-payment/src/migration.rs b/frame/transaction-payment/src/migration.rs index 3c4f597398f77..0ba8ab3401c62 100644 --- a/frame/transaction-payment/src/migration.rs +++ b/frame/transaction-payment/src/migration.rs @@ -17,7 +17,7 @@ //! Migration code to update storage. use super::*; -use frame_support::storage::migration::{put_storage_value, take_storage_value}; +use frame_support::storage::migration::take_storage_value; use frame_support::weights::Weight; use sp_runtime::FixedI64; @@ -32,7 +32,9 @@ pub fn on_runtime_upgrade() -> Weight { // Since the format of the storage items themselves have not changed, we do not // need to keep track of a storage version. If the runtime does not need to be // upgraded, nothing here will happen anyway. - +// +// TODO: Also the type of the multiplier has changed in the mean-time we might want to +// introduce a storage version? fn rename_and_convert() -> Weight { sp_runtime::print("🕊️ Migrating Transaction Payment."); @@ -41,10 +43,11 @@ fn rename_and_convert() -> Weight { if let Some(next_fee_multiplier) = take_storage_value::(b"Balances", b"NextFeeMultiplier", &[]) { - let raw_multiplier = next_fee_multiplier.into_inner() as i128; + let raw_multiplier = next_fee_multiplier.into_inner() as u128; // Fixed64 used 10^9 precision, where Fixed128 uses 10^18, so we need to add 9 zeros. - let new_raw_multiplier: i128 = raw_multiplier.saturating_mul(1_000_000_000); - put_storage_value(b"TransactionPayment", b"NextFeeMultiplier", &[], new_raw_multiplier); + let new_raw_multiplier = raw_multiplier.saturating_mul(1_000_000_000); + let new_multiplier = Multiplier::from_inner(new_raw_multiplier); + NextFeeMultiplier::put(new_multiplier); writes += 2; } reads += 1;