Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplifies parse [sanitized] vote transaction #22127

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)?;
Comment on lines +54 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)?;
let program_id = message.program_id(0)?;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. but I prefer to keep this commit precisely the same logic as the existing code.
We can introduce that change in a separate commit

if !crate::check_id(program_id) {
return None;
}
let first_account = usize::from(*first_instruction.accounts.first()?);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let first_account = usize::from(*first_instruction.accounts.first()?);
let first_instruction = message.instructions.first()?;
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