From e81c2c826c447c897addeea4f8e6cf8737f4f76e Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Mon, 30 Nov 2020 22:47:34 +0900 Subject: [PATCH] Don't reset credits_observed due to stale voters (#13836) * Don't reset credits_observed due to stale voters * Add tests * Fix comment --- programs/stake/src/stake_state.rs | 37 ++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/programs/stake/src/stake_state.rs b/programs/stake/src/stake_state.rs index a1c55d5c8f..9d4752fceb 100644 --- a/programs/stake/src/stake_state.rs +++ b/programs/stake/src/stake_state.rs @@ -553,7 +553,7 @@ impl Stake { /// for a given stake and vote_state, calculate how many /// points were earned (credits * stake) and new value /// for credits_observed were the points paid - pub fn calculate_points_and_credits( + fn calculate_points_and_credits( &self, new_vote_state: &VoteState, stake_history: Option<&StakeHistory>, @@ -562,7 +562,11 @@ impl Stake { ) -> (u128, u64) { // if there is no newer credits since observed, return no point if new_vote_state.credits() <= self.credits_observed { - return (0, 0); + if fix_stake_deactivate { + return (0, self.credits_observed); + } else { + return (0, 0); + } } let mut points = 0; @@ -3435,7 +3439,7 @@ mod tests { ); // 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. assert_eq!( Some((0, 0, 4)), @@ -3450,6 +3454,33 @@ mod tests { 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]