solana_program/vote/state/
vote_state_versions.rs

1use super::{vote_state_0_23_5::VoteState0_23_5, vote_state_1_14_11::VoteState1_14_11, *};
2#[cfg(test)]
3use arbitrary::{Arbitrary, Unstructured};
4
5#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
6pub enum VoteStateVersions {
7    V0_23_5(Box<VoteState0_23_5>),
8    V1_14_11(Box<VoteState1_14_11>),
9    Current(Box<VoteState>),
10}
11
12impl VoteStateVersions {
13    pub fn new_current(vote_state: VoteState) -> Self {
14        Self::Current(Box::new(vote_state))
15    }
16
17    pub fn convert_to_current(self) -> VoteState {
18        match self {
19            VoteStateVersions::V0_23_5(state) => {
20                let authorized_voters =
21                    AuthorizedVoters::new(state.authorized_voter_epoch, state.authorized_voter);
22
23                VoteState {
24                    node_pubkey: state.node_pubkey,
25
26                    authorized_withdrawer: state.authorized_withdrawer,
27
28                    commission: state.commission,
29
30                    votes: Self::landed_votes_from_lockouts(state.votes),
31
32                    root_slot: state.root_slot,
33
34                    authorized_voters,
35
36                    prior_voters: CircBuf::default(),
37
38                    epoch_credits: state.epoch_credits.clone(),
39
40                    last_timestamp: state.last_timestamp.clone(),
41                }
42            }
43
44            VoteStateVersions::V1_14_11(state) => VoteState {
45                node_pubkey: state.node_pubkey,
46                authorized_withdrawer: state.authorized_withdrawer,
47                commission: state.commission,
48
49                votes: Self::landed_votes_from_lockouts(state.votes),
50
51                root_slot: state.root_slot,
52
53                authorized_voters: state.authorized_voters.clone(),
54
55                prior_voters: state.prior_voters,
56
57                epoch_credits: state.epoch_credits,
58
59                last_timestamp: state.last_timestamp,
60            },
61
62            VoteStateVersions::Current(state) => *state,
63        }
64    }
65
66    fn landed_votes_from_lockouts(lockouts: VecDeque<Lockout>) -> VecDeque<LandedVote> {
67        lockouts.into_iter().map(|lockout| lockout.into()).collect()
68    }
69
70    pub fn is_uninitialized(&self) -> bool {
71        match self {
72            VoteStateVersions::V0_23_5(vote_state) => {
73                vote_state.authorized_voter == Pubkey::default()
74            }
75
76            VoteStateVersions::V1_14_11(vote_state) => vote_state.authorized_voters.is_empty(),
77
78            VoteStateVersions::Current(vote_state) => vote_state.authorized_voters.is_empty(),
79        }
80    }
81
82    pub fn vote_state_size_of(is_current: bool) -> usize {
83        if is_current {
84            VoteState::size_of()
85        } else {
86            VoteState1_14_11::size_of()
87        }
88    }
89
90    pub fn is_correct_size_and_initialized(data: &[u8]) -> bool {
91        VoteState::is_correct_size_and_initialized(data)
92            || VoteState1_14_11::is_correct_size_and_initialized(data)
93    }
94}
95
96#[cfg(test)]
97impl Arbitrary<'_> for VoteStateVersions {
98    fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
99        let variant = u.choose_index(2)?;
100        match variant {
101            0 => Ok(Self::Current(Box::new(VoteState::arbitrary(u)?))),
102            1 => Ok(Self::V1_14_11(Box::new(VoteState1_14_11::arbitrary(u)?))),
103            _ => unreachable!(),
104        }
105    }
106}