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