* Refactor vote state to remove double negative (#21244)
(cherry picked from commit ef29d2d172
)
# Conflicts:
# runtime/src/stakes.rs
* resolve conflicts
Co-authored-by: Justin Starry <justin@solana.com>
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]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2082,25 +2082,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(
|
||||||
@ -2113,6 +2119,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
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -203,8 +203,10 @@ 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.
|
||||||
|
let vote_init_check_disabled = !check_vote_init;
|
||||||
if account.lamports() != 0
|
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(
|
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)),
|
||||||
|
Reference in New Issue
Block a user