solana_vote_interface/state/
vote_state_1_14_11.rs

1use super::*;
2#[cfg(feature = "dev-context-only-utils")]
3use arbitrary::Arbitrary;
4
5// Offset used for VoteState version 1_14_11
6const DEFAULT_PRIOR_VOTERS_OFFSET: usize = 82;
7
8#[cfg_attr(
9    feature = "frozen-abi",
10    solana_frozen_abi_macro::frozen_abi(digest = "HF4NfshaLg9e93RURYWTJRowtRrpLf5mWiF4G2Gnfu2r"),
11    derive(solana_frozen_abi_macro::AbiExample)
12)]
13#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
14#[derive(Debug, Default, PartialEq, Eq, Clone)]
15#[cfg_attr(feature = "dev-context-only-utils", derive(Arbitrary))]
16pub struct VoteState1_14_11 {
17    /// the node that votes in this account
18    pub node_pubkey: Pubkey,
19
20    /// the signer for withdrawals
21    pub authorized_withdrawer: Pubkey,
22    /// percentage (0-100) that represents what part of a rewards
23    ///  payout should be given to this VoteAccount
24    pub commission: u8,
25
26    pub votes: VecDeque<Lockout>,
27
28    // This usually the last Lockout which was popped from self.votes.
29    // However, it can be arbitrary slot, when being used inside Tower
30    pub root_slot: Option<Slot>,
31
32    /// the signer for vote transactions
33    pub authorized_voters: AuthorizedVoters,
34
35    /// history of prior authorized voters and the epochs for which
36    /// they were set, the bottom end of the range is inclusive,
37    /// the top of the range is exclusive
38    pub prior_voters: CircBuf<(Pubkey, Epoch, Epoch)>,
39
40    /// history of how many credits earned by the end of each epoch
41    ///  each tuple is (Epoch, credits, prev_credits)
42    pub epoch_credits: Vec<(Epoch, u64, u64)>,
43
44    /// most recent timestamp submitted with a vote
45    pub last_timestamp: BlockTimestamp,
46}
47
48impl VoteState1_14_11 {
49    pub fn get_rent_exempt_reserve(rent: &Rent) -> u64 {
50        rent.minimum_balance(Self::size_of())
51    }
52
53    /// Upper limit on the size of the Vote State
54    /// when votes.len() is MAX_LOCKOUT_HISTORY.
55    pub fn size_of() -> usize {
56        3731 // see test_vote_state_size_of
57    }
58
59    pub fn is_correct_size_and_initialized(data: &[u8]) -> bool {
60        const VERSION_OFFSET: usize = 4;
61        const DEFAULT_PRIOR_VOTERS_END: usize = VERSION_OFFSET + DEFAULT_PRIOR_VOTERS_OFFSET;
62        data.len() == VoteState1_14_11::size_of()
63            && data[VERSION_OFFSET..DEFAULT_PRIOR_VOTERS_END] != [0; DEFAULT_PRIOR_VOTERS_OFFSET]
64    }
65}
66
67impl From<VoteState> for VoteState1_14_11 {
68    fn from(vote_state: VoteState) -> Self {
69        Self {
70            node_pubkey: vote_state.node_pubkey,
71            authorized_withdrawer: vote_state.authorized_withdrawer,
72            commission: vote_state.commission,
73            votes: vote_state
74                .votes
75                .into_iter()
76                .map(|landed_vote| landed_vote.into())
77                .collect(),
78            root_slot: vote_state.root_slot,
79            authorized_voters: vote_state.authorized_voters,
80            prior_voters: vote_state.prior_voters,
81            epoch_credits: vote_state.epoch_credits,
82            last_timestamp: vote_state.last_timestamp,
83        }
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use {super::*, core::mem::MaybeUninit};
90
91    #[test]
92    fn test_vote_deserialize_1_14_11() {
93        // base case
94        let target_vote_state = VoteState1_14_11::default();
95        let target_vote_state_versions = VoteStateVersions::V1_14_11(Box::new(target_vote_state));
96        let vote_state_buf = bincode::serialize(&target_vote_state_versions).unwrap();
97
98        let mut test_vote_state = MaybeUninit::uninit();
99        VoteState::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap();
100        let test_vote_state = unsafe { test_vote_state.assume_init() };
101
102        assert_eq!(
103            target_vote_state_versions.convert_to_current(),
104            test_vote_state
105        );
106
107        // variant
108        // provide 4x the minimum struct size in bytes to ensure we typically touch every field
109        let struct_bytes_x4 = std::mem::size_of::<VoteState1_14_11>() * 4;
110        for _ in 0..1000 {
111            let raw_data: Vec<u8> = (0..struct_bytes_x4).map(|_| rand::random::<u8>()).collect();
112            let mut unstructured = Unstructured::new(&raw_data);
113
114            let arbitrary_vote_state = VoteState1_14_11::arbitrary(&mut unstructured).unwrap();
115            let target_vote_state_versions =
116                VoteStateVersions::V1_14_11(Box::new(arbitrary_vote_state));
117
118            let vote_state_buf = bincode::serialize(&target_vote_state_versions).unwrap();
119            let target_vote_state = target_vote_state_versions.convert_to_current();
120
121            let mut test_vote_state = MaybeUninit::uninit();
122            VoteState::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap();
123            let test_vote_state = unsafe { test_vote_state.assume_init() };
124
125            assert_eq!(target_vote_state, test_vote_state);
126        }
127    }
128}