Skip to content

Commit

Permalink
Use MaxValidatorsCount to bound ErasRewardPoints BoundedVec
Browse files Browse the repository at this point in the history
  • Loading branch information
re-gius committed Dec 2, 2024
1 parent e986264 commit ea59383
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
10 changes: 2 additions & 8 deletions substrate/frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ pub fn create_validator_with_nominators<T: Config>(
assert_ok!(reward_map.try_insert(validator, reward));
}
// Give Era Points
let reward = EraRewardPoints::<
T::AccountId,
<T::ElectionProvider as ElectionProviderBase>::MaxWinners,
> {
let reward = EraRewardPoints::<T::AccountId, T::MaxValidatorsCount> {
total: points_total,
individual: reward_map,
};
Expand Down Expand Up @@ -880,10 +877,7 @@ mod benchmarks {
assert_ok!(reward_map.try_insert(validator, reward));
}
// Give Era Points
let reward = EraRewardPoints::<
T::AccountId,
<T::ElectionProvider as ElectionProviderBase>::MaxWinners,
> {
let reward = EraRewardPoints::<T::AccountId, T::MaxValidatorsCount> {
total: points_total,
individual: reward_map,
};
Expand Down
10 changes: 6 additions & 4 deletions substrate/frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,17 @@ pub struct ActiveEraInfo {
///
/// This points will be used to reward validators and their respective nominators.
#[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxWinners))]
pub struct EraRewardPoints<AccountId: Ord, MaxWinners: Get<u32>> {
#[scale_info(skip_type_params(MaxValidatorsCount))]
pub struct EraRewardPoints<AccountId: Ord, MaxValidatorsCount: Get<u32>> {
/// Total number of points. Equals the sum of reward points for each validator.
pub total: RewardPoint,
/// The reward points earned by a given validator.
pub individual: BoundedBTreeMap<AccountId, RewardPoint, MaxWinners>,
pub individual: BoundedBTreeMap<AccountId, RewardPoint, MaxValidatorsCount>,
}

impl<AccountId: Ord, MaxWinners: Get<u32>> Default for EraRewardPoints<AccountId, MaxWinners> {
impl<AccountId: Ord, MaxValidatorsCount: Get<u32>> Default
for EraRewardPoints<AccountId, MaxValidatorsCount>
{
fn default() -> Self {
EraRewardPoints { total: Default::default(), individual: BoundedBTreeMap::new() }
}
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/staking/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub mod v17 {
Ok(individual_rewards) => {
let bounded_era_rewards = EraRewardPoints::<
<T as frame_system::Config>::AccountId,
<<T as Config>::ElectionProvider as ElectionProviderBase>::MaxWinners,
<T as Config>::MaxValidatorsCount,
> {
individual: individual_rewards,
total: era_rewards.total,
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Default for ExtBuilder {
validator_count: 2,
minimum_validator_count: 0,
balance_factor: 1,
invulnerables: vec![],
invulnerables: BoundedVec::new(),
has_stakers: true,
initialize_first_session: true,
min_nominator_bond: ExistentialDeposit::get(),
Expand Down Expand Up @@ -376,7 +376,7 @@ impl ExtBuilder {
self
}
pub fn invulnerables(mut self, invulnerables: Vec<AccountId>) -> Self {
self.invulnerables = invulnerables;
self.invulnerables = BoundedVec::force_from(invulnerables).unwrap();
self
}
pub fn session_per_era(self, length: SessionIndex) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ pub mod pallet {
_,
Twox64Concat,
EraIndex,
EraRewardPoints<T::AccountId, <T::ElectionProvider as ElectionProviderBase>::MaxWinners>,
EraRewardPoints<T::AccountId, T::MaxValidatorsCount>,
ValueQuery,
>;

Expand Down Expand Up @@ -1050,7 +1050,7 @@ pub mod pallet {
/// Get the rewards for the last [`Config::HistoryDepth`] eras.
pub fn eras_reward_points<EncodeLikeEraIndex>(
era_index: EncodeLikeEraIndex,
) -> EraRewardPoints<T::AccountId, <T::ElectionProvider as ElectionProviderBase>::MaxWinners>
) -> EraRewardPoints<T::AccountId, T::MaxValidatorsCount>
where
EncodeLikeEraIndex: codec::EncodeLike<EraIndex>,
{
Expand Down
2 changes: 0 additions & 2 deletions substrate/frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ fn set_staking_configs_works() {
ConfigOp::Noop,
ConfigOp::Noop,
ConfigOp::Noop,
ConfigOp::Noop,
ConfigOp::Noop
)));

Expand All @@ -84,7 +83,6 @@ fn set_staking_configs_works() {
ConfigOp::Remove,
ConfigOp::Remove,
ConfigOp::Remove,
ConfigOp::Remove,
ConfigOp::Remove
));
assert_eq!(MinNominatorBond::<Test>::get(), 0);
Expand Down

0 comments on commit ea59383

Please sign in to comment.