Make bags-list generic over node value and instantiable (#10997)

* make instantiable

* update

* cargo fmt

* Clean up

* bags-list: Make it generic over node value

* Respond to some feedback

* Apply suggestions from code review

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Add back default impl for weight update worst case

* Update to Score in more places'

* Use VoteWeight, not u64 to reduce test diff

* FMT

* FullCodec implies Codec

* formatting

* Fixup bags list remote test

Co-authored-by: doordashcon <jesse.chejieh@gmail.com>
Co-authored-by: Doordashcon <90750465+Doordashcon@users.noreply.github.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Zeke Mostov
2022-03-09 16:28:28 +00:00
committed by GitHub
parent 64f6664691
commit f8e0e41e15
14 changed files with 428 additions and 283 deletions
+11 -8
View File
@@ -18,8 +18,8 @@
//! Implementations for the Staking FRAME Pallet.
use frame_election_provider_support::{
data_provider, ElectionDataProvider, ElectionProvider, SortedListProvider, Supports,
VoteWeight, VoteWeightProvider, VoterOf,
data_provider, ElectionDataProvider, ElectionProvider, ScoreProvider, SortedListProvider,
Supports, VoteWeight, VoterOf,
};
use frame_support::{
pallet_prelude::*,
@@ -1244,13 +1244,15 @@ where
}
}
impl<T: Config> VoteWeightProvider<T::AccountId> for Pallet<T> {
fn vote_weight(who: &T::AccountId) -> VoteWeight {
impl<T: Config> ScoreProvider<T::AccountId> for Pallet<T> {
type Score = VoteWeight;
fn score(who: &T::AccountId) -> Self::Score {
Self::weight_of(who)
}
#[cfg(feature = "runtime-benchmarks")]
fn set_vote_weight_of(who: &T::AccountId, weight: VoteWeight) {
fn set_score_of(who: &T::AccountId, weight: Self::Score) {
// this will clearly results in an inconsistent state, but it should not matter for a
// benchmark.
let active: BalanceOf<T> = weight.try_into().map_err(|_| ()).unwrap();
@@ -1279,6 +1281,7 @@ impl<T: Config> VoteWeightProvider<T::AccountId> for Pallet<T> {
pub struct UseNominatorsMap<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> {
type Error = ();
type Score = VoteWeight;
/// Returns iterator over voter list, which can have `take` called on it.
fn iter() -> Box<dyn Iterator<Item = T::AccountId>> {
@@ -1290,11 +1293,11 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> {
fn contains(id: &T::AccountId) -> bool {
Nominators::<T>::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(())
}
fn on_update(_: &T::AccountId, _weight: VoteWeight) {
fn on_update(_: &T::AccountId, _weight: Self::Score) {
// nothing to do on update.
}
fn on_remove(_: &T::AccountId) {
@@ -1302,7 +1305,7 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsMap<T> {
}
fn unsafe_regenerate(
_: impl IntoIterator<Item = T::AccountId>,
_: Box<dyn Fn(&T::AccountId) -> VoteWeight>,
_: Box<dyn Fn(&T::AccountId) -> Self::Score>,
) -> u32 {
// nothing to do upon regenerate.
0