2022-01-19 18:39:21 -08:00
|
|
|
use {
|
|
|
|
crate::vote_transaction::VoteTransaction,
|
|
|
|
solana_sdk::{
|
|
|
|
hash::Hash,
|
|
|
|
program_utils::limited_deserialize,
|
|
|
|
pubkey::Pubkey,
|
2022-05-19 18:28:46 -07:00
|
|
|
signature::Signature,
|
2022-01-19 18:39:21 -08:00
|
|
|
transaction::{SanitizedTransaction, Transaction},
|
|
|
|
},
|
2022-08-23 22:29:03 -07:00
|
|
|
solana_vote_program::vote_instruction::VoteInstruction,
|
2022-01-19 18:39:21 -08:00
|
|
|
};
|
|
|
|
|
2022-05-19 18:28:46 -07:00
|
|
|
pub type ParsedVote = (Pubkey, VoteTransaction, Option<Hash>, Signature);
|
2022-01-19 18:39:21 -08:00
|
|
|
|
|
|
|
// Used for locally forwarding processed vote transactions to consensus
|
|
|
|
pub fn parse_sanitized_vote_transaction(tx: &SanitizedTransaction) -> Option<ParsedVote> {
|
|
|
|
// Check first instruction for a vote
|
|
|
|
let message = tx.message();
|
|
|
|
let (program_id, first_instruction) = message.program_instructions_iter().next()?;
|
|
|
|
if !solana_vote_program::check_id(program_id) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let first_account = usize::from(*first_instruction.accounts.first()?);
|
2022-02-05 04:00:31 -08:00
|
|
|
let key = message.account_keys().get(first_account)?;
|
2022-01-19 18:39:21 -08:00
|
|
|
let (vote, switch_proof_hash) = parse_vote_instruction_data(&first_instruction.data)?;
|
2022-05-19 18:28:46 -07:00
|
|
|
let signature = tx.signatures().get(0).cloned().unwrap_or_default();
|
|
|
|
Some((*key, vote, switch_proof_hash, signature))
|
2022-01-19 18:39:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Used for parsing gossip vote transactions
|
|
|
|
pub fn parse_vote_transaction(tx: &Transaction) -> Option<ParsedVote> {
|
|
|
|
// Check first instruction for a vote
|
|
|
|
let message = tx.message();
|
|
|
|
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 !solana_vote_program::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_instruction_data(&first_instruction.data)?;
|
2022-05-19 18:28:46 -07:00
|
|
|
let signature = tx.signatures.get(0).cloned().unwrap_or_default();
|
|
|
|
Some((*key, vote, switch_proof_hash, signature))
|
2022-01-19 18:39:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_vote_instruction_data(
|
|
|
|
vote_instruction_data: &[u8],
|
|
|
|
) -> Option<(VoteTransaction, Option<Hash>)> {
|
|
|
|
match limited_deserialize(vote_instruction_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)))
|
|
|
|
}
|
2022-09-05 10:02:16 -07:00
|
|
|
VoteInstruction::CompactUpdateVoteState(vote_state_update) => {
|
|
|
|
Some((VoteTransaction::from(vote_state_update), None))
|
2022-08-23 22:29:03 -07:00
|
|
|
}
|
2022-09-05 10:02:16 -07:00
|
|
|
VoteInstruction::CompactUpdateVoteStateSwitch(vote_state_update, hash) => {
|
|
|
|
Some((VoteTransaction::from(vote_state_update), Some(hash)))
|
2022-08-23 22:29:03 -07:00
|
|
|
}
|
2022-01-19 18:39:21 -08:00
|
|
|
VoteInstruction::Authorize(_, _)
|
|
|
|
| VoteInstruction::AuthorizeChecked(_)
|
2022-06-13 20:36:44 -07:00
|
|
|
| VoteInstruction::AuthorizeWithSeed(_)
|
|
|
|
| VoteInstruction::AuthorizeCheckedWithSeed(_)
|
2022-01-19 18:39:21 -08:00
|
|
|
| VoteInstruction::InitializeAccount(_)
|
|
|
|
| VoteInstruction::UpdateCommission(_)
|
|
|
|
| VoteInstruction::UpdateValidatorIdentity
|
|
|
|
| VoteInstruction::Withdraw(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2022-02-18 21:32:29 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
solana_sdk::{
|
|
|
|
hash::hash,
|
|
|
|
signature::{Keypair, Signer},
|
|
|
|
},
|
|
|
|
solana_vote_program::{
|
|
|
|
vote_instruction, vote_state::Vote, vote_transaction::new_vote_transaction,
|
|
|
|
},
|
2022-01-19 18:39:21 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
fn run_test_parse_vote_transaction(input_hash: Option<Hash>) {
|
|
|
|
let node_keypair = Keypair::new();
|
|
|
|
let vote_keypair = Keypair::new();
|
|
|
|
let auth_voter_keypair = Keypair::new();
|
|
|
|
let bank_hash = Hash::default();
|
|
|
|
let vote_tx = new_vote_transaction(
|
|
|
|
vec![42],
|
|
|
|
bank_hash,
|
|
|
|
Hash::default(),
|
|
|
|
&node_keypair,
|
|
|
|
&vote_keypair,
|
|
|
|
&auth_voter_keypair,
|
|
|
|
input_hash,
|
|
|
|
);
|
2022-05-19 18:28:46 -07:00
|
|
|
let (key, vote, hash, signature) = parse_vote_transaction(&vote_tx).unwrap();
|
2022-01-19 18:39:21 -08:00
|
|
|
assert_eq!(hash, input_hash);
|
|
|
|
assert_eq!(vote, VoteTransaction::from(Vote::new(vec![42], bank_hash)));
|
|
|
|
assert_eq!(key, vote_keypair.pubkey());
|
2022-05-19 18:28:46 -07:00
|
|
|
assert_eq!(signature, vote_tx.signatures[0]);
|
2022-01-19 18:39:21 -08:00
|
|
|
|
|
|
|
// Test bad program id fails
|
|
|
|
let mut vote_ix = vote_instruction::vote(
|
|
|
|
&vote_keypair.pubkey(),
|
|
|
|
&auth_voter_keypair.pubkey(),
|
|
|
|
Vote::new(vec![1, 2], Hash::default()),
|
|
|
|
);
|
|
|
|
vote_ix.program_id = Pubkey::default();
|
|
|
|
let vote_tx = Transaction::new_with_payer(&[vote_ix], Some(&node_keypair.pubkey()));
|
|
|
|
assert!(parse_vote_transaction(&vote_tx).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_vote_transaction() {
|
|
|
|
run_test_parse_vote_transaction(None);
|
|
|
|
run_test_parse_vote_transaction(Some(hash(&[42u8])));
|
|
|
|
}
|
|
|
|
}
|