Incorporate the new electing/electable naming into the code (#10956)

* Incorporate the new electing/electable naming into the code

* Update frame/election-provider-support/src/lib.rs

Co-authored-by: Zeke Mostov <z.mostov@gmail.com>

* Update frame/election-provider-support/src/lib.rs

Co-authored-by: Zeke Mostov <z.mostov@gmail.com>

* Some additional changes

* fmt

* update codec

* revert lock file to master

* fix doc test

Co-authored-by: Zeke Mostov <z.mostov@gmail.com>
This commit is contained in:
Kian Paimani
2022-03-19 12:24:41 +00:00
committed by GitHub
parent bbfdd38b69
commit 95192f0cda
9 changed files with 81 additions and 69 deletions
+7 -9
View File
@@ -46,11 +46,12 @@ use frame_system::{
pub use node_primitives::{AccountId, Signature};
use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Index, Moment};
use pallet_contracts::weights::WeightInfo;
use pallet_election_provider_multi_phase::SolutionAccuracyOf;
use pallet_grandpa::{
fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_session::historical as pallet_session_historical;
use pallet_session::historical::{self as pallet_session_historical};
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use sp_api::impl_runtime_apis;
@@ -668,20 +669,17 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type DataProvider = Staking;
type Solution = NposSolution16;
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback =
frame_election_provider_support::onchain::OnChainSequentialPhragmen<Self>;
type GovernanceFallback = onchain::OnChainSequentialPhragmen<Self>;
type Solver = frame_election_provider_support::SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Self>,
SolutionAccuracyOf<Self>,
OffchainRandomBalancing,
>;
type WeightInfo = pallet_election_provider_multi_phase::weights::SubstrateWeight<Self>;
type ForceOrigin = EnsureRootOrHalfCouncil;
type MaxElectableTargets = ConstU16<{ u16::MAX }>;
type MaxElectingVoters = ConstU32<10_000>;
type BenchmarkingConfig = ElectionProviderBenchmarkConfig;
// BagsList allows a practically unbounded count of nominators to participate in NPoS elections.
// To ensure we respect memory limits when using the BagsList this must be set to a number of
// voters we know can fit into a single vec allocation.
type VoterSnapshotPerBlock = ConstU32<10_000>;
type WeightInfo = pallet_election_provider_multi_phase::weights::SubstrateWeight<Self>;
}
parameter_types! {
@@ -56,7 +56,8 @@ pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>
);
let voters =
<pallet_staking::Pallet<Runtime> as ElectionDataProvider>::voters(voter_limit).unwrap();
<pallet_staking::Pallet<Runtime> as ElectionDataProvider>::electing_voters(voter_limit)
.unwrap();
let mut voters_nominator_only = voters
.iter()
@@ -260,8 +260,8 @@ frame_benchmarking::benchmarks! {
// we don't directly need the data-provider to be populated, but it is just easy to use it.
set_up_data_provider::<T>(v, t);
let targets = T::DataProvider::targets(None)?;
let voters = T::DataProvider::voters(None)?;
let targets = T::DataProvider::electable_targets(None)?;
let voters = T::DataProvider::electing_voters(None)?;
let desired_targets = T::DataProvider::desired_targets()?;
assert!(<MultiPhase<T>>::snapshot().is_none());
}: {
@@ -69,7 +69,7 @@
//! Upon the end of the signed phase, the solutions are examined from best to worse (i.e. `pop()`ed
//! until drained). Each solution undergoes an expensive `Pallet::feasibility_check`, which ensures
//! the score claimed by this score was correct, and it is valid based on the election data (i.e.
//! votes and candidates). At each step, if the current best solution passes the feasibility check,
//! votes and targets). At each step, if the current best solution passes the feasibility check,
//! it is considered to be the best one. The sender of the origin is rewarded, and the rest of the
//! queued solutions get their deposit back and are discarded, without being checked.
//!
@@ -249,7 +249,6 @@ use sp_npos_elections::{
assignment_ratio_to_staked_normalized, ElectionScore, EvaluateSupport, Supports, VoteWeight,
};
use sp_runtime::{
traits::Bounded,
transaction_validity::{
InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity,
TransactionValidityError, ValidTransaction,
@@ -643,14 +642,15 @@ pub mod pallet {
#[pallet::constant]
type SignedDepositWeight: Get<BalanceOf<Self>>;
/// The maximum number of voters to put in the snapshot. At the moment, snapshots are only
/// over a single block, but once multi-block elections are introduced they will take place
/// over multiple blocks.
///
/// Also, note the data type: If the voters are represented by a `u32` in `type
/// CompactSolution`, the same `u32` is used here to ensure bounds are respected.
/// The maximum number of electing voters to put in the snapshot. At the moment, snapshots
/// are only over a single block, but once multi-block elections are introduced they will
/// take place over multiple blocks.
#[pallet::constant]
type VoterSnapshotPerBlock: Get<SolutionVoterIndexOf<Self>>;
type MaxElectingVoters: Get<SolutionVoterIndexOf<Self>>;
/// The maximum number of electable targets to put in the snapshot.
#[pallet::constant]
type MaxElectableTargets: Get<SolutionTargetIndexOf<Self>>;
/// Handler for the slashed deposits.
type SlashHandler: OnUnbalanced<NegativeImbalanceOf<Self>>;
@@ -817,7 +817,7 @@ pub mod pallet {
fn integrity_test() {
use sp_std::mem::size_of;
// The index type of both voters and targets need to be smaller than that of usize (very
// unlikely to be the case, but anyhow).
// unlikely to be the case, but anyhow)..
assert!(size_of::<SolutionVoterIndexOf<T>>() <= size_of::<usize>());
assert!(size_of::<SolutionTargetIndexOf<T>>() <= size_of::<usize>());
@@ -1338,14 +1338,13 @@ impl<T: Config> Pallet<T> {
/// Extracted for easier weight calculation.
fn create_snapshot_external(
) -> Result<(Vec<T::AccountId>, Vec<VoterOf<T>>, u32), ElectionError<T>> {
let target_limit = <SolutionTargetIndexOf<T>>::max_value().saturated_into::<usize>();
// for now we have just a single block snapshot.
let voter_limit = T::VoterSnapshotPerBlock::get().saturated_into::<usize>();
let target_limit = T::MaxElectableTargets::get().saturated_into::<usize>();
let voter_limit = T::MaxElectingVoters::get().saturated_into::<usize>();
let targets =
T::DataProvider::targets(Some(target_limit)).map_err(ElectionError::DataProvider)?;
let voters =
T::DataProvider::voters(Some(voter_limit)).map_err(ElectionError::DataProvider)?;
let targets = T::DataProvider::electable_targets(Some(target_limit))
.map_err(ElectionError::DataProvider)?;
let voters = T::DataProvider::electing_voters(Some(voter_limit))
.map_err(ElectionError::DataProvider)?;
let mut desired_targets =
T::DataProvider::desired_targets().map_err(ElectionError::DataProvider)?;
@@ -2090,7 +2089,7 @@ mod tests {
// we have 8 voters in total.
assert_eq!(crate::mock::Voters::get().len(), 8);
// but we want to take 2.
crate::mock::VoterSnapshotPerBlock::set(2);
crate::mock::MaxElectingVoters::set(2);
// Signed phase opens just fine.
roll_to(15);
@@ -263,7 +263,8 @@ parameter_types! {
pub static MinerMaxWeight: Weight = BlockWeights::get().max_block;
pub static MinerMaxLength: u32 = 256;
pub static MockWeightInfo: bool = false;
pub static VoterSnapshotPerBlock: VoterIndex = u32::max_value();
pub static MaxElectingVoters: VoterIndex = u32::max_value();
pub static MaxElectableTargets: TargetIndex = TargetIndex::max_value();
pub static EpochLength: u64 = 30;
pub static OnChianFallback: bool = true;
@@ -413,7 +414,8 @@ impl crate::Config for Runtime {
type GovernanceFallback = NoFallback<Self>;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type Solution = TestNposSolution;
type VoterSnapshotPerBlock = VoterSnapshotPerBlock;
type MaxElectingVoters = MaxElectingVoters;
type MaxElectableTargets = MaxElectableTargets;
type Solver = SequentialPhragmen<AccountId, SolutionAccuracyOf<Runtime>, Balancing>;
}
@@ -439,7 +441,8 @@ impl ElectionDataProvider for StakingMock {
type AccountId = AccountId;
type BlockNumber = u64;
type MaxVotesPerVoter = MaxNominations;
fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
fn electable_targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
let targets = Targets::get();
if maybe_max_len.map_or(false, |max_len| targets.len() > max_len) {
@@ -449,7 +452,9 @@ impl ElectionDataProvider for StakingMock {
Ok(targets)
}
fn voters(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<VoterOf<Runtime>>> {
fn electing_voters(
maybe_max_len: Option<usize>,
) -> data_provider::Result<Vec<VoterOf<Runtime>>> {
let mut voters = Voters::get();
if let Some(max_len) = maybe_max_len {
voters.truncate(max_len)
@@ -107,12 +107,12 @@
//! fn desired_targets() -> data_provider::Result<u32> {
//! Ok(1)
//! }
//! fn voters(maybe_max_len: Option<usize>)
//! fn electing_voters(maybe_max_len: Option<usize>)
//! -> data_provider::Result<Vec<VoterOf<Self>>>
//! {
//! Ok(Default::default())
//! }
//! fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
//! fn electable_targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
//! Ok(vec![10, 20, 30])
//! }
//! fn next_election_prediction(now: BlockNumber) -> BlockNumber {
@@ -138,7 +138,7 @@
//! type DataProvider = T::DataProvider;
//!
//! fn elect() -> Result<Supports<AccountId>, Self::Error> {
//! Self::DataProvider::targets(None)
//! Self::DataProvider::electable_targets(None)
//! .map_err(|_| "failed to elect")
//! .map(|t| vec![(t[0], Support::default())])
//! }
@@ -265,16 +265,19 @@ pub trait ElectionDataProvider {
/// Maximum number of votes per voter that this data provider is providing.
type MaxVotesPerVoter: Get<u32>;
/// All possible targets for the election, i.e. the candidates.
/// All possible targets for the election, i.e. the targets that could become elected, thus
/// "electable".
///
/// If `maybe_max_len` is `Some(v)` then the resulting vector MUST NOT be longer than `v` items
/// long.
///
/// This should be implemented as a self-weighing function. The implementor should register its
/// appropriate weight at the end of execution with the system pallet directly.
fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<Self::AccountId>>;
fn electable_targets(
maybe_max_len: Option<usize>,
) -> data_provider::Result<Vec<Self::AccountId>>;
/// All possible voters for the election.
/// All the voters that participate in the election, thus "electing".
///
/// Note that if a notion of self-vote exists, it should be represented here.
///
@@ -283,7 +286,7 @@ pub trait ElectionDataProvider {
///
/// This should be implemented as a self-weighing function. The implementor should register its
/// appropriate weight at the end of execution with the system pallet directly.
fn voters(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>>;
fn electing_voters(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>>;
/// The number of targets to elect.
///
@@ -79,10 +79,11 @@ impl<T: Config> OnChainSequentialPhragmen<T> {
maybe_max_voters: Option<usize>,
maybe_max_targets: Option<usize>,
) -> Result<Supports<T::AccountId>, Error> {
let voters = <Self as ElectionProvider>::DataProvider::voters(maybe_max_voters)
.map_err(Error::DataProvider)?;
let targets = <Self as ElectionProvider>::DataProvider::targets(maybe_max_targets)
let voters = <Self as ElectionProvider>::DataProvider::electing_voters(maybe_max_voters)
.map_err(Error::DataProvider)?;
let targets =
<Self as ElectionProvider>::DataProvider::electable_targets(maybe_max_targets)
.map_err(Error::DataProvider)?;
let desired_targets = <Self as ElectionProvider>::DataProvider::desired_targets()
.map_err(Error::DataProvider)?;
@@ -197,7 +198,7 @@ mod tests {
type AccountId = AccountId;
type BlockNumber = BlockNumber;
type MaxVotesPerVoter = ConstU32<2>;
fn voters(_: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>> {
fn electing_voters(_: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>> {
Ok(vec![
(1, 10, bounded_vec![10, 20]),
(2, 20, bounded_vec![30, 20]),
@@ -205,7 +206,7 @@ mod tests {
])
}
fn targets(_: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
fn electable_targets(_: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
Ok(vec![10, 20, 30])
}
+2 -2
View File
@@ -860,7 +860,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
Ok(Self::validator_count())
}
fn voters(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>> {
fn electing_voters(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>> {
// This can never fail -- if `maybe_max_len` is `Some(_)` we handle it.
let voters = Self::get_npos_voters(maybe_max_len);
debug_assert!(maybe_max_len.map_or(true, |max| voters.len() <= max));
@@ -868,7 +868,7 @@ impl<T: Config> ElectionDataProvider for Pallet<T> {
Ok(voters)
}
fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<T::AccountId>> {
fn electable_targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<T::AccountId>> {
let target_count = Validators::<T>::count();
// We can't handle this case yet -- return an error.
+26 -21
View File
@@ -4055,10 +4055,12 @@ mod election_data_provider {
#[test]
fn voters_include_self_vote() {
ExtBuilder::default().nominate(false).build_and_execute(|| {
assert!(<Validators<Test>>::iter().map(|(x, _)| x).all(|v| Staking::voters(None)
.unwrap()
.into_iter()
.any(|(w, _, t)| { v == w && t[0] == w })))
assert!(<Validators<Test>>::iter().map(|(x, _)| x).all(|v| Staking::electing_voters(
None
)
.unwrap()
.into_iter()
.any(|(w, _, t)| { v == w && t[0] == w })))
})
}
@@ -4067,7 +4069,7 @@ mod election_data_provider {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]);
assert_eq!(
<Staking as ElectionDataProvider>::voters(None)
<Staking as ElectionDataProvider>::electing_voters(None)
.unwrap()
.iter()
.find(|x| x.0 == 101)
@@ -4082,7 +4084,7 @@ mod election_data_provider {
// 11 is gone.
start_active_era(2);
assert_eq!(
<Staking as ElectionDataProvider>::voters(None)
<Staking as ElectionDataProvider>::electing_voters(None)
.unwrap()
.iter()
.find(|x| x.0 == 101)
@@ -4094,7 +4096,7 @@ mod election_data_provider {
// resubmit and it is back
assert_ok!(Staking::nominate(Origin::signed(100), vec![11, 21]));
assert_eq!(
<Staking as ElectionDataProvider>::voters(None)
<Staking as ElectionDataProvider>::electing_voters(None)
.unwrap()
.iter()
.find(|x| x.0 == 101)
@@ -4118,20 +4120,23 @@ mod election_data_provider {
);
// if limits is less..
assert_eq!(Staking::voters(Some(1)).unwrap().len(), 1);
assert_eq!(Staking::electing_voters(Some(1)).unwrap().len(), 1);
// if limit is equal..
assert_eq!(Staking::voters(Some(5)).unwrap().len(), 5);
assert_eq!(Staking::electing_voters(Some(5)).unwrap().len(), 5);
// if limit is more.
assert_eq!(Staking::voters(Some(55)).unwrap().len(), 5);
assert_eq!(Staking::electing_voters(Some(55)).unwrap().len(), 5);
// if target limit is more..
assert_eq!(Staking::targets(Some(6)).unwrap().len(), 4);
assert_eq!(Staking::targets(Some(4)).unwrap().len(), 4);
assert_eq!(Staking::electable_targets(Some(6)).unwrap().len(), 4);
assert_eq!(Staking::electable_targets(Some(4)).unwrap().len(), 4);
// if target limit is less, then we return an error.
assert_eq!(Staking::targets(Some(1)).unwrap_err(), "Target snapshot too big");
assert_eq!(
Staking::electable_targets(Some(1)).unwrap_err(),
"Target snapshot too big"
);
});
}
@@ -4165,7 +4170,7 @@ mod election_data_provider {
// we take 4 voters: 2 validators and 2 nominators (so nominators quota = 2)
assert_eq!(
Staking::voters(Some(3))
Staking::electing_voters(Some(3))
.unwrap()
.iter()
.map(|(stash, _, _)| stash)
@@ -4204,7 +4209,7 @@ mod election_data_provider {
// we take 5 voters
assert_eq!(
Staking::voters(Some(5))
Staking::electing_voters(Some(5))
.unwrap()
.iter()
.map(|(stash, _, _)| stash)
@@ -4225,7 +4230,7 @@ mod election_data_provider {
// we take 4 voters
assert_eq!(
Staking::voters(Some(4))
Staking::electing_voters(Some(4))
.unwrap()
.iter()
.map(|(stash, _, _)| stash)
@@ -4666,7 +4671,7 @@ fn change_of_max_nominations() {
vec![(70, 3), (101, 2), (60, 1)]
);
// 3 validators and 3 nominators
assert_eq!(Staking::voters(None).unwrap().len(), 3 + 3);
assert_eq!(Staking::electing_voters(None).unwrap().len(), 3 + 3);
// abrupt change from 16 to 4, everyone should be fine.
MaxNominations::set(4);
@@ -4677,7 +4682,7 @@ fn change_of_max_nominations() {
.collect::<Vec<_>>(),
vec![(70, 3), (101, 2), (60, 1)]
);
assert_eq!(Staking::voters(None).unwrap().len(), 3 + 3);
assert_eq!(Staking::electing_voters(None).unwrap().len(), 3 + 3);
// abrupt change from 4 to 3, everyone should be fine.
MaxNominations::set(3);
@@ -4688,7 +4693,7 @@ fn change_of_max_nominations() {
.collect::<Vec<_>>(),
vec![(70, 3), (101, 2), (60, 1)]
);
assert_eq!(Staking::voters(None).unwrap().len(), 3 + 3);
assert_eq!(Staking::electing_voters(None).unwrap().len(), 3 + 3);
// abrupt change from 3 to 2, this should cause some nominators to be non-decodable, and
// thus non-existent unless if they update.
@@ -4705,7 +4710,7 @@ fn change_of_max_nominations() {
// but its value cannot be decoded and default is returned.
assert!(Nominators::<Test>::get(70).is_none());
assert_eq!(Staking::voters(None).unwrap().len(), 3 + 2);
assert_eq!(Staking::electing_voters(None).unwrap().len(), 3 + 2);
assert!(Nominators::<Test>::contains_key(101));
// abrupt change from 2 to 1, this should cause some nominators to be non-decodable, and
@@ -4722,7 +4727,7 @@ fn change_of_max_nominations() {
assert!(Nominators::<Test>::contains_key(60));
assert!(Nominators::<Test>::get(70).is_none());
assert!(Nominators::<Test>::get(60).is_some());
assert_eq!(Staking::voters(None).unwrap().len(), 3 + 1);
assert_eq!(Staking::electing_voters(None).unwrap().len(), 3 + 1);
// now one of them can revive themselves by re-nominating to a proper value.
assert_ok!(Staking::nominate(Origin::signed(71), vec![1]));