pub trait SortedListProvider<AccountId> {
type Error: Debug;
type Score: Bounded + Saturating + Zero;
Show 13 methods
// Required methods
fn iter() -> Box<dyn Iterator<Item = AccountId>>;
fn iter_from(
start: &AccountId
) -> Result<Box<dyn Iterator<Item = AccountId>>, Self::Error>;
fn count() -> u32;
fn contains(id: &AccountId) -> bool;
fn on_insert(id: AccountId, score: Self::Score) -> Result<(), Self::Error>;
fn on_update(id: &AccountId, score: Self::Score) -> Result<(), Self::Error>;
fn get_score(id: &AccountId) -> Result<Self::Score, Self::Error>;
fn on_remove(id: &AccountId) -> Result<(), Self::Error>;
fn unsafe_regenerate(
all: impl IntoIterator<Item = AccountId>,
score_of: Box<dyn Fn(&AccountId) -> Self::Score>
) -> u32;
fn unsafe_clear();
fn try_state() -> Result<(), &'static str>;
// Provided methods
fn on_increase(
id: &AccountId,
additional: Self::Score
) -> Result<(), Self::Error> { ... }
fn on_decrease(
id: &AccountId,
decreased: Self::Score
) -> Result<(), Self::Error> { ... }
}
Expand description
A utility trait for something to implement ElectionDataProvider
in a sensible way.
This is generic over AccountId
and it can represent a validator, a nominator, or any other
entity.
The scores (see Self::Score
) are ascending, the higher, the better.
Something that implements this trait will do a best-effort sort over ids, and thus can be
used on the implementing side of ElectionDataProvider
.
Required Associated Types§
sourcetype Score: Bounded + Saturating + Zero
type Score: Bounded + Saturating + Zero
The type used by the list to compare nodes for ordering.
Required Methods§
sourcefn iter() -> Box<dyn Iterator<Item = AccountId>>
fn iter() -> Box<dyn Iterator<Item = AccountId>>
An iterator over the list, which can have take
called on it.
sourcefn iter_from(
start: &AccountId
) -> Result<Box<dyn Iterator<Item = AccountId>>, Self::Error>
fn iter_from( start: &AccountId ) -> Result<Box<dyn Iterator<Item = AccountId>>, Self::Error>
Returns an iterator over the list, starting right after from the given voter.
May return an error if start
is invalid.
sourcefn contains(id: &AccountId) -> bool
fn contains(id: &AccountId) -> bool
Return true if the list already contains id
.
sourcefn on_insert(id: AccountId, score: Self::Score) -> Result<(), Self::Error>
fn on_insert(id: AccountId, score: Self::Score) -> Result<(), Self::Error>
Hook for inserting a new id.
Implementation should return an error if duplicate item is being inserted.
sourcefn on_update(id: &AccountId, score: Self::Score) -> Result<(), Self::Error>
fn on_update(id: &AccountId, score: Self::Score) -> Result<(), Self::Error>
Hook for updating a single id.
The new
score is given.
Returns Ok(())
iff it successfully updates an item, an Err(_)
otherwise.
sourcefn on_remove(id: &AccountId) -> Result<(), Self::Error>
fn on_remove(id: &AccountId) -> Result<(), Self::Error>
Hook for removing am id from the list.
Returns Ok(())
iff it successfully removes an item, an Err(_)
otherwise.
sourcefn unsafe_regenerate(
all: impl IntoIterator<Item = AccountId>,
score_of: Box<dyn Fn(&AccountId) -> Self::Score>
) -> u32
fn unsafe_regenerate( all: impl IntoIterator<Item = AccountId>, score_of: Box<dyn Fn(&AccountId) -> Self::Score> ) -> u32
Regenerate this list from scratch. Returns the count of items inserted.
This should typically only be used at a runtime upgrade.
WARNING
This function should be called with care, regenerate will remove the current list write the new list, which can lead to too many storage accesses, exhausting the block weight.
sourcefn unsafe_clear()
fn unsafe_clear()
Remove all items from the list.
WARNING
This function should never be called in production settings because it can lead to an unbounded amount of storage accesses.
Provided Methods§
sourcefn on_increase(
id: &AccountId,
additional: Self::Score
) -> Result<(), Self::Error>
fn on_increase( id: &AccountId, additional: Self::Score ) -> Result<(), Self::Error>
Same as on_update
, but incorporate some increased score.
sourcefn on_decrease(
id: &AccountId,
decreased: Self::Score
) -> Result<(), Self::Error>
fn on_decrease( id: &AccountId, decreased: Self::Score ) -> Result<(), Self::Error>
Same as on_update
, but incorporate some decreased score.
If the new score of the item is Zero
, it is removed.