From c178fc724926b408aea02bed496bac57a16f0c16 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Tue, 12 Feb 2019 20:48:10 -0700 Subject: [PATCH] Rewrite get_votes() Panic if deserialize fails. --- sdk/src/vote_transaction.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/sdk/src/vote_transaction.rs b/sdk/src/vote_transaction.rs index f2435c515f..476c3c2c0c 100644 --- a/sdk/src/vote_transaction.rs +++ b/sdk/src/vote_transaction.rs @@ -55,17 +55,22 @@ impl VoteTransaction { ) } - pub fn get_votes(tx: &Transaction) -> Vec<(Pubkey, Vote, Hash)> { - let mut votes = vec![]; - for i in 0..tx.instructions.len() { - 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)) - } - } + fn get_vote(tx: &Transaction, ix_index: usize) -> Option<(Pubkey, Vote, Hash)> { + if !vote_program::check_id(&tx.program_id(ix_index)) { + return None; } - 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() } }