-
Couldn't load subscription status.
- Fork 141
vote-interface: add fast deserialize for VoteStateVersions
#383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,13 +4,14 @@ use crate::state::{ | |||||||
| }; | ||||||||
| #[cfg(test)] | ||||||||
| use arbitrary::{Arbitrary, Unstructured}; | ||||||||
| #[cfg(any(target_os = "solana", feature = "bincode"))] | ||||||||
| use solana_instruction_error::InstructionError; | ||||||||
| #[cfg(any(test, all(not(target_os = "solana"), feature = "bincode")))] | ||||||||
| use { | ||||||||
| crate::{ | ||||||||
| authorized_voters::AuthorizedVoters, | ||||||||
| state::{CircBuf, LandedVote, Lockout}, | ||||||||
| }, | ||||||||
| solana_instruction::error::InstructionError, | ||||||||
| solana_pubkey::Pubkey, | ||||||||
| std::collections::VecDeque, | ||||||||
| }; | ||||||||
|
|
@@ -190,6 +191,58 @@ impl VoteStateVersions { | |||||||
| || VoteStateV3::is_correct_size_and_initialized(data) | ||||||||
| || VoteState1_14_11::is_correct_size_and_initialized(data) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Deserializes the input buffer directly into the appropriate `VoteStateVersions` variant. | ||||||||
| /// | ||||||||
| /// V0_23_5 is not supported. All other versions (V1_14_11, V3, V4) are deserialized as-is | ||||||||
| /// without any version coercion. | ||||||||
| #[cfg(any(target_os = "solana", feature = "bincode"))] | ||||||||
| pub fn deserialize(input: &[u8]) -> Result<Self, InstructionError> { | ||||||||
| use { | ||||||||
| crate::state::vote_state_deserialize::{ | ||||||||
| deserialize_vote_state_into_v1_14_11, deserialize_vote_state_into_v3, | ||||||||
| deserialize_vote_state_into_v4, SourceVersion, | ||||||||
| }, | ||||||||
| std::mem::MaybeUninit, | ||||||||
| }; | ||||||||
|
|
||||||||
| let mut cursor = std::io::Cursor::new(input); | ||||||||
|
|
||||||||
| let variant = solana_serialize_utils::cursor::read_u32(&mut cursor)?; | ||||||||
| match variant { | ||||||||
| // V0_23_5 not supported. | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's return |
||||||||
| 0 => Err(InstructionError::UninitializedAccount), | ||||||||
| // V1_14_11 | ||||||||
| 1 => { | ||||||||
| let mut vote_state = Box::new(MaybeUninit::uninit()); | ||||||||
| deserialize_vote_state_into_v1_14_11(&mut cursor, vote_state.as_mut_ptr())?; | ||||||||
| let vote_state = | ||||||||
| unsafe { Box::from_raw(Box::into_raw(vote_state) as *mut VoteState1_14_11) }; | ||||||||
|
Comment on lines
+219
to
+220
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like you can do this without the into/from
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can, but only for Rust 1.82 and above. I would have to reconfigure the MSRV checks on the workspace in order to allow |
||||||||
| Ok(VoteStateVersions::V1_14_11(vote_state)) | ||||||||
| } | ||||||||
| // V3 | ||||||||
| 2 => { | ||||||||
| let mut vote_state = Box::new(MaybeUninit::uninit()); | ||||||||
| deserialize_vote_state_into_v3(&mut cursor, vote_state.as_mut_ptr(), true)?; | ||||||||
| let vote_state = | ||||||||
| unsafe { Box::from_raw(Box::into_raw(vote_state) as *mut VoteStateV3) }; | ||||||||
| Ok(VoteStateVersions::V3(vote_state)) | ||||||||
| } | ||||||||
| // V4 | ||||||||
| 3 => { | ||||||||
| let mut vote_state = Box::new(MaybeUninit::uninit()); | ||||||||
| deserialize_vote_state_into_v4( | ||||||||
| &mut cursor, | ||||||||
| vote_state.as_mut_ptr(), | ||||||||
| SourceVersion::V4, | ||||||||
| )?; | ||||||||
| let vote_state = | ||||||||
| unsafe { Box::from_raw(Box::into_raw(vote_state) as *mut VoteStateV4) }; | ||||||||
| Ok(VoteStateVersions::V4(vote_state)) | ||||||||
| } | ||||||||
| _ => Err(InstructionError::InvalidAccountData), | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| #[cfg(test)] | ||||||||
|
|
@@ -204,3 +257,40 @@ impl Arbitrary<'_> for VoteStateVersions { | |||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| #[cfg(test)] | ||||||||
| mod tests { | ||||||||
| use super::*; | ||||||||
|
|
||||||||
| #[test] | ||||||||
| fn test_vote_state_versions_deserialize() { | ||||||||
| let ser_deser = |original: VoteStateVersions| { | ||||||||
| let serialized = bincode::serialize(&original).unwrap(); | ||||||||
| VoteStateVersions::deserialize(&serialized) | ||||||||
| }; | ||||||||
|
|
||||||||
| let v0_23_5 = VoteStateVersions::V0_23_5(Box::default()); | ||||||||
| assert_eq!( | ||||||||
| ser_deser(v0_23_5), | ||||||||
| Err(InstructionError::UninitializedAccount), // <-- v0_23_5 unsupported | ||||||||
| ); | ||||||||
|
|
||||||||
| let v1_14_11 = VoteStateVersions::V1_14_11(Box::default()); | ||||||||
| assert_eq!( | ||||||||
| ser_deser(v1_14_11.clone()), | ||||||||
| Ok(v1_14_11), // <-- Matches original | ||||||||
| ); | ||||||||
|
|
||||||||
| let v3 = VoteStateVersions::V3(Box::default()); | ||||||||
| assert_eq!( | ||||||||
| ser_deser(v3.clone()), | ||||||||
| Ok(v3), // <-- Matches original | ||||||||
| ); | ||||||||
|
|
||||||||
| let v4 = VoteStateVersions::V4(Box::default()); | ||||||||
| assert_eq!( | ||||||||
| ser_deser(v4.clone()), | ||||||||
| Ok(v4), // <-- Matches original | ||||||||
| ); | ||||||||
| } | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels incredibly pedantic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha yeah but it's also not the correct type so it stood out :P