Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit d7dc28d

Browse files
authored
disputes rewards (#5862)
* refactor backing points to only reward active set * impl disputes::RewardValidators * enable rewards on westend, kusama, polkadot * fmt * make dispute points same as backing * disable on polkadot for now
1 parent 9ec372c commit d7dc28d

3 files changed

Lines changed: 47 additions & 18 deletions

File tree

runtime/kusama/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ impl parachains_initializer::Config for Runtime {
11691169

11701170
impl parachains_disputes::Config for Runtime {
11711171
type Event = Event;
1172-
type RewardValidators = ();
1172+
type RewardValidators = parachains_reward_points::RewardValidatorsWithEraPoints<Runtime>;
11731173
type PunishValidators = ();
11741174
type WeightInfo = weights::runtime_parachains_disputes::WeightInfo<Runtime>;
11751175
}

runtime/parachains/src/reward_points.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,71 @@
2222
//! for the time being, although we will build schemes to do so in the future.
2323
2424
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;
2728

2829
/// The amount of era points given by backing a candidate that is included.
2930
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;
3033

3134
/// Rewards validators for participating in parachains with era points in pallet-staking.
3235
pub struct RewardValidatorsWithEraPoints<C>(sp_std::marker::PhantomData<C>);
3336

34-
impl<C> crate::inclusion::RewardValidators for RewardValidatorsWithEraPoints<C>
37+
impl<C> RewardValidatorsWithEraPoints<C>
3538
where
36-
C: pallet_staking::Config + shared::Config + session_info::Config,
39+
C: pallet_staking::Config + session_info::Config,
3740
C::ValidatorSet: ValidatorSet<C::AccountId, ValidatorId = C::AccountId>,
3841
{
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+
) {
4348
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+
{
4552
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,
5254
};
55+
// limit rewards to the active validator set
56+
let active_set: BTreeSet<_> = C::ValidatorSet::validators().into_iter().collect();
5357

5458
let rewards = indices
5559
.into_iter()
5660
.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));
5863

5964
<pallet_staking::Pallet<C>>::reward_by_ids(rewards);
6065
}
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+
}
6177

6278
fn reward_bitfields(_validators: impl IntoIterator<Item = ValidatorIndex>) {}
6379
}
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+
}

runtime/westend/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ impl assigned_slots::Config for Runtime {
944944

945945
impl parachains_disputes::Config for Runtime {
946946
type Event = Event;
947-
type RewardValidators = ();
947+
type RewardValidators = parachains_reward_points::RewardValidatorsWithEraPoints<Runtime>;
948948
type PunishValidators = ();
949949
type WeightInfo = weights::runtime_parachains_disputes::WeightInfo<Runtime>;
950950
}

0 commit comments

Comments
 (0)