Skip to content

Commit

Permalink
make the uninitialized code nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
2501babe committed Feb 7, 2024
1 parent 992cd7b commit 24870e9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions sdk/program/src/vote/authorized_voters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ impl AuthorizedVoters {
self.authorized_voters.is_empty()
}

// when an uninitialized V0_23_5 account is converted to current, it inserts a null voter
pub fn is_uninitialized(&self) -> bool {
self.is_empty() || (self.len() == 1 && self.first() == Some((&0, &Pubkey::default())))
}

pub fn first(&self) -> Option<(&u64, &Pubkey)> {
self.authorized_voters.iter().next()
}
Expand Down
5 changes: 1 addition & 4 deletions sdk/program/src/vote/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,8 @@ impl VoteState {
&& data[VERSION_OFFSET..DEFAULT_PRIOR_VOTERS_END] != [0; DEFAULT_PRIOR_VOTERS_OFFSET]
}

// HANA do something nicer than this... lol
pub fn is_uninitialized(&self) -> bool {
self.authorized_voters.is_empty()
|| (self.authorized_voters.len() == 1
&& self.authorized_voters.first() == Some((&0, &Pubkey::default())))
self.authorized_voters.is_uninitialized()
}
}

Expand Down
4 changes: 4 additions & 0 deletions sdk/program/src/vote/state/vote_state_1_14_11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl VoteState1_14_11 {
data.len() == VoteState1_14_11::size_of()
&& data[VERSION_OFFSET..DEFAULT_PRIOR_VOTERS_END] != [0; DEFAULT_PRIOR_VOTERS_OFFSET]
}

pub fn is_uninitialized(&self) -> bool {
self.authorized_voters.is_uninitialized()
}
}

impl From<VoteState> for VoteState1_14_11 {
Expand Down
4 changes: 2 additions & 2 deletions sdk/program/src/vote/state/vote_state_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ impl VoteStateVersions {
vote_state.authorized_voter == Pubkey::default()
}

VoteStateVersions::V1_14_11(vote_state) => vote_state.authorized_voters.is_empty(),
VoteStateVersions::V1_14_11(vote_state) => vote_state.is_uninitialized(),

VoteStateVersions::Current(vote_state) => vote_state.authorized_voters.is_empty(),
VoteStateVersions::Current(vote_state) => vote_state.is_uninitialized(),
}
}

Expand Down

0 comments on commit 24870e9

Please sign in to comment.