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

Companion for Store voters in unsorted bags to get good stake iteration properties #3413

Merged
76 commits merged into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
aa0877a
add voter bags generation script and generated values for all runtimes
coriolinus Jul 5, 2021
7dd1981
Merge remote-tracking branch 'origin/master' into prgn-companion-for-…
coriolinus Jul 6, 2021
4c853b7
VoterBagThresholds for test-runtime
coriolinus Jul 6, 2021
9c6013e
semicolon
coriolinus Jul 6, 2021
bd11bcd
Merge remote-tracking branch 'origin/master' into prgn-companion-for-…
coriolinus Jul 7, 2021
77b4c68
Merge remote-tracking branch 'origin/master' into prgn-companion-for-…
coriolinus Jul 7, 2021
fcc1f5d
turns out the header is called file_header.txt
coriolinus Jul 7, 2021
f4d40e6
perform benchmarks to get real numbers
coriolinus Jul 7, 2021
387727b
add note about when to re-run this script
coriolinus Jul 8, 2021
2bca933
Merge remote-tracking branch 'origin/master' into prgn-companion-for-…
coriolinus Jul 9, 2021
e55463c
Merge branch 'prgn-companion-for-9081' of github.com:paritytech/polka…
coriolinus Jul 9, 2021
4ca5722
Suggestions for #3413 (Store voters in unsorted bags companion) (#3488)
emostov Jul 22, 2021
e286a10
Master.into()
kianenigma Jul 22, 2021
f271329
Try resolve Cargo.lock conflicts
emostov Jul 27, 2021
0edb586
Try merge origin master
emostov Jul 27, 2021
13917ad
Add temp VoterSnapshotPerBlock value to compile
emostov Jul 27, 2021
2e1a55f
Improve voter_bags test with nominator/validator breakdown
emostov Jul 28, 2021
7bcac15
Remove miner.log
emostov Jul 28, 2021
9dad2c2
Update README
emostov Jul 28, 2021
8c7bcf8
Module doc comments
emostov Jul 28, 2021
f231603
Try merge origin master
emostov Aug 14, 2021
86a5c75
Build working
emostov Aug 14, 2021
3499379
remote-ext-tests very WIP
emostov Aug 14, 2021
846ee14
repot and fix voterr bags remote ext test
kianenigma Aug 18, 2021
61922be
Move voter-bags cli; grab system pallet
emostov Aug 20, 2021
05bfc77
Remove system module query
emostov Aug 20, 2021
428f55b
formatting and log target
emostov Aug 20, 2021
5c12880
clean up
emostov Aug 20, 2021
bc70a17
try merge origin master
emostov Aug 24, 2021
11dee8d
Save
emostov Sep 1, 2021
4f7eb6a
Try merge origin master
emostov Sep 1, 2021
17814aa
Merge branch 'master' of https://github.com/paritytech/polkadot into …
emostov Sep 1, 2021
0922ab6
Fix issues with polkadot runtime after mergin
emostov Sep 2, 2021
a932af6
Save
emostov Sep 2, 2021
c23cf3d
Try merge origin master
emostov Sep 2, 2021
479e9b9
Some updates to runtimes to make sure things compile
emostov Sep 2, 2021
edc5d78
save
emostov Sep 2, 2021
c0af428
udpate weights
emostov Sep 2, 2021
a104802
Use UseNominatorsAndUpdateBagsList
emostov Sep 2, 2021
d22f42f
make remote-ext tests a bin
emostov Sep 2, 2021
b1383c4
Adjust remote-ext test to use pub; update some comments
emostov Sep 3, 2021
4f0f7c8
Try merge origin master
emostov Sep 3, 2021
5220470
Use list_bags_get
emostov Sep 3, 2021
15ac6c8
Try fix cargo.lock
emostov Sep 5, 2021
ef8bd05
Try merge origin master
emostov Sep 5, 2021
53a3e53
Gate kusama feat code
emostov Sep 5, 2021
1014e22
Carg.lock .. again
emostov Sep 6, 2021
1279c51
point bags-list deps to master
emostov Sep 6, 2021
ed74d4b
Merge remote-tracking branch 'origin' into prgn-companion-for-9081
emostov Sep 6, 2021
f3a209c
Update voter-bags CLI to take total issuance and min balance as args
emostov Sep 7, 2021
24abba0
try merge origin master
emostov Sep 13, 2021
7afff2a
Switch polkadot runtime to UseNominatorMap
emostov Sep 13, 2021
55486a2
Some tweaks to the remote-test
kianenigma Sep 14, 2021
3b4b02b
Delete some stuff for polkadot; comments
emostov Sep 15, 2021
256c95f
Various fixes
emostov Sep 15, 2021
b52fd92
Add comment for VoterSnapshotPerBlock
emostov Sep 15, 2021
bdd266b
Remove bags-list import from polakdot cargo
emostov Sep 15, 2021
55fb64b
use benchmarking feature for pallet-bags-list
emostov Sep 15, 2021
b44ac21
Add custom migration for westend and kusama for bags-list
emostov Sep 15, 2021
b0c5305
:facepalm
emostov Sep 15, 2021
b70adc4
Try merge origin master
emostov Sep 15, 2021
baa8e6d
Merge remote-tracking branch 'origin' into prgn-companion-for-9081
emostov Sep 15, 2021
e29e77b
Fix kusama runtime
emostov Sep 15, 2021
ace47de
Try merge origin master
emostov Sep 15, 2021
3d4997b
Small clean up
emostov Sep 15, 2021
0eccbb6
Merge remote-tracking branch 'origin' into prgn-companion-for-9081
emostov Sep 15, 2021
4482a6d
Be more alphabetical ordered
emostov Sep 16, 2021
548b6ad
Use 22_500 for VoterSnapshotPerBlock
emostov Sep 16, 2021
be1b2c7
point back to master
emostov Sep 16, 2021
c03a5b6
Merge remote-tracking branch 'origin' into prgn-companion-for-9081
emostov Sep 16, 2021
c7aaaf0
remove unused type
shawntabrizi Sep 17, 2021
daa72c1
SortedListProvider for staking runtime
emostov Sep 17, 2021
3608c46
Remove polkadot for remote-ext bags test
emostov Sep 17, 2021
e96da22
Revert "Remove polkadot for remote-ext bags test"
emostov Sep 17, 2021
f4f06b5
Remove polkadot for remote-ext bags test
emostov Sep 17, 2021
225fdbf
update Substrate
Sep 17, 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
416 changes: 262 additions & 154 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ members = [
"parachain/test-parachains/adder",
"parachain/test-parachains/adder/collator",
"utils/staking-miner",
"utils/remote-ext-tests/bags-list",
"utils/voter-bags",
]

# We want to be able to build the bridge relayer without pulling it (and all of its
Expand Down
2 changes: 2 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "m
pallet-election-provider-multi-phase = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-beefy-mmr = { git = "https://github.com/paritytech/grandpa-bridge-gadget", branch = "master", default-features = false }
frame-election-provider-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-bags-list = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
pallet-babe = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
Expand Down Expand Up @@ -96,6 +97,7 @@ std = [
"runtime-parachains/std",
"xcm/std",
"sp-npos-elections/std",
"pallet-bags-list/std"
]
runtime-benchmarks = [
"libsecp256k1/hmac",
Expand Down
58 changes: 58 additions & 0 deletions runtime/common/src/elections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
//! Code for elections.

use super::{BlockExecutionWeight, BlockLength, BlockWeights};
use frame_election_provider_support::{SortedListProvider, VoteWeight};
use frame_support::{
parameter_types,
weights::{DispatchClass, Weight},
};
use sp_runtime::Perbill;
use sp_std::{boxed::Box, convert::From, marker::PhantomData};

parameter_types! {
/// A limit for off-chain phragmen unsigned solution submission.
Expand Down Expand Up @@ -89,3 +91,59 @@ impl frame_support::pallet_prelude::Get<Option<(usize, sp_npos_elections::Extend
Some((iters, 0))
}
}

/// Implementation of `frame_election_provider_support::SortedListProvider` that updates the
/// bags-list but uses [`pallet_staking::Nominators`] for `iter`. This is meant to be a transitionary
/// implementation for runtimes to "test" out the bags-list by keeping it up to date, but not yet
/// using it for snapshot generation. In contrast, a "complete" implementation would use bags-list
/// for `iter`.
pub struct UseNominatorsAndUpdateBagsList<T>(PhantomData<T>);
impl<T: pallet_bags_list::Config + pallet_staking::Config> SortedListProvider<T::AccountId>
for UseNominatorsAndUpdateBagsList<T>
{
type Error = pallet_bags_list::Error;

fn iter() -> Box<dyn Iterator<Item = T::AccountId>> {
Box::new(pallet_staking::Nominators::<T>::iter().map(|(n, _)| n))
}

fn count() -> u32 {
pallet_bags_list::Pallet::<T>::count()
}

fn contains(id: &T::AccountId) -> bool {
pallet_bags_list::Pallet::<T>::contains(id)
}

fn on_insert(id: T::AccountId, weight: VoteWeight) -> Result<(), Self::Error> {
pallet_bags_list::Pallet::<T>::on_insert(id, weight)
}

fn on_update(id: &T::AccountId, new_weight: VoteWeight) {
pallet_bags_list::Pallet::<T>::on_update(id, new_weight);
}

fn on_remove(id: &T::AccountId) {
pallet_bags_list::Pallet::<T>::on_remove(id);
}

fn regenerate(
all: impl IntoIterator<Item = T::AccountId>,
weight_of: Box<dyn Fn(&T::AccountId) -> VoteWeight>,
) -> u32 {
pallet_bags_list::Pallet::<T>::regenerate(all, weight_of)
}

fn sanity_check() -> Result<(), &'static str> {
pallet_bags_list::Pallet::<T>::sanity_check()
}

fn clear(count: Option<u32>) -> u32 {
pallet_bags_list::Pallet::<T>::clear(count)
}

#[cfg(feature = "runtime-benchmarks")]
fn weight_update_worst_case(who: &T::AccountId, is_increase: bool) -> VoteWeight {
pallet_bags_list::Pallet::<T>::weight_update_worst_case(who, is_increase)
}
}
3 changes: 3 additions & 0 deletions runtime/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "m
pallet-utility = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-vesting = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-xcm = { path = "../../xcm/pallet-xcm", default-features = false }
pallet-bags-list = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-election-provider-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
Expand Down Expand Up @@ -174,6 +175,7 @@ std = [
"xcm-executor/std",
"xcm-builder/std",
"frame-election-provider-support/std",
"pallet-bags-list/std",
]
runtime-benchmarks = [
"runtime-common/runtime-benchmarks",
Expand Down Expand Up @@ -211,6 +213,7 @@ runtime-benchmarks = [
"hex-literal",
"xcm-builder/runtime-benchmarks",
"frame-election-provider-support/runtime-benchmarks",
"pallet-bags-list/runtime-benchmarks",
]
try-runtime = [
"frame-executive/try-runtime",
Expand Down
48 changes: 48 additions & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ use constants::{currency::*, fee::*, time::*};
// Weights used in the runtime.
mod weights;

// Voter bag threshold definitions.
mod voter_bags;

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -354,6 +357,11 @@ parameter_types! {

// miner configs
pub OffchainRepeat: BlockNumber = 5;

/// Whilst `UseNominatorsAndUpdateBagsList` or `UseNominatorsMap` is in use, this can still be a
/// very large value. Once the `BagsList` is in full motion, staking might open its door to many
/// more nominators, and this value should instead be what is a "safe" number (e.g. 22500).
pub const VoterSnapshotPerBlock: u32 = 22_500;
}

sp_npos_elections::generate_solution_type!(
Expand Down Expand Up @@ -399,6 +407,18 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
pallet_collective::EnsureProportionAtLeast<_2, _3, AccountId, CouncilCollective>,
>;
type WeightInfo = weights::pallet_election_provider_multi_phase::WeightInfo<Self>;
type VoterSnapshotPerBlock = VoterSnapshotPerBlock;
}

parameter_types! {
pub const BagThresholds: &'static [u64] = &voter_bags::THRESHOLDS;
}

impl pallet_bags_list::Config for Runtime {
type Event = Event;
type VoteWeightProvider = Staking;
type WeightInfo = weights::pallet_bags_list::WeightInfo<Runtime>;
type BagThresholds = BagThresholds;
}

fn era_payout(
Expand Down Expand Up @@ -505,6 +525,9 @@ impl pallet_staking::Config for Runtime {
type EraPayout = EraPayout;
type NextNewSession = Session;
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
// Use the nominators map to iter voters, but also perform the bags-list migration and keep
// it up-to-date.
type SortedListProvider = runtime_common::elections::UseNominatorsAndUpdateBagsList<Runtime>;
type WeightInfo = weights::pallet_staking::WeightInfo<Runtime>;
}

Expand Down Expand Up @@ -1462,6 +1485,9 @@ construct_runtime! {
// Gilts pallet.
Gilt: pallet_gilt::{Pallet, Call, Storage, Event<T>, Config} = 38,

// Provides a semi-sorted list of nominators for staking.
BagsList: pallet_bags_list::{Pallet, Call, Storage, Event<T>} = 39,

// Parachains pallets. Start indices at 50 to leave room.
ParachainsOrigin: parachains_origin::{Pallet, Origin} = 50,
Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>} = 51,
Expand Down Expand Up @@ -1521,6 +1547,7 @@ pub type Executive = frame_executive::Executive<
TechnicalCommitteeStoragePrefixMigration,
TechnicalMembershipStoragePrefixMigration,
MigrateTipsPalletPrefix,
StakingBagsListMigrationV8,
),
>;
/// The payload being signed in the transactions.
Expand Down Expand Up @@ -1638,6 +1665,25 @@ impl OnRuntimeUpgrade for MigrateTipsPalletPrefix {
}
}

// Migration to generate pallet staking's `SortedListProvider` from pre-existing nominators.
pub struct StakingBagsListMigrationV8;

impl OnRuntimeUpgrade for StakingBagsListMigrationV8 {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
pallet_staking::migrations::v8::migrate::<Runtime>()
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
pallet_staking::migrations::v8::pre_migrate::<Runtime>()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
pallet_staking::migrations::v8::post_migrate::<Runtime>()
}
}

#[cfg(not(feature = "disable-runtime-api"))]
sp_api::impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
Expand Down Expand Up @@ -1955,6 +2001,7 @@ sp_api::impl_runtime_apis! {
list_benchmark!(list, extra, runtime_common::slots, Slots);
list_benchmark!(list, extra, runtime_common::paras_registrar, Registrar);
// Substrate
list_benchmark!(list, extra, pallet_bags_list, BagsList);
list_benchmark!(list, extra, pallet_balances, Balances);
list_benchmark!(list, extra, pallet_bounties, Bounties);
list_benchmark!(list, extra, pallet_collective, Council);
Expand Down Expand Up @@ -2029,6 +2076,7 @@ sp_api::impl_runtime_apis! {
add_benchmark!(params, batches, runtime_common::paras_registrar, Registrar);
// Substrate
add_benchmark!(params, batches, pallet_balances, Balances);
add_benchmark!(params, batches, pallet_bags_list, BagsList);
add_benchmark!(params, batches, pallet_bounties, Bounties);
add_benchmark!(params, batches, pallet_collective, Council);
add_benchmark!(params, batches, pallet_collective, TechnicalCommittee);
Expand Down
Loading