Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Abstracts elections-phragmen pallet to use NposSolver #12588

Merged
merged 50 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
b714023
Abstracts elections-phragmen pallet to use NposSolver
gpestana Oct 31, 2022
452292f
Update frame/elections-phragmen/src/lib.rs
gpestana Nov 8, 2022
bf8d1c2
Update frame/elections-phragmen/src/lib.rs
gpestana Nov 8, 2022
5fa443e
changes the name of the pallet; adds changelog
gpestana Nov 8, 2022
f71c095
update changelog
gpestana Nov 8, 2022
e37206c
Adds weight testing
gpestana Nov 9, 2022
f7e970d
Adds log macro_rules
gpestana Nov 9, 2022
7b86c86
renames elections-phragment dir to elections
gpestana Nov 9, 2022
7ab9113
Merge remote-tracking branch 'origin/master' into gpestana8250_nposso…
Nov 13, 2022
e21f6ae
weights rename
gpestana Nov 13, 2022
e9c3daf
fixes typo in cargo toml
gpestana Nov 14, 2022
5d947af
pre/post solve weight scafolding
gpestana Nov 15, 2022
ecd7b63
refactor do_post_election
gpestana Nov 15, 2022
090aa90
refactors into pre and post election solve for independent benchmarking
gpestana Nov 15, 2022
1e2ef46
deconstructs PreElectionResults struct
gpestana Nov 15, 2022
d4b8c4e
updates benchmarking pre and post election solve; mock weights
gpestana Nov 16, 2022
c8a3508
Update frame/elections/src/lib.rs
gpestana Nov 17, 2022
8021572
Update frame/elections/src/lib.rs
gpestana Nov 17, 2022
c3db2cb
addresses PR comments
gpestana Nov 17, 2022
482dde3
adds pre_solve and post_sove weights
gpestana Nov 17, 2022
9749d12
Adds comments on election pallet id param name change
gpestana Nov 20, 2022
cbc8386
resolves conflict
gpestana Nov 20, 2022
4d51390
Merge branch 'master' of https://github.com/paritytech/substrate into…
Nov 21, 2022
26f9848
".git/.scripts/bench-bot.sh" pallet dev pallet_elections
Nov 22, 2022
afce2d2
Finishes pre-post solve weights
gpestana Nov 22, 2022
4208375
Update frame/elections/src/lib.rs
gpestana Nov 29, 2022
85e5a70
Update frame/elections/src/lib.rs
gpestana Nov 29, 2022
443f73d
Addresses PR comments: no panic in on_init path; nits
gpestana Nov 29, 2022
8945d96
Merge branch 'master' into gpestana8250_npossolver
gpestana Dec 8, 2022
7636999
Merge branch 'master' into gpestana8250_npossolver
gpestana Dec 12, 2022
0dc67e0
Merge branch 'master' into gpestana8250_npossolver
gpestana Jan 12, 2023
321b654
Fixes node build
gpestana Jan 12, 2023
ac36d28
Merge remote-tracking branch 'origin/master' into gpestana8250_nposso…
Jan 12, 2023
ee19357
Merge branch 'master' into gpestana8250_npossolver
gpestana Feb 8, 2023
e94d7a5
Merge branch 'master' into gpestana8250_npossolver
gpestana Feb 8, 2023
69111d9
Merge branch 'master' into gpestana8250_npossolver
gpestana Feb 14, 2023
bfe6fd9
Merge branch 'master' into gpestana8250_npossolver
gpestana Feb 15, 2023
8a1ebac
Merge branch 'master' into gpestana8250_npossolver
gpestana Feb 17, 2023
5343a6c
Implements approval voting to use as a `NposSolver` (#13367)
gpestana Feb 17, 2023
59900fc
fmt
gpestana Feb 20, 2023
da3339e
remove unecessary file
gpestana Feb 20, 2023
fea82e8
comment clarification
gpestana Feb 20, 2023
c1f5225
Merge remote-tracking branch 'origin/master' into gpestana8250_nposso…
Feb 20, 2023
64df412
re-run weights
Feb 20, 2023
614fe8a
fix typo
Feb 20, 2023
04483c6
updates MaxVoters in tests for integrity_tests to pass
gpestana Feb 20, 2023
51258d2
Merge remote-tracking branch 'origin/master' into gpestana8250_nposso…
Feb 21, 2023
281f7b8
Merge branch 'master' into gpestana8250_npossolver
gpestana Feb 22, 2023
70ea87a
Refactors election provider support benchmarks outside its own crate …
gpestana Feb 22, 2023
49e36db
Merge branch 'master' into gpestana8250_npossolver
gpestana Feb 22, 2023
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
265 changes: 125 additions & 140 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ members = [
"frame/democracy",
"frame/fast-unstake",
"frame/try-runtime",
"frame/elections-phragmen",
"frame/elections",
"frame/election-provider-multi-phase",
"frame/election-provider-support",
"frame/election-provider-support/benchmarking",
"frame/election-provider-support/solution-type",
"frame/election-provider-support/solution-type/fuzzer",
"frame/examples/basic",
Expand Down
12 changes: 5 additions & 7 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ pallet-contracts-primitives = { version = "7.0.0", default-features = false, pat
pallet-conviction-voting = { version = "4.0.0-dev", default-features = false, path = "../../../frame/conviction-voting" }
pallet-democracy = { version = "4.0.0-dev", default-features = false, path = "../../../frame/democracy" }
pallet-election-provider-multi-phase = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-multi-phase" }
pallet-election-provider-support-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-support/benchmarking", optional = true }
pallet-elections-phragmen = { version = "5.0.0-dev", default-features = false, path = "../../../frame/elections-phragmen" }
pallet-elections = { version = "5.0.0-dev", default-features = false, path = "../../../frame/elections" }
pallet-fast-unstake = { version = "4.0.0-dev", default-features = false, path = "../../../frame/fast-unstake" }
pallet-nis = { version = "4.0.0-dev", default-features = false, path = "../../../frame/nis" }
pallet-grandpa = { version = "4.0.0-dev", default-features = false, path = "../../../frame/grandpa" }
Expand Down Expand Up @@ -124,7 +123,6 @@ with-tracing = ["frame-executive/with-tracing"]
std = [
"pallet-whitelist/std",
"pallet-offences-benchmarking?/std",
"pallet-election-provider-support-benchmarking?/std",
"pallet-asset-tx-payment/std",
"frame-system-benchmarking?/std",
"frame-election-provider-support/std",
Expand All @@ -145,7 +143,7 @@ std = [
"pallet-contracts-primitives/std",
"pallet-conviction-voting/std",
"pallet-democracy/std",
"pallet-elections-phragmen/std",
"pallet-elections/std",
"pallet-fast-unstake/std",
"frame-executive/std",
"pallet-nis/std",
Expand Down Expand Up @@ -218,6 +216,7 @@ runtime-benchmarks = [
"frame-benchmarking-pallet-pov/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"frame-election-provider-support/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-alliance/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
Expand All @@ -231,8 +230,7 @@ runtime-benchmarks = [
"pallet-conviction-voting/runtime-benchmarks",
"pallet-democracy/runtime-benchmarks",
"pallet-election-provider-multi-phase/runtime-benchmarks",
"pallet-election-provider-support-benchmarking/runtime-benchmarks",
"pallet-elections-phragmen/runtime-benchmarks",
"pallet-elections/runtime-benchmarks",
"pallet-fast-unstake/runtime-benchmarks",
"pallet-nis/runtime-benchmarks",
"pallet-grandpa/runtime-benchmarks",
Expand Down Expand Up @@ -289,7 +287,7 @@ try-runtime = [
"pallet-conviction-voting/try-runtime",
"pallet-democracy/try-runtime",
"pallet-election-provider-multi-phase/try-runtime",
"pallet-elections-phragmen/try-runtime",
"pallet-elections/try-runtime",
"pallet-fast-unstake/try-runtime",
"pallet-nis/try-runtime",
"pallet-grandpa/try-runtime",
Expand Down
30 changes: 18 additions & 12 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

use codec::{Decode, Encode, MaxEncodedLen};
use frame_election_provider_support::{
onchain, BalancingConfig, ElectionDataProvider, SequentialPhragmen, VoteWeight,
onchain, weights::SubstrateWeight, ApprovalVoting, BalancingConfig, ElectionDataProvider,
SequentialPhragmen, VoteWeight,
};
use frame_support::{
construct_runtime,
Expand Down Expand Up @@ -1010,18 +1011,21 @@ parameter_types! {
pub const TermDuration: BlockNumber = 7 * DAYS;
pub const DesiredMembers: u32 = 13;
pub const DesiredRunnersUp: u32 = 7;
pub const MaxVotesPerVoter: u32 = 16;
pub const MaxVoters: u32 = 512;
pub const MaxCandidates: u32 = 64;
pub const ElectionsPhragmenPalletId: LockIdentifier = *b"phrelect";
pub const MaxVotesPerVoter: u32 = 16;
// The ElectionsPalletId parameter name was changed along with the renaming of the elections
// pallet, but we keep the same lock ID to prevent a migration from current runtimes.
// Related to https://github.com/paritytech/substrate/issues/8250
pub const ElectionsPalletId: LockIdentifier = *b"phrelect";
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
}

// Make sure that there are no more than `MaxMembers` members elected via elections-phragmen.
const_assert!(DesiredMembers::get() <= CouncilMaxMembers::get());

impl pallet_elections_phragmen::Config for Runtime {
impl pallet_elections::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type PalletId = ElectionsPhragmenPalletId;
type PalletId = ElectionsPalletId;
type Currency = Balances;
type ChangeMembers = Council;
// NOTE: this implies that council's genesis members cannot be set directly and must come from
Expand All @@ -1039,7 +1043,9 @@ impl pallet_elections_phragmen::Config for Runtime {
type MaxVoters = MaxVoters;
type MaxVotesPerVoter = MaxVotesPerVoter;
type MaxCandidates = MaxCandidates;
type WeightInfo = pallet_elections_phragmen::weights::SubstrateWeight<Runtime>;
type ElectionSolver = ApprovalVoting<Self::AccountId, Perbill>;
type SolverWeightInfo = SubstrateWeight<Runtime>;
type WeightInfo = pallet_elections::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -1737,7 +1743,7 @@ construct_runtime!(
Democracy: pallet_democracy,
Council: pallet_collective::<Instance1>,
TechnicalCommittee: pallet_collective::<Instance2>,
Elections: pallet_elections_phragmen,
Elections: pallet_elections,
TechnicalMembership: pallet_membership::<Instance1>,
Grandpa: pallet_grandpa,
Treasury: pallet_treasury,
Expand Down Expand Up @@ -1851,6 +1857,7 @@ mod benches {
frame_benchmarking::define_benchmarks!(
[frame_benchmarking, BaselineBench::<Runtime>]
[frame_benchmarking_pallet_pov, Pov]
[frame_election_provider_support, EPSBench::<Runtime>]
[pallet_alliance, Alliance]
[pallet_assets, Assets]
[pallet_babe, Babe]
Expand All @@ -1863,8 +1870,7 @@ mod benches {
[pallet_contracts, Contracts]
[pallet_democracy, Democracy]
[pallet_election_provider_multi_phase, ElectionProviderMultiPhase]
[pallet_election_provider_support_benchmarking, EPSBench::<Runtime>]
[pallet_elections_phragmen, Elections]
[pallet_elections, Elections]
[pallet_fast_unstake, FastUnstake]
[pallet_nis, Nis]
[pallet_grandpa, Grandpa]
Expand Down Expand Up @@ -2279,7 +2285,7 @@ impl_runtime_apis! {
// which is why we need these two lines below.
use pallet_session_benchmarking::Pallet as SessionBench;
use pallet_offences_benchmarking::Pallet as OffencesBench;
use pallet_election_provider_support_benchmarking::Pallet as EPSBench;
use frame_election_provider_support::benchmarking::Pallet as EPSBench;
use frame_system_benchmarking::Pallet as SystemBench;
use baseline::Pallet as BaselineBench;
use pallet_nomination_pools_benchmarking::Pallet as NominationPoolsBench;
Expand All @@ -2302,14 +2308,14 @@ impl_runtime_apis! {
// which is why we need these two lines below.
use pallet_session_benchmarking::Pallet as SessionBench;
use pallet_offences_benchmarking::Pallet as OffencesBench;
use pallet_election_provider_support_benchmarking::Pallet as EPSBench;
use frame_election_provider_support::benchmarking::Pallet as EPSBench;
use frame_system_benchmarking::Pallet as SystemBench;
use baseline::Pallet as BaselineBench;
use pallet_nomination_pools_benchmarking::Pallet as NominationPoolsBench;

impl pallet_session_benchmarking::Config for Runtime {}
impl pallet_offences_benchmarking::Config for Runtime {}
impl pallet_election_provider_support_benchmarking::Config for Runtime {}
impl frame_election_provider_support::benchmarking::Config for Runtime {}
impl frame_system_benchmarking::Config for Runtime {}
impl baseline::Config for Runtime {}
impl pallet_nomination_pools_benchmarking::Config for Runtime {}
Expand Down
2 changes: 0 additions & 2 deletions frame/election-provider-multi-phase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ frame-election-provider-support = { version = "4.0.0-dev", default-features = fa

# Optional imports for benchmarking
frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../benchmarking", optional = true }
pallet-election-provider-support-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../election-provider-support/benchmarking", optional = true }
rand = { version = "0.8.5", default-features = false, features = ["alloc", "small_rng"], optional = true }
strum = { version = "0.24.1", default-features = false, features = ["derive"], optional = true }

Expand All @@ -50,7 +49,6 @@ frame-benchmarking = { version = "4.0.0-dev", path = "../benchmarking" }
[features]
default = ["std"]
std = [
"pallet-election-provider-support-benchmarking?/std",
"codec/std",
"scale-info/std",
"log/std",
Expand Down
8 changes: 7 additions & 1 deletion frame/election-provider-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ sp-runtime = { version = "7.0.0", default-features = false, path = "../../primit
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }
sp-core = { version = "7.0.0", default-features = false, path = "../../primitives/core" }

frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../benchmarking", optional = true }

[dev-dependencies]
rand = { version = "0.8.5", features = ["small_rng"] }
sp-io = { version = "7.0.0", path = "../../primitives/io" }
Expand All @@ -41,6 +43,10 @@ std = [
"sp-core/std",
"sp-runtime/std",
"sp-std/std",

"frame-benchmarking?/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
]
runtime-benchmarks = []
try-runtime = []
37 changes: 0 additions & 37 deletions frame/election-provider-support/benchmarking/Cargo.toml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
//! Election provider support pallet benchmarking.
//! This is separated into its own crate to avoid bloating the size of the runtime.
#![cfg(feature = "runtime-benchmarks")]
#![cfg_attr(not(feature = "std"), no_std)]

use crate::{ApprovalVoting, NposSolver, PhragMMS, SequentialPhragmen};
use codec::Decode;
use frame_benchmarking::v1::{benchmarks, Vec};
use frame_election_provider_support::{NposSolver, PhragMMS, SequentialPhragmen};

pub struct Pallet<T: Config>(frame_system::Pallet<T>);
pub trait Config: frame_system::Config {}
Expand Down Expand Up @@ -88,4 +85,17 @@ benchmarks! {
::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()
);
}
}
26 changes: 26 additions & 0 deletions frame/election-provider-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>);

Expand Down
30 changes: 29 additions & 1 deletion frame/election-provider-support/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -235,6 +235,7 @@ mod tests {

struct PhragmenParams;
struct PhragMMSParams;
struct ApprovalVotingParams;

parameter_types! {
pub static MaxWinners: u32 = 10;
Expand All @@ -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};

Expand Down Expand Up @@ -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)] })
]
)
})
}
}
Loading