Skip to content

Commit

Permalink
remove special circbuf logic
Browse files Browse the repository at this point in the history
  • Loading branch information
2501babe committed Jul 18, 2024
1 parent a016800 commit 1f39b48
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 43 deletions.
18 changes: 1 addition & 17 deletions sdk/program/src/vote/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ const MAX_ITEMS: usize = 32;

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct CircBuf<I> {
buf: [I; MAX_ITEMS],
/// next pointer
Expand Down Expand Up @@ -368,23 +369,6 @@ impl<I> CircBuf<I> {
}
}

#[cfg(test)]
impl<'a, I: Default + Copy> Arbitrary<'a> for CircBuf<I>
where
I: Arbitrary<'a>,
{
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let mut circbuf = Self::default();

let len = u.arbitrary_len::<I>()?;
for _ in 0..len {
circbuf.append(I::arbitrary(u)?);
}

Ok(circbuf)
}
}

#[cfg_attr(
feature = "frozen-abi",
frozen_abi(digest = "EeenjJaSrm9hRM39gK6raRNtzG61hnk7GciUCJJRDUSQ"),
Expand Down
34 changes: 8 additions & 26 deletions sdk/program/src/vote/state/vote_state_deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,17 @@ fn read_prior_voters_into<T: AsRef<[u8]>>(
cursor: &mut Cursor<T>,
vote_state: &mut VoteState,
) -> Result<(), InstructionError> {
// record our position at the start of the struct
let prior_voters_position = cursor.position();
for i in 0..MAX_ITEMS {
let prior_voter = read_pubkey(cursor)?;
let from_epoch = read_u64(cursor)?;
let until_epoch = read_u64(cursor)?;

let is_empty_position = PRIOR_VOTERS_SERIALIZED_SIZE
.checked_add(prior_voters_position)
.and_then(|v| v.checked_sub(1))
.ok_or(InstructionError::InvalidAccountData)?;

// move to the end, to check if we need to parse the data
cursor.set_position(is_empty_position);

// if empty, we already read past the end of this struct and need to do no further work
// otherwise we go back to the start and proceed to decode the data
let is_empty = read_bool(cursor)?;
if !is_empty {
cursor.set_position(prior_voters_position);

for i in 0..MAX_ITEMS {
let prior_voter = read_pubkey(cursor)?;
let from_epoch = read_u64(cursor)?;
let until_epoch = read_u64(cursor)?;

vote_state.prior_voters.buf[i] = (prior_voter, from_epoch, until_epoch);
}

vote_state.prior_voters.idx = read_u64(cursor)? as usize;
vote_state.prior_voters.is_empty = read_bool(cursor)?;
vote_state.prior_voters.buf[i] = (prior_voter, from_epoch, until_epoch);
}

vote_state.prior_voters.idx = read_u64(cursor)? as usize;
vote_state.prior_voters.is_empty = read_bool(cursor)?;

Ok(())
}

Expand Down

0 comments on commit 1f39b48

Please sign in to comment.