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

[Feature] Part 1: add TargetList for validator ranking #12034

Merged
merged 43 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
64b5c6e
[Feature] Part 1: add TargetList for validator ranking
ruseinov Aug 15, 2022
a6b07a5
remove redundant todo
ruseinov Aug 15, 2022
ec61b29
remove typo
ruseinov Aug 15, 2022
281e5d0
cleanup
ruseinov Aug 15, 2022
73b47cb
implement score
ruseinov Aug 15, 2022
76034bb
more fixes
ruseinov Aug 15, 2022
86631ed
fix thresholds
ruseinov Aug 15, 2022
3a82504
fmt
ruseinov Aug 16, 2022
74b6fba
Remove the stuff that has to come in the next PR, some fixes
ruseinov Aug 16, 2022
8a2fc42
extended balance import
ruseinov Aug 16, 2022
e676398
Change all the references from VoteWeight to Self::Score
ruseinov Aug 16, 2022
7dfbc9b
Add a migration for VoterBagsList
ruseinov Aug 19, 2022
5f4d9a4
fix score
ruseinov Aug 19, 2022
5b2e1f5
add targetList to nomination-pools tests
ruseinov Aug 19, 2022
805349d
fix bench
ruseinov Aug 19, 2022
4329ad2
address review comments
ruseinov Aug 20, 2022
ba11c8a
change get_npos_targets
ruseinov Aug 22, 2022
5286a4f
address more comments
ruseinov Aug 22, 2022
101deee
remove thresholds for the time being
ruseinov Aug 22, 2022
b5109d1
fix instance reference
ruseinov Aug 22, 2022
6f4bf4b
VoterBagsListInstance
ruseinov Aug 22, 2022
0ef8d2c
reus
ruseinov Aug 22, 2022
c03ec08
remove params that are not used yet
ruseinov Aug 23, 2022
1da8bfc
Introduced pre/post upgrade try-runtime checks
ruseinov Aug 24, 2022
223580b
Merge remote-tracking branch 'origin/master' into ru/feature/p1-targe…
Aug 25, 2022
0bde3df
fix
ruseinov Aug 25, 2022
9cb9e01
fixes
ruseinov Aug 25, 2022
4c25988
fix migration
ruseinov Aug 27, 2022
14ce27a
fix migration
ruseinov Aug 27, 2022
67316fb
fix post_upgrade
ruseinov Aug 29, 2022
87b5e0f
change
ruseinov Aug 29, 2022
9dea8f4
Merge remote-tracking branch 'origin/master' into ru/feature/p1-targe…
Sep 12, 2022
03b400c
Master.into()
kianenigma Sep 13, 2022
8f54be5
Fix
kianenigma Sep 13, 2022
39b1cfa
eloquent PhantomData
ruseinov Sep 14, 2022
94efa6e
fix PD
ruseinov Sep 14, 2022
c1547a2
more fixes
ruseinov Sep 14, 2022
194e0e2
Update frame/staking/src/pallet/impls.rs
ruseinov Sep 16, 2022
ed98cca
is_nominator now works
ruseinov Sep 16, 2022
ba0c063
fix test-staking
ruseinov Sep 16, 2022
e6a3871
build fixes
ruseinov Sep 16, 2022
d019f1d
fix remote-tests
ruseinov Sep 17, 2022
93491e2
Apply suggestions from code review
kianenigma Sep 18, 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
15 changes: 10 additions & 5 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,9 @@ impl pallet_staking::Config for Runtime {
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type VoterList = BagsList;
type VoterList = VoterBagsList;
// This a placeholder, to be introduced in the next PR as an instance of bags-list
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type OnStakerSlash = NominationPools;
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
Expand Down Expand Up @@ -724,12 +726,15 @@ parameter_types! {
pub const BagThresholds: &'static [u64] = &voter_bags::THRESHOLDS;
}

impl pallet_bags_list::Config for Runtime {
type VoterBagsListInstance = pallet_bags_list::Instance1;
impl pallet_bags_list::Config<VoterBagsListInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
/// The voter bags-list is loosely kept up to date, and the real source of truth for the score
/// of each node is the staking pallet.
type ScoreProvider = Staking;
type WeightInfo = pallet_bags_list::weights::SubstrateWeight<Runtime>;
type BagThresholds = BagThresholds;
type Score = VoteWeight;
type WeightInfo = pallet_bags_list::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -1636,7 +1641,7 @@ construct_runtime!(
Gilt: pallet_gilt,
Uniques: pallet_uniques,
TransactionStorage: pallet_transaction_storage,
BagsList: pallet_bags_list,
VoterBagsList: pallet_bags_list::<Instance1>,
StateTrieMigration: pallet_state_trie_migration,
ChildBounties: pallet_child_bounties,
Referenda: pallet_referenda,
Expand Down Expand Up @@ -1722,7 +1727,7 @@ mod benches {
[pallet_alliance, Alliance]
[pallet_assets, Assets]
[pallet_babe, Babe]
[pallet_bags_list, BagsList]
[pallet_bags_list, VoterBagsList]
[pallet_balances, Balances]
[pallet_bounties, Bounties]
[pallet_child_bounties, ChildBounties]
Expand Down
9 changes: 6 additions & 3 deletions bin/node/runtime/src/voter_bags.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Substrate.

// Copyright (C) 2021-2022 Parity Technologies (UK) Ltd.
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,9 +15,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Autogenerated voter bag thresholds.
//! Autogenerated bag thresholds.
//!
//! Generated on 2021-07-05T09:17:40.469754927+00:00
//! Generated on 2022-08-15T19:26:59.939787+00:00
//! Arguments
//! Total issuance: 100000000000000
//! Minimum balance: 100000000000000
//! for the node runtime.

/// Existential weight for this runtime.
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 @@ -202,6 +202,7 @@ impl pallet_staking::Config for Test {
type ElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type OnStakerSlash = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
Expand Down
33 changes: 23 additions & 10 deletions frame/bags-list/remote-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//! Utilities for remote-testing pallet-bags-list.

use frame_election_provider_support::ScoreProvider;
use pallet_bags_list::Instance1;
use sp_std::prelude::*;

/// A common log target to use.
Expand All @@ -30,18 +31,26 @@ pub mod try_state;
/// A wrapper for a runtime that the functions of this crate expect.
///
/// For example, this can be the `Runtime` type of the Polkadot runtime.
pub trait RuntimeT:
pallet_staking::Config + pallet_bags_list::Config + frame_system::Config
pub trait RuntimeT<I: 'static>:
pallet_staking::Config + pallet_bags_list::Config<I> + frame_system::Config
{
}
impl<
I: 'static,
T: pallet_staking::Config + pallet_bags_list::Config<I> + frame_system::Config,
> RuntimeT<I> for T
{
}
impl<T: pallet_staking::Config + pallet_bags_list::Config + frame_system::Config> RuntimeT for T {}

fn percent(portion: u32, total: u32) -> f64 {
(portion as f64 / total as f64) * 100f64
}

/// Display the number of nodes in each bag, while identifying those that need a rebag.
pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_name: &'static str) {
pub fn display_and_check_bags<Runtime: RuntimeT<Instance1>>(
currency_unit: u64,
currency_name: &'static str,
) {
use frame_election_provider_support::SortedListProvider;
use frame_support::traits::Get;

Expand All @@ -55,7 +64,8 @@ pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_na
let mut seen_in_bags = 0;
let mut rebaggable = 0;
let mut active_bags = 0;
for vote_weight_thresh in <Runtime as pallet_bags_list::Config>::BagThresholds::get() {
for vote_weight_thresh in <Runtime as pallet_bags_list::Config<Instance1>>::BagThresholds::get()
{
let vote_weight_thresh_u64: u64 = (*vote_weight_thresh)
.try_into()
.map_err(|_| "runtime must configure score to at most u64 to use this test")
Expand All @@ -64,7 +74,9 @@ pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_na
let vote_weight_thresh_as_unit = vote_weight_thresh_u64 as f64 / currency_unit as f64;
let pretty_thresh = format!("Threshold: {}. {}", vote_weight_thresh_as_unit, currency_name);

let bag = match pallet_bags_list::Pallet::<Runtime>::list_bags_get(*vote_weight_thresh) {
let bag = match pallet_bags_list::Pallet::<Runtime, Instance1>::list_bags_get(
*vote_weight_thresh,
) {
Some(bag) => bag,
None => {
log::info!(target: LOG_TARGET, "{} NO VOTERS.", pretty_thresh);
Expand All @@ -75,7 +87,8 @@ pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_na
active_bags += 1;

for id in bag.std_iter().map(|node| node.std_id().clone()) {
let vote_weight = <Runtime as pallet_bags_list::Config>::ScoreProvider::score(&id);
let vote_weight =
<Runtime as pallet_bags_list::Config<Instance1>>::ScoreProvider::score(&id);
let vote_weight_thresh_u64: u64 = (*vote_weight_thresh)
.try_into()
.map_err(|_| "runtime must configure score to at most u64 to use this test")
Expand All @@ -92,8 +105,8 @@ pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_na
);
}

let node =
pallet_bags_list::Node::<Runtime>::get(&id).expect("node in bag must exist.");
let node = pallet_bags_list::Node::<Runtime, Instance1>::get(&id)
.expect("node in bag must exist.");
if node.is_misplaced(vote_weight) {
rebaggable += 1;
let notional_bag = pallet_bags_list::notional_bag_for::<Runtime, _>(vote_weight);
Expand Down Expand Up @@ -141,7 +154,7 @@ pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_na
"a total of {} nodes are in {} active bags [{} total bags], {} of which can be rebagged.",
voter_list_count,
active_bags,
<Runtime as pallet_bags_list::Config>::BagThresholds::get().len(),
<Runtime as pallet_bags_list::Config<Instance1>>::BagThresholds::get().len(),
rebaggable,
);
}
5 changes: 4 additions & 1 deletion frame/bags-list/remote-tests/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use sp_runtime::{traits::Block as BlockT, DeserializeOwned};

/// Test voter bags migration. `currency_unit` is the number of planks per the the runtimes `UNITS`
/// (i.e. number of decimal places per DOT, KSM etc)
pub async fn execute<Runtime: RuntimeT, Block: BlockT + DeserializeOwned>(
pub async fn execute<
Runtime: RuntimeT<pallet_bags_list::Instance1>,
Block: BlockT + DeserializeOwned,
>(
currency_unit: u64,
currency_name: &'static str,
ws_url: String,
Expand Down
8 changes: 6 additions & 2 deletions frame/bags-list/remote-tests/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use remote_externalities::{Builder, Mode, OnlineConfig};
use sp_runtime::{traits::Block as BlockT, DeserializeOwned};

/// Execute create a snapshot from pallet-staking.
pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>(
pub async fn execute<
Runtime: crate::RuntimeT<pallet_bags_list::Instance1>,
Block: BlockT + DeserializeOwned,
>(
voter_limit: Option<usize>,
currency_unit: u64,
ws_url: String,
Expand All @@ -34,7 +37,8 @@ pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>
transport: ws_url.to_string().into(),
// NOTE: we don't scrape pallet-staking, this kinda ensures that the source of the data
// is bags-list.
pallets: vec![pallet_bags_list::Pallet::<Runtime>::name().to_string()],
pallets: vec![pallet_bags_list::Pallet::<Runtime, pallet_bags_list::Instance1>::name()
.to_string()],
at: None,
..Default::default()
}))
Expand Down
10 changes: 7 additions & 3 deletions frame/bags-list/remote-tests/src/try_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ use remote_externalities::{Builder, Mode, OnlineConfig};
use sp_runtime::{traits::Block as BlockT, DeserializeOwned};

/// Execute the sanity check of the bags-list.
pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>(
pub async fn execute<
Runtime: crate::RuntimeT<pallet_bags_list::Instance1>,
Block: BlockT + DeserializeOwned,
>(
currency_unit: u64,
currency_name: &'static str,
ws_url: String,
) {
let mut ext = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
transport: ws_url.to_string().into(),
pallets: vec![pallet_bags_list::Pallet::<Runtime>::name().to_string()],
pallets: vec![pallet_bags_list::Pallet::<Runtime, pallet_bags_list::Instance1>::name()
.to_string()],
..Default::default()
}))
.inject_hashed_prefix(&<pallet_staking::Bonded<Runtime>>::prefix_hash())
Expand All @@ -44,7 +48,7 @@ pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>

ext.execute_with(|| {
sp_core::crypto::set_default_ss58_version(Runtime::SS58Prefix::get().try_into().unwrap());
pallet_bags_list::Pallet::<Runtime>::try_state().unwrap();
pallet_bags_list::Pallet::<Runtime, pallet_bags_list::Instance1>::try_state().unwrap();
log::info!(target: crate::LOG_TARGET, "executed bags-list sanity check with no errors.");

crate::display_and_check_bags::<Runtime>(currency_unit, currency_name);
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 @@ -206,6 +206,7 @@ impl pallet_staking::Config for Test {
type ElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type OnStakerSlash = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
Expand Down
5 changes: 4 additions & 1 deletion frame/nomination-pools/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ type CurrencyOf<T> = <T as pallet_nomination_pools::Config>::Currency;
const USER_SEED: u32 = 0;
const MAX_SPANS: u32 = 100;

type VoterBagsListInstance = pallet_bags_list::Instance1;
pub trait Config:
pallet_nomination_pools::Config + pallet_staking::Config + pallet_bags_list::Config
pallet_nomination_pools::Config
+ pallet_staking::Config
+ pallet_bags_list::Config<VoterBagsListInstance>
{
}

Expand Down
8 changes: 5 additions & 3 deletions frame/nomination-pools/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::VoterBagsListInstance;
use frame_election_provider_support::VoteWeight;
use frame_support::{pallet_prelude::*, parameter_types, traits::ConstU64, PalletId};
use sp_runtime::{
Expand Down Expand Up @@ -111,7 +112,8 @@ impl pallet_staking::Config for Runtime {
type ElectionProvider =
frame_election_provider_support::NoElection<(AccountId, BlockNumber, Staking)>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = pallet_bags_list::Pallet<Self>;
type VoterList = VoterList;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type OnStakerSlash = Pools;
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
Expand All @@ -122,7 +124,7 @@ parameter_types! {
pub static BagThresholds: &'static [VoteWeight] = &[10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000];
}

impl pallet_bags_list::Config for Runtime {
impl pallet_bags_list::Config<VoterBagsListInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type BagThresholds = BagThresholds;
Expand Down Expand Up @@ -180,7 +182,7 @@ frame_support::construct_runtime!(
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Staking: pallet_staking::{Pallet, Call, Config<T>, Storage, Event<T>},
BagsList: pallet_bags_list::{Pallet, Call, Storage, Event<T>},
VoterList: pallet_bags_list::<Instance1>::{Pallet, Call, Storage, Event<T>},
Pools: pallet_nomination_pools::{Pallet, Call, Storage, Event<T>},
}
);
Expand Down
8 changes: 5 additions & 3 deletions frame/nomination-pools/test-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ impl pallet_staking::Config for Runtime {
type ElectionProvider =
frame_election_provider_support::NoElection<(AccountId, BlockNumber, Staking)>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = pallet_bags_list::Pallet<Self>;
type VoterList = VoterList;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type OnStakerSlash = Pools;
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
Expand All @@ -137,7 +138,8 @@ parameter_types! {
pub static BagThresholds: &'static [VoteWeight] = &[10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000];
}

impl pallet_bags_list::Config for Runtime {
type VoterBagsListInstance = pallet_bags_list::Instance1;
impl pallet_bags_list::Config<VoterBagsListInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type BagThresholds = BagThresholds;
Expand Down Expand Up @@ -193,7 +195,7 @@ frame_support::construct_runtime!(
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Staking: pallet_staking::{Pallet, Call, Config<T>, Storage, Event<T>},
BagsList: pallet_bags_list::{Pallet, Call, Storage, Event<T>},
VoterList: pallet_bags_list::<Instance1>::{Pallet, Call, Storage, Event<T>},
Pools: pallet_nomination_pools::{Pallet, Call, Storage, Event<T>},
}
);
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 @@ -180,6 +180,7 @@ impl pallet_staking::Config for Test {
type ElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type OnStakerSlash = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
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 @@ -175,6 +175,7 @@ impl pallet_staking::Config for Test {
type GenesisElectionProvider = Self::ElectionProvider;
type MaxUnlockingChunks = ConstU32<32>;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type OnStakerSlash = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ benchmarks! {
v, n, T::MaxNominations::get() as usize, false, None
)?;
}: {
let targets = <Staking<T>>::get_npos_targets();
let targets = <Staking<T>>::get_npos_targets(None);
assert_eq!(targets.len() as u32, v);
}

Expand Down
3 changes: 2 additions & 1 deletion frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,11 +886,12 @@ enum Releases {
V8_0_0, // populate `VoterList`.
V9_0_0, // inject validators into `VoterList` as well.
V10_0_0, // remove `EarliestUnappliedSlash`.
V11_0_0, // Move pallet storage prefix, e.g. BagsList -> VoterBagsList
}

impl Default for Releases {
fn default() -> Self {
Releases::V10_0_0
Releases::V11_0_0
}
}

Expand Down
Loading