use crate::{session_info, shared};
use alloc::collections::btree_set::BTreeSet;
use frame_support::traits::{Defensive, ValidatorSet};
use polkadot_primitives::{SessionIndex, ValidatorIndex};
pub const BACKING_POINTS: u32 = 20;
pub const DISPUTE_STATEMENT_POINTS: u32 = 20;
pub struct RewardValidatorsWithEraPoints<C>(core::marker::PhantomData<C>);
impl<C> RewardValidatorsWithEraPoints<C>
where
C: pallet_staking::Config + session_info::Config,
C::ValidatorSet: ValidatorSet<C::AccountId, ValidatorId = C::AccountId>,
{
fn reward_only_active(
session_index: SessionIndex,
indices: impl IntoIterator<Item = ValidatorIndex>,
points: u32,
) {
let validators = session_info::AccountKeys::<C>::get(&session_index);
let validators = match validators
.defensive_proof("account_keys are present for dispute_period sessions")
{
Some(validators) => validators,
None => return,
};
let active_set: BTreeSet<_> = C::ValidatorSet::validators().into_iter().collect();
let rewards = indices
.into_iter()
.filter_map(|i| validators.get(i.0 as usize).cloned())
.filter(|v| active_set.contains(v))
.map(|v| (v, points));
<pallet_staking::Pallet<C>>::reward_by_ids(rewards);
}
}
impl<C> crate::inclusion::RewardValidators for RewardValidatorsWithEraPoints<C>
where
C: pallet_staking::Config + shared::Config + session_info::Config,
C::ValidatorSet: ValidatorSet<C::AccountId, ValidatorId = C::AccountId>,
{
fn reward_backing(indices: impl IntoIterator<Item = ValidatorIndex>) {
let session_index = shared::CurrentSessionIndex::<C>::get();
Self::reward_only_active(session_index, indices, BACKING_POINTS);
}
fn reward_bitfields(_validators: impl IntoIterator<Item = ValidatorIndex>) {}
}
impl<C> crate::disputes::RewardValidators for RewardValidatorsWithEraPoints<C>
where
C: pallet_staking::Config + session_info::Config,
C::ValidatorSet: ValidatorSet<C::AccountId, ValidatorId = C::AccountId>,
{
fn reward_dispute_statement(
session: SessionIndex,
validators: impl IntoIterator<Item = ValidatorIndex>,
) {
Self::reward_only_active(session, validators, DISPUTE_STATEMENT_POINTS);
}
}