From 64b5c6eede32e4c3ae05c4fce6c5f570fb59c3f6 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 15 Aug 2022 11:18:47 +0200 Subject: [PATCH 01/40] [Feature] Part 1: add TargetList for validator ranking --- bin/node/runtime/src/lib.rs | 26 +++++- frame/babe/src/mock.rs | 1 + frame/grandpa/src/mock.rs | 1 + .../nomination-pools/benchmarking/src/mock.rs | 1 + frame/offences/benchmarking/src/mock.rs | 1 + frame/session/benchmarking/src/mock.rs | 1 + frame/staking/src/lib.rs | 10 +++ frame/staking/src/mock.rs | 90 ++++++++++++++++++- frame/staking/src/pallet/impls.rs | 44 +++++++++ frame/staking/src/pallet/mod.rs | 34 +++++++ 10 files changed, 202 insertions(+), 7 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index ff793a49b5ce6..260a12d4a4179 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -564,7 +564,8 @@ impl pallet_staking::Config for Runtime { type OffendingValidatorsThreshold = OffendingValidatorsThreshold; type ElectionProvider = ElectionProviderMultiPhase; type GenesisElectionProvider = onchain::UnboundedExecution; - type VoterList = BagsList; + type VoterList = VoterBagsList; + type TargetList = TargetBagsList; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = NominationPools; type WeightInfo = pallet_staking::weights::SubstrateWeight; @@ -718,14 +719,30 @@ impl pallet_election_provider_multi_phase::Config for Runtime { parameter_types! { pub const BagThresholds: &'static [u64] = &voter_bags::THRESHOLDS; + // TODO: revisit to see if we can generate separate thresholds here + pub const BagThresholdsBalance: &'static [u64] = &voter_bags::THRESHOLDS; } -impl pallet_bags_list::Config for Runtime { +type VoterBagsListInstance = pallet_bags_list::Instance1; +impl pallet_bags_list::Config for Runtime { type Event = Event; + /// 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; type BagThresholds = BagThresholds; type Score = VoteWeight; + type WeightInfo = pallet_bags_list::weights::SubstrateWeight; +} + +type TargetBagsListInstance = pallet_bags_list::Instance2; +impl pallet_bags_list::Config for Runtime { + type Event = Event; + // The bags-list itself will be the source of truth about the approval stakes. This implies that + // staking should keep the approval stakes up to date at all times. + type ScoreProvider = TargetBagsList; + type BagThresholds = BagThresholdsBalance; + type Score = Balance; + type WeightInfo = pallet_bags_list::weights::SubstrateWeight; } parameter_types! { @@ -1629,7 +1646,8 @@ construct_runtime!( Gilt: pallet_gilt, Uniques: pallet_uniques, TransactionStorage: pallet_transaction_storage, - BagsList: pallet_bags_list, + VoterBagsList: pallet_bags_list::, + TargetBagsList: pallet_bags_list::, StateTrieMigration: pallet_state_trie_migration, ChildBounties: pallet_child_bounties, Referenda: pallet_referenda, diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index c2ba3c2be06d8..fd8b672fb6b20 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -202,6 +202,7 @@ impl pallet_staking::Config for Test { type ElectionProvider = onchain::UnboundedExecution; type GenesisElectionProvider = Self::ElectionProvider; type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; + type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = (); type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index 5e6c955c441c5..0787d7e3f16e8 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -206,6 +206,7 @@ impl pallet_staking::Config for Test { type ElectionProvider = onchain::UnboundedExecution; type GenesisElectionProvider = Self::ElectionProvider; type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; + type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = (); type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; diff --git a/frame/nomination-pools/benchmarking/src/mock.rs b/frame/nomination-pools/benchmarking/src/mock.rs index d239d4f072b80..b4f707b8152af 100644 --- a/frame/nomination-pools/benchmarking/src/mock.rs +++ b/frame/nomination-pools/benchmarking/src/mock.rs @@ -112,6 +112,7 @@ impl pallet_staking::Config for Runtime { frame_election_provider_support::NoElection<(AccountId, BlockNumber, Staking)>; type GenesisElectionProvider = Self::ElectionProvider; type VoterList = pallet_bags_list::Pallet; + type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = Pools; type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index d51a81b1212c0..3f73de1dce9ae 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -178,6 +178,7 @@ impl pallet_staking::Config for Test { type ElectionProvider = onchain::UnboundedExecution; type GenesisElectionProvider = Self::ElectionProvider; type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; + type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = (); type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index 2181493f72947..4abea482bc526 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -175,6 +175,7 @@ impl pallet_staking::Config for Test { type GenesisElectionProvider = Self::ElectionProvider; type MaxUnlockingChunks = ConstU32<32>; type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; + type TargetList = pallet_staking::UseValidatorsMap; type OnStakerSlash = (); type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; type WeightInfo = (); diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index ab0ab685e6911..5f58ed924c4d5 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -685,6 +685,16 @@ pub struct Nominations { pub suppressed: bool, } +/// An unbounded version of `Nominations`, use for some really wacky hacks. +#[derive(PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo)] +#[codec(mel_bound())] +#[scale_info(skip_type_params(T))] +struct UnboundedNominations { + pub targets: Vec, + pub submitted_in: EraIndex, + pub suppressed: bool, +} + /// The amount of exposure (to slashing) than an individual nominator has. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] pub struct IndividualExposure { diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index d9dc97f9c1127..7d95ce2e3c188 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -100,7 +100,8 @@ frame_support::construct_runtime!( Staking: pallet_staking::{Pallet, Call, Config, Storage, Event}, Session: pallet_session::{Pallet, Call, Storage, Event, Config}, Historical: pallet_session::historical::{Pallet, Storage}, - BagsList: pallet_bags_list::{Pallet, Call, Storage, Event}, + VoterBagsList: pallet_bags_list::::{Pallet, Call, Storage, Event}, + TargetBagsList: pallet_bags_list::::{Pallet, Call, Storage, Event}, } ); @@ -233,22 +234,37 @@ impl OnUnbalanced> for RewardRemainderMock { const THRESHOLDS: [sp_npos_elections::VoteWeight; 9] = [10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000]; +const THRESHOLDS_BALANCE: [Balance; 9] = [10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000]; parameter_types! { pub static BagThresholds: &'static [sp_npos_elections::VoteWeight] = &THRESHOLDS; + // TODO: generate separate thresholds for this + pub static BagThresholdsBalance: &'static [sp_npos_elections::Balance] = &THRESHOLDS_BALANCE; pub static MaxNominations: u32 = 16; pub static RewardOnUnbalanceWasCalled: bool = false; pub static LedgerSlashPerEra: (BalanceOf, BTreeMap>) = (Zero::zero(), BTreeMap::new()); } -impl pallet_bags_list::Config for Test { +type VoterBagsListInstance = pallet_bags_list::Instance1; +impl pallet_bags_list::Config for Test { type Event = Event; type WeightInfo = (); + // Staking is the source of truth for voter bags list, since they are not kept up to date. type ScoreProvider = Staking; type BagThresholds = BagThresholds; type Score = VoteWeight; } +type TargetBagsListInstance = pallet_bags_list::Instance2; +impl pallet_bags_list::Config for Test { + type Event = Event; + type WeightInfo = (); + // Target bags-list are always kept up to date, and in fact Staking does not know them at all! + type ScoreProvider = pallet_bags_list::Pallet; + type BagThresholds = BagThresholdsBalance; + type Score = Balance; +} + pub struct OnChainSeqPhragmen; impl onchain::Config for OnChainSeqPhragmen { type System = Test; @@ -257,6 +273,61 @@ impl onchain::Config for OnChainSeqPhragmen { type WeightInfo = (); } +pub struct TargetBagsListCompat; +impl SortedListProvider for TargetBagsListCompat { + type Error = >::Error; + type Score = >::Score; + + fn iter() -> Box> { + let mut all = TargetBagsList::iter() + .map(|x| (x, TargetBagsList::get_score(&x).unwrap_or_default())) + .collect::>(); + all.sort_by(|a, b| match a.1.partial_cmp(&b.1).unwrap() { + std::cmp::Ordering::Equal => b.0.partial_cmp(&a.0).unwrap(), + // Question: why rerverse? + x @ _ => x.reverse(), + }); + Box::new(all.into_iter().map(|(x, _)| x)) + } + fn iter_from(start: &AccountId) -> Result>, Self::Error> { + TargetBagsList::iter_from(start) + } + fn count() -> u32 { + TargetBagsList::count() + } + fn contains(id: &AccountId) -> bool { + TargetBagsList::contains(id) + } + fn on_insert(id: AccountId, weight: Self::Score) -> Result<(), Self::Error> { + TargetBagsList::on_insert(id, weight) + } + fn on_update(id: &AccountId, weight: Self::Score) -> Result<(), Self::Error> { + TargetBagsList::on_update(id, weight) + } + fn get_score(id: &AccountId) -> Result { + TargetBagsList::get_score(id) + } + fn on_remove(id: &AccountId) -> Result<(), Self::Error> { + TargetBagsList::on_remove(id) + } + fn unsafe_regenerate( + all: impl IntoIterator, + weight_of: Box Self::Score>, + ) -> u32 { + TargetBagsList::unsafe_regenerate(all, weight_of) + } + fn unsafe_clear() { + TargetBagsList::unsafe_clear(); + } + fn sanity_check() -> Result<(), &'static str> { + TargetBagsList::sanity_check() + } + #[cfg(feature = "runtime-benchmarks")] + fn score_update_worst_case(_who: &AccountId, _is_increase: bool) -> Self::Score { + Balance::MAX + } +} + pub struct MockReward {} impl OnUnbalanced> for MockReward { fn on_unbalanced(_: PositiveImbalanceOf) { @@ -297,7 +368,8 @@ impl crate::pallet::pallet::Config for Test { type ElectionProvider = onchain::UnboundedExecution; type GenesisElectionProvider = Self::ElectionProvider; // NOTE: consider a macro and use `UseNominatorsAndValidatorsMap` as well. - type VoterList = BagsList; + type VoterList = VoterBagsList; + type TargetList = TargetBagsListCompat; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = OnStakerSlashMock; type BenchmarkingConfig = TestBenchmarkingConfig; @@ -893,3 +965,15 @@ pub(crate) fn staking_events_since_last_call() -> Vec> { pub(crate) fn balances(who: &AccountId) -> (Balance, Balance) { (Balances::free_balance(who), Balances::reserved_balance(who)) } + +pub(crate) fn validator_ids() -> Vec { + Validators::::iter().map(|(v, _)| v).collect::>() +} + +pub(crate) fn nominator_ids() -> Vec { + Nominators::::iter().map(|(n, _)| n).collect::>() +} + +pub(crate) fn nominator_targets(who: AccountId) -> Vec { + Nominators::::get(&who).map(|n| n.targets).unwrap().into_inner() +} diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 68aa97db8a324..350f21c8e2756 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1309,6 +1309,50 @@ impl ScoreProvider for Pallet { } } +/// A simple sorted list implementation that does not require any additional pallets. Note, this +/// does not provided validators in sorted ordered. If you desire nominators in a sorted order take +/// a look at [`pallet-bags-list]. +pub struct UseValidatorsMap(sp_std::marker::PhantomData); +impl SortedListProvider for UseValidatorsMap { + type Error = (); + + /// Returns iterator over voter list, which can have `take` called on it. + fn iter() -> Box> { + Box::new(Validators::::iter().map(|(v, _)| v)) + } + fn count() -> u32 { + Validators::::count() + } + fn contains(id: &T::AccountId) -> bool { + Validators::::contains_key(id) + } + fn on_insert(_: T::AccountId, _weight: VoteWeight) -> Result<(), Self::Error> { + // nothing to do on insert. + Ok(()) + } + fn on_update(_: &T::AccountId, _weight: VoteWeight) { + // nothing to do on update. + } + fn on_remove(_: &T::AccountId) { + // nothing to do on remove. + } + fn unsafe_regenerate( + _: impl IntoIterator, + _: Box VoteWeight>, + ) -> u32 { + // nothing to do upon regenerate. + 0 + } + fn sanity_check() -> Result<(), &'static str> { + Ok(()) + } + + fn unsafe_clear() { + Validators::::remove_all(); + } +} + + /// A simple voter list implementation that does not require any additional pallets. Note, this /// does not provided nominators in sorted ordered. If you desire nominators in a sorted order take /// a look at [`pallet-bags-list]. diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 4ce96ab68b11a..43bdff62ea801 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -184,8 +184,34 @@ pub mod pallet { /// /// The changes to nominators are reported to this. Moreover, each validator's self-vote is /// also reported as one independent vote. + /// + /// To keep the load off the chain as much as possible, changes made to the staked amount + /// via rewards and slashes are not reported and thus need to be manually fixed by the + /// staker. In case of `bags-list`, this always means using `rebag` and `putInFrontOf`. + /// + /// Invariant: what comes out of this list will always be a nominator. type VoterList: SortedListProvider; + /// Something that provides a best-effort sorted list of targets aka electable validators, + /// used for NPoS election. + /// + /// The changes to the approval stake of each validator are reported to this. This means any + /// change to: + /// 1. The stake of any validator or nominator. + /// 2. The targets of any nominator + /// 3. The role of any staker (e.g. validator -> chilled, nominator -> validator, etc) + /// + /// Unlike `VoterList`, the values in this list are always kept up to date with reward and + /// slash as well, and thus represent the accurate approval stake of all account being + /// nominated by nominators. + /// + /// Note that while at the time of nomination, all targets are checked to be real + /// validators, they can chill at any point, and their approval stakes will still be + /// recorded. This implies that what comes out of iterating this list MIGHT NOT BE AN ACTIVE + /// VALIDATOR. + /// WIP + type TargetList: SortedListProvider> = (); + /// The maximum number of `unlocking` chunks a [`StakingLedger`] can have. Effectively /// determines how many unique eras a staker may be unbonding in. #[pallet::constant] @@ -298,6 +324,14 @@ pub mod pallet { pub type Nominators = CountedStorageMap<_, Twox64Concat, T::AccountId, Nominations>; + /// Get the nominator `who`, regardless of being decodable or not. + pub(crate) fn get_any(who: &T::AccountId) -> Option> { + frame_support::storage::unhashed::get::>( + &Nominators::::hashed_key_for(who), + ) + } + } + /// The maximum nominator count before we stop allowing new validators to join. /// /// When this value is not set, no limits are enforced. From a6b07a5158794e1610c787c3169ac175f9472586 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 15 Aug 2022 11:28:35 +0200 Subject: [PATCH 02/40] remove redundant todo --- frame/staking/src/mock.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 7d95ce2e3c188..5ec1a5b2be82e 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -238,7 +238,6 @@ const THRESHOLDS_BALANCE: [Balance; 9] = [10, 20, 30, 40, 50, 60, 1_000, 2_000, parameter_types! { pub static BagThresholds: &'static [sp_npos_elections::VoteWeight] = &THRESHOLDS; - // TODO: generate separate thresholds for this pub static BagThresholdsBalance: &'static [sp_npos_elections::Balance] = &THRESHOLDS_BALANCE; pub static MaxNominations: u32 = 16; pub static RewardOnUnbalanceWasCalled: bool = false; From ec61b296b7e8d268da25c78a69ef7926fdf6a6c2 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 15 Aug 2022 11:36:51 +0200 Subject: [PATCH 03/40] remove typo --- frame/staking/src/pallet/mod.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 43bdff62ea801..f976defb53b5d 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -324,14 +324,6 @@ pub mod pallet { pub type Nominators = CountedStorageMap<_, Twox64Concat, T::AccountId, Nominations>; - /// Get the nominator `who`, regardless of being decodable or not. - pub(crate) fn get_any(who: &T::AccountId) -> Option> { - frame_support::storage::unhashed::get::>( - &Nominators::::hashed_key_for(who), - ) - } - } - /// The maximum nominator count before we stop allowing new validators to join. /// /// When this value is not set, no limits are enforced. From 281e5d0afeed7f544cf603c3d3aa7903500e93b9 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 15 Aug 2022 11:39:25 +0200 Subject: [PATCH 04/40] cleanup --- frame/staking/src/pallet/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index f976defb53b5d..12ea979e2b50a 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -210,7 +210,7 @@ pub mod pallet { /// recorded. This implies that what comes out of iterating this list MIGHT NOT BE AN ACTIVE /// VALIDATOR. /// WIP - type TargetList: SortedListProvider> = (); + type TargetList: SortedListProvider>; /// The maximum number of `unlocking` chunks a [`StakingLedger`] can have. Effectively /// determines how many unique eras a staker may be unbonding in. From 73b47cbb6f0c6fa549d912b210def4c844b75b64 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 15 Aug 2022 18:35:49 +0200 Subject: [PATCH 05/40] implement score --- frame/staking/src/pallet/impls.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 350f21c8e2756..1b4a7d7f5ace5 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1310,16 +1310,30 @@ impl ScoreProvider for Pallet { } /// A simple sorted list implementation that does not require any additional pallets. Note, this -/// does not provided validators in sorted ordered. If you desire nominators in a sorted order take +/// does not provide validators in sorted order. If you desire nominators in a sorted order take /// a look at [`pallet-bags-list]. pub struct UseValidatorsMap(sp_std::marker::PhantomData); impl SortedListProvider for UseValidatorsMap { + type Score = VoteWeight; type Error = (); /// Returns iterator over voter list, which can have `take` called on it. fn iter() -> Box> { Box::new(Validators::::iter().map(|(v, _)| v)) } + fn iter_from( + start: &T::AccountId, + ) -> Result>, Self::Error> { + if Validators::::contains_key(start) { + let start_key = Validators::::hashed_key_for(start); + Ok(Box::new( + Validators::::iter_from(start_key) + .map(|(n, _)| n) + )) + } else { + Err(()) + } + } fn count() -> u32 { Validators::::count() } @@ -1330,6 +1344,9 @@ impl SortedListProvider for UseValidatorsMap { // nothing to do on insert. Ok(()) } + fn get_score(id: &T::AccountId) -> Result { + Ok(Pallet::::weight_of(id)) + } fn on_update(_: &T::AccountId, _weight: VoteWeight) { // nothing to do on update. } @@ -1348,7 +1365,7 @@ impl SortedListProvider for UseValidatorsMap { } fn unsafe_clear() { - Validators::::remove_all(); + Validators::::clear(); } } From 76034bb1c5768e1f092a09d67f013fd9cf942d22 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 15 Aug 2022 19:45:33 +0200 Subject: [PATCH 06/40] more fixes --- frame/staking/src/pallet/impls.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 1b4a7d7f5ace5..41f4fd5ba04c0 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1347,11 +1347,13 @@ impl SortedListProvider for UseValidatorsMap { fn get_score(id: &T::AccountId) -> Result { Ok(Pallet::::weight_of(id)) } - fn on_update(_: &T::AccountId, _weight: VoteWeight) { + fn on_update(_: &T::AccountId, _weight: Self::Score) -> Result<(), Self::Error> { // nothing to do on update. + Ok(()) } - fn on_remove(_: &T::AccountId) { + fn on_remove(_: &T::AccountId) -> Result<(), Self::Error> { // nothing to do on remove. + Ok(()) } fn unsafe_regenerate( _: impl IntoIterator, @@ -1365,7 +1367,8 @@ impl SortedListProvider for UseValidatorsMap { } fn unsafe_clear() { - Validators::::clear(); + #[allow(deprecated)] + Validators::::remove_all(); } } From 86631edc5292095797378b0444d830e48482a4f3 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 15 Aug 2022 21:30:13 +0200 Subject: [PATCH 07/40] fix thresholds --- bin/node/runtime/src/lib.rs | 2 +- bin/node/runtime/src/voter_bags.rs | 591 ++++++++++++++++++--------- frame/staking/src/mock.rs | 2 +- utils/frame/generate-bags/src/lib.rs | 16 + 4 files changed, 417 insertions(+), 194 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 260a12d4a4179..87fab797d0b58 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -720,7 +720,7 @@ impl pallet_election_provider_multi_phase::Config for Runtime { parameter_types! { pub const BagThresholds: &'static [u64] = &voter_bags::THRESHOLDS; // TODO: revisit to see if we can generate separate thresholds here - pub const BagThresholdsBalance: &'static [u64] = &voter_bags::THRESHOLDS; + pub const BagThresholdsBalance: &'static [u128] = &voter_bags::THRESHOLDS_BALANCES; } type VoterBagsListInstance = pallet_bags_list::Instance1; diff --git a/bin/node/runtime/src/voter_bags.rs b/bin/node/runtime/src/voter_bags.rs index 93790f028f457..74a22fe2632c2 100644 --- a/bin/node/runtime/src/voter_bags.rs +++ b/bin/node/runtime/src/voter_bags.rs @@ -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"); @@ -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. @@ -32,195 +35,399 @@ pub const CONSTANT_RATIO: f64 = 1.0628253590743408; /// Upper thresholds delimiting the bag list. pub const THRESHOLDS: [u64; 200] = [ - 100_000_000_000_000, - 106_282_535_907_434, - 112_959_774_389_150, - 120_056_512_776_105, - 127_599_106_300_477, - 135_615_565_971_369, - 144_135_662_599_590, - 153_191_037_357_827, - 162_815_319_286_803, - 173_044_250_183_800, - 183_915_817_337_347, - 195_470_394_601_017, - 207_750_892_330_229, - 220_802_916_738_890, - 234_674_939_267_673, - 249_418_476_592_914, - 265_088_281_944_639, - 281_742_548_444_211, - 299_443_125_216_738, - 318_255_747_080_822, - 338_250_278_668_647, - 359_500_973_883_001, - 382_086_751_654_776, - 406_091_489_025_036, - 431_604_332_640_068, - 458_720_029_816_222, - 487_539_280_404_019, - 518_169_110_758_247, - 550_723_271_202_866, - 585_322_658_466_782, - 622_095_764_659_305, - 661_179_154_452_653, - 702_717_972_243_610, - 746_866_481_177_808, - 793_788_636_038_393, - 843_658_692_126_636, - 896_661_852_395_681, - 952_994_955_240_703, - 1_012_867_205_499_736, - 1_076_500_951_379_881, - 1_144_132_510_194_192, - 1_216_013_045_975_769, - 1_292_409_502_228_280, - 1_373_605_593_276_862, - 1_459_902_857_901_004, - 1_551_621_779_162_291, - 1_649_102_974_585_730, - 1_752_708_461_114_642, - 1_862_822_999_536_805, - 1_979_855_523_374_646, - 2_104_240_657_545_975, - 2_236_440_332_435_128, - 2_376_945_499_368_703, - 2_526_277_953_866_680, - 2_684_992_273_439_945, - 2_853_677_877_130_641, - 3_032_961_214_443_876, - 3_223_508_091_799_862, - 3_426_026_145_146_232, - 3_641_267_467_913_124, - 3_870_031_404_070_482, - 4_113_167_516_660_186, - 4_371_578_742_827_277, - 4_646_224_747_067_156, - 4_938_125_485_141_739, - 5_248_364_991_899_922, - 5_578_095_407_069_235, - 5_928_541_253_969_291, - 6_301_003_987_036_955, - 6_696_866_825_051_405, - 7_117_599_888_008_300, - 7_564_765_656_719_910, - 8_040_024_775_416_580, - 8_545_142_218_898_723, - 9_081_993_847_142_344, - 9_652_573_371_700_016, - 10_258_999_759_768_490, - 10_903_525_103_419_522, - 11_588_542_983_217_942, - 12_316_597_357_287_042, - 13_090_392_008_832_678, - 13_912_800_587_211_472, - 14_786_877_279_832_732, - 15_715_868_154_526_436, - 16_703_223_214_499_558, - 17_752_609_210_649_358, - 18_867_923_258_814_856, - 20_053_307_312_537_008, - 21_313_163_545_075_252, - 22_652_170_697_804_756, - 24_075_301_455_707_600, - 25_587_840_914_485_432, - 27_195_406_207_875_088, - 28_903_967_368_057_400, - 30_719_869_496_628_636, - 32_649_856_328_471_220, - 34_701_095_276_033_064, - 36_881_204_047_022_752, - 39_198_278_934_370_992, - 41_660_924_883_519_016, - 44_278_287_448_695_240, - 47_060_086_756_856_400, - 50_016_653_605_425_536, - 53_158_967_827_883_320, - 56_498_699_069_691_424, - 60_048_250_125_977_912, - 63_820_803_001_928_304, - 67_830_367_866_937_216, - 72_091_835_084_322_176, - 76_621_030_509_822_880, - 81_434_774_264_248_528, - 86_550_943_198_537_824, - 91_988_537_283_208_848, - 97_767_750_168_749_840, - 103_910_044_178_992_000, - 110_438_230_015_967_792, - 117_376_551_472_255_616, - 124_750_775_465_407_920, - 132_588_287_728_824_640, - 140_918_194_514_440_064, - 149_771_430_684_917_568, - 159_180_874_596_775_264, - 169_181_470_201_085_280, - 179_810_356_815_193_344, - 191_107_007_047_393_216, - 203_113_373_386_768_288, - 215_874_044_002_592_672, - 229_436_408_331_885_600, - 243_850_833_070_063_392, - 259_170_849_218_267_264, - 275_453_350_882_006_752, - 292_758_806_559_399_232, - 311_151_483_703_668_992, - 330_699_687_393_865_920, - 351_476_014_000_157_824, - 373_557_620_785_735_808, - 397_026_512_446_556_096, - 421_969_845_653_044_224, - 448_480_252_724_740_928, - 476_656_185_639_923_904, - 506_602_281_657_757_760, - 538_429_751_910_786_752, - 572_256_794_410_890_176, - 608_209_033_002_485_632, - 646_419_983_893_124_352, - 687_031_551_494_039_552, - 730_194_555_412_054_016, - 776_069_290_549_944_960, - 824_826_122_395_314_176, - 876_646_119_708_695_936, - 931_721_726_960_522_368, - 990_257_479_014_182_144, - 1_052_470_760_709_299_712, - 1_118_592_614_166_106_112, - 1_188_868_596_808_997_376, - 1_263_559_693_295_730_432, - 1_342_943_284_738_898_688, - 1_427_314_178_819_094_784, - 1_516_985_704_615_302_400, - 1_612_290_876_218_400_768, - 1_713_583_629_449_105_408, - 1_821_240_136_273_157_632, - 1_935_660_201_795_120_128, - 2_057_268_749_018_809_600, - 2_186_517_396_888_336_384, - 2_323_886_137_470_138_880, - 2_469_885_118_504_583_168, - 2_625_056_537_947_004_416, - 2_789_976_657_533_970_944, - 2_965_257_942_852_572_160, - 3_151_551_337_860_326_400, - 3_349_548_682_302_620_672, - 3_559_985_281_005_267_968, - 3_783_642_634_583_792_128, - 4_021_351_341_710_503_936, - 4_273_994_183_717_548_544, - 4_542_509_402_991_247_872, - 4_827_894_187_332_742_144, - 5_131_208_373_224_844_288, - 5_453_578_381_757_959_168, - 5_796_201_401_831_965_696, - 6_160_349_836_169_256_960, - 6_547_376_026_650_146_816, - 6_958_717_276_519_173_120, - 7_395_901_188_113_309_696, - 7_860_551_335_934_872_576, - 8_354_393_296_137_270_272, - 8_879_261_054_815_360_000, - 9_437_103_818_898_946_048, + 100_000_000_000_000, + 106_282_535_907_434, + 112_959_774_389_150, + 120_056_512_776_105, + 127_599_106_300_477, + 135_615_565_971_369, + 144_135_662_599_590, + 153_191_037_357_827, + 162_815_319_286_803, + 173_044_250_183_800, + 183_915_817_337_347, + 195_470_394_601_017, + 207_750_892_330_229, + 220_802_916_738_890, + 234_674_939_267_673, + 249_418_476_592_914, + 265_088_281_944_639, + 281_742_548_444_211, + 299_443_125_216_738, + 318_255_747_080_822, + 338_250_278_668_647, + 359_500_973_883_001, + 382_086_751_654_776, + 406_091_489_025_036, + 431_604_332_640_068, + 458_720_029_816_222, + 487_539_280_404_019, + 518_169_110_758_247, + 550_723_271_202_866, + 585_322_658_466_782, + 622_095_764_659_305, + 661_179_154_452_653, + 702_717_972_243_610, + 746_866_481_177_808, + 793_788_636_038_393, + 843_658_692_126_636, + 896_661_852_395_681, + 952_994_955_240_703, + 1_012_867_205_499_736, + 1_076_500_951_379_881, + 1_144_132_510_194_192, + 1_216_013_045_975_769, + 1_292_409_502_228_280, + 1_373_605_593_276_862, + 1_459_902_857_901_004, + 1_551_621_779_162_291, + 1_649_102_974_585_730, + 1_752_708_461_114_642, + 1_862_822_999_536_805, + 1_979_855_523_374_646, + 2_104_240_657_545_975, + 2_236_440_332_435_128, + 2_376_945_499_368_703, + 2_526_277_953_866_680, + 2_684_992_273_439_945, + 2_853_677_877_130_641, + 3_032_961_214_443_876, + 3_223_508_091_799_862, + 3_426_026_145_146_232, + 3_641_267_467_913_124, + 3_870_031_404_070_482, + 4_113_167_516_660_186, + 4_371_578_742_827_277, + 4_646_224_747_067_156, + 4_938_125_485_141_739, + 5_248_364_991_899_922, + 5_578_095_407_069_235, + 5_928_541_253_969_291, + 6_301_003_987_036_955, + 6_696_866_825_051_405, + 7_117_599_888_008_300, + 7_564_765_656_719_910, + 8_040_024_775_416_580, + 8_545_142_218_898_723, + 9_081_993_847_142_344, + 9_652_573_371_700_016, + 10_258_999_759_768_490, + 10_903_525_103_419_522, + 11_588_542_983_217_942, + 12_316_597_357_287_042, + 13_090_392_008_832_678, + 13_912_800_587_211_472, + 14_786_877_279_832_732, + 15_715_868_154_526_436, + 16_703_223_214_499_558, + 17_752_609_210_649_358, + 18_867_923_258_814_856, + 20_053_307_312_537_008, + 21_313_163_545_075_252, + 22_652_170_697_804_756, + 24_075_301_455_707_600, + 25_587_840_914_485_432, + 27_195_406_207_875_088, + 28_903_967_368_057_400, + 30_719_869_496_628_636, + 32_649_856_328_471_220, + 34_701_095_276_033_064, + 36_881_204_047_022_752, + 39_198_278_934_370_992, + 41_660_924_883_519_016, + 44_278_287_448_695_240, + 47_060_086_756_856_400, + 50_016_653_605_425_536, + 53_158_967_827_883_320, + 56_498_699_069_691_424, + 60_048_250_125_977_912, + 63_820_803_001_928_304, + 67_830_367_866_937_216, + 72_091_835_084_322_176, + 76_621_030_509_822_880, + 81_434_774_264_248_528, + 86_550_943_198_537_824, + 91_988_537_283_208_848, + 97_767_750_168_749_840, + 103_910_044_178_992_000, + 110_438_230_015_967_792, + 117_376_551_472_255_616, + 124_750_775_465_407_920, + 132_588_287_728_824_640, + 140_918_194_514_440_064, + 149_771_430_684_917_568, + 159_180_874_596_775_264, + 169_181_470_201_085_280, + 179_810_356_815_193_344, + 191_107_007_047_393_216, + 203_113_373_386_768_288, + 215_874_044_002_592_672, + 229_436_408_331_885_600, + 243_850_833_070_063_392, + 259_170_849_218_267_264, + 275_453_350_882_006_752, + 292_758_806_559_399_232, + 311_151_483_703_668_992, + 330_699_687_393_865_920, + 351_476_014_000_157_824, + 373_557_620_785_735_808, + 397_026_512_446_556_096, + 421_969_845_653_044_224, + 448_480_252_724_740_928, + 476_656_185_639_923_904, + 506_602_281_657_757_760, + 538_429_751_910_786_752, + 572_256_794_410_890_176, + 608_209_033_002_485_632, + 646_419_983_893_124_352, + 687_031_551_494_039_552, + 730_194_555_412_054_016, + 776_069_290_549_944_960, + 824_826_122_395_314_176, + 876_646_119_708_695_936, + 931_721_726_960_522_368, + 990_257_479_014_182_144, + 1_052_470_760_709_299_712, + 1_118_592_614_166_106_112, + 1_188_868_596_808_997_376, + 1_263_559_693_295_730_432, + 1_342_943_284_738_898_688, + 1_427_314_178_819_094_784, + 1_516_985_704_615_302_400, + 1_612_290_876_218_400_768, + 1_713_583_629_449_105_408, + 1_821_240_136_273_157_632, + 1_935_660_201_795_120_128, + 2_057_268_749_018_809_600, + 2_186_517_396_888_336_384, + 2_323_886_137_470_138_880, + 2_469_885_118_504_583_168, + 2_625_056_537_947_004_416, + 2_789_976_657_533_970_944, + 2_965_257_942_852_572_160, + 3_151_551_337_860_326_400, + 3_349_548_682_302_620_672, + 3_559_985_281_005_267_968, + 3_783_642_634_583_792_128, + 4_021_351_341_710_503_936, + 4_273_994_183_717_548_544, + 4_542_509_402_991_247_872, + 4_827_894_187_332_742_144, + 5_131_208_373_224_844_288, + 5_453_578_381_757_959_168, + 5_796_201_401_831_965_696, + 6_160_349_836_169_256_960, + 6_547_376_026_650_146_816, + 6_958_717_276_519_173_120, + 7_395_901_188_113_309_696, + 7_860_551_335_934_872_576, + 8_354_393_296_137_270_272, + 8_879_261_054_815_360_000, + 9_437_103_818_898_946_048, + 10_029_993_254_943_105_024, + 10_660_131_182_698_121_216, + 11_329_857_752_030_707_712, + 12_041_660_133_563_240_448, + 12_798_181_755_305_525_248, + 13_602_232_119_581_272_064, + 14_456_797_236_706_498_560, + 15_365_050_714_167_523_328, + 16_330_365_542_480_556_032, + 17_356_326_621_502_140_416, + 18_446_744_073_709_551_615, +]; + +/// Upper thresholds delimiting the bag list. +pub const THRESHOLDS_BALANCES: [u128; 200] = [ + 100_000_000_000_000, + 106_282_535_907_434, + 112_959_774_389_150, + 120_056_512_776_105, + 127_599_106_300_477, + 135_615_565_971_369, + 144_135_662_599_590, + 153_191_037_357_827, + 162_815_319_286_803, + 173_044_250_183_800, + 183_915_817_337_347, + 195_470_394_601_017, + 207_750_892_330_229, + 220_802_916_738_890, + 234_674_939_267_673, + 249_418_476_592_914, + 265_088_281_944_639, + 281_742_548_444_211, + 299_443_125_216_738, + 318_255_747_080_822, + 338_250_278_668_647, + 359_500_973_883_001, + 382_086_751_654_776, + 406_091_489_025_036, + 431_604_332_640_068, + 458_720_029_816_222, + 487_539_280_404_019, + 518_169_110_758_247, + 550_723_271_202_866, + 585_322_658_466_782, + 622_095_764_659_305, + 661_179_154_452_653, + 702_717_972_243_610, + 746_866_481_177_808, + 793_788_636_038_393, + 843_658_692_126_636, + 896_661_852_395_681, + 952_994_955_240_703, + 1_012_867_205_499_736, + 1_076_500_951_379_881, + 1_144_132_510_194_192, + 1_216_013_045_975_769, + 1_292_409_502_228_280, + 1_373_605_593_276_862, + 1_459_902_857_901_004, + 1_551_621_779_162_291, + 1_649_102_974_585_730, + 1_752_708_461_114_642, + 1_862_822_999_536_805, + 1_979_855_523_374_646, + 2_104_240_657_545_975, + 2_236_440_332_435_128, + 2_376_945_499_368_703, + 2_526_277_953_866_680, + 2_684_992_273_439_945, + 2_853_677_877_130_641, + 3_032_961_214_443_876, + 3_223_508_091_799_862, + 3_426_026_145_146_232, + 3_641_267_467_913_124, + 3_870_031_404_070_482, + 4_113_167_516_660_186, + 4_371_578_742_827_277, + 4_646_224_747_067_156, + 4_938_125_485_141_739, + 5_248_364_991_899_922, + 5_578_095_407_069_235, + 5_928_541_253_969_291, + 6_301_003_987_036_955, + 6_696_866_825_051_405, + 7_117_599_888_008_300, + 7_564_765_656_719_910, + 8_040_024_775_416_580, + 8_545_142_218_898_723, + 9_081_993_847_142_344, + 9_652_573_371_700_016, + 10_258_999_759_768_490, + 10_903_525_103_419_522, + 11_588_542_983_217_942, + 12_316_597_357_287_042, + 13_090_392_008_832_678, + 13_912_800_587_211_472, + 14_786_877_279_832_732, + 15_715_868_154_526_436, + 16_703_223_214_499_558, + 17_752_609_210_649_358, + 18_867_923_258_814_856, + 20_053_307_312_537_008, + 21_313_163_545_075_252, + 22_652_170_697_804_756, + 24_075_301_455_707_600, + 25_587_840_914_485_432, + 27_195_406_207_875_088, + 28_903_967_368_057_400, + 30_719_869_496_628_636, + 32_649_856_328_471_220, + 34_701_095_276_033_064, + 36_881_204_047_022_752, + 39_198_278_934_370_992, + 41_660_924_883_519_016, + 44_278_287_448_695_240, + 47_060_086_756_856_400, + 50_016_653_605_425_536, + 53_158_967_827_883_320, + 56_498_699_069_691_424, + 60_048_250_125_977_912, + 63_820_803_001_928_304, + 67_830_367_866_937_216, + 72_091_835_084_322_176, + 76_621_030_509_822_880, + 81_434_774_264_248_528, + 86_550_943_198_537_824, + 91_988_537_283_208_848, + 97_767_750_168_749_840, + 103_910_044_178_992_000, + 110_438_230_015_967_792, + 117_376_551_472_255_616, + 124_750_775_465_407_920, + 132_588_287_728_824_640, + 140_918_194_514_440_064, + 149_771_430_684_917_568, + 159_180_874_596_775_264, + 169_181_470_201_085_280, + 179_810_356_815_193_344, + 191_107_007_047_393_216, + 203_113_373_386_768_288, + 215_874_044_002_592_672, + 229_436_408_331_885_600, + 243_850_833_070_063_392, + 259_170_849_218_267_264, + 275_453_350_882_006_752, + 292_758_806_559_399_232, + 311_151_483_703_668_992, + 330_699_687_393_865_920, + 351_476_014_000_157_824, + 373_557_620_785_735_808, + 397_026_512_446_556_096, + 421_969_845_653_044_224, + 448_480_252_724_740_928, + 476_656_185_639_923_904, + 506_602_281_657_757_760, + 538_429_751_910_786_752, + 572_256_794_410_890_176, + 608_209_033_002_485_632, + 646_419_983_893_124_352, + 687_031_551_494_039_552, + 730_194_555_412_054_016, + 776_069_290_549_944_960, + 824_826_122_395_314_176, + 876_646_119_708_695_936, + 931_721_726_960_522_368, + 990_257_479_014_182_144, + 1_052_470_760_709_299_712, + 1_118_592_614_166_106_112, + 1_188_868_596_808_997_376, + 1_263_559_693_295_730_432, + 1_342_943_284_738_898_688, + 1_427_314_178_819_094_784, + 1_516_985_704_615_302_400, + 1_612_290_876_218_400_768, + 1_713_583_629_449_105_408, + 1_821_240_136_273_157_632, + 1_935_660_201_795_120_128, + 2_057_268_749_018_809_600, + 2_186_517_396_888_336_384, + 2_323_886_137_470_138_880, + 2_469_885_118_504_583_168, + 2_625_056_537_947_004_416, + 2_789_976_657_533_970_944, + 2_965_257_942_852_572_160, + 3_151_551_337_860_326_400, + 3_349_548_682_302_620_672, + 3_559_985_281_005_267_968, + 3_783_642_634_583_792_128, + 4_021_351_341_710_503_936, + 4_273_994_183_717_548_544, + 4_542_509_402_991_247_872, + 4_827_894_187_332_742_144, + 5_131_208_373_224_844_288, + 5_453_578_381_757_959_168, + 5_796_201_401_831_965_696, + 6_160_349_836_169_256_960, + 6_547_376_026_650_146_816, + 6_958_717_276_519_173_120, + 7_395_901_188_113_309_696, + 7_860_551_335_934_872_576, + 8_354_393_296_137_270_272, + 8_879_261_054_815_360_000, + 9_437_103_818_898_946_048, 10_029_993_254_943_105_024, 10_660_131_182_698_121_216, 11_329_857_752_030_707_712, diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 5ec1a5b2be82e..96fea5fdf5d84 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -238,7 +238,7 @@ const THRESHOLDS_BALANCE: [Balance; 9] = [10, 20, 30, 40, 50, 60, 1_000, 2_000, parameter_types! { pub static BagThresholds: &'static [sp_npos_elections::VoteWeight] = &THRESHOLDS; - pub static BagThresholdsBalance: &'static [sp_npos_elections::Balance] = &THRESHOLDS_BALANCE; + pub static BagThresholdsBalance: &'static [sp_npos_elections::ExtendedBalance] = &THRESHOLDS_BALANCE; pub static MaxNominations: u32 = 16; pub static RewardOnUnbalanceWasCalled: bool = false; pub static LedgerSlashPerEra: (BalanceOf, BTreeMap>) = (Zero::zero(), BTreeMap::new()); diff --git a/utils/frame/generate-bags/src/lib.rs b/utils/frame/generate-bags/src/lib.rs index d4507c3be33ef..49106569e3123 100644 --- a/utils/frame/generate-bags/src/lib.rs +++ b/utils/frame/generate-bags/src/lib.rs @@ -207,6 +207,11 @@ pub fn generate_thresholds( writeln!(buf, "//! Autogenerated bag thresholds.")?; writeln!(buf, "//!")?; writeln!(buf, "//! Generated on {}", now.to_rfc3339())?; + writeln!(buf, "//! Arguments")?; + writeln!(buf, "//! Total issuance: {}", &total_issuance)?; + writeln!(buf, "//! Minimum balance: {}", &minimum_balance)?; + + writeln!( buf, "//! for the {} runtime.", @@ -234,6 +239,17 @@ pub fn generate_thresholds( writeln!(buf)?; writeln!(buf, "/// Upper thresholds delimiting the bag list.")?; writeln!(buf, "pub const THRESHOLDS: [u64; {}] = [", thresholds.len())?; + for threshold in &thresholds { + num_buf.write_formatted(threshold, &format); + // u64::MAX, with spacers every 3 digits, is 26 characters wide + writeln!(buf, " {:>26},", num_buf.as_str())?; + } + writeln!(buf, "];")?; + + // thresholds balance + writeln!(buf)?; + writeln!(buf, "/// Upper thresholds delimiting the bag list.")?; + writeln!(buf, "pub const THRESHOLDS_BALANCES: [u128; {}] = [", thresholds.len())?; for threshold in thresholds { num_buf.write_formatted(&threshold, &format); // u64::MAX, with spacers every 3 digits, is 26 characters wide From 3a825040c599aecad1ddbc517fa9d1562f72f3f4 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 16 Aug 2022 09:31:51 +0200 Subject: [PATCH 08/40] fmt --- bin/node/runtime/src/voter_bags.rs | 756 +++++++++++++-------------- frame/staking/src/pallet/impls.rs | 6 +- utils/frame/generate-bags/src/lib.rs | 1 - 3 files changed, 379 insertions(+), 384 deletions(-) diff --git a/bin/node/runtime/src/voter_bags.rs b/bin/node/runtime/src/voter_bags.rs index 74a22fe2632c2..1633ef7b9df3b 100644 --- a/bin/node/runtime/src/voter_bags.rs +++ b/bin/node/runtime/src/voter_bags.rs @@ -35,195 +35,195 @@ pub const CONSTANT_RATIO: f64 = 1.0628253590743408; /// Upper thresholds delimiting the bag list. pub const THRESHOLDS: [u64; 200] = [ - 100_000_000_000_000, - 106_282_535_907_434, - 112_959_774_389_150, - 120_056_512_776_105, - 127_599_106_300_477, - 135_615_565_971_369, - 144_135_662_599_590, - 153_191_037_357_827, - 162_815_319_286_803, - 173_044_250_183_800, - 183_915_817_337_347, - 195_470_394_601_017, - 207_750_892_330_229, - 220_802_916_738_890, - 234_674_939_267_673, - 249_418_476_592_914, - 265_088_281_944_639, - 281_742_548_444_211, - 299_443_125_216_738, - 318_255_747_080_822, - 338_250_278_668_647, - 359_500_973_883_001, - 382_086_751_654_776, - 406_091_489_025_036, - 431_604_332_640_068, - 458_720_029_816_222, - 487_539_280_404_019, - 518_169_110_758_247, - 550_723_271_202_866, - 585_322_658_466_782, - 622_095_764_659_305, - 661_179_154_452_653, - 702_717_972_243_610, - 746_866_481_177_808, - 793_788_636_038_393, - 843_658_692_126_636, - 896_661_852_395_681, - 952_994_955_240_703, - 1_012_867_205_499_736, - 1_076_500_951_379_881, - 1_144_132_510_194_192, - 1_216_013_045_975_769, - 1_292_409_502_228_280, - 1_373_605_593_276_862, - 1_459_902_857_901_004, - 1_551_621_779_162_291, - 1_649_102_974_585_730, - 1_752_708_461_114_642, - 1_862_822_999_536_805, - 1_979_855_523_374_646, - 2_104_240_657_545_975, - 2_236_440_332_435_128, - 2_376_945_499_368_703, - 2_526_277_953_866_680, - 2_684_992_273_439_945, - 2_853_677_877_130_641, - 3_032_961_214_443_876, - 3_223_508_091_799_862, - 3_426_026_145_146_232, - 3_641_267_467_913_124, - 3_870_031_404_070_482, - 4_113_167_516_660_186, - 4_371_578_742_827_277, - 4_646_224_747_067_156, - 4_938_125_485_141_739, - 5_248_364_991_899_922, - 5_578_095_407_069_235, - 5_928_541_253_969_291, - 6_301_003_987_036_955, - 6_696_866_825_051_405, - 7_117_599_888_008_300, - 7_564_765_656_719_910, - 8_040_024_775_416_580, - 8_545_142_218_898_723, - 9_081_993_847_142_344, - 9_652_573_371_700_016, - 10_258_999_759_768_490, - 10_903_525_103_419_522, - 11_588_542_983_217_942, - 12_316_597_357_287_042, - 13_090_392_008_832_678, - 13_912_800_587_211_472, - 14_786_877_279_832_732, - 15_715_868_154_526_436, - 16_703_223_214_499_558, - 17_752_609_210_649_358, - 18_867_923_258_814_856, - 20_053_307_312_537_008, - 21_313_163_545_075_252, - 22_652_170_697_804_756, - 24_075_301_455_707_600, - 25_587_840_914_485_432, - 27_195_406_207_875_088, - 28_903_967_368_057_400, - 30_719_869_496_628_636, - 32_649_856_328_471_220, - 34_701_095_276_033_064, - 36_881_204_047_022_752, - 39_198_278_934_370_992, - 41_660_924_883_519_016, - 44_278_287_448_695_240, - 47_060_086_756_856_400, - 50_016_653_605_425_536, - 53_158_967_827_883_320, - 56_498_699_069_691_424, - 60_048_250_125_977_912, - 63_820_803_001_928_304, - 67_830_367_866_937_216, - 72_091_835_084_322_176, - 76_621_030_509_822_880, - 81_434_774_264_248_528, - 86_550_943_198_537_824, - 91_988_537_283_208_848, - 97_767_750_168_749_840, - 103_910_044_178_992_000, - 110_438_230_015_967_792, - 117_376_551_472_255_616, - 124_750_775_465_407_920, - 132_588_287_728_824_640, - 140_918_194_514_440_064, - 149_771_430_684_917_568, - 159_180_874_596_775_264, - 169_181_470_201_085_280, - 179_810_356_815_193_344, - 191_107_007_047_393_216, - 203_113_373_386_768_288, - 215_874_044_002_592_672, - 229_436_408_331_885_600, - 243_850_833_070_063_392, - 259_170_849_218_267_264, - 275_453_350_882_006_752, - 292_758_806_559_399_232, - 311_151_483_703_668_992, - 330_699_687_393_865_920, - 351_476_014_000_157_824, - 373_557_620_785_735_808, - 397_026_512_446_556_096, - 421_969_845_653_044_224, - 448_480_252_724_740_928, - 476_656_185_639_923_904, - 506_602_281_657_757_760, - 538_429_751_910_786_752, - 572_256_794_410_890_176, - 608_209_033_002_485_632, - 646_419_983_893_124_352, - 687_031_551_494_039_552, - 730_194_555_412_054_016, - 776_069_290_549_944_960, - 824_826_122_395_314_176, - 876_646_119_708_695_936, - 931_721_726_960_522_368, - 990_257_479_014_182_144, - 1_052_470_760_709_299_712, - 1_118_592_614_166_106_112, - 1_188_868_596_808_997_376, - 1_263_559_693_295_730_432, - 1_342_943_284_738_898_688, - 1_427_314_178_819_094_784, - 1_516_985_704_615_302_400, - 1_612_290_876_218_400_768, - 1_713_583_629_449_105_408, - 1_821_240_136_273_157_632, - 1_935_660_201_795_120_128, - 2_057_268_749_018_809_600, - 2_186_517_396_888_336_384, - 2_323_886_137_470_138_880, - 2_469_885_118_504_583_168, - 2_625_056_537_947_004_416, - 2_789_976_657_533_970_944, - 2_965_257_942_852_572_160, - 3_151_551_337_860_326_400, - 3_349_548_682_302_620_672, - 3_559_985_281_005_267_968, - 3_783_642_634_583_792_128, - 4_021_351_341_710_503_936, - 4_273_994_183_717_548_544, - 4_542_509_402_991_247_872, - 4_827_894_187_332_742_144, - 5_131_208_373_224_844_288, - 5_453_578_381_757_959_168, - 5_796_201_401_831_965_696, - 6_160_349_836_169_256_960, - 6_547_376_026_650_146_816, - 6_958_717_276_519_173_120, - 7_395_901_188_113_309_696, - 7_860_551_335_934_872_576, - 8_354_393_296_137_270_272, - 8_879_261_054_815_360_000, - 9_437_103_818_898_946_048, + 100_000_000_000_000, + 106_282_535_907_434, + 112_959_774_389_150, + 120_056_512_776_105, + 127_599_106_300_477, + 135_615_565_971_369, + 144_135_662_599_590, + 153_191_037_357_827, + 162_815_319_286_803, + 173_044_250_183_800, + 183_915_817_337_347, + 195_470_394_601_017, + 207_750_892_330_229, + 220_802_916_738_890, + 234_674_939_267_673, + 249_418_476_592_914, + 265_088_281_944_639, + 281_742_548_444_211, + 299_443_125_216_738, + 318_255_747_080_822, + 338_250_278_668_647, + 359_500_973_883_001, + 382_086_751_654_776, + 406_091_489_025_036, + 431_604_332_640_068, + 458_720_029_816_222, + 487_539_280_404_019, + 518_169_110_758_247, + 550_723_271_202_866, + 585_322_658_466_782, + 622_095_764_659_305, + 661_179_154_452_653, + 702_717_972_243_610, + 746_866_481_177_808, + 793_788_636_038_393, + 843_658_692_126_636, + 896_661_852_395_681, + 952_994_955_240_703, + 1_012_867_205_499_736, + 1_076_500_951_379_881, + 1_144_132_510_194_192, + 1_216_013_045_975_769, + 1_292_409_502_228_280, + 1_373_605_593_276_862, + 1_459_902_857_901_004, + 1_551_621_779_162_291, + 1_649_102_974_585_730, + 1_752_708_461_114_642, + 1_862_822_999_536_805, + 1_979_855_523_374_646, + 2_104_240_657_545_975, + 2_236_440_332_435_128, + 2_376_945_499_368_703, + 2_526_277_953_866_680, + 2_684_992_273_439_945, + 2_853_677_877_130_641, + 3_032_961_214_443_876, + 3_223_508_091_799_862, + 3_426_026_145_146_232, + 3_641_267_467_913_124, + 3_870_031_404_070_482, + 4_113_167_516_660_186, + 4_371_578_742_827_277, + 4_646_224_747_067_156, + 4_938_125_485_141_739, + 5_248_364_991_899_922, + 5_578_095_407_069_235, + 5_928_541_253_969_291, + 6_301_003_987_036_955, + 6_696_866_825_051_405, + 7_117_599_888_008_300, + 7_564_765_656_719_910, + 8_040_024_775_416_580, + 8_545_142_218_898_723, + 9_081_993_847_142_344, + 9_652_573_371_700_016, + 10_258_999_759_768_490, + 10_903_525_103_419_522, + 11_588_542_983_217_942, + 12_316_597_357_287_042, + 13_090_392_008_832_678, + 13_912_800_587_211_472, + 14_786_877_279_832_732, + 15_715_868_154_526_436, + 16_703_223_214_499_558, + 17_752_609_210_649_358, + 18_867_923_258_814_856, + 20_053_307_312_537_008, + 21_313_163_545_075_252, + 22_652_170_697_804_756, + 24_075_301_455_707_600, + 25_587_840_914_485_432, + 27_195_406_207_875_088, + 28_903_967_368_057_400, + 30_719_869_496_628_636, + 32_649_856_328_471_220, + 34_701_095_276_033_064, + 36_881_204_047_022_752, + 39_198_278_934_370_992, + 41_660_924_883_519_016, + 44_278_287_448_695_240, + 47_060_086_756_856_400, + 50_016_653_605_425_536, + 53_158_967_827_883_320, + 56_498_699_069_691_424, + 60_048_250_125_977_912, + 63_820_803_001_928_304, + 67_830_367_866_937_216, + 72_091_835_084_322_176, + 76_621_030_509_822_880, + 81_434_774_264_248_528, + 86_550_943_198_537_824, + 91_988_537_283_208_848, + 97_767_750_168_749_840, + 103_910_044_178_992_000, + 110_438_230_015_967_792, + 117_376_551_472_255_616, + 124_750_775_465_407_920, + 132_588_287_728_824_640, + 140_918_194_514_440_064, + 149_771_430_684_917_568, + 159_180_874_596_775_264, + 169_181_470_201_085_280, + 179_810_356_815_193_344, + 191_107_007_047_393_216, + 203_113_373_386_768_288, + 215_874_044_002_592_672, + 229_436_408_331_885_600, + 243_850_833_070_063_392, + 259_170_849_218_267_264, + 275_453_350_882_006_752, + 292_758_806_559_399_232, + 311_151_483_703_668_992, + 330_699_687_393_865_920, + 351_476_014_000_157_824, + 373_557_620_785_735_808, + 397_026_512_446_556_096, + 421_969_845_653_044_224, + 448_480_252_724_740_928, + 476_656_185_639_923_904, + 506_602_281_657_757_760, + 538_429_751_910_786_752, + 572_256_794_410_890_176, + 608_209_033_002_485_632, + 646_419_983_893_124_352, + 687_031_551_494_039_552, + 730_194_555_412_054_016, + 776_069_290_549_944_960, + 824_826_122_395_314_176, + 876_646_119_708_695_936, + 931_721_726_960_522_368, + 990_257_479_014_182_144, + 1_052_470_760_709_299_712, + 1_118_592_614_166_106_112, + 1_188_868_596_808_997_376, + 1_263_559_693_295_730_432, + 1_342_943_284_738_898_688, + 1_427_314_178_819_094_784, + 1_516_985_704_615_302_400, + 1_612_290_876_218_400_768, + 1_713_583_629_449_105_408, + 1_821_240_136_273_157_632, + 1_935_660_201_795_120_128, + 2_057_268_749_018_809_600, + 2_186_517_396_888_336_384, + 2_323_886_137_470_138_880, + 2_469_885_118_504_583_168, + 2_625_056_537_947_004_416, + 2_789_976_657_533_970_944, + 2_965_257_942_852_572_160, + 3_151_551_337_860_326_400, + 3_349_548_682_302_620_672, + 3_559_985_281_005_267_968, + 3_783_642_634_583_792_128, + 4_021_351_341_710_503_936, + 4_273_994_183_717_548_544, + 4_542_509_402_991_247_872, + 4_827_894_187_332_742_144, + 5_131_208_373_224_844_288, + 5_453_578_381_757_959_168, + 5_796_201_401_831_965_696, + 6_160_349_836_169_256_960, + 6_547_376_026_650_146_816, + 6_958_717_276_519_173_120, + 7_395_901_188_113_309_696, + 7_860_551_335_934_872_576, + 8_354_393_296_137_270_272, + 8_879_261_054_815_360_000, + 9_437_103_818_898_946_048, 10_029_993_254_943_105_024, 10_660_131_182_698_121_216, 11_329_857_752_030_707_712, @@ -239,195 +239,195 @@ pub const THRESHOLDS: [u64; 200] = [ /// Upper thresholds delimiting the bag list. pub const THRESHOLDS_BALANCES: [u128; 200] = [ - 100_000_000_000_000, - 106_282_535_907_434, - 112_959_774_389_150, - 120_056_512_776_105, - 127_599_106_300_477, - 135_615_565_971_369, - 144_135_662_599_590, - 153_191_037_357_827, - 162_815_319_286_803, - 173_044_250_183_800, - 183_915_817_337_347, - 195_470_394_601_017, - 207_750_892_330_229, - 220_802_916_738_890, - 234_674_939_267_673, - 249_418_476_592_914, - 265_088_281_944_639, - 281_742_548_444_211, - 299_443_125_216_738, - 318_255_747_080_822, - 338_250_278_668_647, - 359_500_973_883_001, - 382_086_751_654_776, - 406_091_489_025_036, - 431_604_332_640_068, - 458_720_029_816_222, - 487_539_280_404_019, - 518_169_110_758_247, - 550_723_271_202_866, - 585_322_658_466_782, - 622_095_764_659_305, - 661_179_154_452_653, - 702_717_972_243_610, - 746_866_481_177_808, - 793_788_636_038_393, - 843_658_692_126_636, - 896_661_852_395_681, - 952_994_955_240_703, - 1_012_867_205_499_736, - 1_076_500_951_379_881, - 1_144_132_510_194_192, - 1_216_013_045_975_769, - 1_292_409_502_228_280, - 1_373_605_593_276_862, - 1_459_902_857_901_004, - 1_551_621_779_162_291, - 1_649_102_974_585_730, - 1_752_708_461_114_642, - 1_862_822_999_536_805, - 1_979_855_523_374_646, - 2_104_240_657_545_975, - 2_236_440_332_435_128, - 2_376_945_499_368_703, - 2_526_277_953_866_680, - 2_684_992_273_439_945, - 2_853_677_877_130_641, - 3_032_961_214_443_876, - 3_223_508_091_799_862, - 3_426_026_145_146_232, - 3_641_267_467_913_124, - 3_870_031_404_070_482, - 4_113_167_516_660_186, - 4_371_578_742_827_277, - 4_646_224_747_067_156, - 4_938_125_485_141_739, - 5_248_364_991_899_922, - 5_578_095_407_069_235, - 5_928_541_253_969_291, - 6_301_003_987_036_955, - 6_696_866_825_051_405, - 7_117_599_888_008_300, - 7_564_765_656_719_910, - 8_040_024_775_416_580, - 8_545_142_218_898_723, - 9_081_993_847_142_344, - 9_652_573_371_700_016, - 10_258_999_759_768_490, - 10_903_525_103_419_522, - 11_588_542_983_217_942, - 12_316_597_357_287_042, - 13_090_392_008_832_678, - 13_912_800_587_211_472, - 14_786_877_279_832_732, - 15_715_868_154_526_436, - 16_703_223_214_499_558, - 17_752_609_210_649_358, - 18_867_923_258_814_856, - 20_053_307_312_537_008, - 21_313_163_545_075_252, - 22_652_170_697_804_756, - 24_075_301_455_707_600, - 25_587_840_914_485_432, - 27_195_406_207_875_088, - 28_903_967_368_057_400, - 30_719_869_496_628_636, - 32_649_856_328_471_220, - 34_701_095_276_033_064, - 36_881_204_047_022_752, - 39_198_278_934_370_992, - 41_660_924_883_519_016, - 44_278_287_448_695_240, - 47_060_086_756_856_400, - 50_016_653_605_425_536, - 53_158_967_827_883_320, - 56_498_699_069_691_424, - 60_048_250_125_977_912, - 63_820_803_001_928_304, - 67_830_367_866_937_216, - 72_091_835_084_322_176, - 76_621_030_509_822_880, - 81_434_774_264_248_528, - 86_550_943_198_537_824, - 91_988_537_283_208_848, - 97_767_750_168_749_840, - 103_910_044_178_992_000, - 110_438_230_015_967_792, - 117_376_551_472_255_616, - 124_750_775_465_407_920, - 132_588_287_728_824_640, - 140_918_194_514_440_064, - 149_771_430_684_917_568, - 159_180_874_596_775_264, - 169_181_470_201_085_280, - 179_810_356_815_193_344, - 191_107_007_047_393_216, - 203_113_373_386_768_288, - 215_874_044_002_592_672, - 229_436_408_331_885_600, - 243_850_833_070_063_392, - 259_170_849_218_267_264, - 275_453_350_882_006_752, - 292_758_806_559_399_232, - 311_151_483_703_668_992, - 330_699_687_393_865_920, - 351_476_014_000_157_824, - 373_557_620_785_735_808, - 397_026_512_446_556_096, - 421_969_845_653_044_224, - 448_480_252_724_740_928, - 476_656_185_639_923_904, - 506_602_281_657_757_760, - 538_429_751_910_786_752, - 572_256_794_410_890_176, - 608_209_033_002_485_632, - 646_419_983_893_124_352, - 687_031_551_494_039_552, - 730_194_555_412_054_016, - 776_069_290_549_944_960, - 824_826_122_395_314_176, - 876_646_119_708_695_936, - 931_721_726_960_522_368, - 990_257_479_014_182_144, - 1_052_470_760_709_299_712, - 1_118_592_614_166_106_112, - 1_188_868_596_808_997_376, - 1_263_559_693_295_730_432, - 1_342_943_284_738_898_688, - 1_427_314_178_819_094_784, - 1_516_985_704_615_302_400, - 1_612_290_876_218_400_768, - 1_713_583_629_449_105_408, - 1_821_240_136_273_157_632, - 1_935_660_201_795_120_128, - 2_057_268_749_018_809_600, - 2_186_517_396_888_336_384, - 2_323_886_137_470_138_880, - 2_469_885_118_504_583_168, - 2_625_056_537_947_004_416, - 2_789_976_657_533_970_944, - 2_965_257_942_852_572_160, - 3_151_551_337_860_326_400, - 3_349_548_682_302_620_672, - 3_559_985_281_005_267_968, - 3_783_642_634_583_792_128, - 4_021_351_341_710_503_936, - 4_273_994_183_717_548_544, - 4_542_509_402_991_247_872, - 4_827_894_187_332_742_144, - 5_131_208_373_224_844_288, - 5_453_578_381_757_959_168, - 5_796_201_401_831_965_696, - 6_160_349_836_169_256_960, - 6_547_376_026_650_146_816, - 6_958_717_276_519_173_120, - 7_395_901_188_113_309_696, - 7_860_551_335_934_872_576, - 8_354_393_296_137_270_272, - 8_879_261_054_815_360_000, - 9_437_103_818_898_946_048, + 100_000_000_000_000, + 106_282_535_907_434, + 112_959_774_389_150, + 120_056_512_776_105, + 127_599_106_300_477, + 135_615_565_971_369, + 144_135_662_599_590, + 153_191_037_357_827, + 162_815_319_286_803, + 173_044_250_183_800, + 183_915_817_337_347, + 195_470_394_601_017, + 207_750_892_330_229, + 220_802_916_738_890, + 234_674_939_267_673, + 249_418_476_592_914, + 265_088_281_944_639, + 281_742_548_444_211, + 299_443_125_216_738, + 318_255_747_080_822, + 338_250_278_668_647, + 359_500_973_883_001, + 382_086_751_654_776, + 406_091_489_025_036, + 431_604_332_640_068, + 458_720_029_816_222, + 487_539_280_404_019, + 518_169_110_758_247, + 550_723_271_202_866, + 585_322_658_466_782, + 622_095_764_659_305, + 661_179_154_452_653, + 702_717_972_243_610, + 746_866_481_177_808, + 793_788_636_038_393, + 843_658_692_126_636, + 896_661_852_395_681, + 952_994_955_240_703, + 1_012_867_205_499_736, + 1_076_500_951_379_881, + 1_144_132_510_194_192, + 1_216_013_045_975_769, + 1_292_409_502_228_280, + 1_373_605_593_276_862, + 1_459_902_857_901_004, + 1_551_621_779_162_291, + 1_649_102_974_585_730, + 1_752_708_461_114_642, + 1_862_822_999_536_805, + 1_979_855_523_374_646, + 2_104_240_657_545_975, + 2_236_440_332_435_128, + 2_376_945_499_368_703, + 2_526_277_953_866_680, + 2_684_992_273_439_945, + 2_853_677_877_130_641, + 3_032_961_214_443_876, + 3_223_508_091_799_862, + 3_426_026_145_146_232, + 3_641_267_467_913_124, + 3_870_031_404_070_482, + 4_113_167_516_660_186, + 4_371_578_742_827_277, + 4_646_224_747_067_156, + 4_938_125_485_141_739, + 5_248_364_991_899_922, + 5_578_095_407_069_235, + 5_928_541_253_969_291, + 6_301_003_987_036_955, + 6_696_866_825_051_405, + 7_117_599_888_008_300, + 7_564_765_656_719_910, + 8_040_024_775_416_580, + 8_545_142_218_898_723, + 9_081_993_847_142_344, + 9_652_573_371_700_016, + 10_258_999_759_768_490, + 10_903_525_103_419_522, + 11_588_542_983_217_942, + 12_316_597_357_287_042, + 13_090_392_008_832_678, + 13_912_800_587_211_472, + 14_786_877_279_832_732, + 15_715_868_154_526_436, + 16_703_223_214_499_558, + 17_752_609_210_649_358, + 18_867_923_258_814_856, + 20_053_307_312_537_008, + 21_313_163_545_075_252, + 22_652_170_697_804_756, + 24_075_301_455_707_600, + 25_587_840_914_485_432, + 27_195_406_207_875_088, + 28_903_967_368_057_400, + 30_719_869_496_628_636, + 32_649_856_328_471_220, + 34_701_095_276_033_064, + 36_881_204_047_022_752, + 39_198_278_934_370_992, + 41_660_924_883_519_016, + 44_278_287_448_695_240, + 47_060_086_756_856_400, + 50_016_653_605_425_536, + 53_158_967_827_883_320, + 56_498_699_069_691_424, + 60_048_250_125_977_912, + 63_820_803_001_928_304, + 67_830_367_866_937_216, + 72_091_835_084_322_176, + 76_621_030_509_822_880, + 81_434_774_264_248_528, + 86_550_943_198_537_824, + 91_988_537_283_208_848, + 97_767_750_168_749_840, + 103_910_044_178_992_000, + 110_438_230_015_967_792, + 117_376_551_472_255_616, + 124_750_775_465_407_920, + 132_588_287_728_824_640, + 140_918_194_514_440_064, + 149_771_430_684_917_568, + 159_180_874_596_775_264, + 169_181_470_201_085_280, + 179_810_356_815_193_344, + 191_107_007_047_393_216, + 203_113_373_386_768_288, + 215_874_044_002_592_672, + 229_436_408_331_885_600, + 243_850_833_070_063_392, + 259_170_849_218_267_264, + 275_453_350_882_006_752, + 292_758_806_559_399_232, + 311_151_483_703_668_992, + 330_699_687_393_865_920, + 351_476_014_000_157_824, + 373_557_620_785_735_808, + 397_026_512_446_556_096, + 421_969_845_653_044_224, + 448_480_252_724_740_928, + 476_656_185_639_923_904, + 506_602_281_657_757_760, + 538_429_751_910_786_752, + 572_256_794_410_890_176, + 608_209_033_002_485_632, + 646_419_983_893_124_352, + 687_031_551_494_039_552, + 730_194_555_412_054_016, + 776_069_290_549_944_960, + 824_826_122_395_314_176, + 876_646_119_708_695_936, + 931_721_726_960_522_368, + 990_257_479_014_182_144, + 1_052_470_760_709_299_712, + 1_118_592_614_166_106_112, + 1_188_868_596_808_997_376, + 1_263_559_693_295_730_432, + 1_342_943_284_738_898_688, + 1_427_314_178_819_094_784, + 1_516_985_704_615_302_400, + 1_612_290_876_218_400_768, + 1_713_583_629_449_105_408, + 1_821_240_136_273_157_632, + 1_935_660_201_795_120_128, + 2_057_268_749_018_809_600, + 2_186_517_396_888_336_384, + 2_323_886_137_470_138_880, + 2_469_885_118_504_583_168, + 2_625_056_537_947_004_416, + 2_789_976_657_533_970_944, + 2_965_257_942_852_572_160, + 3_151_551_337_860_326_400, + 3_349_548_682_302_620_672, + 3_559_985_281_005_267_968, + 3_783_642_634_583_792_128, + 4_021_351_341_710_503_936, + 4_273_994_183_717_548_544, + 4_542_509_402_991_247_872, + 4_827_894_187_332_742_144, + 5_131_208_373_224_844_288, + 5_453_578_381_757_959_168, + 5_796_201_401_831_965_696, + 6_160_349_836_169_256_960, + 6_547_376_026_650_146_816, + 6_958_717_276_519_173_120, + 7_395_901_188_113_309_696, + 7_860_551_335_934_872_576, + 8_354_393_296_137_270_272, + 8_879_261_054_815_360_000, + 9_437_103_818_898_946_048, 10_029_993_254_943_105_024, 10_660_131_182_698_121_216, 11_329_857_752_030_707_712, diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 41f4fd5ba04c0..ec89877dcf274 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1326,10 +1326,7 @@ impl SortedListProvider for UseValidatorsMap { ) -> Result>, Self::Error> { if Validators::::contains_key(start) { let start_key = Validators::::hashed_key_for(start); - Ok(Box::new( - Validators::::iter_from(start_key) - .map(|(n, _)| n) - )) + Ok(Box::new(Validators::::iter_from(start_key).map(|(n, _)| n))) } else { Err(()) } @@ -1372,7 +1369,6 @@ impl SortedListProvider for UseValidatorsMap { } } - /// A simple voter list implementation that does not require any additional pallets. Note, this /// does not provided nominators in sorted ordered. If you desire nominators in a sorted order take /// a look at [`pallet-bags-list]. diff --git a/utils/frame/generate-bags/src/lib.rs b/utils/frame/generate-bags/src/lib.rs index 49106569e3123..23da131a668d8 100644 --- a/utils/frame/generate-bags/src/lib.rs +++ b/utils/frame/generate-bags/src/lib.rs @@ -211,7 +211,6 @@ pub fn generate_thresholds( writeln!(buf, "//! Total issuance: {}", &total_issuance)?; writeln!(buf, "//! Minimum balance: {}", &minimum_balance)?; - writeln!( buf, "//! for the {} runtime.", From 74b6fba024eeaae38b78a143a2c70358dec22450 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 16 Aug 2022 09:47:10 +0200 Subject: [PATCH 09/40] Remove the stuff that has to come in the next PR, some fixes --- bin/node/runtime/src/lib.rs | 17 +++-------------- frame/staking/src/lib.rs | 10 ---------- frame/staking/src/pallet/impls.rs | 2 +- frame/staking/src/pallet/mod.rs | 3 ++- 4 files changed, 6 insertions(+), 26 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 87fab797d0b58..af343c3ffcf49 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -565,7 +565,8 @@ impl pallet_staking::Config for Runtime { type ElectionProvider = ElectionProviderMultiPhase; type GenesisElectionProvider = onchain::UnboundedExecution; type VoterList = VoterBagsList; - type TargetList = TargetBagsList; + // This a placeholder, to be introduced in the next PR as an instance of bags-list + type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = NominationPools; type WeightInfo = pallet_staking::weights::SubstrateWeight; @@ -719,7 +720,7 @@ impl pallet_election_provider_multi_phase::Config for Runtime { parameter_types! { pub const BagThresholds: &'static [u64] = &voter_bags::THRESHOLDS; - // TODO: revisit to see if we can generate separate thresholds here + // This parameter is going to be needed for the operational TargetList implementation pub const BagThresholdsBalance: &'static [u128] = &voter_bags::THRESHOLDS_BALANCES; } @@ -734,17 +735,6 @@ impl pallet_bags_list::Config for Runtime { type WeightInfo = pallet_bags_list::weights::SubstrateWeight; } -type TargetBagsListInstance = pallet_bags_list::Instance2; -impl pallet_bags_list::Config for Runtime { - type Event = Event; - // The bags-list itself will be the source of truth about the approval stakes. This implies that - // staking should keep the approval stakes up to date at all times. - type ScoreProvider = TargetBagsList; - type BagThresholds = BagThresholdsBalance; - type Score = Balance; - type WeightInfo = pallet_bags_list::weights::SubstrateWeight; -} - parameter_types! { pub const PostUnbondPoolsWindow: u32 = 4; pub const NominationPoolsPalletId: PalletId = PalletId(*b"py/nopls"); @@ -1647,7 +1637,6 @@ construct_runtime!( Uniques: pallet_uniques, TransactionStorage: pallet_transaction_storage, VoterBagsList: pallet_bags_list::, - TargetBagsList: pallet_bags_list::, StateTrieMigration: pallet_state_trie_migration, ChildBounties: pallet_child_bounties, Referenda: pallet_referenda, diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 5f58ed924c4d5..ab0ab685e6911 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -685,16 +685,6 @@ pub struct Nominations { pub suppressed: bool, } -/// An unbounded version of `Nominations`, use for some really wacky hacks. -#[derive(PartialEqNoBound, EqNoBound, Clone, Encode, Decode, RuntimeDebugNoBound, TypeInfo)] -#[codec(mel_bound())] -#[scale_info(skip_type_params(T))] -struct UnboundedNominations { - pub targets: Vec, - pub submitted_in: EraIndex, - pub suppressed: bool, -} - /// The amount of exposure (to slashing) than an individual nominator has. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] pub struct IndividualExposure { diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index ec89877dcf274..6a946c62f2386 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1314,7 +1314,7 @@ impl ScoreProvider for Pallet { /// a look at [`pallet-bags-list]. pub struct UseValidatorsMap(sp_std::marker::PhantomData); impl SortedListProvider for UseValidatorsMap { - type Score = VoteWeight; + type Score = ExtendedBalance; type Error = (); /// Returns iterator over voter list, which can have `take` called on it. diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 12ea979e2b50a..b1e645a894ec2 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -192,6 +192,8 @@ pub mod pallet { /// Invariant: what comes out of this list will always be a nominator. type VoterList: SortedListProvider; + /// WIP: This is a noop as of now, the actual business logic that's described below is going + /// to be introduced in a follow-up PR. /// Something that provides a best-effort sorted list of targets aka electable validators, /// used for NPoS election. /// @@ -209,7 +211,6 @@ pub mod pallet { /// validators, they can chill at any point, and their approval stakes will still be /// recorded. This implies that what comes out of iterating this list MIGHT NOT BE AN ACTIVE /// VALIDATOR. - /// WIP type TargetList: SortedListProvider>; /// The maximum number of `unlocking` chunks a [`StakingLedger`] can have. Effectively From 8a2fc42159fafcdbda741ddd2f8eacacea162607 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 16 Aug 2022 09:53:56 +0200 Subject: [PATCH 10/40] extended balance import --- frame/staking/src/pallet/impls.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 6a946c62f2386..753a2989298d3 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -18,8 +18,8 @@ //! Implementations for the Staking FRAME Pallet. use frame_election_provider_support::{ - data_provider, ElectionDataProvider, ElectionProvider, ScoreProvider, SortedListProvider, - Supports, VoteWeight, VoterOf, + data_provider, ElectionDataProvider, ElectionProvider, ExtendedBalance, ScoreProvider, + SortedListProvider, Supports, VoteWeight, VoterOf, }; use frame_support::{ pallet_prelude::*, From e676398e7fc9499b2125fdc34ef4bcc0dfe6c0d2 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 16 Aug 2022 10:12:44 +0200 Subject: [PATCH 11/40] Change all the references from VoteWeight to Self::Score --- frame/staking/src/pallet/impls.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 753a2989298d3..ee5b5ece48d4e 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1337,7 +1337,7 @@ impl SortedListProvider for UseValidatorsMap { fn contains(id: &T::AccountId) -> bool { Validators::::contains_key(id) } - fn on_insert(_: T::AccountId, _weight: VoteWeight) -> Result<(), Self::Error> { + fn on_insert(_: T::AccountId, _weight: Self::Score) -> Result<(), Self::Error> { // nothing to do on insert. Ok(()) } @@ -1354,7 +1354,7 @@ impl SortedListProvider for UseValidatorsMap { } fn unsafe_regenerate( _: impl IntoIterator, - _: Box VoteWeight>, + _: Box Self::Score>, ) -> u32 { // nothing to do upon regenerate. 0 From 7dfbc9b790979726049135c3b7925925b67e0fd0 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Fri, 19 Aug 2022 16:05:29 +0200 Subject: [PATCH 12/40] Add a migration for VoterBagsList --- frame/staking/src/lib.rs | 3 ++- frame/staking/src/migrations.rs | 36 +++++++++++++++++++++++++++++++ frame/staking/src/pallet/impls.rs | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index ab0ab685e6911..8c83e853bf24b 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -878,11 +878,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 } } diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 7e3bf6ccb93e1..1fea1b8b96ce3 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -20,6 +20,42 @@ use super::*; use frame_election_provider_support::SortedListProvider; use frame_support::traits::OnRuntimeUpgrade; +pub mod v11 { + use super::*; + use frame_support::{ + storage::migration::move_pallet, + traits::{GetStorageVersion, PalletInfoAccess}, + }; + + /// Migrate the entire storage of this pallet to a new prefix. + /// + /// This new prefix must be the same as the one set in construct_runtime. For safety, use + /// `PalletInfo` to get it, as: + /// `::PalletInfo::name::`. + /// + /// The migration will look into the storage version in order to avoide triggering a migration + /// on an up to date storage. + pub fn migrate>( + old_pallet_name: N, + ) -> Weight { + let old_pallet_name = old_pallet_name.as_ref(); + let new_pallet_name =

::name(); + + if new_pallet_name == old_pallet_name { + log!(warn, "new bags-list name is equal to the old one, no need to migrate"); + return 0 + } + + if StorageVersion::::get() == Releases::V10_0_0 { + move_pallet(old_pallet_name.as_bytes(), new_pallet_name.as_bytes()); + ::BlockWeights::get().max_block + } else { + log!(warn, "v11::migrate should be removed."); + T::DbWeight::get().reads(1) + } + } +} + pub mod v10 { use super::*; use frame_support::storage_alias; diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index ee5b5ece48d4e..aaafa8d4c3bb0 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1342,7 +1342,7 @@ impl SortedListProvider for UseValidatorsMap { Ok(()) } fn get_score(id: &T::AccountId) -> Result { - Ok(Pallet::::weight_of(id)) + Ok(Pallet::::weight_of(id).into()) } fn on_update(_: &T::AccountId, _weight: Self::Score) -> Result<(), Self::Error> { // nothing to do on update. From 5f4d9a4e4b3fd1ef5ce048b6ebffddeae7e65898 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Fri, 19 Aug 2022 16:17:43 +0200 Subject: [PATCH 13/40] fix score --- frame/staking/src/pallet/impls.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index aaafa8d4c3bb0..99438f533635a 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -18,8 +18,8 @@ //! Implementations for the Staking FRAME Pallet. use frame_election_provider_support::{ - data_provider, ElectionDataProvider, ElectionProvider, ExtendedBalance, ScoreProvider, - SortedListProvider, Supports, VoteWeight, VoterOf, + data_provider, ElectionDataProvider, ElectionProvider, ScoreProvider, SortedListProvider, + Supports, VoteWeight, VoterOf, }; use frame_support::{ pallet_prelude::*, @@ -1314,7 +1314,7 @@ impl ScoreProvider for Pallet { /// a look at [`pallet-bags-list]. pub struct UseValidatorsMap(sp_std::marker::PhantomData); impl SortedListProvider for UseValidatorsMap { - type Score = ExtendedBalance; + type Score = BalanceOf; type Error = (); /// Returns iterator over voter list, which can have `take` called on it. From 5b2e1f508a17e9f7836358f9b6e6f03274791089 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Fri, 19 Aug 2022 16:25:57 +0200 Subject: [PATCH 14/40] add targetList to nomination-pools tests --- frame/nomination-pools/test-staking/src/mock.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/nomination-pools/test-staking/src/mock.rs b/frame/nomination-pools/test-staking/src/mock.rs index 055ba7b4b3c06..d2dbbca676d0e 100644 --- a/frame/nomination-pools/test-staking/src/mock.rs +++ b/frame/nomination-pools/test-staking/src/mock.rs @@ -127,6 +127,7 @@ impl pallet_staking::Config for Runtime { frame_election_provider_support::NoElection<(AccountId, BlockNumber, Staking)>; type GenesisElectionProvider = Self::ElectionProvider; type VoterList = pallet_bags_list::Pallet; + type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = Pools; type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; From 805349d9a567bc94c1f9cb86960a8ac9266accb8 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Fri, 19 Aug 2022 16:38:22 +0200 Subject: [PATCH 15/40] fix bench --- bin/node/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index af343c3ffcf49..dd99ecbc911fc 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1712,7 +1712,7 @@ mod benches { [frame_benchmarking, BaselineBench::] [pallet_assets, Assets] [pallet_babe, Babe] - [pallet_bags_list, BagsList] + [pallet_bags_list, VoterBagsList] [pallet_balances, Balances] [pallet_bounties, Bounties] [pallet_child_bounties, ChildBounties] From 4329ad2048b73f5253c01f78416da8fd42509602 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Sat, 20 Aug 2022 11:35:58 +0200 Subject: [PATCH 16/40] address review comments --- bin/node/runtime/src/lib.rs | 2 - frame/staking/src/migrations.rs | 1 + frame/staking/src/mock.rs | 68 +-------------------------------- 3 files changed, 2 insertions(+), 69 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index dd99ecbc911fc..1a3c835514fdd 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -720,8 +720,6 @@ impl pallet_election_provider_multi_phase::Config for Runtime { parameter_types! { pub const BagThresholds: &'static [u64] = &voter_bags::THRESHOLDS; - // This parameter is going to be needed for the operational TargetList implementation - pub const BagThresholdsBalance: &'static [u128] = &voter_bags::THRESHOLDS_BALANCES; } type VoterBagsListInstance = pallet_bags_list::Instance1; diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 1fea1b8b96ce3..4a0e6addf1b37 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -48,6 +48,7 @@ pub mod v11 { if StorageVersion::::get() == Releases::V10_0_0 { move_pallet(old_pallet_name.as_bytes(), new_pallet_name.as_bytes()); + StorageVersion::::put(Releases::V11_0_0); ::BlockWeights::get().max_block } else { log!(warn, "v11::migrate should be removed."); diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 96fea5fdf5d84..41ffd265ed3b2 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -101,7 +101,6 @@ frame_support::construct_runtime!( Session: pallet_session::{Pallet, Call, Storage, Event, Config}, Historical: pallet_session::historical::{Pallet, Storage}, VoterBagsList: pallet_bags_list::::{Pallet, Call, Storage, Event}, - TargetBagsList: pallet_bags_list::::{Pallet, Call, Storage, Event}, } ); @@ -254,16 +253,6 @@ impl pallet_bags_list::Config for Test { type Score = VoteWeight; } -type TargetBagsListInstance = pallet_bags_list::Instance2; -impl pallet_bags_list::Config for Test { - type Event = Event; - type WeightInfo = (); - // Target bags-list are always kept up to date, and in fact Staking does not know them at all! - type ScoreProvider = pallet_bags_list::Pallet; - type BagThresholds = BagThresholdsBalance; - type Score = Balance; -} - pub struct OnChainSeqPhragmen; impl onchain::Config for OnChainSeqPhragmen { type System = Test; @@ -272,61 +261,6 @@ impl onchain::Config for OnChainSeqPhragmen { type WeightInfo = (); } -pub struct TargetBagsListCompat; -impl SortedListProvider for TargetBagsListCompat { - type Error = >::Error; - type Score = >::Score; - - fn iter() -> Box> { - let mut all = TargetBagsList::iter() - .map(|x| (x, TargetBagsList::get_score(&x).unwrap_or_default())) - .collect::>(); - all.sort_by(|a, b| match a.1.partial_cmp(&b.1).unwrap() { - std::cmp::Ordering::Equal => b.0.partial_cmp(&a.0).unwrap(), - // Question: why rerverse? - x @ _ => x.reverse(), - }); - Box::new(all.into_iter().map(|(x, _)| x)) - } - fn iter_from(start: &AccountId) -> Result>, Self::Error> { - TargetBagsList::iter_from(start) - } - fn count() -> u32 { - TargetBagsList::count() - } - fn contains(id: &AccountId) -> bool { - TargetBagsList::contains(id) - } - fn on_insert(id: AccountId, weight: Self::Score) -> Result<(), Self::Error> { - TargetBagsList::on_insert(id, weight) - } - fn on_update(id: &AccountId, weight: Self::Score) -> Result<(), Self::Error> { - TargetBagsList::on_update(id, weight) - } - fn get_score(id: &AccountId) -> Result { - TargetBagsList::get_score(id) - } - fn on_remove(id: &AccountId) -> Result<(), Self::Error> { - TargetBagsList::on_remove(id) - } - fn unsafe_regenerate( - all: impl IntoIterator, - weight_of: Box Self::Score>, - ) -> u32 { - TargetBagsList::unsafe_regenerate(all, weight_of) - } - fn unsafe_clear() { - TargetBagsList::unsafe_clear(); - } - fn sanity_check() -> Result<(), &'static str> { - TargetBagsList::sanity_check() - } - #[cfg(feature = "runtime-benchmarks")] - fn score_update_worst_case(_who: &AccountId, _is_increase: bool) -> Self::Score { - Balance::MAX - } -} - pub struct MockReward {} impl OnUnbalanced> for MockReward { fn on_unbalanced(_: PositiveImbalanceOf) { @@ -368,7 +302,7 @@ impl crate::pallet::pallet::Config for Test { type GenesisElectionProvider = Self::ElectionProvider; // NOTE: consider a macro and use `UseNominatorsAndValidatorsMap` as well. type VoterList = VoterBagsList; - type TargetList = TargetBagsListCompat; + type TargetList = UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = OnStakerSlashMock; type BenchmarkingConfig = TestBenchmarkingConfig; From ba11c8af61b2467e6db2d0e2b4de4b89654d3c7b Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 22 Aug 2022 14:33:41 +0200 Subject: [PATCH 17/40] change get_npos_targets --- frame/staking/src/benchmarking.rs | 2 +- frame/staking/src/pallet/impls.rs | 36 +++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 12de0ff9cc665..ba2d0e8733310 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -845,7 +845,7 @@ benchmarks! { v, n, T::MaxNominations::get() as usize, false, None )?; }: { - let targets = >::get_npos_targets(); + let targets = >::get_npos_targets(None); assert_eq!(targets.len() as u32, v); } diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 99438f533635a..e97ce51639c2f 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -755,18 +755,32 @@ impl Pallet { /// Get the targets for an upcoming npos election. /// /// This function is self-weighing as [`DispatchClass::Mandatory`]. - pub fn get_npos_targets() -> Vec { - let mut validator_count = 0u32; - let targets = Validators::::iter() - .map(|(v, _)| { - validator_count.saturating_inc(); - v - }) - .collect::>(); + pub fn get_npos_targets(maybe_max_len: Option) -> Vec { + let max_allowed_len = maybe_max_len.unwrap_or_else(|| T::TargetList::count() as usize); + let mut all_targets = Vec::::with_capacity(max_allowed_len); + let mut targets_seen = 0; + + let mut targets_iter = T::TargetList::iter(); + while all_targets.len() < max_allowed_len && + targets_seen < (NPOS_MAX_ITERATIONS_COEFFICIENT * max_allowed_len as u32) + { + let target = match targets_iter.next() { + Some(target) => { + targets_seen.saturating_inc(); + target + }, + None => break, + }; + + if Validators::::contains_key(&target) { + all_targets.push(target); + } + } - Self::register_weight(T::WeightInfo::get_npos_targets(validator_count)); + Self::register_weight(T::WeightInfo::get_npos_targets(all_targets.len() as u32)); + log!(info, "generated {} npos targets", all_targets.len()); - targets + all_targets } /// This function will add a nominator to the `Nominators` storage map, @@ -902,7 +916,7 @@ impl ElectionDataProvider for Pallet { return Err("Target snapshot too big") } - Ok(Self::get_npos_targets()) + Ok(Self::get_npos_targets(None)) } fn next_election_prediction(now: T::BlockNumber) -> T::BlockNumber { From 5286a4f05ad4e4ad8bb02c2b264cd67cec219b4c Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 22 Aug 2022 14:39:05 +0200 Subject: [PATCH 18/40] address more comments --- frame/staking/src/mock.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 41ffd265ed3b2..4443db03d96dd 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -898,15 +898,3 @@ pub(crate) fn staking_events_since_last_call() -> Vec> { pub(crate) fn balances(who: &AccountId) -> (Balance, Balance) { (Balances::free_balance(who), Balances::reserved_balance(who)) } - -pub(crate) fn validator_ids() -> Vec { - Validators::::iter().map(|(v, _)| v).collect::>() -} - -pub(crate) fn nominator_ids() -> Vec { - Nominators::::iter().map(|(n, _)| n).collect::>() -} - -pub(crate) fn nominator_targets(who: AccountId) -> Vec { - Nominators::::get(&who).map(|n| n.targets).unwrap().into_inner() -} From 101deeeb5e3e6396cf0461b21972005bdf0365fe Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 22 Aug 2022 14:45:50 +0200 Subject: [PATCH 19/40] remove thresholds for the time being --- bin/node/runtime/src/voter_bags.rs | 204 ----------------------------- 1 file changed, 204 deletions(-) diff --git a/bin/node/runtime/src/voter_bags.rs b/bin/node/runtime/src/voter_bags.rs index 1633ef7b9df3b..eb540c27abcc7 100644 --- a/bin/node/runtime/src/voter_bags.rs +++ b/bin/node/runtime/src/voter_bags.rs @@ -236,207 +236,3 @@ pub const THRESHOLDS: [u64; 200] = [ 17_356_326_621_502_140_416, 18_446_744_073_709_551_615, ]; - -/// Upper thresholds delimiting the bag list. -pub const THRESHOLDS_BALANCES: [u128; 200] = [ - 100_000_000_000_000, - 106_282_535_907_434, - 112_959_774_389_150, - 120_056_512_776_105, - 127_599_106_300_477, - 135_615_565_971_369, - 144_135_662_599_590, - 153_191_037_357_827, - 162_815_319_286_803, - 173_044_250_183_800, - 183_915_817_337_347, - 195_470_394_601_017, - 207_750_892_330_229, - 220_802_916_738_890, - 234_674_939_267_673, - 249_418_476_592_914, - 265_088_281_944_639, - 281_742_548_444_211, - 299_443_125_216_738, - 318_255_747_080_822, - 338_250_278_668_647, - 359_500_973_883_001, - 382_086_751_654_776, - 406_091_489_025_036, - 431_604_332_640_068, - 458_720_029_816_222, - 487_539_280_404_019, - 518_169_110_758_247, - 550_723_271_202_866, - 585_322_658_466_782, - 622_095_764_659_305, - 661_179_154_452_653, - 702_717_972_243_610, - 746_866_481_177_808, - 793_788_636_038_393, - 843_658_692_126_636, - 896_661_852_395_681, - 952_994_955_240_703, - 1_012_867_205_499_736, - 1_076_500_951_379_881, - 1_144_132_510_194_192, - 1_216_013_045_975_769, - 1_292_409_502_228_280, - 1_373_605_593_276_862, - 1_459_902_857_901_004, - 1_551_621_779_162_291, - 1_649_102_974_585_730, - 1_752_708_461_114_642, - 1_862_822_999_536_805, - 1_979_855_523_374_646, - 2_104_240_657_545_975, - 2_236_440_332_435_128, - 2_376_945_499_368_703, - 2_526_277_953_866_680, - 2_684_992_273_439_945, - 2_853_677_877_130_641, - 3_032_961_214_443_876, - 3_223_508_091_799_862, - 3_426_026_145_146_232, - 3_641_267_467_913_124, - 3_870_031_404_070_482, - 4_113_167_516_660_186, - 4_371_578_742_827_277, - 4_646_224_747_067_156, - 4_938_125_485_141_739, - 5_248_364_991_899_922, - 5_578_095_407_069_235, - 5_928_541_253_969_291, - 6_301_003_987_036_955, - 6_696_866_825_051_405, - 7_117_599_888_008_300, - 7_564_765_656_719_910, - 8_040_024_775_416_580, - 8_545_142_218_898_723, - 9_081_993_847_142_344, - 9_652_573_371_700_016, - 10_258_999_759_768_490, - 10_903_525_103_419_522, - 11_588_542_983_217_942, - 12_316_597_357_287_042, - 13_090_392_008_832_678, - 13_912_800_587_211_472, - 14_786_877_279_832_732, - 15_715_868_154_526_436, - 16_703_223_214_499_558, - 17_752_609_210_649_358, - 18_867_923_258_814_856, - 20_053_307_312_537_008, - 21_313_163_545_075_252, - 22_652_170_697_804_756, - 24_075_301_455_707_600, - 25_587_840_914_485_432, - 27_195_406_207_875_088, - 28_903_967_368_057_400, - 30_719_869_496_628_636, - 32_649_856_328_471_220, - 34_701_095_276_033_064, - 36_881_204_047_022_752, - 39_198_278_934_370_992, - 41_660_924_883_519_016, - 44_278_287_448_695_240, - 47_060_086_756_856_400, - 50_016_653_605_425_536, - 53_158_967_827_883_320, - 56_498_699_069_691_424, - 60_048_250_125_977_912, - 63_820_803_001_928_304, - 67_830_367_866_937_216, - 72_091_835_084_322_176, - 76_621_030_509_822_880, - 81_434_774_264_248_528, - 86_550_943_198_537_824, - 91_988_537_283_208_848, - 97_767_750_168_749_840, - 103_910_044_178_992_000, - 110_438_230_015_967_792, - 117_376_551_472_255_616, - 124_750_775_465_407_920, - 132_588_287_728_824_640, - 140_918_194_514_440_064, - 149_771_430_684_917_568, - 159_180_874_596_775_264, - 169_181_470_201_085_280, - 179_810_356_815_193_344, - 191_107_007_047_393_216, - 203_113_373_386_768_288, - 215_874_044_002_592_672, - 229_436_408_331_885_600, - 243_850_833_070_063_392, - 259_170_849_218_267_264, - 275_453_350_882_006_752, - 292_758_806_559_399_232, - 311_151_483_703_668_992, - 330_699_687_393_865_920, - 351_476_014_000_157_824, - 373_557_620_785_735_808, - 397_026_512_446_556_096, - 421_969_845_653_044_224, - 448_480_252_724_740_928, - 476_656_185_639_923_904, - 506_602_281_657_757_760, - 538_429_751_910_786_752, - 572_256_794_410_890_176, - 608_209_033_002_485_632, - 646_419_983_893_124_352, - 687_031_551_494_039_552, - 730_194_555_412_054_016, - 776_069_290_549_944_960, - 824_826_122_395_314_176, - 876_646_119_708_695_936, - 931_721_726_960_522_368, - 990_257_479_014_182_144, - 1_052_470_760_709_299_712, - 1_118_592_614_166_106_112, - 1_188_868_596_808_997_376, - 1_263_559_693_295_730_432, - 1_342_943_284_738_898_688, - 1_427_314_178_819_094_784, - 1_516_985_704_615_302_400, - 1_612_290_876_218_400_768, - 1_713_583_629_449_105_408, - 1_821_240_136_273_157_632, - 1_935_660_201_795_120_128, - 2_057_268_749_018_809_600, - 2_186_517_396_888_336_384, - 2_323_886_137_470_138_880, - 2_469_885_118_504_583_168, - 2_625_056_537_947_004_416, - 2_789_976_657_533_970_944, - 2_965_257_942_852_572_160, - 3_151_551_337_860_326_400, - 3_349_548_682_302_620_672, - 3_559_985_281_005_267_968, - 3_783_642_634_583_792_128, - 4_021_351_341_710_503_936, - 4_273_994_183_717_548_544, - 4_542_509_402_991_247_872, - 4_827_894_187_332_742_144, - 5_131_208_373_224_844_288, - 5_453_578_381_757_959_168, - 5_796_201_401_831_965_696, - 6_160_349_836_169_256_960, - 6_547_376_026_650_146_816, - 6_958_717_276_519_173_120, - 7_395_901_188_113_309_696, - 7_860_551_335_934_872_576, - 8_354_393_296_137_270_272, - 8_879_261_054_815_360_000, - 9_437_103_818_898_946_048, - 10_029_993_254_943_105_024, - 10_660_131_182_698_121_216, - 11_329_857_752_030_707_712, - 12_041_660_133_563_240_448, - 12_798_181_755_305_525_248, - 13_602_232_119_581_272_064, - 14_456_797_236_706_498_560, - 15_365_050_714_167_523_328, - 16_330_365_542_480_556_032, - 17_356_326_621_502_140_416, - 18_446_744_073_709_551_615, -]; From b5109d113e4edadb21f0e52a1b23b797c462b5a5 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 22 Aug 2022 15:07:48 +0200 Subject: [PATCH 20/40] fix instance reference --- frame/nomination-pools/benchmarking/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/nomination-pools/benchmarking/src/lib.rs b/frame/nomination-pools/benchmarking/src/lib.rs index 4c2c902846a85..83ef289f168bd 100644 --- a/frame/nomination-pools/benchmarking/src/lib.rs +++ b/frame/nomination-pools/benchmarking/src/lib.rs @@ -24,7 +24,7 @@ mod mock; use frame_benchmarking::{account, frame_support::traits::Currency, vec, whitelist_account, Vec}; use frame_election_provider_support::SortedListProvider; -use frame_support::{assert_ok, ensure, traits::Get}; +use frame_support::{assert_ok, ensure, instances::Instance1, traits::Get}; use frame_system::RawOrigin as Origin; use pallet_nomination_pools::{ BalanceOf, BondExtra, BondedPoolInner, BondedPools, ConfigOp, MaxPoolMembers, @@ -42,7 +42,7 @@ const USER_SEED: u32 = 0; const MAX_SPANS: u32 = 100; 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 { } From 6f4bf4bae23d04891e5e40f2209b209e1538207b Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 22 Aug 2022 15:24:14 +0200 Subject: [PATCH 21/40] VoterBagsListInstance --- frame/nomination-pools/benchmarking/src/lib.rs | 5 ++++- frame/nomination-pools/benchmarking/src/mock.rs | 3 ++- frame/nomination-pools/test-staking/src/mock.rs | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/frame/nomination-pools/benchmarking/src/lib.rs b/frame/nomination-pools/benchmarking/src/lib.rs index 83ef289f168bd..2dea86ad5aa4a 100644 --- a/frame/nomination-pools/benchmarking/src/lib.rs +++ b/frame/nomination-pools/benchmarking/src/lib.rs @@ -41,8 +41,11 @@ type CurrencyOf = ::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 { } diff --git a/frame/nomination-pools/benchmarking/src/mock.rs b/frame/nomination-pools/benchmarking/src/mock.rs index b4f707b8152af..0e594137407fe 100644 --- a/frame/nomination-pools/benchmarking/src/mock.rs +++ b/frame/nomination-pools/benchmarking/src/mock.rs @@ -123,7 +123,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 for Runtime { type Event = Event; type WeightInfo = (); type BagThresholds = BagThresholds; diff --git a/frame/nomination-pools/test-staking/src/mock.rs b/frame/nomination-pools/test-staking/src/mock.rs index d2dbbca676d0e..5288db52d4aaa 100644 --- a/frame/nomination-pools/test-staking/src/mock.rs +++ b/frame/nomination-pools/test-staking/src/mock.rs @@ -18,6 +18,7 @@ use frame_election_provider_support::VoteWeight; use frame_support::{ assert_ok, + instances::Instance1, pallet_prelude::*, parameter_types, traits::{ConstU64, ConstU8}, @@ -138,7 +139,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 for Runtime { type Event = Event; type WeightInfo = (); type BagThresholds = BagThresholds; From 0ef8d2c324eb5647dafcd00298f4ab062573e603 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 22 Aug 2022 15:25:35 +0200 Subject: [PATCH 22/40] reus --- frame/nomination-pools/benchmarking/src/mock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/nomination-pools/benchmarking/src/mock.rs b/frame/nomination-pools/benchmarking/src/mock.rs index 0e594137407fe..13a407d990139 100644 --- a/frame/nomination-pools/benchmarking/src/mock.rs +++ b/frame/nomination-pools/benchmarking/src/mock.rs @@ -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::{ @@ -123,7 +124,6 @@ parameter_types! { pub static BagThresholds: &'static [VoteWeight] = &[10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000]; } -type VoterBagsListInstance = pallet_bags_list::Instance1; impl pallet_bags_list::Config for Runtime { type Event = Event; type WeightInfo = (); From c03ec08c5cc49ec5823ceae7b19498549da0e53f Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 23 Aug 2022 15:22:58 +0200 Subject: [PATCH 23/40] remove params that are not used yet --- frame/staking/src/mock.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 4443db03d96dd..0132980066d7b 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -233,11 +233,9 @@ impl OnUnbalanced> for RewardRemainderMock { const THRESHOLDS: [sp_npos_elections::VoteWeight; 9] = [10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000]; -const THRESHOLDS_BALANCE: [Balance; 9] = [10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000]; parameter_types! { pub static BagThresholds: &'static [sp_npos_elections::VoteWeight] = &THRESHOLDS; - pub static BagThresholdsBalance: &'static [sp_npos_elections::ExtendedBalance] = &THRESHOLDS_BALANCE; pub static MaxNominations: u32 = 16; pub static RewardOnUnbalanceWasCalled: bool = false; pub static LedgerSlashPerEra: (BalanceOf, BTreeMap>) = (Zero::zero(), BTreeMap::new()); From 1da8bfc721b38a9502ff33829b1cb07d6de82412 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Wed, 24 Aug 2022 17:50:18 +0200 Subject: [PATCH 24/40] Introduced pre/post upgrade try-runtime checks --- frame/staking/src/migrations.rs | 49 ++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 4a0e6addf1b37..5c08cf64377cc 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -26,6 +26,27 @@ pub mod v11 { storage::migration::move_pallet, traits::{GetStorageVersion, PalletInfoAccess}, }; + use sp_core::twox_128; + + #[cfg(feature = "try-runtime")] + fn pre_upgrade>( + old_pallet_name: N, + ) -> Result<(), &'static str> { + use frame_support::traits::OnRuntimeUpgradeHelpersExt; + frame_support::ensure!( + StorageVersion::::get() == crate::Releases::V10_0_0, + "must upgrade linearly" + ); + + let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); + + frame_support::ensure!( + sp_io::storage::next_key(&old_pallet_prefix).is_some(); + "no data for the old pallet name has been detected" + ); + + Ok(()) + } /// Migrate the entire storage of this pallet to a new prefix. /// @@ -33,7 +54,7 @@ pub mod v11 { /// `PalletInfo` to get it, as: /// `::PalletInfo::name::`. /// - /// The migration will look into the storage version in order to avoide triggering a migration + /// The migration will look into the storage version in order to avoid triggering a migration /// on an up to date storage. pub fn migrate>( old_pallet_name: N, @@ -55,6 +76,32 @@ pub mod v11 { T::DbWeight::get().reads(1) } } + + #[cfg(feature = "try-runtime")] + fn post_upgrade>( + old_pallet_name: N, + ) -> Result<(), &'static str> { + use frame_support::traits::OnRuntimeUpgradeHelpersExt; + frame_support::ensure!( + StorageVersion::::get() == crate::Releases::V11_0_0, + "wrong version after the upgrade" + ); + + let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); + frame_support::ensure!( + sp_io::storage::next_key(&old_pallet_prefix).is_none(); + "old pallet data hasn't been removed" + ); + + let new_pallet_name =

::name(); + let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); + frame_support::ensure!( + sp_io::storage::next_key(&new_pallet_prefix).is_some(); + "new pallet data hasn't been created" + ); + + Ok(()) + } } pub mod v10 { From 0bde3dfa4b72fe2bf12a23ac6a3d882ca139754c Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Thu, 25 Aug 2022 14:13:23 +0200 Subject: [PATCH 25/40] fix --- frame/staking/src/migrations.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 5c08cf64377cc..e15877bfa2791 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -26,7 +26,9 @@ pub mod v11 { storage::migration::move_pallet, traits::{GetStorageVersion, PalletInfoAccess}, }; - use sp_core::twox_128; + + #[cfg(feature = "try-runtime")] + use sp_io::hashing::twox_128; #[cfg(feature = "try-runtime")] fn pre_upgrade>( From 9cb9e01ab09256eba8015176ac44d84426f7b10a Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Thu, 25 Aug 2022 14:50:43 +0200 Subject: [PATCH 26/40] fixes --- frame/staking/src/migrations.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index e15877bfa2791..d259947bf360c 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -31,19 +31,17 @@ pub mod v11 { use sp_io::hashing::twox_128; #[cfg(feature = "try-runtime")] - fn pre_upgrade>( + pub fn pre_upgrade>( old_pallet_name: N, ) -> Result<(), &'static str> { - use frame_support::traits::OnRuntimeUpgradeHelpersExt; frame_support::ensure!( StorageVersion::::get() == crate::Releases::V10_0_0, "must upgrade linearly" ); - - let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); + let old_pallet_prefix = twox_128(old_pallet_name.as_ref().as_bytes()); frame_support::ensure!( - sp_io::storage::next_key(&old_pallet_prefix).is_some(); + sp_io::storage::next_key(&old_pallet_prefix).is_some(), "no data for the old pallet name has been detected" ); @@ -80,25 +78,24 @@ pub mod v11 { } #[cfg(feature = "try-runtime")] - fn post_upgrade>( + pub fn post_upgrade>( old_pallet_name: N, ) -> Result<(), &'static str> { - use frame_support::traits::OnRuntimeUpgradeHelpersExt; frame_support::ensure!( StorageVersion::::get() == crate::Releases::V11_0_0, "wrong version after the upgrade" ); - let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); + let old_pallet_prefix = twox_128(old_pallet_name.as_ref().as_bytes()); frame_support::ensure!( - sp_io::storage::next_key(&old_pallet_prefix).is_none(); + sp_io::storage::next_key(&old_pallet_prefix).is_none(), "old pallet data hasn't been removed" ); let new_pallet_name =

::name(); let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); frame_support::ensure!( - sp_io::storage::next_key(&new_pallet_prefix).is_some(); + sp_io::storage::next_key(&new_pallet_prefix).is_some(), "new pallet data hasn't been created" ); From 4c2598843f3e78fe13a4d5cf592edc0a18167fdb Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Sat, 27 Aug 2022 09:48:31 +0200 Subject: [PATCH 27/40] fix migration --- frame/nomination-pools/benchmarking/src/lib.rs | 2 +- frame/staking/src/migrations.rs | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/frame/nomination-pools/benchmarking/src/lib.rs b/frame/nomination-pools/benchmarking/src/lib.rs index ca374aec6b73f..1fdc46aa42549 100644 --- a/frame/nomination-pools/benchmarking/src/lib.rs +++ b/frame/nomination-pools/benchmarking/src/lib.rs @@ -24,7 +24,7 @@ mod mock; use frame_benchmarking::{account, frame_support::traits::Currency, vec, whitelist_account, Vec}; use frame_election_provider_support::SortedListProvider; -use frame_support::{assert_ok, ensure, instances::Instance1, traits::Get}; +use frame_support::{assert_ok, ensure, traits::Get}; use frame_system::RawOrigin as Origin; use pallet_nomination_pools::{ BalanceOf, BondExtra, BondedPoolInner, BondedPools, ConfigOp, MaxPoolMembers, diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index d259947bf360c..a08b25195f871 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -62,14 +62,15 @@ pub mod v11 { let old_pallet_name = old_pallet_name.as_ref(); let new_pallet_name =

::name(); - if new_pallet_name == old_pallet_name { - log!(warn, "new bags-list name is equal to the old one, no need to migrate"); - return 0 - } - if StorageVersion::::get() == Releases::V10_0_0 { - move_pallet(old_pallet_name.as_bytes(), new_pallet_name.as_bytes()); + // bump version anyway, even if we don't need to move the prefix StorageVersion::::put(Releases::V11_0_0); + if new_pallet_name == old_pallet_name { + log!(warn, "new bags-list name is equal to the old one, only bumping the version"); + return T::DbWeight::get().reads(1).saturating_add(T::DbWeight::get().writes(1)) + } + + move_pallet(old_pallet_name.as_bytes(), new_pallet_name.as_bytes()); ::BlockWeights::get().max_block } else { log!(warn, "v11::migrate should be removed."); From 14ce27a5aaa536664beb0c71c752537b15df67e2 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Sat, 27 Aug 2022 12:41:14 +0200 Subject: [PATCH 28/40] fix migration --- frame/staking/src/migrations.rs | 131 +++++++++++++++++--------------- 1 file changed, 68 insertions(+), 63 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index a08b25195f871..f4654742c3c94 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -26,81 +26,86 @@ pub mod v11 { storage::migration::move_pallet, traits::{GetStorageVersion, PalletInfoAccess}, }; - #[cfg(feature = "try-runtime")] use sp_io::hashing::twox_128; - #[cfg(feature = "try-runtime")] - pub fn pre_upgrade>( - old_pallet_name: N, - ) -> Result<(), &'static str> { - frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V10_0_0, - "must upgrade linearly" - ); - let old_pallet_prefix = twox_128(old_pallet_name.as_ref().as_bytes()); + pub struct MigrateToV11( + sp_std::marker::PhantomData, + sp_std::marker::PhantomData

, + sp_std::marker::PhantomData, + ); + impl> OnRuntimeUpgrade + for MigrateToV11 + { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<(), &'static str> { + frame_support::ensure!( + StorageVersion::::get() == crate::Releases::V10_0_0, + "must upgrade linearly" + ); + let old_pallet_prefix = twox_128(N::get().as_bytes()); - frame_support::ensure!( - sp_io::storage::next_key(&old_pallet_prefix).is_some(), - "no data for the old pallet name has been detected" - ); + frame_support::ensure!( + sp_io::storage::next_key(&old_pallet_prefix).is_some(), + "no data for the old pallet name has been detected" + ); - Ok(()) - } + Ok(()) + } - /// Migrate the entire storage of this pallet to a new prefix. - /// - /// This new prefix must be the same as the one set in construct_runtime. For safety, use - /// `PalletInfo` to get it, as: - /// `::PalletInfo::name::`. - /// - /// The migration will look into the storage version in order to avoid triggering a migration - /// on an up to date storage. - pub fn migrate>( - old_pallet_name: N, - ) -> Weight { - let old_pallet_name = old_pallet_name.as_ref(); - let new_pallet_name =

::name(); - - if StorageVersion::::get() == Releases::V10_0_0 { - // bump version anyway, even if we don't need to move the prefix - StorageVersion::::put(Releases::V11_0_0); - if new_pallet_name == old_pallet_name { - log!(warn, "new bags-list name is equal to the old one, only bumping the version"); - return T::DbWeight::get().reads(1).saturating_add(T::DbWeight::get().writes(1)) - } + /// Migrate the entire storage of this pallet to a new prefix. + /// + /// This new prefix must be the same as the one set in construct_runtime. For safety, use + /// `PalletInfo` to get it, as: + /// `::PalletInfo::name::`. + /// + /// The migration will look into the storage version in order to avoid triggering a + /// migration on an up to date storage. + fn on_runtime_upgrade() -> Weight { + let old_pallet_name = N::get(); + let new_pallet_name =

::name(); + + if StorageVersion::::get() == Releases::V10_0_0 { + // bump version anyway, even if we don't need to move the prefix + StorageVersion::::put(Releases::V11_0_0); + if new_pallet_name == old_pallet_name { + log!( + warn, + "new bags-list name is equal to the old one, only bumping the version" + ); + return T::DbWeight::get().reads(1).saturating_add(T::DbWeight::get().writes(1)) + } - move_pallet(old_pallet_name.as_bytes(), new_pallet_name.as_bytes()); - ::BlockWeights::get().max_block - } else { - log!(warn, "v11::migrate should be removed."); - T::DbWeight::get().reads(1) + move_pallet(old_pallet_name.as_bytes(), new_pallet_name.as_bytes()); + ::BlockWeights::get().max_block + } else { + log!(warn, "v11::migrate should be removed."); + T::DbWeight::get().reads(1) + } } - } - #[cfg(feature = "try-runtime")] - pub fn post_upgrade>( - old_pallet_name: N, - ) -> Result<(), &'static str> { - frame_support::ensure!( - StorageVersion::::get() == crate::Releases::V11_0_0, - "wrong version after the upgrade" - ); + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + frame_support::ensure!( + StorageVersion::::get() == crate::Releases::V11_0_0, + "wrong version after the upgrade" + ); - let old_pallet_prefix = twox_128(old_pallet_name.as_ref().as_bytes()); - frame_support::ensure!( - sp_io::storage::next_key(&old_pallet_prefix).is_none(), - "old pallet data hasn't been removed" - ); + let old_pallet_prefix = twox_128(N::get().as_bytes()); + frame_support::ensure!( + sp_io::storage::next_key(&old_pallet_prefix).is_none(), + "old pallet data hasn't been removed" + ); - let new_pallet_name =

::name(); - let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); - frame_support::ensure!( - sp_io::storage::next_key(&new_pallet_prefix).is_some(), - "new pallet data hasn't been created" - ); + let new_pallet_name =

::name(); + let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); + frame_support::ensure!( + sp_io::storage::next_key(&new_pallet_prefix).is_some(), + "new pallet data hasn't been created" + ); - Ok(()) + Ok(()) + } } } From 67316fb696e16ef44f9921ce18e348de301ae619 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 29 Aug 2022 14:26:14 +0200 Subject: [PATCH 29/40] fix post_upgrade --- frame/staking/src/migrations.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index f4654742c3c94..7130e28821708 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -91,6 +91,14 @@ pub mod v11 { "wrong version after the upgrade" ); + let old_pallet_name = N::get(); + let new_pallet_name =

::name(); + + // skip storage prefix checks for the same pallet names + if new_pallet_name == old_pallet_name { + return Ok(()) + } + let old_pallet_prefix = twox_128(N::get().as_bytes()); frame_support::ensure!( sp_io::storage::next_key(&old_pallet_prefix).is_none(), From 87b5e0f7fac6eb711179f7b473a727dea5cddece Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 29 Aug 2022 14:34:33 +0200 Subject: [PATCH 30/40] change --- frame/staking/src/pallet/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index bbca88ea1f3c5..5d5be3526c57f 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -194,6 +194,7 @@ pub mod pallet { /// WIP: This is a noop as of now, the actual business logic that's described below is going /// to be introduced in a follow-up PR. + /// /// Something that provides a best-effort sorted list of targets aka electable validators, /// used for NPoS election. /// From 8f54be58e437cceb275c2ea2c75c146846fb5e35 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 13 Sep 2022 14:19:36 +0000 Subject: [PATCH 31/40] Fix --- frame/staking/src/pallet/impls.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index a14efb473ceda..e8006c297de2d 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1370,10 +1370,9 @@ impl SortedListProvider for UseValidatorsMap { // nothing to do upon regenerate. 0 } - fn sanity_check() -> Result<(), &'static str> { + fn try_state() -> Result<(), &'static str> { Ok(()) } - fn unsafe_clear() { #[allow(deprecated)] Validators::::remove_all(); From 39b1cfac901fe79ec9c03cc36b31dd9c374db7b4 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Wed, 14 Sep 2022 10:20:07 +0200 Subject: [PATCH 32/40] eloquent PhantomData --- frame/staking/src/migrations.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 15b46f90a605a..8083f552257e0 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -29,11 +29,7 @@ pub mod v11 { #[cfg(feature = "try-runtime")] use sp_io::hashing::twox_128; - pub struct MigrateToV11( - sp_std::marker::PhantomData, - sp_std::marker::PhantomData

, - sp_std::marker::PhantomData, - ); + pub struct MigrateToV11(sp_std::marker::PhantomData); impl> OnRuntimeUpgrade for MigrateToV11 { From 94efa6e6192e07fea9c7417f0532a6f166c06bd6 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Wed, 14 Sep 2022 10:40:57 +0200 Subject: [PATCH 33/40] fix PD --- frame/staking/src/migrations.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/src/migrations.rs b/frame/staking/src/migrations.rs index 8083f552257e0..2556f6d989e1b 100644 --- a/frame/staking/src/migrations.rs +++ b/frame/staking/src/migrations.rs @@ -29,7 +29,7 @@ pub mod v11 { #[cfg(feature = "try-runtime")] use sp_io::hashing::twox_128; - pub struct MigrateToV11(sp_std::marker::PhantomData); + pub struct MigrateToV11(sp_std::marker::PhantomData<(T, P, N)>); impl> OnRuntimeUpgrade for MigrateToV11 { From c1547a2d14135a5ee47e80db4b29e1c4adbcdc33 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Wed, 14 Sep 2022 10:52:56 +0200 Subject: [PATCH 34/40] more fixes --- frame/nomination-pools/test-staking/src/mock.rs | 1 - frame/staking/src/pallet/impls.rs | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/nomination-pools/test-staking/src/mock.rs b/frame/nomination-pools/test-staking/src/mock.rs index d91d2fa762cc9..0e8d119482e76 100644 --- a/frame/nomination-pools/test-staking/src/mock.rs +++ b/frame/nomination-pools/test-staking/src/mock.rs @@ -18,7 +18,6 @@ use frame_election_provider_support::VoteWeight; use frame_support::{ assert_ok, - instances::Instance1, pallet_prelude::*, parameter_types, traits::{ConstU64, ConstU8}, diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index e8006c297de2d..74108bc560e98 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1526,6 +1526,7 @@ impl StakingInterface for Pallet { #[cfg(feature = "try-runtime")] impl Pallet { pub(crate) fn do_try_state(_: BlockNumberFor) -> Result<(), &'static str> { + T::VoterList::iter().collect().all(|x| x.is_nominator()); T::VoterList::try_state()?; Self::check_nominators()?; Self::check_exposures()?; From 194e0e25063a25703a210fcdab9c7cc020fad54a Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Fri, 16 Sep 2022 10:52:53 +0200 Subject: [PATCH 35/40] Update frame/staking/src/pallet/impls.rs Co-authored-by: Squirrel --- frame/staking/src/pallet/impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 74108bc560e98..47037dec1dcf6 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1322,7 +1322,7 @@ impl ScoreProvider for Pallet { /// A simple sorted list implementation that does not require any additional pallets. Note, this /// does not provide validators in sorted order. If you desire nominators in a sorted order take -/// a look at [`pallet-bags-list]. +/// a look at [`pallet-bags-list`]. pub struct UseValidatorsMap(sp_std::marker::PhantomData); impl SortedListProvider for UseValidatorsMap { type Score = BalanceOf; From ed98cca6ba52a97745537d5e4ac5ef1acecf557c Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Fri, 16 Sep 2022 15:48:14 +0200 Subject: [PATCH 36/40] is_nominator now works --- frame/staking/src/pallet/impls.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 47037dec1dcf6..7901ec1128add 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1526,7 +1526,10 @@ impl StakingInterface for Pallet { #[cfg(feature = "try-runtime")] impl Pallet { pub(crate) fn do_try_state(_: BlockNumberFor) -> Result<(), &'static str> { - T::VoterList::iter().collect().all(|x| x.is_nominator()); + ensure!( + T::VoterList::iter().all(|x| Self::is_nominator(&x)), + "VoterList contains non-nominators" + ); T::VoterList::try_state()?; Self::check_nominators()?; Self::check_exposures()?; @@ -1534,6 +1537,10 @@ impl Pallet { Self::check_count() } + fn is_nominator(who: &T::AccountId) -> bool { + >::contains_key(who) + } + fn check_count() -> Result<(), &'static str> { ensure!( ::VoterList::count() == From ba0c063e590cba7df91160313ff0d09fe8cd04b1 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Fri, 16 Sep 2022 16:04:00 +0200 Subject: [PATCH 37/40] fix test-staking --- frame/nomination-pools/test-staking/src/mock.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frame/nomination-pools/test-staking/src/mock.rs b/frame/nomination-pools/test-staking/src/mock.rs index 0e8d119482e76..bcbf5bc3ec3fc 100644 --- a/frame/nomination-pools/test-staking/src/mock.rs +++ b/frame/nomination-pools/test-staking/src/mock.rs @@ -138,8 +138,7 @@ parameter_types! { pub static BagThresholds: &'static [VoteWeight] = &[10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000]; } -type VoterBagsListInstance = pallet_bags_list::Instance1; -impl pallet_bags_list::Config for Runtime { +impl pallet_bags_list::Config for Runtime { type RuntimeEvent = RuntimeEvent; type WeightInfo = (); type BagThresholds = BagThresholds; From e6a3871d7ff2dbf520ed6b1514689266953f76e1 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Fri, 16 Sep 2022 16:58:01 +0200 Subject: [PATCH 38/40] build fixes --- frame/nomination-pools/benchmarking/src/mock.rs | 4 ++-- frame/nomination-pools/test-staking/src/mock.rs | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/frame/nomination-pools/benchmarking/src/mock.rs b/frame/nomination-pools/benchmarking/src/mock.rs index 101b2ba9a3549..2dcac34d39a11 100644 --- a/frame/nomination-pools/benchmarking/src/mock.rs +++ b/frame/nomination-pools/benchmarking/src/mock.rs @@ -112,7 +112,7 @@ 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; + type VoterList = VoterList; type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = Pools; @@ -182,7 +182,7 @@ frame_support::construct_runtime!( Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, Staking: pallet_staking::{Pallet, Call, Config, Storage, Event}, - BagsList: pallet_bags_list::{Pallet, Call, Storage, Event}, + VoterList: pallet_bags_list::::{Pallet, Call, Storage, Event}, Pools: pallet_nomination_pools::{Pallet, Call, Storage, Event}, } ); diff --git a/frame/nomination-pools/test-staking/src/mock.rs b/frame/nomination-pools/test-staking/src/mock.rs index bcbf5bc3ec3fc..cb63ad04d1a38 100644 --- a/frame/nomination-pools/test-staking/src/mock.rs +++ b/frame/nomination-pools/test-staking/src/mock.rs @@ -126,7 +126,7 @@ 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; + type VoterList = VoterList; type TargetList = pallet_staking::UseValidatorsMap; type MaxUnlockingChunks = ConstU32<32>; type OnStakerSlash = Pools; @@ -138,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 for Runtime { type RuntimeEvent = RuntimeEvent; type WeightInfo = (); type BagThresholds = BagThresholds; @@ -194,7 +195,7 @@ frame_support::construct_runtime!( Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, Staking: pallet_staking::{Pallet, Call, Config, Storage, Event}, - BagsList: pallet_bags_list::{Pallet, Call, Storage, Event}, + VoterList: pallet_bags_list::::{Pallet, Call, Storage, Event}, Pools: pallet_nomination_pools::{Pallet, Call, Storage, Event}, } ); From d019f1de2ed22f5df7abf93fe980f5ff0ad017e2 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Sat, 17 Sep 2022 13:53:50 +0200 Subject: [PATCH 39/40] fix remote-tests --- frame/bags-list/remote-tests/src/lib.rs | 33 +++++++++++++------ frame/bags-list/remote-tests/src/migration.rs | 5 ++- frame/bags-list/remote-tests/src/snapshot.rs | 8 +++-- frame/bags-list/remote-tests/src/try_state.rs | 10 ++++-- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/frame/bags-list/remote-tests/src/lib.rs b/frame/bags-list/remote-tests/src/lib.rs index 927c3dc91cb58..fc25e3b65ddb1 100644 --- a/frame/bags-list/remote-tests/src/lib.rs +++ b/frame/bags-list/remote-tests/src/lib.rs @@ -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. @@ -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: + pallet_staking::Config + pallet_bags_list::Config + frame_system::Config +{ +} +impl< + I: 'static, + T: pallet_staking::Config + pallet_bags_list::Config + frame_system::Config, + > RuntimeT for T { } -impl 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(currency_unit: u64, currency_name: &'static str) { +pub fn display_and_check_bags>( + currency_unit: u64, + currency_name: &'static str, +) { use frame_election_provider_support::SortedListProvider; use frame_support::traits::Get; @@ -55,7 +64,8 @@ pub fn display_and_check_bags(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 ::BagThresholds::get() { + for vote_weight_thresh in >::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") @@ -64,7 +74,9 @@ pub fn display_and_check_bags(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::::list_bags_get(*vote_weight_thresh) { + let bag = match pallet_bags_list::Pallet::::list_bags_get( + *vote_weight_thresh, + ) { Some(bag) => bag, None => { log::info!(target: LOG_TARGET, "{} NO VOTERS.", pretty_thresh); @@ -75,7 +87,8 @@ pub fn display_and_check_bags(currency_unit: u64, currency_na active_bags += 1; for id in bag.std_iter().map(|node| node.std_id().clone()) { - let vote_weight = ::ScoreProvider::score(&id); + let vote_weight = + >::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") @@ -92,8 +105,8 @@ pub fn display_and_check_bags(currency_unit: u64, currency_na ); } - let node = - pallet_bags_list::Node::::get(&id).expect("node in bag must exist."); + let node = pallet_bags_list::Node::::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::(vote_weight); @@ -141,7 +154,7 @@ pub fn display_and_check_bags(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, - ::BagThresholds::get().len(), + >::BagThresholds::get().len(), rebaggable, ); } diff --git a/frame/bags-list/remote-tests/src/migration.rs b/frame/bags-list/remote-tests/src/migration.rs index c4cd73c45d377..675dfbe072670 100644 --- a/frame/bags-list/remote-tests/src/migration.rs +++ b/frame/bags-list/remote-tests/src/migration.rs @@ -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( +pub async fn execute< + Runtime: RuntimeT, + Block: BlockT + DeserializeOwned, +>( currency_unit: u64, currency_name: &'static str, ws_url: String, diff --git a/frame/bags-list/remote-tests/src/snapshot.rs b/frame/bags-list/remote-tests/src/snapshot.rs index 408f5f2bd8aa2..655de10c4af9b 100644 --- a/frame/bags-list/remote-tests/src/snapshot.rs +++ b/frame/bags-list/remote-tests/src/snapshot.rs @@ -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( +pub async fn execute< + Runtime: crate::RuntimeT, + Block: BlockT + DeserializeOwned, +>( voter_limit: Option, currency_unit: u64, ws_url: String, @@ -34,7 +37,8 @@ pub async fn execute 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::::name().to_string()], + pallets: vec![pallet_bags_list::Pallet::::name() + .to_string()], at: None, ..Default::default() })) diff --git a/frame/bags-list/remote-tests/src/try_state.rs b/frame/bags-list/remote-tests/src/try_state.rs index 11278c20eb8ed..9817ef4ceb9e4 100644 --- a/frame/bags-list/remote-tests/src/try_state.rs +++ b/frame/bags-list/remote-tests/src/try_state.rs @@ -25,7 +25,10 @@ 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( +pub async fn execute< + Runtime: crate::RuntimeT, + Block: BlockT + DeserializeOwned, +>( currency_unit: u64, currency_name: &'static str, ws_url: String, @@ -33,7 +36,8 @@ pub async fn execute let mut ext = Builder::::new() .mode(Mode::Online(OnlineConfig { transport: ws_url.to_string().into(), - pallets: vec![pallet_bags_list::Pallet::::name().to_string()], + pallets: vec![pallet_bags_list::Pallet::::name() + .to_string()], ..Default::default() })) .inject_hashed_prefix(&>::prefix_hash()) @@ -44,7 +48,7 @@ pub async fn execute ext.execute_with(|| { sp_core::crypto::set_default_ss58_version(Runtime::SS58Prefix::get().try_into().unwrap()); - pallet_bags_list::Pallet::::try_state().unwrap(); + pallet_bags_list::Pallet::::try_state().unwrap(); log::info!(target: crate::LOG_TARGET, "executed bags-list sanity check with no errors."); crate::display_and_check_bags::(currency_unit, currency_name); From 93491e2e6983bb126f84d81dd71b0a032c98db28 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Sun, 18 Sep 2022 08:46:42 +0000 Subject: [PATCH 40/40] Apply suggestions from code review --- frame/staking/src/pallet/impls.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 7901ec1128add..b7c30cf16db94 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -1527,7 +1527,7 @@ impl StakingInterface for Pallet { impl Pallet { pub(crate) fn do_try_state(_: BlockNumberFor) -> Result<(), &'static str> { ensure!( - T::VoterList::iter().all(|x| Self::is_nominator(&x)), + T::VoterList::iter().all(|x| >::contains_key(&x)), "VoterList contains non-nominators" ); T::VoterList::try_state()?; @@ -1537,10 +1537,6 @@ impl Pallet { Self::check_count() } - fn is_nominator(who: &T::AccountId) -> bool { - >::contains_key(who) - } - fn check_count() -> Result<(), &'static str> { ensure!( ::VoterList::count() ==