Skip to content

Commit

Permalink
simplifies parse [sanitized] vote transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
behzadnouri committed Dec 29, 2021
1 parent c9c7862 commit c14f30d
Showing 1 changed file with 35 additions and 61 deletions.
96 changes: 35 additions & 61 deletions programs/vote/src/vote_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,77 +16,51 @@ use {

pub type ParsedVote = (Pubkey, VoteTransaction, Option<Hash>);

fn parse_vote(vote_ix: &CompiledInstruction, vote_key: &Pubkey) -> Option<ParsedVote> {
let vote_instruction = limited_deserialize(&vote_ix.data).ok();
vote_instruction.and_then(|vote_instruction| {
let result: Option<ParsedVote> = match vote_instruction {
VoteInstruction::Vote(vote) => Some((*vote_key, VoteTransaction::from(vote), None)),
VoteInstruction::VoteSwitch(vote, hash) => {
Some((*vote_key, VoteTransaction::from(vote), Some(hash)))
}
VoteInstruction::UpdateVoteState(vote_state_update) => {
Some((*vote_key, VoteTransaction::from(vote_state_update), None))
}
VoteInstruction::UpdateVoteStateSwitch(vote_state_update, hash) => Some((
*vote_key,
VoteTransaction::from(vote_state_update),
Some(hash),
)),
VoteInstruction::Authorize(_, _)
| VoteInstruction::AuthorizeChecked(_)
| VoteInstruction::InitializeAccount(_)
| VoteInstruction::UpdateCommission(_)
| VoteInstruction::UpdateValidatorIdentity
| VoteInstruction::Withdraw(_) => None,
};
result
})
fn parse_vote(vote: &CompiledInstruction) -> Option<(VoteTransaction, Option<Hash>)> {
match limited_deserialize(&vote.data).ok()? {
VoteInstruction::Vote(vote) => Some((VoteTransaction::from(vote), None)),
VoteInstruction::VoteSwitch(vote, hash) => Some((VoteTransaction::from(vote), Some(hash))),
VoteInstruction::UpdateVoteState(vote_state_update) => {
Some((VoteTransaction::from(vote_state_update), None))
}
VoteInstruction::UpdateVoteStateSwitch(vote_state_update, hash) => {
Some((VoteTransaction::from(vote_state_update), Some(hash)))
}
VoteInstruction::Authorize(_, _)
| VoteInstruction::AuthorizeChecked(_)
| VoteInstruction::InitializeAccount(_)
| VoteInstruction::UpdateCommission(_)
| VoteInstruction::UpdateValidatorIdentity
| VoteInstruction::Withdraw(_) => None,
}
}

pub fn parse_sanitized_vote_transaction(tx: &SanitizedTransaction) -> Option<ParsedVote> {
// Check first instruction for a vote
let message = tx.message();
message
.program_instructions_iter()
.next()
.and_then(|(program_id, first_ix)| {
if !crate::check_id(program_id) {
return None;
}

first_ix.accounts.first().and_then(|first_account| {
message
.get_account_key(*first_account as usize)
.and_then(|key| parse_vote(first_ix, key))
})
})
let (program_id, first_instruction) = message.program_instructions_iter().next()?;
if !crate::check_id(program_id) {
return None;
}
let first_account = usize::from(*first_instruction.accounts.first()?);
let key = message.get_account_key(first_account)?;
let (vote, switch_proof_hash) = parse_vote(first_instruction)?;
Some((*key, vote, switch_proof_hash))
}

pub fn parse_vote_transaction(tx: &Transaction) -> Option<ParsedVote> {
// Check first instruction for a vote
let message = tx.message();
message.instructions.get(0).and_then(|first_instruction| {
let prog_id_idx = first_instruction.program_id_index as usize;
match message.account_keys.get(prog_id_idx) {
Some(program_id) => {
if !crate::check_id(program_id) {
return None;
}
}
_ => {
return None;
}
};
first_instruction
.accounts
.first()
.and_then(|first_account| {
message
.account_keys
.get(*first_account as usize)
.and_then(|key| parse_vote(first_instruction, key))
})
})
let first_instruction = message.instructions.first()?;
let program_id_index = usize::from(first_instruction.program_id_index);
let program_id = message.account_keys.get(program_id_index)?;
if !crate::check_id(program_id) {
return None;
}
let first_account = usize::from(*first_instruction.accounts.first()?);
let key = message.account_keys.get(first_account)?;
let (vote, switch_proof_hash) = parse_vote(first_instruction)?;
Some((*key, vote, switch_proof_hash))
}

pub fn new_vote_transaction(
Expand Down

0 comments on commit c14f30d

Please sign in to comment.