Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indices migration and fixes #14

Merged
merged 3 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion frame/indices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ 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;

mod mock;
pub mod address;
mod tests;
mod benchmarking;
pub mod migration;

pub type Address<T> = RawAddress<<T as frame_system::Trait>::AccountId, <T as Trait>::AccountIndex>;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
Expand Down Expand Up @@ -104,6 +105,10 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system = frame_system {
fn deposit_event() = default;

fn on_runtime_upgrade() -> Weight {
migration::migrate_enum_set::<T>()
}

/// Assign an previously unassigned index.
///
/// Payment: `Deposit` is reserved from the sender account.
Expand Down
51 changes: 51 additions & 0 deletions frame/indices/src/migration.rs
Original file line number Diff line number Diff line change
@@ -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<T: Trait> 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<T::AccountId>;
}
}
decl_module! {
pub struct Module<T: Trait> 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<T: Trait>() -> Weight {
if deprecated::NextEnumSet::<T>::exists() {
// migrations need doing.
let set_count = deprecated::NextEnumSet::<T>::take();
let set_size: T::AccountIndex = 64.into();

let mut set_index: T::AccountIndex = Zero::zero();
while set_index < set_count {
if !deprecated::EnumSet::<T>::contains_key(&set_index) {
break;
}
let accounts = deprecated::EnumSet::<T>::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::<T>::insert(index, (target, BalanceOf::<T>::zero(), false));
}
}
set_index += One::one();
}
T::MaximumBlockWeight::get()
} else {
0
}
}
10 changes: 4 additions & 6 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ use frame_support::{
},
weights::{
Weight, RuntimeDbWeight, DispatchInfo, PostDispatchInfo, DispatchClass,
extract_actual_weight, Pays, FunctionOf,
extract_actual_weight,
},
dispatch::DispatchResultWithPostInfo,
};
Expand Down Expand Up @@ -758,11 +758,9 @@ decl_module! {
Self::kill_account(&who);
}

#[weight = FunctionOf(
|(accounts,): (&Vec<T::AccountId>,)| 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<T::AccountId>) {
let _ = ensure_signed(origin)?;
for a in &accounts {
Expand Down
13 changes: 8 additions & 5 deletions frame/transaction-payment/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,7 +32,9 @@ pub fn on_runtime_upgrade<T: Trait>() -> 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<T: Trait>() -> Weight {
sp_runtime::print("🕊️ Migrating Transaction Payment.");

Expand All @@ -41,10 +43,11 @@ fn rename_and_convert<T: Trait>() -> Weight {
if let Some(next_fee_multiplier) =
take_storage_value::<OldMultiplier>(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;
Expand Down