Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Migration
  • Loading branch information
gavofyork committed Mar 21, 2020
commit 03e168421a3a5149ef00b05c2bc41c908d444951
43 changes: 30 additions & 13 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,9 @@ decl_storage! {
/// The total number of vote rounds that have happened, excluding the upcoming one.
pub ElectionRounds get(fn election_rounds): u32 = Zero::zero();

// TODO: combine the next two.
/// Votes of a particular voter.
pub VotesOf get(fn votes_of): map hasher(twox_64_concat) T::AccountId => Vec<T::AccountId>;
/// Locked stake of a voter.
pub StakeOf get(fn stake_of): map hasher(twox_64_concat) T::AccountId => BalanceOf<T>;
/// Votes and locked stake of a particular voter.
pub Voting get:
map hasher(twox_64_concat) T::AccountId => (BalanceOf<T>, Vec<T::AccountId>);

/// The present candidate list. Sorted based on account-id. A current member or runner-up
/// can never enter this vector and is always implicitly assumed to be a candidate.
Expand Down Expand Up @@ -204,6 +202,22 @@ decl_error! {
}
}

mod migrations {
use super::*;
use frame_support::{migration::{StorageKeyIterator, take_storage_item}, Twox64Concat};
fn migrate<T: Trait>() {
for (who, votes) in StorageKeyIterator
::<T::AccountId, Vec<T::AccountId>, Twox64Concat>
::new(b"PhragmenElection", b"VotesOf")
.drain()
{
if let Some(stake) = take_storage_item::<T::AccountId, BalanceOf<T>, Twox64Concat>(b"PhragmenElection", b"VotesOf", &who) {
Voting::<T>::put(who, (stake, votes));
}
}
}
}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
Expand Down Expand Up @@ -266,8 +280,7 @@ decl_module! {
WithdrawReasons::except(WithdrawReason::TransactionPayment),
);

<StakeOf<T>>::insert(&who, locked_balance);
<VotesOf<T>>::insert(&who, &votes);
Voting::<T>::insert(&who, (locked_balance, votes));
}

/// Remove `origin` as a voter. This removes the lock and returns the bond.
Expand Down Expand Up @@ -587,8 +600,7 @@ impl<T: Trait> Module<T> {
/// lock. Optionally, it would also return the reserved voting bond if indicated by `unreserve`.
fn do_remove_voter(who: &T::AccountId, unreserve: bool) {
// remove storage and lock.
<VotesOf<T>>::remove(who);
<StakeOf<T>>::remove(who);
Voting::<T>::remove(who);
T::Currency::remove_lock(MODULE_ID, who);

if unreserve {
Expand All @@ -598,7 +610,12 @@ impl<T: Trait> Module<T> {

/// The locked stake of a voter.
fn locked_stake_of(who: &T::AccountId) -> BalanceOf<T> {
Self::stake_of(who)
Voting::<T>::get(who).0
}

/// The locked stake of a voter.
fn votes_of(who: &T::AccountId) -> BalanceOf<T> {
Voting::<T>::get(who).1
}

/// Check there's nothing to do this block.
Expand Down Expand Up @@ -638,8 +655,8 @@ impl<T: Trait> Module<T> {
// previous runners_up are also always candidates for the next round.
candidates.append(&mut Self::runners_up_ids());

let voters_and_votes = VotesOf::<T>::iter()
.map(|(voter, targets)| { let s = StakeOf::<T>::get(&voter); (voter, s, targets) })
let voters_and_votes = Voting::<T>::iter()
.map(|(voter, (stake, targets))| { (voter, stake, targets) })
.collect::<Vec<_>>();
let maybe_phragmen_result = sp_phragmen::elect::<_, _, T::CurrencyToVote, Perbill>(
num_to_elect,
Expand Down Expand Up @@ -1025,7 +1042,7 @@ mod tests {
}

fn all_voters() -> Vec<u64> {
<VotesOf<Test>>::iter().map(|(v, _)| v).collect::<Vec<u64>>()
Voting::<Test>::iter().map(|(v, _)| v).collect::<Vec<u64>>()
}

fn balances(who: &u64) -> (u64, u64) {
Expand Down
9 changes: 9 additions & 0 deletions frame/support/src/storage/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,12 @@ pub fn remove_storage_prefix(module: &[u8], item: &[u8], hash: &[u8]) {
key[32..].copy_from_slice(hash);
frame_support::storage::unhashed::kill_prefix(&key)
}

/// Get a particular value in storage by the `module`, the map's `item` name and the key `hash`.
pub fn take_storage_item<K: Encode + Sized, T: Decode + Sized, H: StorageHasher>(
module: &[u8],
item: &[u8],
key: EncodeLike<K>,
) -> Option<T> {
take_storage_value(module, item, &item_key.using_encoded(H::hash))
}