Skip to content

Commit

Permalink
Add convenience methods to VoteInstruction to distinguish vote types (s…
Browse files Browse the repository at this point in the history
…olana-labs#28526)

* Add convenience methods to VoteInstruction to distinguish vote types

* use matches! macro instead
  • Loading branch information
AshwinSekar authored Oct 21, 2022
1 parent ff18bb3 commit 9eafad4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
9 changes: 4 additions & 5 deletions core/src/latest_unprocessed_votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ impl LatestValidatorVotePacket {
.ok_or(DeserializedPacketError::VoteTransactionError)?;

match limited_deserialize::<VoteInstruction>(&instruction.data) {
Ok(VoteInstruction::UpdateVoteState(vote_state_update))
| Ok(VoteInstruction::UpdateVoteStateSwitch(vote_state_update, _))
| Ok(VoteInstruction::CompactUpdateVoteState(vote_state_update))
| Ok(VoteInstruction::CompactUpdateVoteStateSwitch(vote_state_update, _)) => {
Ok(vote_state_update_instruction)
if vote_state_update_instruction.is_single_vote_state_update() =>
{
let &pubkey = message
.message
.static_account_keys()
.get(0)
.ok_or(DeserializedPacketError::VoteTransactionError)?;
let slot = vote_state_update.last_voted_slot().unwrap_or(0);
let slot = vote_state_update_instruction.last_voted_slot().unwrap_or(0);

Ok(Self {
vote: Some(vote),
Expand Down
2 changes: 1 addition & 1 deletion programs/vote/src/vote_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl VoteTransaction {

pub fn last_voted_slot(&self) -> Option<Slot> {
match self {
VoteTransaction::Vote(vote) => vote.slots.last().copied(),
VoteTransaction::Vote(vote) => vote.last_voted_slot(),
VoteTransaction::VoteStateUpdate(vote_state_update)
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.last_voted_slot()
Expand Down
10 changes: 1 addition & 9 deletions runtime/src/vote_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ pub(crate) fn is_simple_vote_transaction(transaction: &SanitizedTransaction) ->
if program_pubkey == &solana_vote_program::id() {
if let Ok(vote_instruction) = limited_deserialize::<VoteInstruction>(&instruction.data)
{
return matches!(
vote_instruction,
VoteInstruction::Vote(_)
| VoteInstruction::VoteSwitch(_, _)
| VoteInstruction::UpdateVoteState(_)
| VoteInstruction::UpdateVoteStateSwitch(_, _)
| VoteInstruction::CompactUpdateVoteState(_)
| VoteInstruction::CompactUpdateVoteStateSwitch(..)
);
return vote_instruction.is_simple_vote();
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions sdk/program/src/vote/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use {
crate::{
clock::Slot,
hash::Hash,
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
Expand Down Expand Up @@ -147,6 +148,45 @@ pub enum VoteInstruction {
),
}

impl VoteInstruction {
pub fn is_simple_vote(&self) -> bool {
matches!(
self,
Self::Vote(_)
| Self::VoteSwitch(_, _)
| Self::UpdateVoteState(_)
| Self::UpdateVoteStateSwitch(_, _)
| Self::CompactUpdateVoteState(_)
| Self::CompactUpdateVoteStateSwitch(_, _),
)
}

pub fn is_single_vote_state_update(&self) -> bool {
matches!(
self,
Self::UpdateVoteState(_)
| Self::UpdateVoteStateSwitch(_, _)
| Self::CompactUpdateVoteState(_)
| Self::CompactUpdateVoteStateSwitch(_, _),
)
}

/// Only to be used on vote instructions (guard with is_simple_vote), panics otherwise
pub fn last_voted_slot(&self) -> Option<Slot> {
assert!(self.is_simple_vote());
match self {
Self::Vote(v) | Self::VoteSwitch(v, _) => v.last_voted_slot(),
Self::UpdateVoteState(vote_state_update)
| Self::UpdateVoteStateSwitch(vote_state_update, _)
| Self::CompactUpdateVoteState(vote_state_update)
| Self::CompactUpdateVoteStateSwitch(vote_state_update, _) => {
vote_state_update.last_voted_slot()
}
_ => panic!("Tried to get slot on non simple vote instruction"),
}
}
}

fn initialize_account(vote_pubkey: &Pubkey, vote_init: &VoteInit) -> Instruction {
let account_metas = vec![
AccountMeta::new(*vote_pubkey, false),
Expand Down
4 changes: 4 additions & 0 deletions sdk/program/src/vote/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl Vote {
timestamp: None,
}
}

pub fn last_voted_slot(&self) -> Option<Slot> {
self.slots.last().copied()
}
}

#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Copy, Clone, AbiExample)]
Expand Down

0 comments on commit 9eafad4

Please sign in to comment.