Skip to content

Commit

Permalink
Refactor: Split vote_instruction.rs into multiple files (#22502)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored Jan 14, 2022
1 parent 93a7b94 commit ae6c511
Show file tree
Hide file tree
Showing 7 changed files with 615 additions and 581 deletions.
3 changes: 2 additions & 1 deletion cli/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ use {
transaction::Transaction,
},
solana_vote_program::{
vote_instruction::{self, withdraw, VoteError},
vote_error::VoteError,
vote_instruction::{self, withdraw},
vote_state::{VoteAuthorize, VoteInit, VoteState},
},
std::sync::Arc,
Expand Down
2 changes: 2 additions & 0 deletions programs/vote/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#![allow(clippy::integer_arithmetic)]

pub mod authorized_voters;
pub mod vote_error;
pub mod vote_instruction;
pub mod vote_processor;
pub mod vote_state;
pub mod vote_transaction;

Expand Down
101 changes: 101 additions & 0 deletions programs/vote/src/vote_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//! Vote program errors
use {
log::*,
num_derive::{FromPrimitive, ToPrimitive},
solana_sdk::decode_error::DecodeError,
thiserror::Error,
};

/// Reasons the vote might have had an error
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
pub enum VoteError {
#[error("vote already recorded or not in slot hashes history")]
VoteTooOld,

#[error("vote slots do not match bank history")]
SlotsMismatch,

#[error("vote hash does not match bank hash")]
SlotHashMismatch,

#[error("vote has no slots, invalid")]
EmptySlots,

#[error("vote timestamp not recent")]
TimestampTooOld,

#[error("authorized voter has already been changed this epoch")]
TooSoonToReauthorize,

// TODO: figure out how to migrate these new errors
#[error("Old state had vote which should not have been popped off by vote in new state")]
LockoutConflict,

#[error("Proposed state had earlier slot which should have been popped off by later vote")]
NewVoteStateLockoutMismatch,

#[error("Vote slots are not ordered")]
SlotsNotOrdered,

#[error("Confirmations are not ordered")]
ConfirmationsNotOrdered,

#[error("Zero confirmations")]
ZeroConfirmations,

#[error("Confirmation exceeds limit")]
ConfirmationTooLarge,

#[error("Root rolled back")]
RootRollBack,

#[error("Confirmations for same vote were smaller in new proposed state")]
ConfirmationRollBack,

#[error("New state contained a vote slot smaller than the root")]
SlotSmallerThanRoot,

#[error("New state contained too many votes")]
TooManyVotes,

#[error("every slot in the vote was older than the SlotHashes history")]
VotesTooOldAllFiltered,
}

impl<E> DecodeError<E> for VoteError {
fn type_of() -> &'static str {
"VoteError"
}
}

#[cfg(test)]
mod tests {
use {super::*, solana_sdk::instruction::InstructionError};

#[test]
fn test_custom_error_decode() {
use num_traits::FromPrimitive;
fn pretty_err<T>(err: InstructionError) -> String
where
T: 'static + std::error::Error + DecodeError<T> + FromPrimitive,
{
if let InstructionError::Custom(code) = err {
let specific_error: T = T::decode_custom_error_to_enum(code).unwrap();
format!(
"{:?}: {}::{:?} - {}",
err,
T::type_of(),
specific_error,
specific_error,
)
} else {
"".to_string()
}
}
assert_eq!(
"Custom(0): VoteError::VoteTooOld - vote already recorded or not in slot hashes history",
pretty_err::<VoteError>(VoteError::VoteTooOld.into())
)
}
}
Loading

0 comments on commit ae6c511

Please sign in to comment.