diff --git a/programs/vote_api/src/vote_state.rs b/programs/vote_api/src/vote_state.rs index ce1e1aa9ba..b74d307f1f 100644 --- a/programs/vote_api/src/vote_state.rs +++ b/programs/vote_api/src/vote_state.rs @@ -264,8 +264,10 @@ impl VoteState { if epoch != self.epoch { // encode the delta, but be able to return partial for stakers who // attach halfway through an epoch - self.epoch_credits - .push((self.epoch, self.credits, self.last_epoch_credits)); + if self.credits > 0 { + self.epoch_credits + .push((self.epoch, self.credits, self.last_epoch_credits)); + } // if stakers do not claim before the epoch goes away they lose the // credits... if self.epoch_credits.len() > MAX_EPOCH_CREDITS_HISTORY { @@ -1143,4 +1145,36 @@ mod tests { ); } + #[test] + fn test_vote_state_epoch0_no_credits() { + let mut vote_state = VoteState::default(); + + assert_eq!( + vote_state + .epoch_credits() + .cloned() + .collect::>() + .len(), + 0 + ); + vote_state.increment_credits(1); + assert_eq!( + vote_state + .epoch_credits() + .cloned() + .collect::>() + .len(), + 0 + ); + + vote_state.increment_credits(2); + assert_eq!( + vote_state + .epoch_credits() + .cloned() + .collect::>() + .len(), + 1 + ); + } }