Abstracts elections-phragmen pallet to use NposSolver (#12588)

* Abstracts elections-phragmen pallet to use NposSolver

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

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

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

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* changes the name of the pallet; adds changelog

* update changelog

* Adds weight testing

* Adds log macro_rules

* renames elections-phragment dir to elections

* weights rename

* fixes typo in cargo toml

* pre/post solve weight scafolding

* refactor do_post_election

* refactors into pre and post election solve for independent benchmarking

* deconstructs PreElectionResults struct

* updates benchmarking pre and post election solve; mock weights

* Update frame/elections/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/elections/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* addresses PR comments

* adds pre_solve and post_sove weights

* Adds comments on election pallet id param name change

* ".git/.scripts/bench-bot.sh" pallet dev pallet_elections

* Finishes pre-post solve weights

* Update frame/elections/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/elections/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Addresses PR comments: no panic in on_init path; nits

* Fixes node build

* Implements approval voting to use as a `NposSolver` (#13367)

* Implements the approval voting methods in sp_npos_elections

* fmt

* remove unecessary file

* comment clarification

* re-run weights

* fix typo

* updates MaxVoters in tests for integrity_tests to pass

* Refactors election provider support benchmarks outside its own crate (#13431)

* Refactors election provider support benchmarks outside its own crate
---------

Co-authored-by: command-bot <>

---------

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: parity-processbot <>
Co-authored-by: Ross Bulat <ross@parity.io>
This commit is contained in:
Gonçalo Pestana
2023-02-23 11:21:00 +00:00
committed by GitHub
parent 17e055e594
commit b793666ca5
28 changed files with 1197 additions and 1018 deletions
@@ -0,0 +1,101 @@
// This file is part of Substrate.
// Copyright (C) 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.
//! Election provider support pallet benchmarking.
//! This is separated into its own crate to avoid bloating the size of the runtime.
use crate::{ApprovalVoting, NposSolver, PhragMMS, SequentialPhragmen};
use codec::Decode;
use frame_benchmarking::v1::{benchmarks, Vec};
pub struct Pallet<T: Config>(frame_system::Pallet<T>);
pub trait Config: frame_system::Config {}
const VOTERS: [u32; 2] = [1_000, 2_000];
const TARGETS: [u32; 2] = [500, 1_000];
const VOTES_PER_VOTER: [u32; 2] = [5, 16];
const SEED: u32 = 999;
fn set_up_voters_targets<AccountId: Decode + Clone>(
voters_len: u32,
targets_len: u32,
degree: usize,
) -> (Vec<(AccountId, u64, impl IntoIterator<Item = AccountId>)>, Vec<AccountId>) {
// fill targets.
let mut targets = (0..targets_len)
.map(|i| frame_benchmarking::account::<AccountId>("Target", i, SEED))
.collect::<Vec<_>>();
assert!(targets.len() > degree, "we should always have enough voters to fill");
targets.truncate(degree);
// fill voters.
let voters = (0..voters_len)
.map(|i| {
let voter = frame_benchmarking::account::<AccountId>("Voter", i, SEED);
(voter, 1_000, targets.clone())
})
.collect::<Vec<_>>();
(voters, targets)
}
benchmarks! {
phragmen {
// number of votes in snapshot.
let v in (VOTERS[0]) .. VOTERS[1];
// number of targets in snapshot.
let t in (TARGETS[0]) .. TARGETS[1];
// number of votes per voter (ie the degree).
let d in (VOTES_PER_VOTER[0]) .. VOTES_PER_VOTER[1];
let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as usize);
}: {
assert!(
SequentialPhragmen::<T::AccountId, sp_runtime::Perbill>
::solve(d as usize, targets, voters).is_ok()
);
}
phragmms {
// number of votes in snapshot.
let v in (VOTERS[0]) .. VOTERS[1];
// number of targets in snapshot.
let t in (TARGETS[0]) .. TARGETS[1];
// number of votes per voter (ie the degree).
let d in (VOTES_PER_VOTER[0]) .. VOTES_PER_VOTER[1];
let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as usize);
}: {
assert!(
PhragMMS::<T::AccountId, sp_runtime::Perbill>
::solve(d as usize, targets, voters).is_ok()
);
}
approval_voting {
let v in (VOTERS[0]) .. VOTERS[1];
let t in (TARGETS[0]) .. TARGETS[1];
let d in (VOTES_PER_VOTER[0]) .. VOTES_PER_VOTER[1];
let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as usize);
}: {
assert!(
ApprovalVoting::<T::AccountId, sp_runtime::Perbill>
::solve(d as usize, targets, voters).is_ok()
);
}
}
@@ -199,6 +199,9 @@ pub use sp_arithmetic;
#[doc(hidden)]
pub use sp_std;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
pub mod weights;
pub use weights::WeightInfo;
@@ -660,6 +663,29 @@ impl<AccountId: IdentifierT, Accuracy: PerThing128, Balancing: Get<Option<Balanc
}
}
/// A wrapper for [`sp_npos_elections::approval_voting()`] that implements [`NposSolver`]. See the
/// documentation of [`sp_npos_elections::approval_voting()`] for more info.
pub struct ApprovalVoting<AccountId, Accuracy>(sp_std::marker::PhantomData<(AccountId, Accuracy)>);
impl<AccountId: IdentifierT, Accuracy: PerThing128> NposSolver
for ApprovalVoting<AccountId, Accuracy>
{
type AccountId = AccountId;
type Accuracy = Accuracy;
type Error = sp_npos_elections::Error;
fn solve(
winners: usize,
targets: Vec<Self::AccountId>,
voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
sp_npos_elections::approval_voting(winners, targets, voters)
}
fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight {
T::approval_voting(voters, targets, vote_degree)
}
}
/// A voter, at the level of abstraction of this crate.
pub type Voter<AccountId, Bound> = (AccountId, VoteWeight, BoundedVec<AccountId, Bound>);
@@ -185,7 +185,7 @@ impl<T: Config> ElectionProvider for OnChainExecution<T> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{ElectionProvider, PhragMMS, SequentialPhragmen};
use crate::{ApprovalVoting, ElectionProvider, PhragMMS, SequentialPhragmen};
use frame_support::{assert_noop, parameter_types, traits::ConstU32};
use sp_npos_elections::Support;
use sp_runtime::Perbill;
@@ -235,6 +235,7 @@ mod tests {
struct PhragmenParams;
struct PhragMMSParams;
struct ApprovalVotingParams;
parameter_types! {
pub static MaxWinners: u32 = 10;
@@ -261,6 +262,16 @@ mod tests {
type TargetsBound = ConstU32<400>;
}
impl Config for ApprovalVotingParams {
type System = Runtime;
type Solver = ApprovalVoting<AccountId, Perbill>;
type DataProvider = mock_data_provider::DataProvider;
type WeightInfo = ();
type MaxWinners = MaxWinners;
type VotersBound = ConstU32<600>;
type TargetsBound = ConstU32<400>;
}
mod mock_data_provider {
use frame_support::{bounded_vec, traits::ConstU32};
@@ -333,4 +344,21 @@ mod tests {
);
})
}
#[test]
fn onchain_approval_voting_works() {
sp_io::TestExternalities::new_empty().execute_with(|| {
DesiredTargets::set(3);
// note that the `OnChainExecution::elect` implementation normalizes the vote weights.
assert_eq!(
<OnChainExecution::<ApprovalVotingParams> as ElectionProvider>::elect().unwrap(),
vec![
(10, Support { total: 20, voters: vec![(1, 5), (3, 15)] }),
(20, Support { total: 15, voters: vec![(1, 5), (2, 10)] }),
(30, Support { total: 25, voters: vec![(2, 10), (3, 15)] })
]
)
})
}
}
@@ -1,13 +1,13 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// Copyright (C) 2023 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
// 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,
@@ -15,25 +15,29 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! Autogenerated weights for pallet_election_provider_support_benchmarking
//! Autogenerated weights for frame_election_provider_support
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-04-23, STEPS: `1`, REPEAT: 1, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-02-22, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `runner-ehxwxxsd-project-145-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
// Executed Command:
// target/release/substrate
// target/production/substrate
// benchmark
// pallet
// --chain=dev
// --steps=1
// --repeat=1
// --pallet=pallet_election_provider_support_benchmarking
// --steps=50
// --repeat=20
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --output=frame/election-provider-support/src/weights.rs
// --json-file=/builds/parity/mirrors/substrate/.git/.artifacts/bench.json
// --pallet=frame_election_provider_support
// --chain=dev
// --header=./HEADER-APACHE2
// --output=./frame/election-provider-support/src/weights.rs
// --template=./.maintain/frame-weight-template.hbs
#![cfg_attr(rustfmt, rustfmt_skip)]
@@ -43,53 +47,108 @@
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;
/// Weight functions needed for pallet_election_provider_support_benchmarking.
/// Weight functions needed for frame_election_provider_support.
pub trait WeightInfo {
fn phragmen(v: u32, t: u32, d: u32, ) -> Weight;
fn phragmms(v: u32, t: u32, d: u32, ) -> Weight;
fn approval_voting(v: u32, t: u32, d: u32, ) -> Weight;
}
/// Weights for pallet_election_provider_support_benchmarking using the Substrate node and recommended hardware.
/// Weights for frame_election_provider_support using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn phragmen(v: u32, t: u32, d: u32, ) -> Weight {
Weight::from_ref_time(0 as u64)
// Standard Error: 667_000
.saturating_add(Weight::from_ref_time(32_973_000 as u64).saturating_mul(v as u64))
// Standard Error: 1_334_000
.saturating_add(Weight::from_ref_time(1_334_000 as u64).saturating_mul(t as u64))
// Standard Error: 60_644_000
.saturating_add(Weight::from_ref_time(2_636_364_000 as u64).saturating_mul(d as u64))
/// The range of component `v` is `[1000, 2000]`.
/// The range of component `t` is `[500, 1000]`.
/// The range of component `d` is `[5, 16]`.
fn phragmen(v: u32, _t: u32, d: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 5_789_174 nanoseconds.
Weight::from_ref_time(5_826_449_000)
.saturating_add(Weight::from_proof_size(0))
// Standard Error: 130_342
.saturating_add(Weight::from_ref_time(5_332_741).saturating_mul(v.into()))
// Standard Error: 13_325_769
.saturating_add(Weight::from_ref_time(1_416_874_101).saturating_mul(d.into()))
}
fn phragmms(v: u32, t: u32, d: u32, ) -> Weight {
Weight::from_ref_time(0 as u64)
// Standard Error: 73_000
.saturating_add(Weight::from_ref_time(21_073_000 as u64).saturating_mul(v as u64))
// Standard Error: 146_000
.saturating_add(Weight::from_ref_time(65_000 as u64).saturating_mul(t as u64))
// Standard Error: 6_649_000
.saturating_add(Weight::from_ref_time(1_711_424_000 as u64).saturating_mul(d as u64))
/// The range of component `v` is `[1000, 2000]`.
/// The range of component `t` is `[500, 1000]`.
/// The range of component `d` is `[5, 16]`.
fn phragmms(v: u32, _t: u32, d: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 4_151_790 nanoseconds.
Weight::from_ref_time(4_215_936_000)
.saturating_add(Weight::from_proof_size(0))
// Standard Error: 125_135
.saturating_add(Weight::from_ref_time(4_730_609).saturating_mul(v.into()))
// Standard Error: 12_793_390
.saturating_add(Weight::from_ref_time(1_474_383_961).saturating_mul(d.into()))
}
/// The range of component `v` is `[1000, 2000]`.
/// The range of component `t` is `[500, 1000]`.
/// The range of component `d` is `[5, 16]`.
fn approval_voting(v: u32, _t: u32, d: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 1_800_445 nanoseconds.
Weight::from_ref_time(1_824_645_000)
.saturating_add(Weight::from_proof_size(0))
// Standard Error: 26_266
.saturating_add(Weight::from_ref_time(1_229_576).saturating_mul(v.into()))
// Standard Error: 2_685_343
.saturating_add(Weight::from_ref_time(213_080_804).saturating_mul(d.into()))
}
}
// For backwards compatibility and tests
impl WeightInfo for () {
fn phragmen(v: u32, t: u32, d: u32, ) -> Weight {
Weight::from_ref_time(0 as u64)
// Standard Error: 667_000
.saturating_add(Weight::from_ref_time(32_973_000 as u64).saturating_mul(v as u64))
// Standard Error: 1_334_000
.saturating_add(Weight::from_ref_time(1_334_000 as u64).saturating_mul(t as u64))
// Standard Error: 60_644_000
.saturating_add(Weight::from_ref_time(2_636_364_000 as u64).saturating_mul(d as u64))
/// The range of component `v` is `[1000, 2000]`.
/// The range of component `t` is `[500, 1000]`.
/// The range of component `d` is `[5, 16]`.
fn phragmen(v: u32, _t: u32, d: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 5_789_174 nanoseconds.
Weight::from_ref_time(5_826_449_000)
.saturating_add(Weight::from_proof_size(0))
// Standard Error: 130_342
.saturating_add(Weight::from_ref_time(5_332_741).saturating_mul(v.into()))
// Standard Error: 13_325_769
.saturating_add(Weight::from_ref_time(1_416_874_101).saturating_mul(d.into()))
}
fn phragmms(v: u32, t: u32, d: u32, ) -> Weight {
Weight::from_ref_time(0 as u64)
// Standard Error: 73_000
.saturating_add(Weight::from_ref_time(21_073_000 as u64).saturating_mul(v as u64))
// Standard Error: 146_000
.saturating_add(Weight::from_ref_time(65_000 as u64).saturating_mul(t as u64))
// Standard Error: 6_649_000
.saturating_add(Weight::from_ref_time(1_711_424_000 as u64).saturating_mul(d as u64))
/// The range of component `v` is `[1000, 2000]`.
/// The range of component `t` is `[500, 1000]`.
/// The range of component `d` is `[5, 16]`.
fn phragmms(v: u32, _t: u32, d: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 4_151_790 nanoseconds.
Weight::from_ref_time(4_215_936_000)
.saturating_add(Weight::from_proof_size(0))
// Standard Error: 125_135
.saturating_add(Weight::from_ref_time(4_730_609).saturating_mul(v.into()))
// Standard Error: 12_793_390
.saturating_add(Weight::from_ref_time(1_474_383_961).saturating_mul(d.into()))
}
/// The range of component `v` is `[1000, 2000]`.
/// The range of component `t` is `[500, 1000]`.
/// The range of component `d` is `[5, 16]`.
fn approval_voting(v: u32, _t: u32, d: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 1_800_445 nanoseconds.
Weight::from_ref_time(1_824_645_000)
.saturating_add(Weight::from_proof_size(0))
// Standard Error: 26_266
.saturating_add(Weight::from_ref_time(1_229_576).saturating_mul(v.into()))
// Standard Error: 2_685_343
.saturating_add(Weight::from_ref_time(213_080_804).saturating_mul(d.into()))
}
}