Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
d998b12
Base features and traits.
kianenigma Jan 15, 2021
5aea9cc
pallet and unsigned phase
kianenigma Jan 15, 2021
632e107
Undo bad formattings.
kianenigma Jan 15, 2021
c52e65d
Master.into()
kianenigma Jan 18, 2021
b4fc5e1
some formatting cleanup.
kianenigma Jan 18, 2021
cc26881
Small self-cleanup.
kianenigma Jan 18, 2021
67a9fae
Master.into()
kianenigma Jan 27, 2021
75eca3f
Make it all build
kianenigma Jan 27, 2021
8daec3a
self-review
kianenigma Jan 27, 2021
49613ed
Some doc tests.
kianenigma Jan 27, 2021
35f1faf
Some changes from other PR
kianenigma Jan 27, 2021
e01cacc
Fix session test
kianenigma Jan 27, 2021
6278150
Merge branch 'master' into kiz-election-provider-2-two-phase-unsigned
shawntabrizi Feb 4, 2021
4c516cb
Update Cargo.lock
shawntabrizi Feb 4, 2021
8c8d1e6
Update frame/election-provider-multi-phase/src/lib.rs
kianenigma Feb 11, 2021
4b58c91
Some review comments
kianenigma Feb 11, 2021
f4029aa
Merge branch 'kiz-election-provider-2-two-phase-unsigned' of github.c…
kianenigma Feb 11, 2021
f16ac88
Master.into()
kianenigma Feb 11, 2021
528917e
Rename + make encode/decode
kianenigma Feb 12, 2021
1a5794a
Do an assert as well, just in case.
kianenigma Feb 12, 2021
01e63ed
Fix build
kianenigma Feb 12, 2021
4ccecdf
Update frame/election-provider-multi-phase/src/unsigned.rs
kianenigma Feb 12, 2021
83a789e
Las comment
kianenigma Feb 12, 2021
adb3618
Merge branch 'kiz-election-provider-2-two-phase-unsigned' of github.c…
kianenigma Feb 12, 2021
7c71df0
fix staking fuzzer.
kianenigma Feb 12, 2021
12b1640
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Feb 12, 2021
6397d4a
Add one last layer of feasibility check as well.
kianenigma Feb 12, 2021
80d3c31
Merge branch 'kiz-election-provider-2-two-phase-unsigned' of github.c…
kianenigma Feb 12, 2021
7d6f6ad
Last fixes to benchmarks
kianenigma Feb 12, 2021
2e28437
Some more docs.
kianenigma Feb 12, 2021
5fda744
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Feb 12, 2021
1ce6c8e
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Feb 12, 2021
f63c5b8
Some nits
kianenigma Feb 12, 2021
8029c01
Merge branch 'kiz-election-provider-2-two-phase-unsigned' of github.c…
kianenigma Feb 12, 2021
5973b61
It all works
kianenigma Feb 13, 2021
0882c62
Master.into()
kianenigma Mar 8, 2021
e137035
Some self cleanup
kianenigma Mar 9, 2021
428c4c9
Master.into()
kianenigma Mar 12, 2021
a16b964
Update frame/staking/src/lib.rs
kianenigma Mar 12, 2021
6d506a4
remove most todos.
kianenigma Mar 12, 2021
75accd3
Merge branch 'kiz-election-provider-21-enable-multi-phase' of github.…
kianenigma Mar 12, 2021
df3fb9a
Round of self-review.
kianenigma Mar 12, 2021
e76af15
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Mar 12, 2021
530ab32
Fix migration
kianenigma Mar 13, 2021
9a61b20
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Mar 14, 2021
c92be7e
clean macro
kianenigma Mar 14, 2021
794f0ed
Master.into()
kianenigma Mar 16, 2021
789b35c
Revert wrong merge
kianenigma Mar 16, 2021
cfcdb54
Make the number of nominations configurable
kianenigma Mar 16, 2021
6ecafa2
Master.into()
kianenigma Mar 23, 2021
2004acb
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Mar 24, 2021
0b1cb15
Self reivew
kianenigma Mar 24, 2021
07e7285
renmae.
kianenigma Mar 24, 2021
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sp-keyring = { version = "3.0.0", optional = true, path = "../../../primitives/k
sp-session = { version = "3.0.0", default-features = false, path = "../../../primitives/session" }
sp-transaction-pool = { version = "3.0.0", default-features = false, path = "../../../primitives/transaction-pool" }
sp-version = { version = "3.0.0", default-features = false, path = "../../../primitives/version" }
sp-npos-elections = { version = "3.0.0", default-features = false, path = "../../../primitives/npos-elections" }

# frame dependencies
frame-executive = { version = "3.0.0", default-features = false, path = "../../../frame/executive" }
Expand Down Expand Up @@ -159,6 +160,7 @@ std = [
"pallet-vesting/std",
"log/std",
"frame-try-runtime/std",
"sp-npos-elections/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
10 changes: 9 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ parameter_types! {
}

impl pallet_staking::Config for Runtime {
const MAX_NOMINATIONS: u32 =
<NposSolution16 as sp_npos_elections::CompactSolution>::LIMIT as u32;
type Currency = Balances;
type UnixTime = Timestamp;
type CurrencyToVote = U128CurrencyToVote;
Expand Down Expand Up @@ -515,6 +517,12 @@ parameter_types! {
.saturating_sub(BlockExecutionWeight::get());
}

sp_npos_elections::generate_solution_type!(
#[compact]
pub struct NposSolution16::<u32, u16, sp_runtime::PerU16>(16)
// -------------------- ^^ <NominatorIndex, ValidatorIndex, Accuracy>
);

impl pallet_election_provider_multi_phase::Config for Runtime {
type Event = Event;
type Currency = Balances;
Expand All @@ -526,7 +534,7 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type MinerTxPriority = MultiPhaseUnsignedPriority;
type DataProvider = Staking;
type OnChainAccuracy = Perbill;
type CompactSolution = pallet_staking::CompactAssignments;
type CompactSolution = NposSolution16;
type Fallback = Fallback;
type WeightInfo = pallet_election_provider_multi_phase::weights::SubstrateWeight<Runtime>;
type BenchmarkingConfig = ();
Expand Down
1 change: 1 addition & 0 deletions frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl onchain::Config for Test {
}

impl pallet_staking::Config for Test {
const MAX_NOMINATIONS: u32 = 16;
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type Event = Event;
Expand Down
7 changes: 7 additions & 0 deletions frame/election-provider-multi-phase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,13 @@ pub mod pallet {
let _: UpperOf<CompactAccuracyOf<T>> = maximum_chain_accuracy
.iter()
.fold(Zero::zero(), |acc, x| acc.checked_add(x).unwrap());

// We only accept data provider who's maximum votes per voter matches our
// `T::CompactSolution`'s `LIMIT`.
assert_eq!(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that the main reason why I need to add MAXIMUM_VOTES_PER_VOTER const is so that I can make this assertion -- otherwise this might be a foot gun.

<T::DataProvider as ElectionDataProvider<T::AccountId, T::BlockNumber>>::MAXIMUM_VOTES_PER_VOTER,
<CompactOf<T> as CompactSolution>::LIMIT as u32,
);
}
}

Expand Down
1 change: 1 addition & 0 deletions frame/election-provider-multi-phase/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ pub struct ExtBuilder {}

pub struct StakingMock;
impl ElectionDataProvider<AccountId, u64> for StakingMock {
const MAXIMUM_VOTES_PER_VOTER: u32 = <TestCompact as CompactSolution>::LIMIT as u32;
fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<(Vec<AccountId>, Weight)> {
let targets = Targets::get();

Expand Down
5 changes: 5 additions & 0 deletions frame/election-provider-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
//! pub struct Module<T: Config>(std::marker::PhantomData<T>);
//!
//! impl<T: Config> ElectionDataProvider<AccountId, BlockNumber> for Module<T> {
//! const MAXIMUM_VOTES_PER_VOTER: u32 = 1;
//! fn desired_targets() -> data_provider::Result<(u32, Weight)> {
//! Ok((1, 0))
//! }
Expand Down Expand Up @@ -180,6 +181,9 @@ pub mod data_provider {

/// Something that can provide the data to an [`ElectionProvider`].
pub trait ElectionDataProvider<AccountId, BlockNumber> {
/// Maximum number of votes per voter that this data provider is providing.
const MAXIMUM_VOTES_PER_VOTER: u32;

/// All possible targets for the election, i.e. the candidates.
///
/// If `maybe_max_len` is `Some(v)` then the resulting vector MUST NOT be longer than `v` items
Expand Down Expand Up @@ -226,6 +230,7 @@ pub trait ElectionDataProvider<AccountId, BlockNumber> {

#[cfg(feature = "std")]
impl<AccountId, BlockNumber> ElectionDataProvider<AccountId, BlockNumber> for () {
const MAXIMUM_VOTES_PER_VOTER: u32 = 0;
fn targets(_maybe_max_len: Option<usize>) -> data_provider::Result<(Vec<AccountId>, Weight)> {
Ok(Default::default())
}
Expand Down
1 change: 1 addition & 0 deletions frame/election-provider-support/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ mod tests {
pub struct DataProvider;

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

impl pallet_staking::Config for Test {
const MAX_NOMINATIONS: u32 = 16;
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type Event = Event;
Expand Down
15 changes: 9 additions & 6 deletions frame/offences/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use frame_system::{RawOrigin, Pallet as System, Config as SystemConfig};
use frame_benchmarking::{benchmarks, account, impl_benchmark_test_suite};
use frame_support::traits::{Currency, OnInitialize, ValidatorSet, ValidatorSetWithIdentification};

use sp_runtime::{Perbill, traits::{Convert, StaticLookup, Saturating, UniqueSaturatedInto}};
use sp_runtime::{
Perbill,
traits::{Convert, StaticLookup, Saturating, UniqueSaturatedInto},
};
use sp_staking::offence::{ReportOffence, Offence, OffenceDetails};

use pallet_balances::Config as BalancesConfig;
Expand All @@ -39,8 +42,8 @@ use pallet_offences::{Config as OffencesConfig, Module as Offences};
use pallet_session::historical::{Config as HistoricalConfig, IdentificationTuple};
use pallet_session::{Config as SessionConfig, SessionManager};
use pallet_staking::{
Module as Staking, Config as StakingConfig, RewardDestination, ValidatorPrefs,
Exposure, IndividualExposure, MAX_NOMINATIONS, Event as StakingEvent
Module as Staking, Config as StakingConfig, RewardDestination, ValidatorPrefs, Exposure,
IndividualExposure, Event as StakingEvent,
};

const SEED: u32 = 0;
Expand Down Expand Up @@ -236,7 +239,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(MAX_NOMINATIONS as u32);
let n in 0 .. MAX_NOMINATORS.min(<T as pallet_staking::Config>::MAX_NOMINATIONS);

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

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

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

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

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

impl pallet_staking::Config for Test {
const MAX_NOMINATIONS: u32 = 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 @@ -35,7 +35,7 @@ use frame_system::RawOrigin;
use pallet_session::{historical::Module as Historical, Module as Session, *};
use pallet_staking::{
benchmarking::create_validator_with_nominators, testing_utils::create_validators,
MAX_NOMINATIONS, RewardDestination,
RewardDestination,
};
use sp_runtime::traits::{One, StaticLookup};

Expand All @@ -52,10 +52,10 @@ impl<T: Config> OnInitialize<T::BlockNumber> for Pallet<T> {

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

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

impl pallet_staking::Config for Test {
const MAX_NOMINATIONS: u32 = 16;
type Currency = Balances;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
Expand Down
2 changes: 0 additions & 2 deletions frame/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ static_assertions = "1.1.0"
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
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" }
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" }
Expand Down Expand Up @@ -54,7 +53,6 @@ std = [
"serde",
"codec/std",
"sp-std/std",
"sp-npos-elections/std",
"sp-io/std",
"frame-support/std",
"sp-runtime/std",
Expand Down
53 changes: 39 additions & 14 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,20 @@ 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 `MAX_NOMINATIONS` validators nominated, and our validator
// each nominator should have `T::MAX_NOMINATIONS` validators nominated, and our validator
// should be somewhere in there.
let k in 1 .. 128;

// these are the other validators; there are `MAX_NOMINATIONS - 1` of them, so there are a
// total of `MAX_NOMINATIONS` validators in the system.
let rest_of_validators = create_validators::<T>(MAX_NOMINATIONS as u32 - 1, 100)?;
// 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::<T>(T::MAX_NOMINATIONS - 1, 100)?;

// this is the validator that will be kicking.
let (stash, controller) = create_stash_controller::<T>(MAX_NOMINATIONS as u32 - 1, 100, Default::default())?;
let (stash, controller) = create_stash_controller::<T>(
T::MAX_NOMINATIONS - 1,
100,
Default::default(),
)?;
let stash_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(stash.clone());

// they start validating.
Expand All @@ -224,7 +228,11 @@ benchmarks! {
let mut nominator_stashes = Vec::with_capacity(k as usize);
for i in 0 .. k {
// create a nominator stash.
let (n_stash, n_controller) = create_stash_controller::<T>(MAX_NOMINATIONS as u32 + i, 100, Default::default())?;
let (n_stash, n_controller) = create_stash_controller::<T>(
T::MAX_NOMINATIONS + i,
100,
Default::default(),
)?;

// bake the nominations; we first clone them from the rest of the validators.
let mut nominations = rest_of_validators.clone();
Expand Down Expand Up @@ -256,9 +264,9 @@ benchmarks! {
}
}

// Worst case scenario, MAX_NOMINATIONS
// Worst case scenario, T::MAX_NOMINATIONS
nominate {
let n in 1 .. MAX_NOMINATIONS as u32;
let n in 1 .. T::MAX_NOMINATIONS;
let (stash, controller) = create_stash_controller::<T>(n + 1, 100, Default::default())?;
let validators = create_validators::<T>(n, 100)?;
whitelist_account!(controller);
Expand Down Expand Up @@ -467,7 +475,13 @@ benchmarks! {
let v in 1 .. 10;
let n in 1 .. 100;

create_validators_with_nominators_for_era::<T>(v, n, MAX_NOMINATIONS, false, None)?;
create_validators_with_nominators_for_era::<T>(
v,
n,
<T as Config>::MAX_NOMINATIONS as usize,
false,
None,
)?;
let session_index = SessionIndex::one();
}: {
let validators = Staking::<T>::new_era(session_index).ok_or("`new_era` failed")?;
Expand All @@ -478,7 +492,13 @@ benchmarks! {
payout_all {
let v in 1 .. 10;
let n in 1 .. 100;
create_validators_with_nominators_for_era::<T>(v, n, MAX_NOMINATIONS, false, None)?;
create_validators_with_nominators_for_era::<T>(
v,
n,
<T as Config>::MAX_NOMINATIONS as usize,
false,
None,
)?;
// Start a new Era
let new_validators = Staking::<T>::new_era(SessionIndex::one()).unwrap();
assert!(new_validators.len() == v as usize);
Expand Down Expand Up @@ -548,7 +568,7 @@ benchmarks! {
// total number of slashing spans. Assigned to validators randomly.
let s in 1 .. 20;

let validators = create_validators_with_nominators_for_era::<T>(v, n, MAX_NOMINATIONS, false, None)?
let validators = create_validators_with_nominators_for_era::<T>(v, n, T::MAX_NOMINATIONS as usize, false, None)?
.into_iter()
.map(|v| T::Lookup::lookup(v).unwrap())
.collect::<Vec<_>>();
Expand All @@ -567,7 +587,7 @@ benchmarks! {
// number of nominator intention.
let n = 500;

let _ = create_validators_with_nominators_for_era::<T>(v, n, MAX_NOMINATIONS, false, None)?;
let _ = create_validators_with_nominators_for_era::<T>(v, n, T::MAX_NOMINATIONS as usize, false, None)?;
}: {
let targets = <Staking<T>>::get_npos_targets();
assert_eq!(targets.len() as u32, v);
Expand All @@ -586,8 +606,13 @@ mod tests {
let v = 10;
let n = 100;

create_validators_with_nominators_for_era::<Test>(v, n, MAX_NOMINATIONS, false, None)
.unwrap();
create_validators_with_nominators_for_era::<Test>(
v,
n,
<Test as Config>::MAX_NOMINATIONS as usize,
false,
None,
).unwrap();

let count_validators = Validators::<Test>::iter().count();
let count_nominators = Nominators::<Test>::iter().count();
Expand Down
Loading