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 2 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
6 changes: 2 additions & 4 deletions srml/elections-phragmen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false }
runtime_io = { package = "sr-io", path = "../../core/sr-io", default-features = false }
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
phragmen = { package = "substrate-phragmen", path = "../../core/phragmen", default-features = false }
Expand All @@ -18,13 +16,13 @@ rstd = { package = "sr-std", path = "../../core/sr-std", default-features = fals
[dev-dependencies]
hex-literal = "0.2.1"
balances = { package = "srml-balances", path = "../balances" }
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
serde = { version = "1.0.101" }

[features]
default = ["std"]
std = [
"codec/std",
"primitives/std",
"serde",
"runtime_io/std",
"srml-support/std",
"sr-primitives/std",
Expand Down
42 changes: 42 additions & 0 deletions srml/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@
#![cfg_attr(not(feature = "std"), no_std)]

use rstd::prelude::*;
use codec::Decode;
use sr_primitives::{print, traits::{Zero, StaticLookup, Bounded, Convert}};
use sr_primitives::weights::SimpleDispatchInfo;
use srml_support::{
decl_storage, decl_event, ensure, decl_module, dispatch,
storage::hashed::get_raw,
traits::{
Currency, Get, LockableCurrency, LockIdentifier, ReservableCurrency, WithdrawReasons,
ChangeMembers, OnUnbalanced, WithdrawReason
}
};
use runtime_io::twox_128;
use phragmen::ExtendedBalance;
use system::{self, ensure_signed, ensure_root};

Expand Down Expand Up @@ -159,6 +162,11 @@ decl_storage! {
/// The present candidate list. Sorted based on account id. A current member can never enter
/// this vector and is always implicitly assumed to be a candidate.
pub Candidates get(fn candidates): Vec<T::AccountId>;

/// Has the storage format been updated?
/// NOTE: Only use and set to false if you have used an early version of this module. Should
/// be set to true otherwise.
pub DidUpdate: bool = false;
}
}

Expand Down Expand Up @@ -373,6 +381,10 @@ decl_module! {

/// What to do at the end of each block. Checks if an election needs to happen or not.
fn on_initialize(n: T::BlockNumber) {
if !DidUpdate::get() {
DidUpdate::put(true);
Self::do_update();
}
if let Err(e) = Self::end_block(n) {
print("Guru meditation");
print(e);
Expand Down Expand Up @@ -626,6 +638,36 @@ impl<T: Trait> Module<T> {

ElectionRounds::mutate(|v| *v += 1);
}

/// Perform the storage update needed to migrate the module from the initial version of the
/// storage.
///
/// If decoding the old storage fails in any way, the consequence is that we start with an empty
/// set.
fn do_update() {
let members_key = "PhragmenElection Members";
let runners_key = "PhragmenElection RunnersUp";

// old storage format.
let old_members: Vec<T::AccountId> = get_raw(&twox_128, members_key.as_bytes())
.map_or_else(|| vec![], |bytes| Decode::decode(&mut &*bytes).unwrap_or_default());
let old_runners: Vec<T::AccountId> = get_raw(&twox_128, runners_key.as_bytes())
.map_or_else(|| vec![], |bytes| Decode::decode(&mut &*bytes).unwrap_or_default());

// new storage format.
let new_runners: Vec<(T::AccountId, BalanceOf<T>)> = old_runners
.into_iter()
.map(|r| (r, Zero::zero()))
.collect();
let new_members: Vec<(T::AccountId, BalanceOf<T>)> = old_members
.into_iter()
.map(|r| (r, Zero::zero()))
.collect();

<Members<T>>::put(new_members);
<RunnersUp<T>>::put(new_runners);

}
}

#[cfg(test)]
Expand Down