Don't reset credits_observed due to stale voters (#13836)
* Don't reset credits_observed due to stale voters * Add tests * Fix comment
This commit is contained in:
@ -553,7 +553,7 @@ impl Stake {
|
|||||||
/// for a given stake and vote_state, calculate how many
|
/// for a given stake and vote_state, calculate how many
|
||||||
/// points were earned (credits * stake) and new value
|
/// points were earned (credits * stake) and new value
|
||||||
/// for credits_observed were the points paid
|
/// for credits_observed were the points paid
|
||||||
pub fn calculate_points_and_credits(
|
fn calculate_points_and_credits(
|
||||||
&self,
|
&self,
|
||||||
new_vote_state: &VoteState,
|
new_vote_state: &VoteState,
|
||||||
stake_history: Option<&StakeHistory>,
|
stake_history: Option<&StakeHistory>,
|
||||||
@ -562,8 +562,12 @@ impl Stake {
|
|||||||
) -> (u128, u64) {
|
) -> (u128, u64) {
|
||||||
// if there is no newer credits since observed, return no point
|
// if there is no newer credits since observed, return no point
|
||||||
if new_vote_state.credits() <= self.credits_observed {
|
if new_vote_state.credits() <= self.credits_observed {
|
||||||
|
if fix_stake_deactivate {
|
||||||
|
return (0, self.credits_observed);
|
||||||
|
} else {
|
||||||
return (0, 0);
|
return (0, 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut points = 0;
|
let mut points = 0;
|
||||||
let mut new_credits_observed = self.credits_observed;
|
let mut new_credits_observed = self.credits_observed;
|
||||||
@ -3435,7 +3439,7 @@ mod tests {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// now one with inflation disabled. no one gets paid, but we still need
|
// now one with inflation disabled. no one gets paid, but we still need
|
||||||
// to advance the stake state's observed_credits field to prevent back-
|
// to advance the stake state's credits_observed field to prevent back-
|
||||||
// paying rewards when inflation is turned on.
|
// paying rewards when inflation is turned on.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Some((0, 0, 4)),
|
Some((0, 0, 4)),
|
||||||
@ -3450,6 +3454,33 @@ mod tests {
|
|||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// credits_observed remains at previous level when vote_state credits are
|
||||||
|
// not advancing and inflation is disabled
|
||||||
|
stake.credits_observed = 4;
|
||||||
|
assert_eq!(
|
||||||
|
Some((0, 0, 4)),
|
||||||
|
stake.calculate_rewards(
|
||||||
|
&PointValue {
|
||||||
|
rewards: 0,
|
||||||
|
points: 4
|
||||||
|
},
|
||||||
|
&vote_state,
|
||||||
|
None,
|
||||||
|
&mut null_tracer(),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// assert the previous behavior is preserved where fix_stake_deactivate=false
|
||||||
|
assert_eq!(
|
||||||
|
(0, 0),
|
||||||
|
stake.calculate_points_and_credits(&vote_state, None, &mut null_tracer(), false)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
(0, 4),
|
||||||
|
stake.calculate_points_and_credits(&vote_state, None, &mut null_tracer(), true)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Reference in New Issue
Block a user