-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
simplifies parse [sanitized] vote transaction #22127
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,77 +16,51 @@ use { | |||||||
|
||||||||
pub type ParsedVote = (Pubkey, VoteTransaction, Option<Hash>); | ||||||||
|
||||||||
fn parse_vote(vote_ix: &CompiledInstruction, vote_key: &Pubkey) -> Option<ParsedVote> { | ||||||||
let vote_instruction = limited_deserialize(&vote_ix.data).ok(); | ||||||||
vote_instruction.and_then(|vote_instruction| { | ||||||||
let result: Option<ParsedVote> = match vote_instruction { | ||||||||
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) => { | ||||||||
Some((*vote_key, VoteTransaction::from(vote_state_update), None)) | ||||||||
} | ||||||||
VoteInstruction::UpdateVoteStateSwitch(vote_state_update, hash) => Some(( | ||||||||
*vote_key, | ||||||||
VoteTransaction::from(vote_state_update), | ||||||||
Some(hash), | ||||||||
)), | ||||||||
VoteInstruction::Authorize(_, _) | ||||||||
| VoteInstruction::AuthorizeChecked(_) | ||||||||
| VoteInstruction::InitializeAccount(_) | ||||||||
| VoteInstruction::UpdateCommission(_) | ||||||||
| VoteInstruction::UpdateValidatorIdentity | ||||||||
| VoteInstruction::Withdraw(_) => None, | ||||||||
}; | ||||||||
result | ||||||||
}) | ||||||||
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(); | ||||||||
message | ||||||||
.program_instructions_iter() | ||||||||
.next() | ||||||||
.and_then(|(program_id, first_ix)| { | ||||||||
if !crate::check_id(program_id) { | ||||||||
return None; | ||||||||
} | ||||||||
|
||||||||
first_ix.accounts.first().and_then(|first_account| { | ||||||||
message | ||||||||
.get_account_key(*first_account as usize) | ||||||||
.and_then(|key| parse_vote(first_ix, key)) | ||||||||
}) | ||||||||
}) | ||||||||
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(); | ||||||||
message.instructions.get(0).and_then(|first_instruction| { | ||||||||
let prog_id_idx = first_instruction.program_id_index as usize; | ||||||||
match message.account_keys.get(prog_id_idx) { | ||||||||
Some(program_id) => { | ||||||||
if !crate::check_id(program_id) { | ||||||||
return None; | ||||||||
} | ||||||||
} | ||||||||
_ => { | ||||||||
return None; | ||||||||
} | ||||||||
}; | ||||||||
first_instruction | ||||||||
.accounts | ||||||||
.first() | ||||||||
.and_then(|first_account| { | ||||||||
message | ||||||||
.account_keys | ||||||||
.get(*first_account as usize) | ||||||||
.and_then(|key| parse_vote(first_instruction, key)) | ||||||||
}) | ||||||||
}) | ||||||||
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()?); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
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( | ||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. but I prefer to keep this commit precisely the same logic as the existing code.
We can introduce that change in a separate commit