Rewrite get_votes()

Panic if deserialize fails.
This commit is contained in:
Greg Fitzgerald 2019-02-12 20:48:10 -07:00
parent 41554f433b
commit c178fc7249

View File

@ -55,17 +55,22 @@ impl VoteTransaction {
) )
} }
pub fn get_votes(tx: &Transaction) -> Vec<(Pubkey, Vote, Hash)> { fn get_vote(tx: &Transaction, ix_index: usize) -> Option<(Pubkey, Vote, Hash)> {
let mut votes = vec![]; if !vote_program::check_id(&tx.program_id(ix_index)) {
for i in 0..tx.instructions.len() { return None;
let tx_program_id = tx.program_id(i);
if vote_program::check_id(&tx_program_id) {
if let Ok(VoteInstruction::Vote(vote)) = deserialize(&tx.userdata(i)) {
votes.push((tx.account_keys[0], vote, tx.last_id))
}
}
} }
votes let instruction = deserialize(&tx.userdata(ix_index)).unwrap();
if let VoteInstruction::Vote(vote) = instruction {
Some((tx.account_keys[0], vote, tx.last_id))
} else {
None
}
}
pub fn get_votes(tx: &Transaction) -> Vec<(Pubkey, Vote, Hash)> {
(0..tx.instructions.len())
.filter_map(|i| Self::get_vote(tx, i))
.collect()
} }
} }