Refactor: move simple vote parsing to runtime (#22537)

This commit is contained in:
Justin Starry
2022-01-20 10:39:21 +08:00
committed by GitHub
parent d343713f61
commit 7f20c6149e
16 changed files with 239 additions and 220 deletions

View File

@ -41,73 +41,6 @@ pub const MAX_EPOCH_CREDITS_HISTORY: usize = 64;
// Offset of VoteState::prior_voters, for determining initialization status without deserialization
const DEFAULT_PRIOR_VOTERS_OFFSET: usize = 82;
#[derive(Debug, PartialEq)]
pub enum VoteTransaction {
Vote(Vote),
VoteStateUpdate(VoteStateUpdate),
}
impl VoteTransaction {
pub fn slots(&self) -> Vec<Slot> {
match self {
VoteTransaction::Vote(vote) => vote.slots.clone(),
VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update
.lockouts
.iter()
.map(|lockout| lockout.slot)
.collect(),
}
}
pub fn is_empty(&self) -> bool {
match self {
VoteTransaction::Vote(vote) => vote.slots.is_empty(),
VoteTransaction::VoteStateUpdate(vote_state_update) => {
vote_state_update.lockouts.is_empty()
}
}
}
pub fn hash(&self) -> Hash {
match self {
VoteTransaction::Vote(vote) => vote.hash,
VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update.hash,
}
}
pub fn timestamp(&self) -> Option<UnixTimestamp> {
match self {
VoteTransaction::Vote(vote) => vote.timestamp,
VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update.timestamp,
}
}
pub fn last_voted_slot(&self) -> Option<Slot> {
match self {
VoteTransaction::Vote(vote) => vote.slots.last().copied(),
VoteTransaction::VoteStateUpdate(vote_state_update) => {
Some(vote_state_update.lockouts.back()?.slot)
}
}
}
pub fn last_voted_slot_hash(&self) -> Option<(Slot, Hash)> {
Some((self.last_voted_slot()?, self.hash()))
}
}
impl From<Vote> for VoteTransaction {
fn from(vote: Vote) -> Self {
VoteTransaction::Vote(vote)
}
}
impl From<VoteStateUpdate> for VoteTransaction {
fn from(vote_state_update: VoteStateUpdate) -> Self {
VoteTransaction::VoteStateUpdate(vote_state_update)
}
}
#[frozen_abi(digest = "Ch2vVEwos2EjAVqSHCyJjnN2MNX1yrpapZTGhMSCjWUH")]
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone, AbiExample)]
pub struct Vote {

View File

@ -1,68 +1,13 @@
use {
crate::{
vote_instruction::{self, VoteInstruction},
vote_state::{Vote, VoteTransaction},
},
crate::{vote_instruction, vote_state::Vote},
solana_sdk::{
clock::Slot,
hash::Hash,
instruction::CompiledInstruction,
program_utils::limited_deserialize,
pubkey::Pubkey,
signature::{Keypair, Signer},
transaction::{SanitizedTransaction, Transaction},
transaction::Transaction,
},
};
pub type ParsedVote = (Pubkey, VoteTransaction, Option<Hash>);
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();
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();
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 !crate::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(first_instruction)?;
Some((*key, vote, switch_proof_hash))
}
pub fn new_vote_transaction(
slots: Vec<Slot>,
bank_hash: Hash,
@ -94,44 +39,3 @@ pub fn new_vote_transaction(
vote_tx.partial_sign(&[authorized_voter_keypair], blockhash);
vote_tx
}
#[cfg(test)]
mod test {
use {super::*, solana_sdk::hash::hash};
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,
);
let (key, vote, hash) = parse_vote_transaction(&vote_tx).unwrap();
assert_eq!(hash, input_hash);
assert_eq!(vote, VoteTransaction::from(Vote::new(vec![42], bank_hash)));
assert_eq!(key, vote_keypair.pubkey());
// 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])));
}
}