Skip to content

Commit

Permalink
add indices migration and adjust it for frozen indices
Browse files Browse the repository at this point in the history
  • Loading branch information
apopiak committed Jul 7, 2020
1 parent 64df587 commit 5f3933f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
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
}
}

0 comments on commit 5f3933f

Please sign in to comment.