|
22 | 22 | //! for the time being, although we will build schemes to do so in the future. |
23 | 23 |
|
24 | 24 | use crate::{session_info, shared}; |
25 | | -use frame_support::traits::ValidatorSet; |
26 | | -use primitives::v2::ValidatorIndex; |
| 25 | +use frame_support::traits::{Defensive, ValidatorSet}; |
| 26 | +use primitives::v2::{SessionIndex, ValidatorIndex}; |
| 27 | +use sp_std::collections::btree_set::BTreeSet; |
27 | 28 |
|
28 | 29 | /// The amount of era points given by backing a candidate that is included. |
29 | 30 | pub const BACKING_POINTS: u32 = 20; |
| 31 | +/// The amount of era points given by dispute voting on a candidate. |
| 32 | +pub const DISPUTE_STATEMENT_POINTS: u32 = 20; |
30 | 33 |
|
31 | 34 | /// Rewards validators for participating in parachains with era points in pallet-staking. |
32 | 35 | pub struct RewardValidatorsWithEraPoints<C>(sp_std::marker::PhantomData<C>); |
33 | 36 |
|
34 | | -impl<C> crate::inclusion::RewardValidators for RewardValidatorsWithEraPoints<C> |
| 37 | +impl<C> RewardValidatorsWithEraPoints<C> |
35 | 38 | where |
36 | | - C: pallet_staking::Config + shared::Config + session_info::Config, |
| 39 | + C: pallet_staking::Config + session_info::Config, |
37 | 40 | C::ValidatorSet: ValidatorSet<C::AccountId, ValidatorId = C::AccountId>, |
38 | 41 | { |
39 | | - fn reward_backing(indices: impl IntoIterator<Item = ValidatorIndex>) { |
40 | | - // Fetch the validators from the _session_ because sessions are offset from eras |
41 | | - // and we are rewarding for behavior in current session. |
42 | | - let session_index = shared::Pallet::<C>::session_index(); |
| 42 | + /// Reward validators in session with points, but only if they are in the active set. |
| 43 | + fn reward_only_active( |
| 44 | + session_index: SessionIndex, |
| 45 | + indices: impl IntoIterator<Item = ValidatorIndex>, |
| 46 | + points: u32, |
| 47 | + ) { |
43 | 48 | let validators = session_info::Pallet::<C>::account_keys(&session_index); |
44 | | - let validators = match validators { |
| 49 | + let validators = match validators |
| 50 | + .defensive_proof("account_keys are present for dispute_period sessions") |
| 51 | + { |
45 | 52 | Some(validators) => validators, |
46 | | - None => { |
47 | | - // Account keys are missing for the current session. |
48 | | - // This might happen only for the first session after |
49 | | - // `AccountKeys` were introduced via runtime upgrade. |
50 | | - return |
51 | | - }, |
| 53 | + None => return, |
52 | 54 | }; |
| 55 | + // limit rewards to the active validator set |
| 56 | + let active_set: BTreeSet<_> = C::ValidatorSet::validators().into_iter().collect(); |
53 | 57 |
|
54 | 58 | let rewards = indices |
55 | 59 | .into_iter() |
56 | 60 | .filter_map(|i| validators.get(i.0 as usize).cloned()) |
57 | | - .map(|v| (v, BACKING_POINTS)); |
| 61 | + .filter(|v| active_set.contains(v)) |
| 62 | + .map(|v| (v, points)); |
58 | 63 |
|
59 | 64 | <pallet_staking::Pallet<C>>::reward_by_ids(rewards); |
60 | 65 | } |
| 66 | +} |
| 67 | + |
| 68 | +impl<C> crate::inclusion::RewardValidators for RewardValidatorsWithEraPoints<C> |
| 69 | +where |
| 70 | + C: pallet_staking::Config + shared::Config + session_info::Config, |
| 71 | + C::ValidatorSet: ValidatorSet<C::AccountId, ValidatorId = C::AccountId>, |
| 72 | +{ |
| 73 | + fn reward_backing(indices: impl IntoIterator<Item = ValidatorIndex>) { |
| 74 | + let session_index = shared::Pallet::<C>::session_index(); |
| 75 | + Self::reward_only_active(session_index, indices, BACKING_POINTS); |
| 76 | + } |
61 | 77 |
|
62 | 78 | fn reward_bitfields(_validators: impl IntoIterator<Item = ValidatorIndex>) {} |
63 | 79 | } |
| 80 | + |
| 81 | +impl<C> crate::disputes::RewardValidators for RewardValidatorsWithEraPoints<C> |
| 82 | +where |
| 83 | + C: pallet_staking::Config + session_info::Config, |
| 84 | + C::ValidatorSet: ValidatorSet<C::AccountId, ValidatorId = C::AccountId>, |
| 85 | +{ |
| 86 | + fn reward_dispute_statement( |
| 87 | + session: SessionIndex, |
| 88 | + validators: impl IntoIterator<Item = ValidatorIndex>, |
| 89 | + ) { |
| 90 | + Self::reward_only_active(session, validators, DISPUTE_STATEMENT_POINTS); |
| 91 | + } |
| 92 | +} |
0 commit comments