use codec::{Encode, Decode, FullCodec};
use tetcore_std::prelude::*;
use fabric_support::{
RuntimeDebug, weights::Weight, Twox64Concat,
storage::types::{StorageMap, StorageValue},
traits::{GetNobleVersion, NobleVersion},
};
#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq)]
struct SeatHolder<AccountId, Balance> {
who: AccountId,
stake: Balance,
deposit: Balance,
}
#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq)]
struct Voter<AccountId, Balance> {
votes: Vec<AccountId>,
stake: Balance,
deposit: Balance,
}
pub trait V2ToV3 {
type Module: GetNobleVersion;
type AccountId: 'static + FullCodec;
type Balance: 'static + FullCodec + Copy;
}
struct __Candidates;
impl fabric_support::traits::StorageInstance for __Candidates {
fn noble_prefix() -> &'static str { "PhragmenElection" }
const STORAGE_PREFIX: &'static str = "Candidates";
}
#[allow(type_alias_bounds)]
type Candidates<T: V2ToV3> = StorageValue<__Candidates, Vec<(T::AccountId, T::Balance)>>;
struct __Members;
impl fabric_support::traits::StorageInstance for __Members {
fn noble_prefix() -> &'static str { "PhragmenElection" }
const STORAGE_PREFIX: &'static str = "Members";
}
#[allow(type_alias_bounds)]
type Members<T: V2ToV3> = StorageValue<__Members, Vec<SeatHolder<T::AccountId, T::Balance>>>;
struct __RunnersUp;
impl fabric_support::traits::StorageInstance for __RunnersUp {
fn noble_prefix() -> &'static str { "PhragmenElection" }
const STORAGE_PREFIX: &'static str = "RunnersUp";
}
#[allow(type_alias_bounds)]
type RunnersUp<T: V2ToV3> = StorageValue<__RunnersUp, Vec<SeatHolder<T::AccountId, T::Balance>>>;
struct __Voting;
impl fabric_support::traits::StorageInstance for __Voting {
fn noble_prefix() -> &'static str { "PhragmenElection" }
const STORAGE_PREFIX: &'static str = "Voting";
}
#[allow(type_alias_bounds)]
type Voting<T: V2ToV3> = StorageMap<__Voting, Twox64Concat, T::AccountId, Voter<T::AccountId, T::Balance>>;
pub fn apply<T: V2ToV3>(old_voter_bond: T::Balance, old_candidacy_bond: T::Balance) -> Weight {
let maybe_storage_version = <T::Module as GetNobleVersion>::storage_version();
fabric_support::debug::info!(
"Running migration for elections-phragmen with storage version {:?}",
maybe_storage_version
);
match maybe_storage_version {
Some(storage_version) if storage_version <= NobleVersion::new(2, 0, 0) => {
migrate_voters_to_recorded_deposit::<T>(old_voter_bond);
migrate_candidates_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_runners_up_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_members_to_recorded_deposit::<T>(old_candidacy_bond);
Weight::max_value()
}
_ => {
fabric_support::debug::warn!(
"Attempted to apply migration to V3 but failed because storage version is {:?}",
maybe_storage_version
);
0
},
}
}
pub fn migrate_voters_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
<Voting<T>>::translate::<(T::Balance, Vec<T::AccountId>), _>(
|_who, (stake, votes)| {
Some(Voter {
votes,
stake,
deposit: old_deposit,
})
},
);
fabric_support::debug::info!(
"migrated {} voter accounts.",
<Voting<T>>::iter().count(),
);
}
pub fn migrate_candidates_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
let _ = <Candidates<T>>::translate::<Vec<T::AccountId>, _>(
|maybe_old_candidates| {
maybe_old_candidates.map(|old_candidates| {
fabric_support::debug::info!(
"migrated {} candidate accounts.",
old_candidates.len()
);
old_candidates
.into_iter()
.map(|c| (c, old_deposit))
.collect::<Vec<_>>()
})
},
);
}
pub fn migrate_members_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
let _ = <Members<T>>::translate::<Vec<(T::AccountId, T::Balance)>, _>(
|maybe_old_members| {
maybe_old_members.map(|old_members| {
fabric_support::debug::info!("migrated {} member accounts.", old_members.len());
old_members
.into_iter()
.map(|(who, stake)| SeatHolder {
who,
stake,
deposit: old_deposit,
})
.collect::<Vec<_>>()
})
},
);
}
pub fn migrate_runners_up_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
let _ = <RunnersUp<T>>::translate::<Vec<(T::AccountId, T::Balance)>, _>(
|maybe_old_runners_up| {
maybe_old_runners_up.map(|old_runners_up| {
fabric_support::debug::info!(
"migrated {} runner-up accounts.",
old_runners_up.len()
);
old_runners_up
.into_iter()
.map(|(who, stake)| SeatHolder {
who,
stake,
deposit: old_deposit,
})
.collect::<Vec<_>>()
})
},
);
}