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

Use proper bounded vector type for nominations #10601

Merged
merged 27 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5a990c4
Use proper bounded vector type for nominations
kianenigma Jan 6, 2022
fb60d30
add docs and tweak chill_other for cleanup purposes
kianenigma Jan 6, 2022
b96f3af
Fix the build
kianenigma Jan 6, 2022
1b8376d
remove TODO
kianenigma Jan 6, 2022
b93f109
add a bit more doc
kianenigma Jan 6, 2022
d3cb930
even more docs
kianenigma Jan 6, 2022
b55f9e2
Update frame/staking/src/pallet/mod.rs
kianenigma Jan 7, 2022
85ff60f
Update frame/staking/src/pallet/mod.rs
kianenigma Jan 7, 2022
ef51ec4
Fix the nasty bug
kianenigma Jan 7, 2022
6f5db81
Merge branch 'kiz-bound-nominator-votes' of github.com:paritytech/sub…
kianenigma Jan 7, 2022
5ec63fb
Merge branch 'master' of github.com:paritytech/substrate into kiz-bou…
kianenigma Jan 7, 2022
477079c
also bound the Snapshot type
kianenigma Jan 7, 2022
419354c
fix doc test
kianenigma Jan 7, 2022
ec6b11b
document bounded_vec
kianenigma Jan 7, 2022
2864d22
self-review
kianenigma Jan 7, 2022
7ca67ee
remove unused
kianenigma Jan 8, 2022
c868c41
Fix build
kianenigma Jan 8, 2022
104ba4a
frame-support: repetition overload for bounded_vec
ggwpez Jan 12, 2022
eb8904e
fix
kianenigma Jan 19, 2022
587f715
remove the need to allocate into unbounded voters etc etc
kianenigma Jan 19, 2022
88cb4a6
Don't expect
kianenigma Jan 19, 2022
17bad6a
Merge branch 'master' of github.com:paritytech/substrate into kiz-bou…
kianenigma Jan 19, 2022
437722a
Master.into()
kianenigma Jan 19, 2022
a831b08
unbreal the build again
kianenigma Jan 19, 2022
ac7fc12
Merge branch 'master' of github.com:paritytech/substrate into kiz-bou…
kianenigma Jan 20, 2022
5e1b8f0
handle macro a bit better
kianenigma Jan 20, 2022
7fe85d2
Merge branch 'master' of github.com:paritytech/substrate into kiz-bou…
kianenigma Jan 24, 2022
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
6 changes: 3 additions & 3 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use hex_literal::hex;
use node_runtime::{
constants::currency::*, wasm_binary_unwrap, AuthorityDiscoveryConfig, BabeConfig,
BalancesConfig, Block, CouncilConfig, DemocracyConfig, ElectionsConfig, GrandpaConfig,
ImOnlineConfig, IndicesConfig, SessionConfig, SessionKeys, SocietyConfig, StakerStatus,
StakingConfig, SudoConfig, SystemConfig, TechnicalCommitteeConfig, MAX_NOMINATIONS,
ImOnlineConfig, IndicesConfig, MaxNominations, SessionConfig, SessionKeys, SocietyConfig,
StakerStatus, StakingConfig, SudoConfig, SystemConfig, TechnicalCommitteeConfig,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use sc_chain_spec::ChainSpecExtension;
Expand Down Expand Up @@ -278,7 +278,7 @@ pub fn testnet_genesis(
.map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator))
.chain(initial_nominators.iter().map(|x| {
use rand::{seq::SliceRandom, Rng};
let limit = (MAX_NOMINATIONS as usize).min(initial_authorities.len());
let limit = (MaxNominations::get() as usize).min(initial_authorities.len());
let count = rng.gen::<usize>() % limit;
let nominations = initial_authorities
.as_slice()
Expand Down
8 changes: 5 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ impl pallet_staking::BenchmarkingConfig for StakingBenchmarkingConfig {
}

impl pallet_staking::Config for Runtime {
const MAX_NOMINATIONS: u32 = MAX_NOMINATIONS;
type MaxNominations = MaxNominations;
type Currency = Balances;
type UnixTime = Timestamp;
type CurrencyToVote = U128CurrencyToVote;
Expand Down Expand Up @@ -605,7 +605,9 @@ sp_npos_elections::generate_solution_type!(
>(16)
);

pub const MAX_NOMINATIONS: u32 = <NposSolution16 as sp_npos_elections::NposSolution>::LIMIT as u32;
parameter_types! {
pub MaxNominations: u32 = <NposSolution16 as sp_npos_elections::NposSolution>::LIMIT as u32;
}

/// The numbers configured here could always be more than the the maximum limits of staking pallet
/// to ensure election snapshot will not run out of memory. For now, we set them to smaller values
Expand Down Expand Up @@ -1816,7 +1818,7 @@ mod tests {
#[test]
fn perbill_as_onchain_accuracy() {
type OnChainAccuracy = <Runtime as onchain::Config>::Accuracy;
let maximum_chain_accuracy: Vec<UpperOf<OnChainAccuracy>> = (0..MAX_NOMINATIONS)
let maximum_chain_accuracy: Vec<UpperOf<OnChainAccuracy>> = (0..MaxNominations::get())
.map(|_| <UpperOf<OnChainAccuracy>>::from(OnChainAccuracy::one().deconstruct()))
.collect();
let _: UpperOf<OnChainAccuracy> =
Expand Down
2 changes: 1 addition & 1 deletion frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl onchain::Config for Test {
}

impl pallet_staking::Config for Test {
const MAX_NOMINATIONS: u32 = 16;
type MaxNominations = ConstU32<16>;
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type Event = Event;
Expand Down
8 changes: 5 additions & 3 deletions frame/election-provider-multi-phase/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn set_up_data_provider<T: Config>(v: u32, t: u32) {
info,
"setting up with voters = {} [degree = {}], targets = {}",
v,
T::DataProvider::MAXIMUM_VOTES_PER_VOTER,
<T::DataProvider as ElectionDataProvider>::MaxVotesPerVoter::get(),
t
);

Expand All @@ -165,8 +165,10 @@ fn set_up_data_provider<T: Config>(v: u32, t: u32) {
})
.collect::<Vec<_>>();
// we should always have enough voters to fill.
assert!(targets.len() > T::DataProvider::MAXIMUM_VOTES_PER_VOTER as usize);
targets.truncate(T::DataProvider::MAXIMUM_VOTES_PER_VOTER as usize);
assert!(
targets.len() > <T::DataProvider as ElectionDataProvider>::MaxVotesPerVoter::get() as usize
);
targets.truncate(<T::DataProvider as ElectionDataProvider>::MaxVotesPerVoter::get() as usize);

// fill voters.
(0..v).for_each(|i| {
Expand Down
2 changes: 1 addition & 1 deletion frame/election-provider-multi-phase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ pub mod pallet {
// NOTE that this pallet does not really need to enforce this in runtime. The
// solution cannot represent any voters more than `LIMIT` anyhow.
assert_eq!(
<T::DataProvider as ElectionDataProvider>::MAXIMUM_VOTES_PER_VOTER,
<T::DataProvider as ElectionDataProvider>::MaxVotesPerVoter::get(),
<SolutionOf<T> as NposSolution>::LIMIT as u32,
);
}
Expand Down
6 changes: 5 additions & 1 deletion frame/election-provider-multi-phase/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,18 @@ where

pub type Extrinsic = sp_runtime::testing::TestXt<Call, ()>;

parameter_types! {
pub MaxNominations: u32 = <TestNposSolution as NposSolution>::LIMIT as u32;
}

#[derive(Default)]
pub struct ExtBuilder {}

pub struct StakingMock;
impl ElectionDataProvider for StakingMock {
type AccountId = AccountId;
type BlockNumber = u64;
const MAXIMUM_VOTES_PER_VOTER: u32 = <TestNposSolution as NposSolution>::LIMIT as u32;
type MaxVotesPerVoter = MaxNominations;
fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
let targets = Targets::get();

Expand Down
10 changes: 7 additions & 3 deletions frame/election-provider-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
//! ```rust
//! # use frame_election_provider_support::{*, data_provider};
//! # use sp_npos_elections::{Support, Assignment};
//! # use frame_support::traits::ConstU32;
//!
//! type AccountId = u64;
//! type Balance = u64;
Expand All @@ -101,7 +102,7 @@
//! impl<T: Config> ElectionDataProvider for Pallet<T> {
//! type AccountId = AccountId;
//! type BlockNumber = BlockNumber;
//! const MAXIMUM_VOTES_PER_VOTER: u32 = 1;
//! type MaxVotesPerVoter = ConstU32<1>;
//!
//! fn desired_targets() -> data_provider::Result<u32> {
//! Ok(1)
Expand Down Expand Up @@ -191,7 +192,7 @@ pub trait ElectionDataProvider {
type BlockNumber;

/// Maximum number of votes per voter that this data provider is providing.
const MAXIMUM_VOTES_PER_VOTER: u32;
type MaxVotesPerVoter: Get<u32>;

/// All possible targets for the election, i.e. the candidates.
///
Expand Down Expand Up @@ -266,19 +267,22 @@ pub struct TestDataProvider<X>(sp_std::marker::PhantomData<X>);
impl<AccountId, BlockNumber> ElectionDataProvider for TestDataProvider<(AccountId, BlockNumber)> {
type AccountId = AccountId;
type BlockNumber = BlockNumber;
type MaxVotesPerVoter = ();

const MAXIMUM_VOTES_PER_VOTER: u32 = 0;
fn targets(_maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
Ok(Default::default())
}

fn voters(
_maybe_max_len: Option<usize>,
) -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>> {
Ok(Default::default())
}

fn desired_targets() -> data_provider::Result<u32> {
Ok(Default::default())
}

fn next_election_prediction(now: BlockNumber) -> BlockNumber {
now
}
Expand Down
4 changes: 3 additions & 1 deletion frame/election-provider-support/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,16 @@ mod tests {
type OnChainPhragmen = OnChainSequentialPhragmen<Runtime>;

mod mock_data_provider {
use frame_support::traits::ConstU32;

use super::*;
use crate::data_provider;

pub struct DataProvider;
impl ElectionDataProvider for DataProvider {
type AccountId = AccountId;
type BlockNumber = BlockNumber;
const MAXIMUM_VOTES_PER_VOTER: u32 = 2;
type MaxVotesPerVoter = ConstU32<2>;
fn voters(
_: Option<usize>,
) -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>> {
Expand Down
2 changes: 1 addition & 1 deletion frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl onchain::Config for Test {
}

impl pallet_staking::Config for Test {
const MAX_NOMINATIONS: u32 = 16;
type MaxNominations = ConstU32<16>;
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type Event = Event;
Expand Down
8 changes: 4 additions & 4 deletions frame/offences/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod mock;
use sp_std::{prelude::*, vec};

use frame_benchmarking::{account, benchmarks};
use frame_support::traits::{Currency, ValidatorSet, ValidatorSetWithIdentification};
use frame_support::traits::{Currency, Get, ValidatorSet, ValidatorSetWithIdentification};
use frame_system::{Config as SystemConfig, Pallet as System, RawOrigin};

use sp_runtime::{
Expand Down Expand Up @@ -275,7 +275,7 @@ benchmarks! {
let r in 1 .. MAX_REPORTERS;
// we skip 1 offender, because in such case there is no slashing
let o in 2 .. MAX_OFFENDERS;
let n in 0 .. MAX_NOMINATORS.min(<T as pallet_staking::Config>::MAX_NOMINATIONS);
let n in 0 .. MAX_NOMINATORS.min(<T as pallet_staking::Config>::MaxNominations::get());

// Make r reporters
let mut reporters = vec![];
Expand Down Expand Up @@ -381,7 +381,7 @@ benchmarks! {
}

report_offence_grandpa {
let n in 0 .. MAX_NOMINATORS.min(<T as pallet_staking::Config>::MAX_NOMINATIONS);
let n in 0 .. MAX_NOMINATORS.min(<T as pallet_staking::Config>::MaxNominations::get());

// for grandpa equivocation reports the number of reporters
// and offenders is always 1
Expand Down Expand Up @@ -416,7 +416,7 @@ benchmarks! {
}

report_offence_babe {
let n in 0 .. MAX_NOMINATORS.min(<T as pallet_staking::Config>::MAX_NOMINATIONS);
let n in 0 .. MAX_NOMINATORS.min(<T as pallet_staking::Config>::MaxNominations::get());

// for babe equivocation reports the number of reporters
// and offenders is always 1
Expand Down
2 changes: 1 addition & 1 deletion frame/offences/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl onchain::Config for Test {
}

impl pallet_staking::Config for Test {
const MAX_NOMINATIONS: u32 = 16;
type MaxNominations = ConstU32<16>;
type Currency = Balances;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
Expand Down
10 changes: 5 additions & 5 deletions frame/session/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sp_std::{prelude::*, vec};
use frame_benchmarking::benchmarks;
use frame_support::{
codec::Decode,
traits::{KeyOwnerProofSystem, OnInitialize},
traits::{Get, KeyOwnerProofSystem, OnInitialize},
};
use frame_system::RawOrigin;
use pallet_session::{historical::Pallet as Historical, Pallet as Session, *};
Expand All @@ -53,10 +53,10 @@ impl<T: Config> OnInitialize<T::BlockNumber> for Pallet<T> {

benchmarks! {
set_keys {
let n = <T as pallet_staking::Config>::MAX_NOMINATIONS;
let n = <T as pallet_staking::Config>::MaxNominations::get();
let (v_stash, _) = create_validator_with_nominators::<T>(
n,
<T as pallet_staking::Config>::MAX_NOMINATIONS,
<T as pallet_staking::Config>::MaxNominations::get(),
false,
RewardDestination::Staked,
)?;
Expand All @@ -70,10 +70,10 @@ benchmarks! {
}: _(RawOrigin::Signed(v_controller), keys, proof)

purge_keys {
let n = <T as pallet_staking::Config>::MAX_NOMINATIONS;
let n = <T as pallet_staking::Config>::MaxNominations::get();
let (v_stash, _) = create_validator_with_nominators::<T>(
n,
<T as pallet_staking::Config>::MAX_NOMINATIONS,
<T as pallet_staking::Config>::MaxNominations::get(),
false,
RewardDestination::Staked
)?;
Expand Down
2 changes: 1 addition & 1 deletion frame/session/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl onchain::Config for Test {
}

impl pallet_staking::Config for Test {
const MAX_NOMINATIONS: u32 = 16;
type MaxNominations = ConstU32<16>;
type Currency = Balances;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
Expand Down
28 changes: 14 additions & 14 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,17 @@ benchmarks! {
kick {
// scenario: we want to kick `k` nominators from nominating us (we are a validator).
// we'll assume that `k` is under 128 for the purposes of determining the slope.
// each nominator should have `T::MAX_NOMINATIONS` validators nominated, and our validator
// each nominator should have `T::MaxNominations::get()` validators nominated, and our validator
// should be somewhere in there.
let k in 1 .. 128;

// these are the other validators; there are `T::MAX_NOMINATIONS - 1` of them, so
// there are a total of `T::MAX_NOMINATIONS` validators in the system.
let rest_of_validators = create_validators_with_seed::<T>(T::MAX_NOMINATIONS - 1, 100, 415)?;
// these are the other validators; there are `T::MaxNominations::get() - 1` of them, so
// there are a total of `T::MaxNominations::get()` validators in the system.
let rest_of_validators = create_validators_with_seed::<T>(T::MaxNominations::get() - 1, 100, 415)?;

// this is the validator that will be kicking.
let (stash, controller) = create_stash_controller::<T>(
T::MAX_NOMINATIONS - 1,
T::MaxNominations::get() - 1,
100,
Default::default(),
)?;
Expand All @@ -380,7 +380,7 @@ benchmarks! {
for i in 0 .. k {
// create a nominator stash.
let (n_stash, n_controller) = create_stash_controller::<T>(
T::MAX_NOMINATIONS + i,
T::MaxNominations::get() + i,
100,
Default::default(),
)?;
Expand Down Expand Up @@ -415,9 +415,9 @@ benchmarks! {
}
}

// Worst case scenario, T::MAX_NOMINATIONS
// Worst case scenario, T::MaxNominations::get()
nominate {
let n in 1 .. T::MAX_NOMINATIONS;
let n in 1 .. T::MaxNominations::get();

// clean up any existing state.
clear_validators_and_nominators::<T>();
Expand All @@ -428,7 +428,7 @@ benchmarks! {
// we are just doing an insert into the origin position.
let scenario = ListScenario::<T>::new(origin_weight, true)?;
let (stash, controller) = create_stash_controller_with_balance::<T>(
SEED + T::MAX_NOMINATIONS + 1, // make sure the account does not conflict with others
SEED + T::MaxNominations::get() + 1, // make sure the account does not conflict with others
origin_weight,
Default::default(),
).unwrap();
Expand Down Expand Up @@ -724,7 +724,7 @@ benchmarks! {
create_validators_with_nominators_for_era::<T>(
v,
n,
<T as Config>::MAX_NOMINATIONS as usize,
<T as Config>::MaxNominations::get() as usize,
false,
None,
)?;
Expand All @@ -742,7 +742,7 @@ benchmarks! {
create_validators_with_nominators_for_era::<T>(
v,
n,
<T as Config>::MAX_NOMINATIONS as usize,
<T as Config>::MaxNominations::get() as usize,
false,
None,
)?;
Expand Down Expand Up @@ -822,7 +822,7 @@ benchmarks! {
let s in 1 .. 20;

let validators = create_validators_with_nominators_for_era::<T>(
v, n, T::MAX_NOMINATIONS as usize, false, None
v, n, T::MaxNominations::get() as usize, false, None
)?
.into_iter()
.map(|v| T::Lookup::lookup(v).unwrap())
Expand All @@ -845,7 +845,7 @@ benchmarks! {
let n = MaxNominators::<T>::get();

let _ = create_validators_with_nominators_for_era::<T>(
v, n, T::MAX_NOMINATIONS as usize, false, None
v, n, T::MaxNominations::get() as usize, false, None
)?;
}: {
let targets = <Staking<T>>::get_npos_targets();
Expand Down Expand Up @@ -923,7 +923,7 @@ mod tests {
create_validators_with_nominators_for_era::<Test>(
v,
n,
<Test as Config>::MAX_NOMINATIONS as usize,
<Test as Config>::MaxNominations::get() as usize,
false,
None,
)
Expand Down
9 changes: 6 additions & 3 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ use codec::{Decode, Encode, HasCompact};
use frame_support::{
traits::{ConstU32, Currency, Get},
weights::Weight,
BoundedVec, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
};
use scale_info::TypeInfo;
use sp_runtime::{
Expand Down Expand Up @@ -577,10 +578,12 @@ where
}

/// A record of the nominations made by a specific account.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct Nominations<AccountId> {
#[derive(PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo)]
#[codec(mel_bound(T: Config))]
#[scale_info(skip_type_params(T))]
pub struct Nominations<T: Config> {
/// The targets of nomination.
pub targets: Vec<AccountId>,
pub targets: BoundedVec<T::AccountId, T::MaxNominations>,
/// The era the nominations were submitted.
///
/// Except for initial nominations which are considered submitted at era 0.
Expand Down
Loading