Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threw an error if weighted_stake_diffs.entry() is empty #25

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions programs/rewards/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ pub enum MplxRewardsError {
/// No need to transfer zero amount of rewards.
#[error("Rewards: rewards amount must be positive")]
RewardsMustBeGreaterThanZero,

/// 13
/// No need to transfer zero amount of rewards.
#[error("'weighted_stake_diffs' entry cannot be empty")]
Copy link
Collaborator

@kstepanovdev kstepanovdev Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[error("'weighted_stake_diffs' entry cannot be empty")]
#[error("No changes at the date in weighted stake modifiers while they're expected")]

WeightedStakeDiffEntryIsEmpty,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May this error be slightly more meaningful? What about NoWeightedStakeModifiersAtADate or something like that?

}

impl PrintProgramError for MplxRewardsError {
Expand Down
38 changes: 28 additions & 10 deletions programs/rewards/src/state/reward_pool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{btree_map::Entry, BTreeMap};

use crate::{
error::MplxRewardsError,
Expand Down Expand Up @@ -216,16 +216,17 @@ impl RewardPool {
.checked_sub(curr_part_of_weighted_stake_for_flex)
.ok_or(MplxRewardsError::MathOverflow)?;

self.calculator
.weighted_stake_diffs
.entry(deposit_old_expiration_ts)
.and_modify(|modifier| *modifier -= weighted_stake_diff);
Self::modify_weighted_stake_diffs(
&mut self.calculator.weighted_stake_diffs,
deposit_old_expiration_ts,
weighted_stake_diff,
)?;

mining
.index
.weighted_stake_diffs
.entry(deposit_old_expiration_ts)
.and_modify(|modifier| *modifier -= weighted_stake_diff);
Self::modify_weighted_stake_diffs(
&mut mining.index.weighted_stake_diffs,
deposit_old_expiration_ts,
weighted_stake_diff,
)?;

// also, we need to reduce staking power because we want to extend stake from "scratch"
mining.share = mining
Expand Down Expand Up @@ -258,6 +259,23 @@ impl RewardPool {

Ok(())
}

fn modify_weighted_stake_diffs(
diffs: &mut BTreeMap<u64, u64>,
timestamp: u64,
weighted_stake_diff: u64,
) -> Result<(), MplxRewardsError> {
match diffs.entry(timestamp) {
Entry::Vacant(_) => Err(MplxRewardsError::WeightedStakeDiffEntryIsEmpty),
Entry::Occupied(mut entry) => {
let modifier = entry.get_mut();
*modifier = modifier
.checked_sub(weighted_stake_diff)
.ok_or(MplxRewardsError::MathOverflow)?;
Ok(())
}
}
}
}

impl Sealed for RewardPool {}
Expand Down
Loading