Refactor vote state to remove double negative (#21244)
This commit is contained in:
@ -585,11 +585,11 @@ impl VoteState {
|
|||||||
Ok(())
|
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;
|
const VERSION_OFFSET: usize = 4;
|
||||||
data.len() != VoteState::size_of()
|
data.len() == VoteState::size_of()
|
||||||
|| data[VERSION_OFFSET..VERSION_OFFSET + DEFAULT_PRIOR_VOTERS_OFFSET]
|
&& data[VERSION_OFFSET..VERSION_OFFSET + DEFAULT_PRIOR_VOTERS_OFFSET]
|
||||||
== [0; DEFAULT_PRIOR_VOTERS_OFFSET]
|
!= [0; DEFAULT_PRIOR_VOTERS_OFFSET]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2077,25 +2077,31 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_uninitialized_no_deser() {
|
fn test_is_correct_size_and_initialized() {
|
||||||
// Check all zeroes
|
// Check all zeroes
|
||||||
let mut vote_account_data = vec![0; VoteState::size_of()];
|
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
|
// Check default VoteState
|
||||||
let default_account_state = VoteStateVersions::new_current(VoteState::default());
|
let default_account_state = VoteStateVersions::new_current(VoteState::default());
|
||||||
VoteState::serialize(&default_account_state, &mut vote_account_data).unwrap();
|
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
|
// Check non-zero data shorter than offset index used
|
||||||
let short_data = vec![1; DEFAULT_PRIOR_VOTERS_OFFSET];
|
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
|
// Check non-zero large account
|
||||||
let mut large_vote_data = vec![1; 2 * VoteState::size_of()];
|
let mut large_vote_data = vec![1; 2 * VoteState::size_of()];
|
||||||
let default_account_state = VoteStateVersions::new_current(VoteState::default());
|
let default_account_state = VoteStateVersions::new_current(VoteState::default());
|
||||||
VoteState::serialize(&default_account_state, &mut large_vote_data).unwrap();
|
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
|
// Check populated VoteState
|
||||||
let account_state = VoteStateVersions::new_current(VoteState::new(
|
let account_state = VoteStateVersions::new_current(VoteState::new(
|
||||||
@ -2108,6 +2114,8 @@ mod tests {
|
|||||||
&Clock::default(),
|
&Clock::default(),
|
||||||
));
|
));
|
||||||
VoteState::serialize(&account_state, &mut vote_account_data).unwrap();
|
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
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -204,7 +204,8 @@ impl Stakes {
|
|||||||
let old = self.vote_accounts.remove(pubkey);
|
let old = self.vote_accounts.remove(pubkey);
|
||||||
// when account is removed (lamports == 0 or data uninitialized), don't read so that
|
// 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.
|
// given `pubkey` can be used for any owner in the future, while not affecting Stakes.
|
||||||
if account.lamports() != 0 && !VoteState::is_uninitialized_no_deser(account.data()) {
|
if account.lamports() != 0 && VoteState::is_correct_size_and_initialized(account.data())
|
||||||
|
{
|
||||||
let stake = old.as_ref().map_or_else(
|
let stake = old.as_ref().map_or_else(
|
||||||
|| self.calculate_stake(pubkey, self.epoch, Some(&self.stake_history)),
|
|| self.calculate_stake(pubkey, self.epoch, Some(&self.stake_history)),
|
||||||
|v| v.0,
|
|v| v.0,
|
||||||
|
Reference in New Issue
Block a user