Drop the serialization length
This commit is contained in:
@ -10,7 +10,6 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bincode = "1.0.0"
|
bincode = "1.0.0"
|
||||||
byteorder = "1.2.1"
|
|
||||||
bs58 = "0.2.0"
|
bs58 = "0.2.0"
|
||||||
chrono = { version = "0.4.0", features = ["serde"] }
|
chrono = { version = "0.4.0", features = ["serde"] }
|
||||||
generic-array = { version = "0.12.0", default-features = false, features = ["serde"] }
|
generic-array = { version = "0.12.0", default-features = false, features = ["serde"] }
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
|
|
||||||
use crate::native_program::ProgramError;
|
use crate::native_program::ProgramError;
|
||||||
use crate::pubkey::Pubkey;
|
use crate::pubkey::Pubkey;
|
||||||
use bincode::{deserialize, serialize, serialized_size};
|
use bincode::{deserialize, serialize_into, serialized_size, ErrorKind};
|
||||||
use byteorder::{ByteOrder, LittleEndian};
|
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
pub const VOTE_PROGRAM_ID: [u8; 32] = [
|
pub const VOTE_PROGRAM_ID: [u8; 32] = [
|
||||||
@ -50,34 +49,21 @@ pub struct VoteProgram {
|
|||||||
pub fn get_max_size() -> usize {
|
pub fn get_max_size() -> usize {
|
||||||
// Upper limit on the size of the Vote State. Equal to
|
// Upper limit on the size of the Vote State. Equal to
|
||||||
// sizeof(VoteProgram) when votes.len() is MAX_VOTE_HISTORY
|
// sizeof(VoteProgram) when votes.len() is MAX_VOTE_HISTORY
|
||||||
// + 2 (2 bytes for the size)
|
|
||||||
let mut vote_program = VoteProgram::default();
|
let mut vote_program = VoteProgram::default();
|
||||||
vote_program.votes = VecDeque::from(vec![Vote::default(); MAX_VOTE_HISTORY]);
|
vote_program.votes = VecDeque::from(vec![Vote::default(); MAX_VOTE_HISTORY]);
|
||||||
serialized_size(&vote_program).unwrap() as usize + 2
|
serialized_size(&vote_program).unwrap() as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VoteProgram {
|
impl VoteProgram {
|
||||||
pub fn deserialize(input: &[u8]) -> Result<VoteProgram, ProgramError> {
|
pub fn deserialize(input: &[u8]) -> Result<VoteProgram, ProgramError> {
|
||||||
let len = LittleEndian::read_u16(&input[0..2]) as usize;
|
deserialize(input).map_err(|_| ProgramError::InvalidUserdata)
|
||||||
|
|
||||||
if len == 0 || input.len() < len + 2 {
|
|
||||||
Err(ProgramError::InvalidUserdata)
|
|
||||||
} else {
|
|
||||||
deserialize(&input[2..=len + 1]).map_err(|_| ProgramError::InvalidUserdata)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialize(self: &VoteProgram, output: &mut [u8]) -> Result<(), ProgramError> {
|
pub fn serialize(self: &VoteProgram, output: &mut [u8]) -> Result<(), ProgramError> {
|
||||||
let self_serialized = serialize(self).unwrap();
|
serialize_into(output, self).map_err(|err| match *err {
|
||||||
|
ErrorKind::SizeLimit => ProgramError::UserdataTooSmall,
|
||||||
if output.len() + 2 < self_serialized.len() {
|
_ => ProgramError::GenericError,
|
||||||
return Err(ProgramError::UserdataTooSmall);
|
})
|
||||||
}
|
|
||||||
|
|
||||||
let serialized_len = self_serialized.len() as u16;
|
|
||||||
LittleEndian::write_u16(&mut output[0..2], serialized_len);
|
|
||||||
output[2..=serialized_len as usize + 1].clone_from_slice(&self_serialized);
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user