migrate pallet-elections-phragmen to attribute macros (#8044)

* All done

* Fix benchmarks

* Apply suggestions from code review

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Fix metadata.

* Fix build

* Add migrations

* Fix

* Update frame/elections-phragmen/src/migrations/v4.rs

* Better migeation test

* More test

* Fix warn

* Update frame/elections-phragmen/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Fix test

* early exit

* Fix

* Fix build

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Kian Paimani
2021-04-23 09:12:34 +02:00
committed by GitHub
parent 327934c19e
commit 8cc1af31c4
9 changed files with 657 additions and 455 deletions
@@ -0,0 +1,23 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! All migrations of this pallet.
/// Version 3.
pub mod v3;
/// Version 4.
pub mod v4;
@@ -0,0 +1,186 @@
// This file is part of Substrate.
// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Migrations to version [`3.0.0`], as denoted by the changelog.
use codec::{Encode, Decode, FullCodec};
use sp_std::prelude::*;
use frame_support::{
RuntimeDebug, weights::Weight, Twox64Concat,
traits::{GetPalletVersion, PalletVersion},
};
#[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,
}
/// Trait to implement to give information about types used for migration
pub trait V2ToV3 {
/// elections-phragmen module, used to check storage version.
type Module: GetPalletVersion;
/// System config account id
type AccountId: 'static + FullCodec;
/// Elections-phragmen currency balance.
type Balance: 'static + FullCodec + Copy;
}
frame_support::generate_storage_alias!(
PhragmenElection, Candidates<T: V2ToV3> => Value<Vec<(T::AccountId, T::Balance)>>
);
frame_support::generate_storage_alias!(
PhragmenElection, Members<T: V2ToV3> => Value<Vec<SeatHolder<T::AccountId, T::Balance>>>
);
frame_support::generate_storage_alias!(
PhragmenElection, RunnersUp<T: V2ToV3> => Value<Vec<SeatHolder<T::AccountId, T::Balance>>>
);
frame_support::generate_storage_alias!(
PhragmenElection, Voting<T: V2ToV3> => Map<
(Twox64Concat, T::AccountId),
Voter<T::AccountId, T::Balance>
>
);
/// Apply all of the migrations from 2_0_0 to 3_0_0.
///
/// ### Warning
///
/// This code will **ONLY** check that the storage version is less than or equal to 2_0_0.
/// Further check might be needed at the user runtime.
///
/// Be aware that this migration is intended to be used only for the mentioned versions. Use
/// with care and run at your own risk.
pub fn apply<T: V2ToV3>(old_voter_bond: T::Balance, old_candidacy_bond: T::Balance) -> Weight {
let maybe_storage_version = <T::Module as GetPalletVersion>::storage_version();
log::info!(
target: "runtime::elections-phragmen",
"Running migration for elections-phragmen with storage version {:?}",
maybe_storage_version,
);
match maybe_storage_version {
Some(storage_version) if storage_version <= PalletVersion::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()
}
_ => {
log::warn!(
target: "runtime::elections-phragmen",
"Attempted to apply migration to V3 but failed because storage version is {:?}",
maybe_storage_version,
);
0
},
}
}
/// Migrate from the old legacy voting bond (fixed) to the new one (per-vote dynamic).
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,
})
},
);
log::info!(
target: "runtime::elections-phragmen",
"migrated {} voter accounts.",
<Voting<T>>::iter().count(),
);
}
/// Migrate all candidates to recorded deposit.
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| {
log::info!(
target: "runtime::elections-phragmen",
"migrated {} candidate accounts.",
old_candidates.len(),
);
old_candidates
.into_iter()
.map(|c| (c, old_deposit))
.collect::<Vec<_>>()
})
},
);
}
/// Migrate all members to recorded deposit.
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| {
log::info!(
target: "runtime::elections-phragmen",
"migrated {} member accounts.",
old_members.len(),
);
old_members
.into_iter()
.map(|(who, stake)| SeatHolder {
who,
stake,
deposit: old_deposit,
})
.collect::<Vec<_>>()
})
},
);
}
/// Migrate all runners-up to recorded deposit.
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| {
log::info!(
target: "runtime::elections-phragmen",
"migrated {} runner-up accounts.",
old_runners_up.len(),
);
old_runners_up
.into_iter()
.map(|(who, stake)| SeatHolder {
who,
stake,
deposit: old_deposit,
})
.collect::<Vec<_>>()
})
},
);
}
@@ -0,0 +1,110 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Migrations to version [`4.0.0`], as denoted by the changelog.
use frame_support::{
weights::Weight,
traits::{GetPalletVersion, PalletVersion, Get},
};
/// The old prefix.
pub const OLD_PREFIX: &[u8] = b"PhragmenElection";
/// Migrate the entire storage of this pallet to a new prefix.
///
/// This new prefix must be the same as the one set in construct_runtime. For safety, use
/// `PalletInfo` to get it, as:
/// `<Runtime as frame_system::Config>::PalletInfo::name::<ElectionsPhragmenPallet>`.
///
/// The old storage prefix, `PhragmenElection` is hardcoded in the migration code.
pub fn migrate<
T: frame_system::Config,
P: GetPalletVersion,
N: AsRef<str>,
>(new_pallet_name: N) -> Weight {
if new_pallet_name.as_ref().as_bytes() == OLD_PREFIX {
log::info!(
target: "runtime::elections-phragmen",
"New pallet name is equal to the old prefix. No migration needs to be done.",
);
return 0;
}
let maybe_storage_version = <P as GetPalletVersion>::storage_version();
log::info!(
target: "runtime::elections-phragmen",
"Running migration to v4 for elections-phragmen with storage version {:?}",
maybe_storage_version,
);
match maybe_storage_version {
Some(storage_version) if storage_version <= PalletVersion::new(3, 0, 0) => {
log::info!("new prefix: {}", new_pallet_name.as_ref());
frame_support::storage::migration::move_pallet(
OLD_PREFIX,
new_pallet_name.as_ref().as_bytes(),
);
<T as frame_system::Config>::BlockWeights::get().max_block
}
_ => {
log::warn!(
target: "runtime::elections-phragmen",
"Attempted to apply migration to v4 but failed because storage version is {:?}",
maybe_storage_version,
);
0
},
}
}
/// Some checks prior to migration. This can be linked to
/// [`frame_support::traits::OnRuntimeUpgrade::pre_upgrade`] for further testing.
///
/// Panics if anything goes wrong.
pub fn pre_migration<P: GetPalletVersion, N: AsRef<str>>(new: N) {
let new = new.as_ref();
log::info!("pre-migration elections-phragmen test with new = {}", new);
// the next key must exist, and start with the hash of `OLD_PREFIX`.
let next_key = sp_io::storage::next_key(OLD_PREFIX).unwrap();
assert!(next_key.starts_with(&sp_io::hashing::twox_128(OLD_PREFIX)));
// ensure nothing is stored in the new prefix.
assert!(
sp_io::storage::next_key(new.as_bytes()).map_or(
// either nothing is there
true,
// or we ensure that it has no common prefix with twox_128(new).
|next_key| !next_key.starts_with(&sp_io::hashing::twox_128(new.as_bytes()))
),
"unexpected next_key({}) = {:?}",
new,
sp_core::hexdisplay::HexDisplay::from(&sp_io::storage::next_key(new.as_bytes()).unwrap())
);
// ensure storage version is 3.
assert!(<P as GetPalletVersion>::storage_version().unwrap().major == 3);
}
/// Some checks for after migration. This can be linked to
/// [`frame_support::traits::OnRuntimeUpgrade::post_upgrade`] for further testing.
///
/// Panics if anything goes wrong.
pub fn post_migration<P : GetPalletVersion>() {
log::info!("post-migration elections-phragmen");
// ensure we've been updated to v4 by the automatic write of crate version -> storage version.
assert!(<P as GetPalletVersion>::storage_version().unwrap().major == 4);
}