Skip to content
Snippets Groups Projects
Commit 8cc1af31 authored by Kian Paimani's avatar Kian Paimani Committed by GitHub
Browse files

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


* All done

* Fix benchmarks

* Apply suggestions from code review

Co-authored-by: default avatarGuillaume 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: default avatarGuillaume Thiolliere <gui.thiolliere@gmail.com>

* Fix test

* early exit

* Fix

* Fix build

Co-authored-by: default avatarGuillaume Thiolliere <gui.thiolliere@gmail.com>
parent 327934c1
No related merge requests found
......@@ -4911,7 +4911,7 @@ dependencies = [
[[package]]
name = "pallet-elections-phragmen"
version = "3.0.0"
version = "4.0.0"
dependencies = [
"frame-benchmarking",
"frame-support",
......
......@@ -57,7 +57,7 @@ pallet-contracts-primitives = { version = "3.0.0", default-features = false, pat
pallet-contracts-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/contracts/rpc/runtime-api/" }
pallet-democracy = { version = "3.0.0", default-features = false, path = "../../../frame/democracy" }
pallet-election-provider-multi-phase = { version = "3.0.0", default-features = false, path = "../../../frame/election-provider-multi-phase" }
pallet-elections-phragmen = { version = "3.0.0", default-features = false, path = "../../../frame/elections-phragmen" }
pallet-elections-phragmen = { version = "4.0.0", default-features = false, path = "../../../frame/elections-phragmen" }
pallet-gilt = { version = "3.0.0", default-features = false, path = "../../../frame/gilt" }
pallet-grandpa = { version = "3.0.0", default-features = false, path = "../../../frame/grandpa" }
pallet-im-online = { version = "3.0.0", default-features = false, path = "../../../frame/im-online" }
......
......@@ -4,7 +4,18 @@ All notable changes to this crate will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this crate adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [3.0.0] - UNRELEASED
## [4.0.0] - UNRELEASED
### Added
### Changed
\[**Needs Migration**\] [migrate pallet-elections-phragmen to attribute macros](https://github.com/paritytech/substrate/pull/8044)
### Fixed
### Security
## [3.0.0]
### Added
[Add slashing events to elections-phragmen](https://github.com/paritytech/substrate/pull/7543)
......
[package]
name = "pallet-elections-phragmen"
version = "3.0.0"
version = "4.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "Apache-2.0"
......@@ -16,14 +16,15 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
sp-npos-elections = { version = "3.0.0", default-features = false, path = "../../primitives/npos-elections" }
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" }
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
log = { version = "0.4.14", default-features = false }
[dev-dependencies]
sp-io = { version = "3.0.0", path = "../../primitives/io" }
hex-literal = "0.3.1"
pallet-balances = { version = "3.0.0", path = "../balances" }
sp-core = { version = "3.0.0", path = "../../primitives/core" }
......@@ -38,6 +39,8 @@ std = [
"sp-npos-elections/std",
"frame-system/std",
"sp-std/std",
"sp-io/std",
"sp-core/std",
"log/std",
]
runtime-benchmarks = [
......
......@@ -23,9 +23,9 @@ use super::*;
use frame_system::RawOrigin;
use frame_benchmarking::{benchmarks, account, whitelist, impl_benchmark_test_suite};
use frame_support::traits::OnInitialize;
use frame_support::{traits::OnInitialize, dispatch::DispatchResultWithPostInfo};
use crate::Module as Elections;
use crate::Pallet as Elections;
const BALANCE_FACTOR: u32 = 250;
const MAX_VOTERS: u32 = 500;
......@@ -87,11 +87,12 @@ fn submit_candidates_with_self_vote<T: Config>(c: u32, prefix: &'static str)
Ok(candidates)
}
/// Submit one voter.
fn submit_voter<T: Config>(caller: T::AccountId, votes: Vec<T::AccountId>, stake: BalanceOf<T>)
-> frame_support::dispatch::DispatchResult
{
fn submit_voter<T: Config>(
caller: T::AccountId,
votes: Vec<T::AccountId>,
stake: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
<Elections<T>>::vote(RawOrigin::Signed(caller).into(), votes, stake)
}
......
This diff is collapsed.
// 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;
// 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);
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment