Skip to main content

solana_vote_interface/state/
vote_state_1_14_11.rs

1use super::*;
2#[cfg(feature = "dev-context-only-utils")]
3use arbitrary::Arbitrary;
4#[cfg(feature = "frozen-abi")]
5use solana_frozen_abi_macro::{frozen_abi, AbiExample, StableAbi, StableAbiSample};
6
7// Offset used for VoteState version 1_14_11
8const DEFAULT_PRIOR_VOTERS_OFFSET: usize = 82;
9
10#[cfg_attr(
11    feature = "frozen-abi",
12    frozen_abi(
13        api_digest = "2rjXSWaNeAdoUNJDC5otC7NPR1qXHvLMuAs5faE4DPEt",
14        abi_digest = "3zLH9BFk2HeY4UTTWY82p6z4v9gNyyGkH3qZYWeGP2Fw",
15        abi_serializer = ["bincode", "wincode"]
16    ),
17    derive(AbiExample, StableAbi, StableAbiSample)
18)]
19#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
20#[cfg_attr(feature = "wincode", derive(wincode::SchemaWrite, wincode::SchemaRead))]
21#[derive(Debug, Default, PartialEq, Eq, Clone)]
22#[cfg_attr(feature = "dev-context-only-utils", derive(Arbitrary))]
23pub struct VoteState1_14_11 {
24    /// the node that votes in this account
25    pub node_pubkey: Pubkey,
26
27    /// the signer for withdrawals
28    pub authorized_withdrawer: Pubkey,
29    /// percentage (0-100) that represents what part of a rewards
30    ///  payout should be given to this VoteAccount
31    pub commission: u8,
32
33    pub votes: VecDeque<Lockout>,
34
35    // This usually the last Lockout which was popped from self.votes.
36    // However, it can be arbitrary slot, when being used inside Tower
37    pub root_slot: Option<Slot>,
38
39    /// the signer for vote transactions
40    pub authorized_voters: AuthorizedVoters,
41
42    /// history of prior authorized voters and the epochs for which
43    /// they were set, the bottom end of the range is inclusive,
44    /// the top of the range is exclusive
45    pub prior_voters: CircBuf<(Pubkey, Epoch, Epoch)>,
46
47    /// history of how many credits earned by the end of each epoch
48    ///  each tuple is (Epoch, credits, prev_credits)
49    pub epoch_credits: Vec<(Epoch, u64, u64)>,
50
51    /// most recent timestamp submitted with a vote
52    pub last_timestamp: BlockTimestamp,
53}
54
55impl VoteState1_14_11 {
56    #[deprecated(
57        since = "5.1.0",
58        note = "Use `rent.minimum_balance(VoteState1_14_11::size_of())` directly"
59    )]
60    pub fn get_rent_exempt_reserve(rent: &Rent) -> u64 {
61        rent.minimum_balance(Self::size_of())
62    }
63
64    /// Upper limit on the size of the Vote State
65    /// when votes.len() is MAX_LOCKOUT_HISTORY.
66    pub fn size_of() -> usize {
67        3731 // see test_vote_state_size_of
68    }
69
70    pub fn is_uninitialized(&self) -> bool {
71        self.authorized_voters.is_empty()
72    }
73
74    pub fn is_correct_size_and_initialized(data: &[u8]) -> bool {
75        const VERSION_OFFSET: usize = 4;
76        const DEFAULT_PRIOR_VOTERS_END: usize = VERSION_OFFSET + DEFAULT_PRIOR_VOTERS_OFFSET;
77        data.len() == VoteState1_14_11::size_of()
78            && data[VERSION_OFFSET..DEFAULT_PRIOR_VOTERS_END] != [0; DEFAULT_PRIOR_VOTERS_OFFSET]
79    }
80}
81
82impl From<VoteStateV3> for VoteState1_14_11 {
83    fn from(vote_state: VoteStateV3) -> Self {
84        Self {
85            node_pubkey: vote_state.node_pubkey,
86            authorized_withdrawer: vote_state.authorized_withdrawer,
87            commission: vote_state.commission,
88            votes: vote_state
89                .votes
90                .into_iter()
91                .map(|landed_vote| landed_vote.into())
92                .collect(),
93            root_slot: vote_state.root_slot,
94            authorized_voters: vote_state.authorized_voters,
95            prior_voters: vote_state.prior_voters,
96            epoch_credits: vote_state.epoch_credits,
97            last_timestamp: vote_state.last_timestamp,
98        }
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use {super::*, arbitrary::Unstructured, core::mem::MaybeUninit};
105
106    #[test]
107    fn test_vote_deserialize_1_14_11() {
108        // base case
109        let target_vote_state = VoteState1_14_11::default();
110        let target_vote_state_versions = VoteStateVersions::V1_14_11(Box::new(target_vote_state));
111        let vote_state_buf = bincode::serialize(&target_vote_state_versions).unwrap();
112
113        // v3
114        let mut test_vote_state_v3 = MaybeUninit::uninit();
115        VoteStateV3::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state_v3).unwrap();
116        let test_vote_state = unsafe { test_vote_state_v3.assume_init() };
117
118        assert_eq!(
119            target_vote_state_versions
120                .clone()
121                .try_convert_to_v3()
122                .unwrap(),
123            test_vote_state
124        );
125
126        // v4
127        let vote_pubkey = Pubkey::new_unique();
128        let mut test_vote_state_v4 = MaybeUninit::uninit();
129        VoteStateV4::deserialize_into_uninit(
130            &vote_state_buf,
131            &mut test_vote_state_v4,
132            &vote_pubkey,
133        )
134        .unwrap();
135        let test_vote_state = unsafe { test_vote_state_v4.assume_init() };
136
137        assert_eq!(
138            target_vote_state_versions
139                .try_convert_to_v4(&vote_pubkey)
140                .unwrap(),
141            test_vote_state
142        );
143
144        // variant
145        // provide 4x the minimum struct size in bytes to ensure we typically touch every field
146        let struct_bytes_x4 = std::mem::size_of::<VoteState1_14_11>() * 4;
147        for _ in 0..1000 {
148            let raw_data: Vec<u8> = (0..struct_bytes_x4).map(|_| rand::random::<u8>()).collect();
149            let mut unstructured = Unstructured::new(&raw_data);
150
151            let arbitrary_vote_state = VoteState1_14_11::arbitrary(&mut unstructured).unwrap();
152            let target_vote_state_versions =
153                VoteStateVersions::V1_14_11(Box::new(arbitrary_vote_state));
154
155            // v3
156            let vote_state_buf = bincode::serialize(&target_vote_state_versions).unwrap();
157            let target_vote_state_v3 = target_vote_state_versions
158                .clone()
159                .try_convert_to_v3()
160                .unwrap();
161
162            let mut test_vote_state_v3 = MaybeUninit::uninit();
163            VoteStateV3::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state_v3).unwrap();
164            let test_vote_state = unsafe { test_vote_state_v3.assume_init() };
165
166            assert_eq!(target_vote_state_v3, test_vote_state);
167
168            // v4
169            let vote_pubkey = Pubkey::new_unique();
170            let target_vote_state_v4 = target_vote_state_versions
171                .try_convert_to_v4(&vote_pubkey)
172                .unwrap();
173
174            let mut test_vote_state_v4 = MaybeUninit::uninit();
175            VoteStateV4::deserialize_into_uninit(
176                &vote_state_buf,
177                &mut test_vote_state_v4,
178                &vote_pubkey,
179            )
180            .unwrap();
181            let test_vote_state = unsafe { test_vote_state_v4.assume_init() };
182
183            assert_eq!(target_vote_state_v4, test_vote_state);
184        }
185    }
186}