simplifies parse [sanitized] vote transaction (#22127)

This commit is contained in:
behzad nouri 2021-12-30 16:03:03 +00:00 committed by GitHub
parent c0c6038654
commit d421ccb330
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 61 deletions

View File

@ -16,77 +16,51 @@ use {
pub type ParsedVote = (Pubkey, VoteTransaction, Option<Hash>); pub type ParsedVote = (Pubkey, VoteTransaction, Option<Hash>);
fn parse_vote(vote_ix: &CompiledInstruction, vote_key: &Pubkey) -> Option<ParsedVote> { fn parse_vote(vote: &CompiledInstruction) -> Option<(VoteTransaction, Option<Hash>)> {
let vote_instruction = limited_deserialize(&vote_ix.data).ok(); match limited_deserialize(&vote.data).ok()? {
vote_instruction.and_then(|vote_instruction| { VoteInstruction::Vote(vote) => Some((VoteTransaction::from(vote), None)),
let result: Option<ParsedVote> = match vote_instruction { VoteInstruction::VoteSwitch(vote, hash) => Some((VoteTransaction::from(vote), Some(hash))),
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) => { VoteInstruction::UpdateVoteState(vote_state_update) => {
Some((*vote_key, VoteTransaction::from(vote_state_update), None)) Some((VoteTransaction::from(vote_state_update), None))
}
VoteInstruction::UpdateVoteStateSwitch(vote_state_update, hash) => {
Some((VoteTransaction::from(vote_state_update), Some(hash)))
} }
VoteInstruction::UpdateVoteStateSwitch(vote_state_update, hash) => Some((
*vote_key,
VoteTransaction::from(vote_state_update),
Some(hash),
)),
VoteInstruction::Authorize(_, _) VoteInstruction::Authorize(_, _)
| VoteInstruction::AuthorizeChecked(_) | VoteInstruction::AuthorizeChecked(_)
| VoteInstruction::InitializeAccount(_) | VoteInstruction::InitializeAccount(_)
| VoteInstruction::UpdateCommission(_) | VoteInstruction::UpdateCommission(_)
| VoteInstruction::UpdateValidatorIdentity | VoteInstruction::UpdateValidatorIdentity
| VoteInstruction::Withdraw(_) => None, | VoteInstruction::Withdraw(_) => None,
}; }
result
})
} }
pub fn parse_sanitized_vote_transaction(tx: &SanitizedTransaction) -> Option<ParsedVote> { pub fn parse_sanitized_vote_transaction(tx: &SanitizedTransaction) -> Option<ParsedVote> {
// Check first instruction for a vote // Check first instruction for a vote
let message = tx.message(); let message = tx.message();
message let (program_id, first_instruction) = message.program_instructions_iter().next()?;
.program_instructions_iter()
.next()
.and_then(|(program_id, first_ix)| {
if !crate::check_id(program_id) { if !crate::check_id(program_id) {
return None; return None;
} }
let first_account = usize::from(*first_instruction.accounts.first()?);
first_ix.accounts.first().and_then(|first_account| { let key = message.get_account_key(first_account)?;
message let (vote, switch_proof_hash) = parse_vote(first_instruction)?;
.get_account_key(*first_account as usize) Some((*key, vote, switch_proof_hash))
.and_then(|key| parse_vote(first_ix, key))
})
})
} }
pub fn parse_vote_transaction(tx: &Transaction) -> Option<ParsedVote> { pub fn parse_vote_transaction(tx: &Transaction) -> Option<ParsedVote> {
// Check first instruction for a vote // Check first instruction for a vote
let message = tx.message(); let message = tx.message();
message.instructions.get(0).and_then(|first_instruction| { let first_instruction = message.instructions.first()?;
let prog_id_idx = first_instruction.program_id_index as usize; let program_id_index = usize::from(first_instruction.program_id_index);
match message.account_keys.get(prog_id_idx) { let program_id = message.account_keys.get(program_id_index)?;
Some(program_id) => {
if !crate::check_id(program_id) { if !crate::check_id(program_id) {
return None; return None;
} }
} let first_account = usize::from(*first_instruction.accounts.first()?);
_ => { let key = message.account_keys.get(first_account)?;
return None; let (vote, switch_proof_hash) = parse_vote(first_instruction)?;
} Some((*key, vote, switch_proof_hash))
};
first_instruction
.accounts
.first()
.and_then(|first_account| {
message
.account_keys
.get(*first_account as usize)
.and_then(|key| parse_vote(first_instruction, key))
})
})
} }
pub fn new_vote_transaction( pub fn new_vote_transaction(