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

Hasher Migration #7

Merged
merged 2 commits into from
Jun 18, 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
2 changes: 1 addition & 1 deletion bin/node-template/pallets/template/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
impl Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl system::Trait for Runtime {
/// This type is being generated by `construct_runtime!`.
type ModuleToIndex = ModuleToIndex;
/// What to do if a new account is created.
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
/// What to do if an account is fully reaped from the system.
type OnKilledAccount = ();
/// The data to be stored in an account.
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl frame_system::Trait for Runtime {
type Version = Version;
type ModuleToIndex = ModuleToIndex;
type AccountData = pallet_balances::AccountData<Balance>;
type MigrateAccount = (Balances, Identity, Democracy, Elections, ImOnline, Recovery, Session, Society, Staking, Vesting);
type OnNewAccount = ();
type OnKilledAccount = ();
}
Expand Down
2 changes: 1 addition & 1 deletion frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
impl Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion frame/aura/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/authorship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
8 changes: 8 additions & 0 deletions frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ decl_module! {
// remove temporary "environment" entry from storage
Lateness::<T>::kill();
}

fn on_runtime_upgrade() -> Weight {
for i in 0..=SegmentIndex::get() {
UnderConstruction::migrate_key_from_blake(i);
}
// TODO: determine weight
0
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl frame_system::Trait for Test {
type MaximumBlockLength = MaximumBlockLength;
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
10 changes: 8 additions & 2 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ use frame_support::{
Currency, OnKilledAccount, OnUnbalanced, TryDrop, StoredMap,
WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement,
Imbalance, SignedImbalance, ReservableCurrency, Get, ExistenceRequirement::KeepAlive,
ExistenceRequirement::AllowDeath, IsDeadAccount, BalanceStatus as Status,
ExistenceRequirement::AllowDeath, IsDeadAccount, BalanceStatus as Status, MigrateAccount
},
weights::Weight,
};
Expand Down Expand Up @@ -569,6 +569,12 @@ impl<Balance, BlockNumber> OldBalanceLock<Balance, BlockNumber> {
}
}

impl<T: Trait<I>, I: Instance> MigrateAccount<T::AccountId> for Module<T, I> {
fn migrate_account(account: &T::AccountId) {
Locks::<T, I>::migrate_key_from_blake(account);
}
}

impl<T: Trait<I>, I: Instance> Module<T, I> {
// PRIVATE MUTABLES

Expand Down Expand Up @@ -892,7 +898,7 @@ impl<T: Subtrait<I>, I: Instance> frame_system::Trait for ElevatedTrait<T, I> {
type AvailableBlockRatio = T::AvailableBlockRatio;
type Version = T::Version;
type ModuleToIndex = T::ModuleToIndex;
type OnNewAccount = T::OnNewAccount;
type MigrateAccount = (); type OnNewAccount = T::OnNewAccount;
type OnKilledAccount = T::OnKilledAccount;
type AccountData = T::AccountData;
}
Expand Down
2 changes: 1 addition & 1 deletion frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = super::AccountData<u64>;
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = super::AccountData<u64>;
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = Module<Test>;
}
parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion frame/benchmarking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
18 changes: 17 additions & 1 deletion frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ fn get_result_weight(result: DispatchResultWithPostInfo) -> Option<Weight> {
}
}

mod migration {
use super::*;

pub fn migrate<T: Trait<I>, I: Instance>() {
for p in Proposals::<T, I>::get().into_iter() {
ProposalOf::<T, I>::migrate_key_from_blake(&p);
Voting::<T, I>::migrate_key_from_blake(&p);
}
}
}

// Note that councillor operations are assigned to the operational class.
decl_module! {
Expand All @@ -342,6 +352,12 @@ decl_module! {

fn deposit_event() = default;

fn on_runtime_upgrade() -> Weight {
migration::migrate::<T, I>();
// TODO: determine actual weight
0
}

/// Set the collective's membership.
///
/// - `new_members`: The new member list. Be nice to the chain and provide it sorted.
Expand Down Expand Up @@ -990,7 +1006,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
impl Trait<Instance1> for Test {
Expand Down
1 change: 1 addition & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type MigrateAccount = ();
type OnNewAccount = ();
type OnKilledAccount = ();
}
Expand Down
98 changes: 97 additions & 1 deletion frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ use frame_support::{
weights::{Weight, DispatchClass},
traits::{
Currency, ReservableCurrency, LockableCurrency, WithdrawReason, LockIdentifier, Get,
OnUnbalanced, BalanceStatus, schedule::Named as ScheduleNamed, EnsureOrigin
OnUnbalanced, BalanceStatus, schedule::Named as ScheduleNamed, EnsureOrigin, MigrateAccount,
},
dispatch::DispatchResultWithPostInfo,
};
Expand Down Expand Up @@ -623,6 +623,99 @@ mod weight_for {
}
}

impl<T: Trait> MigrateAccount<T::AccountId> for Module<T> {
fn migrate_account(a: &T::AccountId) {
mod deprecated {
use sp_std::prelude::*;

use codec::{Encode, EncodeLike, Decode, Input, Output};
use frame_support::{decl_module, decl_storage};
use sp_runtime::RuntimeDebug;
use sp_std::convert::TryFrom;

use crate::{Trait, ReferendumIndex, Conviction};

#[derive(Copy, Clone, Eq, PartialEq, Default, RuntimeDebug)]
pub struct Vote {
pub aye: bool,
pub conviction: Conviction,
}

impl Encode for Vote {
fn encode_to<T: Output>(&self, output: &mut T) {
output.push_byte(u8::from(self.conviction) | if self.aye { 0b1000_0000 } else { 0 });
}
}

impl EncodeLike for Vote {}

impl Decode for Vote {
fn decode<I: Input>(input: &mut I) -> core::result::Result<Self, codec::Error> {
let b = input.read_byte()?;
Ok(Vote {
aye: (b & 0b1000_0000) == 0b1000_0000,
conviction: Conviction::try_from(b & 0b0111_1111)
.map_err(|_| codec::Error::from("Invalid conviction"))?,
})
}
}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { }
}
decl_storage! {
trait Store for Module<T: Trait> as Democracy {
pub VoteOf get(fn vote_of):
map hasher(opaque_blake2_256) (ReferendumIndex, T::AccountId) => Vote;
pub Delegations get(fn delegations):
map hasher(opaque_blake2_256) T::AccountId => (T::AccountId, Conviction);
}
}
}

Proxy::<T>::migrate_key_from_blake(a);
Locks::<T>::migrate_key_from_blake(a);
// TODO: will not actually do any useful migration
deprecated::Delegations::<T>::migrate_key_from_blake(a);
for i in LowestUnbaked::get()..ReferendumCount::get() {
// TODO: will not actually do any useful migration
deprecated::VoteOf::<T>::migrate_key_from_blake((i, a));
}
}
}

mod migration {
use super::*;

pub fn migrate<T: Trait>() {
mod deprecated {
use super::*;

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { }
}
decl_storage! {
trait Store for Module<T: Trait> as Democracy {
pub VotersFor get(fn voters_for):
map hasher(opaque_blake2_256) ReferendumIndex => Vec<T::AccountId>;
}
}
}

Blacklist::<T>::remove_all();
Cancellations::<T>::remove_all();
for i in LowestUnbaked::get()..ReferendumCount::get() {
// TODO: will not actually do any useful migration
deprecated::VotersFor::<T>::migrate_key_from_blake(i);
ReferendumInfoOf::<T>::migrate_key_from_blake(i);
}
for (p, h, _) in PublicProps::<T>::get().into_iter() {
DepositOf::<T>::migrate_key_from_blake(p);
Preimages::<T>::migrate_key_from_blake(h);
}
}
}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
Expand Down Expand Up @@ -658,6 +751,9 @@ decl_module! {
fn deposit_event() = default;

fn on_runtime_upgrade() -> Weight {
// TODO: determine correct weight
migration::migrate::<T>();

if let None = StorageVersion::get() {
StorageVersion::put(Releases::V1);

Expand Down
1 change: 1 addition & 0 deletions frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type MigrateAccount = ();
type OnNewAccount = ();
type OnKilledAccount = ();
}
Expand Down
28 changes: 27 additions & 1 deletion frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ use frame_support::{
};
use sp_phragmen::{build_support_map, ExtendedBalance, VoteWeight, PhragmenResult};
use frame_system::{self as system, ensure_signed, ensure_root};
use frame_support::traits::MigrateAccount;

mod benchmarking;

Expand Down Expand Up @@ -694,6 +695,31 @@ decl_event!(
}
);

impl<T: Trait> MigrateAccount<T::AccountId> for Module<T> {
fn migrate_account(a: &T::AccountId) {
mod deprecated {
use super::*;

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { }
}
decl_storage! {
trait Store for Module<T: Trait> as PhragmenElection {
pub VotesOf get(fn votes_of):
map hasher(opaque_blake2_256) T::AccountId => Vec<T::AccountId>;
pub StakeOf get(fn stake_of):
map hasher(opaque_blake2_256) T::AccountId => BalanceOf<T>;
}
}
}

// TODO: will not actually do any useful migration
if deprecated::StakeOf::<T>::migrate_key_from_blake(a).is_some() {
deprecated::VotesOf::<T>::migrate_key_from_blake(a);
}
}
}

impl<T: Trait> Module<T> {
/// Attempts to remove a member `who`. If a runner-up exists, it is used as the replacement and
/// Ok(true). is returned.
Expand Down Expand Up @@ -1095,7 +1121,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/elections/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/example-offchain-worker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
parameter_types! {
Expand Down
Loading