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

Adding additional logging when overflow occurs in calculate_stake_rew… #3783

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
83 changes: 77 additions & 6 deletions programs/stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,28 @@ fn calculate_stake_rewards(
return None;
}

let rewards = points
.checked_mul(u128::from(point_value.rewards))
.unwrap()
.checked_div(point_value.points)
.unwrap();
let rewards = points.checked_mul(u128::from(point_value.rewards));

let rewards = match rewards {
// Unwrap on this division is safe, point_value.points being non zero is guaranteed above
Some(value) => u64::try_from(value.checked_div(point_value.points).unwrap()),
None => {
panic!(
"Overflowing u128 when multiplying points by rewards {} and {}",
points, point_value.rewards
)
}
};

let rewards = u64::try_from(rewards).unwrap();
let rewards = match rewards {
Ok(value) => value,
Err(e) => {
panic!(
"Error {}, inputs were points {}, point_value.rewards {} and point_value.points {}",
e, points, point_value.rewards, point_value.points
);
}
};

// don't bother trying to split if fractional lamports got truncated
if rewards == 0 {
Expand Down Expand Up @@ -610,6 +625,62 @@ mod tests {
);
}

#[test]
#[should_panic]
fn test_overflow_on_umul_calculate_rewards() {
Comment on lines +628 to +630

Choose a reason for hiding this comment

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

Rather than creating two separate tests, how about parametrizing the stake passed to new_stake and using the test_case crate to combine the two tests?

let mut vote_state = VoteState::default();

// assume stake.stake() is right
// bootstrap means fully-vested stake at epoch 0
Comment on lines +633 to +634

Choose a reason for hiding this comment

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

Is this comment intentional?


// Set arbitrarily large values to force overflows on later calculations
let stake = new_stake(
4_000_000_000_000_000_000,
&Pubkey::default(),
&vote_state,
u64::MAX,
);

vote_state.increment_credits(0, 10_000_000_000_000_000_000);

calculate_stake_rewards(
0,
&stake,
&PointValue {
rewards: 1_000,
points: 1,
},
&vote_state,
&StakeHistory::default(),
null_tracer(),
None,
);
}

#[test]
#[should_panic]
fn test_overflow_on_conversion_calculate_rewards() {
let mut vote_state = VoteState::default();

// assume stake.stake() is right
let stake = new_stake(1, &Pubkey::default(), &vote_state, u64::MAX);

vote_state.increment_credits(0, 10_000_000_000_000_000_000);

calculate_stake_rewards(
0,
&stake,
&PointValue {
rewards: 1_000_000_000,
points: 1,
},
&vote_state,
&StakeHistory::default(),
null_tracer(),
None,
);
}

#[test]
fn test_stake_state_calculate_points_with_typical_values() {
let vote_state = VoteState::default();
Expand Down