From f57e48a20961d1af7a1cdd64ca0854068b039c4e Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Thu, 26 Sep 2019 20:57:35 -0700 Subject: [PATCH] Avoid storing epoch 0 credits if no credits where earned in epoch 0 (#6132) --- programs/vote_api/src/vote_state.rs | 38 +++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) 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 + ); + } }