From 2992a7154a5726b26597eb9bdebdba6e5c6f19be Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sat, 13 Nov 2021 01:27:23 +0000 Subject: [PATCH] Refactor vote state to remove double negative (backport #21244) (#21250) * Refactor vote state to remove double negative (#21244) (cherry picked from commit ef29d2d1729988946afb8feea02bb6889c7dfd15) # Conflicts: # runtime/src/stakes.rs * resolve conflicts Co-authored-by: Justin Starry --- programs/vote/src/vote_state/mod.rs | 28 ++++++++++++++++++---------- runtime/src/stakes.rs | 4 +++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/programs/vote/src/vote_state/mod.rs b/programs/vote/src/vote_state/mod.rs index 70e59c3d28..78d4282822 100644 --- a/programs/vote/src/vote_state/mod.rs +++ b/programs/vote/src/vote_state/mod.rs @@ -585,11 +585,11 @@ impl VoteState { Ok(()) } - pub fn is_uninitialized_no_deser(data: &[u8]) -> bool { + pub fn is_correct_size_and_initialized(data: &[u8]) -> bool { const VERSION_OFFSET: usize = 4; - data.len() != VoteState::size_of() - || data[VERSION_OFFSET..VERSION_OFFSET + DEFAULT_PRIOR_VOTERS_OFFSET] - == [0; DEFAULT_PRIOR_VOTERS_OFFSET] + data.len() == VoteState::size_of() + && data[VERSION_OFFSET..VERSION_OFFSET + DEFAULT_PRIOR_VOTERS_OFFSET] + != [0; DEFAULT_PRIOR_VOTERS_OFFSET] } } @@ -2082,25 +2082,31 @@ mod tests { } #[test] - fn test_is_uninitialized_no_deser() { + fn test_is_correct_size_and_initialized() { // Check all zeroes let mut vote_account_data = vec![0; VoteState::size_of()]; - assert!(VoteState::is_uninitialized_no_deser(&vote_account_data)); + assert!(!VoteState::is_correct_size_and_initialized( + &vote_account_data + )); // Check default VoteState let default_account_state = VoteStateVersions::new_current(VoteState::default()); VoteState::serialize(&default_account_state, &mut vote_account_data).unwrap(); - assert!(VoteState::is_uninitialized_no_deser(&vote_account_data)); + assert!(!VoteState::is_correct_size_and_initialized( + &vote_account_data + )); // Check non-zero data shorter than offset index used let short_data = vec![1; DEFAULT_PRIOR_VOTERS_OFFSET]; - assert!(VoteState::is_uninitialized_no_deser(&short_data)); + assert!(!VoteState::is_correct_size_and_initialized(&short_data)); // Check non-zero large account let mut large_vote_data = vec![1; 2 * VoteState::size_of()]; let default_account_state = VoteStateVersions::new_current(VoteState::default()); VoteState::serialize(&default_account_state, &mut large_vote_data).unwrap(); - assert!(VoteState::is_uninitialized_no_deser(&vote_account_data)); + assert!(!VoteState::is_correct_size_and_initialized( + &vote_account_data + )); // Check populated VoteState let account_state = VoteStateVersions::new_current(VoteState::new( @@ -2113,6 +2119,8 @@ mod tests { &Clock::default(), )); VoteState::serialize(&account_state, &mut vote_account_data).unwrap(); - assert!(!VoteState::is_uninitialized_no_deser(&vote_account_data)); + assert!(VoteState::is_correct_size_and_initialized( + &vote_account_data + )); } } diff --git a/runtime/src/stakes.rs b/runtime/src/stakes.rs index e07e1ec7db..b95306f094 100644 --- a/runtime/src/stakes.rs +++ b/runtime/src/stakes.rs @@ -203,8 +203,10 @@ impl Stakes { let old = self.vote_accounts.remove(pubkey); // when account is removed (lamports == 0 or data uninitialized), don't read so that // given `pubkey` can be used for any owner in the future, while not affecting Stakes. + let vote_init_check_disabled = !check_vote_init; if account.lamports() != 0 - && !(check_vote_init && VoteState::is_uninitialized_no_deser(account.data())) + && (vote_init_check_disabled + || VoteState::is_correct_size_and_initialized(account.data())) { let stake = old.as_ref().map_or_else( || self.calculate_stake(pubkey, self.epoch, Some(&self.stake_history)),