From 121153b7a2debac4546b97e1dc3070ae3742c4b9 Mon Sep 17 00:00:00 2001 From: Joe C Date: Fri, 11 Jul 2025 11:02:52 +0800 Subject: [PATCH] vote-interface: rename `VoteState` to `VoteStateV3` (#221) * vote-interface: rename `VoteState` to `VoteStateV3` * add deprecatio attr (cherry picked from commit 72ae3e2316cbf3be2c74a73863c4d3da6270203f) --- vote-interface/src/state/mod.rs | 114 +++++++++--------- vote-interface/src/state/vote_state_0_23_5.rs | 4 +- .../src/state/vote_state_1_14_11.rs | 8 +- vote-interface/src/state/vote_state_v3.rs | 68 +++++------ .../src/state/vote_state_versions.rs | 18 +-- 5 files changed, 108 insertions(+), 104 deletions(-) diff --git a/vote-interface/src/state/mod.rs b/vote-interface/src/state/mod.rs index e98ecfdd6..6ba862c2e 100644 --- a/vote-interface/src/state/mod.rs +++ b/vote-interface/src/state/mod.rs @@ -22,9 +22,12 @@ pub use vote_state_1_14_11::*; pub mod vote_state_versions; pub use vote_state_versions::*; pub mod vote_state_v3; -pub use vote_state_v3::VoteState; +pub use vote_state_v3::VoteStateV3; mod vote_instruction_data; pub use vote_instruction_data::*; +// The struct's name has changed. +#[deprecated(since = "2.2.6", note = "Use vote_state_v3::VoteStateV3 instead")] +pub use vote_state_v3::VoteStateV3 as VoteState; // Maximum number of votes to keep around, tightly coupled with epoch_schedule::MINIMUM_SLOTS_PER_EPOCH pub const MAX_LOCKOUT_HISTORY: usize = 31; @@ -396,17 +399,17 @@ mod tests { #[test] fn test_vote_serialize() { - let mut buffer: Vec = vec![0; VoteState::size_of()]; - let mut vote_state = VoteState::default(); + let mut buffer: Vec = vec![0; VoteStateV3::size_of()]; + let mut vote_state = VoteStateV3::default(); vote_state .votes .resize(MAX_LOCKOUT_HISTORY, LandedVote::default()); vote_state.root_slot = Some(1); let versioned = VoteStateVersions::new_current(vote_state); - assert!(VoteState::serialize(&versioned, &mut buffer[0..4]).is_err()); - VoteState::serialize(&versioned, &mut buffer).unwrap(); + assert!(VoteStateV3::serialize(&versioned, &mut buffer[0..4]).is_err()); + VoteStateV3::serialize(&versioned, &mut buffer).unwrap(); assert_eq!( - VoteState::deserialize(&buffer).unwrap(), + VoteStateV3::deserialize(&buffer).unwrap(), versioned.convert_to_current() ); } @@ -414,18 +417,18 @@ mod tests { #[test] fn test_vote_deserialize_into() { // base case - let target_vote_state = VoteState::default(); + let target_vote_state = VoteStateV3::default(); let vote_state_buf = bincode::serialize(&VoteStateVersions::new_current(target_vote_state.clone())).unwrap(); - let mut test_vote_state = VoteState::default(); - VoteState::deserialize_into(&vote_state_buf, &mut test_vote_state).unwrap(); + let mut test_vote_state = VoteStateV3::default(); + VoteStateV3::deserialize_into(&vote_state_buf, &mut test_vote_state).unwrap(); assert_eq!(target_vote_state, test_vote_state); // variant // provide 4x the minimum struct size in bytes to ensure we typically touch every field - let struct_bytes_x4 = std::mem::size_of::() * 4; + let struct_bytes_x4 = std::mem::size_of::() * 4; for _ in 0..1000 { let raw_data: Vec = (0..struct_bytes_x4).map(|_| rand::random::()).collect(); let mut unstructured = Unstructured::new(&raw_data); @@ -435,8 +438,8 @@ mod tests { let vote_state_buf = bincode::serialize(&target_vote_state_versions).unwrap(); let target_vote_state = target_vote_state_versions.convert_to_current(); - let mut test_vote_state = VoteState::default(); - VoteState::deserialize_into(&vote_state_buf, &mut test_vote_state).unwrap(); + let mut test_vote_state = VoteStateV3::default(); + VoteStateV3::deserialize_into(&vote_state_buf, &mut test_vote_state).unwrap(); assert_eq!(target_vote_state, test_vote_state); } @@ -444,33 +447,33 @@ mod tests { #[test] fn test_vote_deserialize_into_error() { - let target_vote_state = VoteState::new_rand_for_tests(Pubkey::new_unique(), 42); + let target_vote_state = VoteStateV3::new_rand_for_tests(Pubkey::new_unique(), 42); let mut vote_state_buf = bincode::serialize(&VoteStateVersions::new_current(target_vote_state.clone())).unwrap(); let len = vote_state_buf.len(); vote_state_buf.truncate(len - 1); - let mut test_vote_state = VoteState::default(); - VoteState::deserialize_into(&vote_state_buf, &mut test_vote_state).unwrap_err(); - assert_eq!(test_vote_state, VoteState::default()); + let mut test_vote_state = VoteStateV3::default(); + VoteStateV3::deserialize_into(&vote_state_buf, &mut test_vote_state).unwrap_err(); + assert_eq!(test_vote_state, VoteStateV3::default()); } #[test] fn test_vote_deserialize_into_uninit() { // base case - let target_vote_state = VoteState::default(); + let target_vote_state = VoteStateV3::default(); let vote_state_buf = bincode::serialize(&VoteStateVersions::new_current(target_vote_state.clone())).unwrap(); let mut test_vote_state = MaybeUninit::uninit(); - VoteState::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); + VoteStateV3::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); let test_vote_state = unsafe { test_vote_state.assume_init() }; assert_eq!(target_vote_state, test_vote_state); // variant // provide 4x the minimum struct size in bytes to ensure we typically touch every field - let struct_bytes_x4 = std::mem::size_of::() * 4; + let struct_bytes_x4 = std::mem::size_of::() * 4; for _ in 0..1000 { let raw_data: Vec = (0..struct_bytes_x4).map(|_| rand::random::()).collect(); let mut unstructured = Unstructured::new(&raw_data); @@ -481,7 +484,7 @@ mod tests { let target_vote_state = target_vote_state_versions.convert_to_current(); let mut test_vote_state = MaybeUninit::uninit(); - VoteState::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); + VoteStateV3::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); let test_vote_state = unsafe { test_vote_state.assume_init() }; assert_eq!(target_vote_state, test_vote_state); @@ -492,11 +495,11 @@ mod tests { fn test_vote_deserialize_into_uninit_nopanic() { // base case let mut test_vote_state = MaybeUninit::uninit(); - let e = VoteState::deserialize_into_uninit(&[], &mut test_vote_state).unwrap_err(); + let e = VoteStateV3::deserialize_into_uninit(&[], &mut test_vote_state).unwrap_err(); assert_eq!(e, InstructionError::InvalidAccountData); // variant - let serialized_len_x4 = serialized_size(&VoteState::default()).unwrap() * 4; + let serialized_len_x4 = serialized_size(&VoteStateV3::default()).unwrap() * 4; let mut rng = rand::thread_rng(); for _ in 0..1000 { let raw_data_length = rng.gen_range(1..serialized_len_x4); @@ -514,7 +517,7 @@ mod tests { // it is extremely improbable, though theoretically possible, for random bytes to be syntactically valid // so we only check that the parser does not panic and that it succeeds or fails exactly in line with bincode let mut test_vote_state = MaybeUninit::uninit(); - let test_res = VoteState::deserialize_into_uninit(&raw_data, &mut test_vote_state); + let test_res = VoteStateV3::deserialize_into_uninit(&raw_data, &mut test_vote_state); let bincode_res = bincode::deserialize::(&raw_data) .map(|versioned| versioned.convert_to_current()); @@ -530,7 +533,7 @@ mod tests { #[test] fn test_vote_deserialize_into_uninit_ill_sized() { // provide 4x the minimum struct size in bytes to ensure we typically touch every field - let struct_bytes_x4 = std::mem::size_of::() * 4; + let struct_bytes_x4 = std::mem::size_of::() * 4; for _ in 0..1000 { let raw_data: Vec = (0..struct_bytes_x4).map(|_| rand::random::()).collect(); let mut unstructured = Unstructured::new(&raw_data); @@ -547,7 +550,8 @@ mod tests { // truncated fails let mut test_vote_state = MaybeUninit::uninit(); - let test_res = VoteState::deserialize_into_uninit(&truncated_buf, &mut test_vote_state); + let test_res = + VoteStateV3::deserialize_into_uninit(&truncated_buf, &mut test_vote_state); let bincode_res = bincode::deserialize::(&truncated_buf) .map(|versioned| versioned.convert_to_current()); @@ -556,7 +560,7 @@ mod tests { // expanded succeeds let mut test_vote_state = MaybeUninit::uninit(); - VoteState::deserialize_into_uninit(&expanded_buf, &mut test_vote_state).unwrap(); + VoteStateV3::deserialize_into_uninit(&expanded_buf, &mut test_vote_state).unwrap(); let bincode_res = bincode::deserialize::(&expanded_buf) .map(|versioned| versioned.convert_to_current()); @@ -568,13 +572,13 @@ mod tests { #[test] #[allow(deprecated)] fn test_vote_state_commission_split() { - let vote_state = VoteState::default(); + let vote_state = VoteStateV3::default(); assert_eq!(vote_state.commission_split(1), (0, 1, false)); - let mut vote_state = VoteState { + let mut vote_state = VoteStateV3 { commission: u8::MAX, - ..VoteState::default() + ..VoteStateV3::default() }; assert_eq!(vote_state.commission_split(1), (1, 0, false)); @@ -592,7 +596,7 @@ mod tests { #[test] fn test_vote_state_epoch_credits() { - let mut vote_state = VoteState::default(); + let mut vote_state = VoteStateV3::default(); assert_eq!(vote_state.credits(), 0); assert_eq!(vote_state.epoch_credits().clone(), vec![]); @@ -618,7 +622,7 @@ mod tests { #[test] fn test_vote_state_epoch0_no_credits() { - let mut vote_state = VoteState::default(); + let mut vote_state = VoteStateV3::default(); assert_eq!(vote_state.epoch_credits().len(), 0); vote_state.increment_credits(1, 1); @@ -630,7 +634,7 @@ mod tests { #[test] fn test_vote_state_increment_credits() { - let mut vote_state = VoteState::default(); + let mut vote_state = VoteStateV3::default(); let credits = (MAX_EPOCH_CREDITS_HISTORY + 2) as u64; for i in 0..credits { @@ -643,9 +647,9 @@ mod tests { #[test] fn test_vote_process_timestamp() { let (slot, timestamp) = (15, 1_575_412_285); - let mut vote_state = VoteState { + let mut vote_state = VoteStateV3 { last_timestamp: BlockTimestamp { slot, timestamp }, - ..VoteState::default() + ..VoteStateV3::default() }; assert_eq!( @@ -697,7 +701,7 @@ mod tests { #[test] fn test_get_and_update_authorized_voter() { let original_voter = Pubkey::new_unique(); - let mut vote_state = VoteState::new( + let mut vote_state = VoteStateV3::new( &VoteInit { node_pubkey: original_voter, authorized_voter: original_voter, @@ -764,7 +768,7 @@ mod tests { fn test_set_new_authorized_voter() { let original_voter = Pubkey::new_unique(); let epoch_offset = 15; - let mut vote_state = VoteState::new( + let mut vote_state = VoteStateV3::new( &VoteInit { node_pubkey: original_voter, authorized_voter: original_voter, @@ -862,7 +866,7 @@ mod tests { #[test] fn test_authorized_voter_is_locked_within_epoch() { let original_voter = Pubkey::new_unique(); - let mut vote_state = VoteState::new( + let mut vote_state = VoteStateV3::new( &VoteInit { node_pubkey: original_voter, authorized_voter: original_voter, @@ -902,16 +906,16 @@ mod tests { #[test] fn test_vote_state_size_of() { - let vote_state = VoteState::get_max_sized_vote_state(); + let vote_state = VoteStateV3::get_max_sized_vote_state(); let vote_state = VoteStateVersions::new_current(vote_state); let size = serialized_size(&vote_state).unwrap(); - assert_eq!(VoteState::size_of() as u64, size); + assert_eq!(VoteStateV3::size_of() as u64, size); } #[test] fn test_vote_state_max_size() { - let mut max_sized_data = vec![0; VoteState::size_of()]; - let vote_state = VoteState::get_max_sized_vote_state(); + let mut max_sized_data = vec![0; VoteStateV3::size_of()]; + let vote_state = VoteStateV3::get_max_sized_vote_state(); let (start_leader_schedule_epoch, _) = vote_state.authorized_voters.last().unwrap(); let start_current_epoch = start_leader_schedule_epoch - MAX_LEADER_SCHEDULE_EPOCH_OFFSET + 1; @@ -928,17 +932,17 @@ mod tests { }); let versioned = VoteStateVersions::new_current(vote_state.take().unwrap()); - VoteState::serialize(&versioned, &mut max_sized_data).unwrap(); + VoteStateV3::serialize(&versioned, &mut max_sized_data).unwrap(); vote_state = Some(versioned.convert_to_current()); } } #[test] fn test_default_vote_state_is_uninitialized() { - // The default `VoteState` is stored to de-initialize a zero-balance vote account, + // The default `VoteStateV3` is stored to de-initialize a zero-balance vote account, // so must remain such that `VoteStateVersions::is_uninitialized()` returns true // when called on a `VoteStateVersions` that stores it - assert!(VoteStateVersions::new_current(VoteState::default()).is_uninitialized()); + assert!(VoteStateVersions::new_current(VoteStateV3::default()).is_uninitialized()); } #[test] @@ -949,9 +953,9 @@ mod tests { &vote_account_data )); - // Check default VoteState - let default_account_state = VoteStateVersions::new_current(VoteState::default()); - VoteState::serialize(&default_account_state, &mut vote_account_data).unwrap(); + // Check default VoteStateV3 + let default_account_state = VoteStateVersions::new_current(VoteStateV3::default()); + VoteStateV3::serialize(&default_account_state, &mut vote_account_data).unwrap(); assert!(!VoteStateVersions::is_correct_size_and_initialized( &vote_account_data )); @@ -964,14 +968,14 @@ mod tests { // Check non-zero large account let mut large_vote_data = vec![1; 2 * VoteStateVersions::vote_state_size_of(true)]; - let default_account_state = VoteStateVersions::new_current(VoteState::default()); - VoteState::serialize(&default_account_state, &mut large_vote_data).unwrap(); + let default_account_state = VoteStateVersions::new_current(VoteStateV3::default()); + VoteStateV3::serialize(&default_account_state, &mut large_vote_data).unwrap(); assert!(!VoteStateVersions::is_correct_size_and_initialized( &vote_account_data )); - // Check populated VoteState - let vote_state = VoteState::new( + // Check populated VoteStateV3 + let vote_state = VoteStateV3::new( &VoteInit { node_pubkey: Pubkey::new_unique(), authorized_voter: Pubkey::new_unique(), @@ -981,16 +985,16 @@ mod tests { &Clock::default(), ); let account_state = VoteStateVersions::new_current(vote_state.clone()); - VoteState::serialize(&account_state, &mut vote_account_data).unwrap(); + VoteStateV3::serialize(&account_state, &mut vote_account_data).unwrap(); assert!(VoteStateVersions::is_correct_size_and_initialized( &vote_account_data )); - // Check old VoteState that hasn't been upgraded to newest version yet + // Check old VoteStateV3 that hasn't been upgraded to newest version yet let old_vote_state = VoteState1_14_11::from(vote_state); let account_state = VoteStateVersions::V1_14_11(Box::new(old_vote_state)); let mut vote_account_data = vec![0; VoteStateVersions::vote_state_size_of(false)]; - VoteState::serialize(&account_state, &mut vote_account_data).unwrap(); + VoteStateV3::serialize(&account_state, &mut vote_account_data).unwrap(); assert!(VoteStateVersions::is_correct_size_and_initialized( &vote_account_data )); @@ -999,7 +1003,7 @@ mod tests { #[test] fn test_minimum_balance() { let rent = solana_rent::Rent::default(); - let minimum_balance = rent.minimum_balance(VoteState::size_of()); + let minimum_balance = rent.minimum_balance(VoteStateV3::size_of()); // golden, may need updating when vote_state grows assert!(minimum_balance as f64 / 10f64.powf(9.0) < 0.04) } diff --git a/vote-interface/src/state/vote_state_0_23_5.rs b/vote-interface/src/state/vote_state_0_23_5.rs index c3977ae5d..20fefc7f9 100644 --- a/vote-interface/src/state/vote_state_0_23_5.rs +++ b/vote-interface/src/state/vote_state_0_23_5.rs @@ -78,7 +78,7 @@ mod tests { let vote_state_buf = bincode::serialize(&target_vote_state_versions).unwrap(); let mut test_vote_state = MaybeUninit::uninit(); - VoteState::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); + VoteStateV3::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); let test_vote_state = unsafe { test_vote_state.assume_init() }; assert_eq!( @@ -101,7 +101,7 @@ mod tests { let target_vote_state = target_vote_state_versions.convert_to_current(); let mut test_vote_state = MaybeUninit::uninit(); - VoteState::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); + VoteStateV3::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); let test_vote_state = unsafe { test_vote_state.assume_init() }; assert_eq!(target_vote_state, test_vote_state); diff --git a/vote-interface/src/state/vote_state_1_14_11.rs b/vote-interface/src/state/vote_state_1_14_11.rs index 4f8784e13..250ba8bd0 100644 --- a/vote-interface/src/state/vote_state_1_14_11.rs +++ b/vote-interface/src/state/vote_state_1_14_11.rs @@ -64,8 +64,8 @@ impl VoteState1_14_11 { } } -impl From for VoteState1_14_11 { - fn from(vote_state: VoteState) -> Self { +impl From for VoteState1_14_11 { + fn from(vote_state: VoteStateV3) -> Self { Self { node_pubkey: vote_state.node_pubkey, authorized_withdrawer: vote_state.authorized_withdrawer, @@ -96,7 +96,7 @@ mod tests { let vote_state_buf = bincode::serialize(&target_vote_state_versions).unwrap(); let mut test_vote_state = MaybeUninit::uninit(); - VoteState::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); + VoteStateV3::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); let test_vote_state = unsafe { test_vote_state.assume_init() }; assert_eq!( @@ -119,7 +119,7 @@ mod tests { let target_vote_state = target_vote_state_versions.convert_to_current(); let mut test_vote_state = MaybeUninit::uninit(); - VoteState::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); + VoteStateV3::deserialize_into_uninit(&vote_state_buf, &mut test_vote_state).unwrap(); let test_vote_state = unsafe { test_vote_state.assume_init() }; assert_eq!(target_vote_state, test_vote_state); diff --git a/vote-interface/src/state/vote_state_v3.rs b/vote-interface/src/state/vote_state_v3.rs index c12ee6a24..d893be49c 100644 --- a/vote-interface/src/state/vote_state_v3.rs +++ b/vote-interface/src/state/vote_state_v3.rs @@ -23,13 +23,13 @@ use { #[cfg_attr( feature = "frozen-abi", - frozen_abi(digest = "BRwozbypfYXsHqFVj9w3iH5x1ak2NWHqCCn6pr3gHBkG"), + frozen_abi(digest = "4cUA9matKNYX1R9TceL4D14w23AfLXVjhZaStwuermQX"), derive(AbiExample) )] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[derive(Debug, Default, PartialEq, Eq, Clone)] #[cfg_attr(feature = "dev-context-only-utils", derive(Arbitrary))] -pub struct VoteState { +pub struct VoteStateV3 { /// the node that votes in this account pub node_pubkey: Pubkey, @@ -61,14 +61,14 @@ pub struct VoteState { pub last_timestamp: BlockTimestamp, } -impl VoteState { +impl VoteStateV3 { pub fn new(vote_init: &VoteInit, clock: &Clock) -> Self { Self { node_pubkey: vote_init.node_pubkey, authorized_voters: AuthorizedVoters::new(clock.epoch, vote_init.authorized_voter), authorized_withdrawer: vote_init.authorized_withdrawer, commission: vote_init.commission, - ..VoteState::default() + ..VoteStateV3::default() } } @@ -86,7 +86,7 @@ impl VoteState { node_pubkey, root_slot: Some(root_slot), votes, - ..VoteState::default() + ..VoteStateV3::default() } } @@ -103,7 +103,7 @@ impl VoteState { } pub fn get_rent_exempt_reserve(rent: &Rent) -> u64 { - rent.minimum_balance(VoteState::size_of()) + rent.minimum_balance(VoteStateV3::size_of()) } /// Upper limit on the size of the Vote State @@ -113,7 +113,7 @@ impl VoteState { } // NOTE we retain `bincode::deserialize` for `not(target_os = "solana")` pending testing on mainnet-beta - // once that testing is done, `VoteState::deserialize_into` may be used for all targets + // once that testing is done, `VoteStateV3::deserialize_into` may be used for all targets // conversion of V0_23_5 to current must be handled specially, however // because it inserts a null voter into `authorized_voters` // which `VoteStateVersions::is_uninitialized` erroneously reports as initialized @@ -133,23 +133,23 @@ impl VoteState { } } - /// Deserializes the input `VoteStateVersions` buffer directly into the provided `VoteState`. + /// Deserializes the input `VoteStateVersions` buffer directly into the provided `VoteStateV3`. /// /// In a SBPF context, V0_23_5 is not supported, but in non-SBPF, all versions are supported for /// compatibility with `bincode::deserialize`. /// /// On success, `vote_state` reflects the state of the input data. On failure, `vote_state` is - /// reset to `VoteState::default()`. + /// reset to `VoteStateV3::default()`. #[cfg(any(target_os = "solana", feature = "bincode"))] pub fn deserialize_into( input: &[u8], - vote_state: &mut VoteState, + vote_state: &mut VoteStateV3, ) -> Result<(), InstructionError> { - // Rebind vote_state to *mut VoteState so that the &mut binding isn't + // Rebind vote_state to *mut VoteStateV3 so that the &mut binding isn't // accessible anymore, preventing accidental use after this point. // // NOTE: switch to ptr::from_mut() once platform-tools moves to rustc >= 1.76 - let vote_state = vote_state as *mut VoteState; + let vote_state = vote_state as *mut VoteStateV3; // Safety: vote_state is valid to_drop (see drop_in_place() docs). After // dropping, the pointer is treated as uninitialized and only accessed @@ -158,9 +158,9 @@ impl VoteState { std::ptr::drop_in_place(vote_state); } - // This is to reset vote_state to VoteState::default() if deserialize fails or panics. + // This is to reset vote_state to VoteStateV3::default() if deserialize fails or panics. struct DropGuard { - vote_state: *mut VoteState, + vote_state: *mut VoteStateV3, } impl Drop for DropGuard { @@ -169,20 +169,20 @@ impl VoteState { // // Deserialize failed or panicked so at this point vote_state is uninitialized. We // must write a new _valid_ value into it or after returning (or unwinding) from - // this function the caller is left with an uninitialized `&mut VoteState`, which is + // this function the caller is left with an uninitialized `&mut VoteStateV3`, which is // UB (references must always be valid). // // This is always safe and doesn't leak memory because deserialize_into_ptr() writes // into the fields that heap alloc only when it returns Ok(). unsafe { - self.vote_state.write(VoteState::default()); + self.vote_state.write(VoteStateV3::default()); } } } let guard = DropGuard { vote_state }; - let res = VoteState::deserialize_into_ptr(input, vote_state); + let res = VoteStateV3::deserialize_into_ptr(input, vote_state); if res.is_ok() { std::mem::forget(guard); } @@ -191,26 +191,26 @@ impl VoteState { } /// Deserializes the input `VoteStateVersions` buffer directly into the provided - /// `MaybeUninit`. + /// `MaybeUninit`. /// /// In a SBPF context, V0_23_5 is not supported, but in non-SBPF, all versions are supported for /// compatibility with `bincode::deserialize`. /// - /// On success, `vote_state` is fully initialized and can be converted to `VoteState` using + /// On success, `vote_state` is fully initialized and can be converted to `VoteStateV3` using /// [MaybeUninit::assume_init]. On failure, `vote_state` may still be uninitialized and must not - /// be converted to `VoteState`. + /// be converted to `VoteStateV3`. #[cfg(any(target_os = "solana", feature = "bincode"))] pub fn deserialize_into_uninit( input: &[u8], - vote_state: &mut std::mem::MaybeUninit, + vote_state: &mut std::mem::MaybeUninit, ) -> Result<(), InstructionError> { - VoteState::deserialize_into_ptr(input, vote_state.as_mut_ptr()) + VoteStateV3::deserialize_into_ptr(input, vote_state.as_mut_ptr()) } #[cfg(any(target_os = "solana", feature = "bincode"))] fn deserialize_into_ptr( input: &[u8], - vote_state: *mut VoteState, + vote_state: *mut VoteStateV3, ) -> Result<(), InstructionError> { use vote_state_deserialize::deserialize_vote_state_into; @@ -223,8 +223,8 @@ impl VoteState { 0 => { #[cfg(not(target_os = "solana"))] { - // Safety: vote_state is valid as it comes from `&mut MaybeUninit` or - // `&mut VoteState`. In the first case, the value is uninitialized so we write() + // Safety: vote_state is valid as it comes from `&mut MaybeUninit` or + // `&mut VoteStateV3`. In the first case, the value is uninitialized so we write() // to avoid dropping invalid data; in the latter case, we `drop_in_place()` // before writing so the value has already been dropped and we just write a new // one in place. @@ -303,14 +303,14 @@ impl VoteState { } #[cfg(test)] - pub(crate) fn get_max_sized_vote_state() -> VoteState { + pub(crate) fn get_max_sized_vote_state() -> VoteStateV3 { use solana_epoch_schedule::MAX_LEADER_SCHEDULE_EPOCH_OFFSET; let mut authorized_voters = AuthorizedVoters::default(); for i in 0..=MAX_LEADER_SCHEDULE_EPOCH_OFFSET { authorized_voters.insert(i, Pubkey::new_unique()); } - VoteState { + VoteStateV3 { votes: VecDeque::from(vec![LandedVote::default(); MAX_LOCKOUT_HISTORY]), root_slot: Some(u64::MAX), epoch_credits: vec![(0, 0, 0); MAX_EPOCH_CREDITS_HISTORY], @@ -454,7 +454,7 @@ impl VoteState { } /// Number of "credits" owed to this account from the mining pool. Submit this - /// VoteState to the Rewards program to trade credits for lamports. + /// VoteStateV3 to the Rewards program to trade credits for lamports. pub fn credits(&self) -> u64 { if self.epoch_credits.is_empty() { 0 @@ -592,7 +592,7 @@ impl VoteState { pub fn is_correct_size_and_initialized(data: &[u8]) -> bool { const VERSION_OFFSET: usize = 4; const DEFAULT_PRIOR_VOTERS_END: usize = VERSION_OFFSET + DEFAULT_PRIOR_VOTERS_OFFSET; - data.len() == VoteState::size_of() + data.len() == VoteStateV3::size_of() && data[VERSION_OFFSET..DEFAULT_PRIOR_VOTERS_END] != [0; DEFAULT_PRIOR_VOTERS_OFFSET] } } @@ -603,7 +603,7 @@ mod vote_state_deserialize { crate::{ authorized_voters::AuthorizedVoters, state::{ - BlockTimestamp, LandedVote, Lockout, VoteState, MAX_EPOCH_CREDITS_HISTORY, + BlockTimestamp, LandedVote, Lockout, VoteStateV3, MAX_EPOCH_CREDITS_HISTORY, MAX_ITEMS, MAX_LOCKOUT_HISTORY, }, }, @@ -619,7 +619,7 @@ mod vote_state_deserialize { pub(super) fn deserialize_vote_state_into( cursor: &mut Cursor<&[u8]>, - vote_state: *mut VoteState, + vote_state: *mut VoteStateV3, has_latency: bool, ) -> Result<(), InstructionError> { // General safety note: we must use add_or_mut! to access the `vote_state` fields as the value @@ -648,7 +648,7 @@ mod vote_state_deserialize { // valid pointers. // // Heap allocated collections - votes, authorized_voters and epoch_credits - - // are guaranteed not to leak after this point as the VoteState is fully + // are guaranteed not to leak after this point as the VoteStateV3 is fully // initialized and will be regularly dropped. unsafe { addr_of_mut!((*vote_state).commission).write(commission); @@ -698,7 +698,7 @@ mod vote_state_deserialize { fn read_prior_voters_into>( cursor: &mut Cursor, - vote_state: *mut VoteState, + vote_state: *mut VoteStateV3, ) -> Result<(), InstructionError> { // Safety: if vote_state is non-null, prior_voters is guaranteed to be valid too unsafe { @@ -740,7 +740,7 @@ mod vote_state_deserialize { fn read_last_timestamp_into>( cursor: &mut Cursor, - vote_state: *mut VoteState, + vote_state: *mut VoteStateV3, ) -> Result<(), InstructionError> { let slot = read_u64(cursor)?; let timestamp = read_i64(cursor)?; diff --git a/vote-interface/src/state/vote_state_versions.rs b/vote-interface/src/state/vote_state_versions.rs index 74afac088..8ffde1065 100644 --- a/vote-interface/src/state/vote_state_versions.rs +++ b/vote-interface/src/state/vote_state_versions.rs @@ -5,7 +5,7 @@ use { authorized_voters::AuthorizedVoters, state::{ vote_state_0_23_5::VoteState0_23_5, vote_state_1_14_11::VoteState1_14_11, CircBuf, - LandedVote, Lockout, VoteState, + LandedVote, Lockout, VoteStateV3, }, }, solana_pubkey::Pubkey, @@ -20,21 +20,21 @@ use { pub enum VoteStateVersions { V0_23_5(Box), V1_14_11(Box), - Current(Box), + Current(Box), } impl VoteStateVersions { - pub fn new_current(vote_state: VoteState) -> Self { + pub fn new_current(vote_state: VoteStateV3) -> Self { Self::Current(Box::new(vote_state)) } - pub fn convert_to_current(self) -> VoteState { + pub fn convert_to_current(self) -> VoteStateV3 { match self { VoteStateVersions::V0_23_5(state) => { let authorized_voters = AuthorizedVoters::new(state.authorized_voter_epoch, state.authorized_voter); - VoteState { + VoteStateV3 { node_pubkey: state.node_pubkey, authorized_withdrawer: state.authorized_withdrawer, @@ -55,7 +55,7 @@ impl VoteStateVersions { } } - VoteStateVersions::V1_14_11(state) => VoteState { + VoteStateVersions::V1_14_11(state) => VoteStateV3 { node_pubkey: state.node_pubkey, authorized_withdrawer: state.authorized_withdrawer, commission: state.commission, @@ -95,14 +95,14 @@ impl VoteStateVersions { pub fn vote_state_size_of(is_current: bool) -> usize { if is_current { - VoteState::size_of() + VoteStateV3::size_of() } else { VoteState1_14_11::size_of() } } pub fn is_correct_size_and_initialized(data: &[u8]) -> bool { - VoteState::is_correct_size_and_initialized(data) + VoteStateV3::is_correct_size_and_initialized(data) || VoteState1_14_11::is_correct_size_and_initialized(data) } } @@ -112,7 +112,7 @@ impl Arbitrary<'_> for VoteStateVersions { fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result { let variant = u.choose_index(2)?; match variant { - 0 => Ok(Self::Current(Box::new(VoteState::arbitrary(u)?))), + 0 => Ok(Self::Current(Box::new(VoteStateV3::arbitrary(u)?))), 1 => Ok(Self::V1_14_11(Box::new(VoteState1_14_11::arbitrary(u)?))), _ => unreachable!(), }