From 4f79a8a204561de3818b10adadbfee8b1230f71d Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Thu, 10 Jan 2019 16:47:59 -0700 Subject: [PATCH] Use serialized_size - less fragile --- sdk/src/vote_program.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/sdk/src/vote_program.rs b/sdk/src/vote_program.rs index 18f11e8c8d..d809bd6079 100644 --- a/sdk/src/vote_program.rs +++ b/sdk/src/vote_program.rs @@ -3,10 +3,9 @@ use crate::native_program::ProgramError; use crate::pubkey::Pubkey; -use bincode::{deserialize, serialize}; +use bincode::{deserialize, serialize, serialized_size}; use byteorder::{ByteOrder, LittleEndian}; use std::collections::VecDeque; -use std::mem; pub const VOTE_PROGRAM_ID: [u8; 32] = [ 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -50,12 +49,11 @@ pub struct VoteProgram { pub fn get_max_size() -> usize { // Upper limit on the size of the Vote State. Equal to - // sizeof(VoteProgram) + MAX_VOTE_HISTORY * sizeof(Vote) + - // 32 (the size of the Pubkey) + 2 (2 bytes for the size) - mem::size_of::() - + MAX_VOTE_HISTORY * mem::size_of::() - + mem::size_of::() - + mem::size_of::() + // sizeof(VoteProgram) when votes.len() is MAX_VOTE_HISTORY + // + 2 (2 bytes for the size) + let mut vote_program = VoteProgram::default(); + vote_program.votes = VecDeque::from(vec![Vote::default(); MAX_VOTE_HISTORY]); + serialized_size(&vote_program).unwrap() as usize + 2 } impl VoteProgram {