Skip to content

Commit

Permalink
init errors
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Apr 4, 2024
1 parent 27bbb69 commit 9580741
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { version = "1.0.193", features = ["derive"] }
solana-frozen-abi-macro = "1.18.2"
solana-frozen-abi = "1.18.2"
solana-program = "1.18.2"
spl-program-error = "0.3.1"

[lib]
crate-type = ["cdylib", "lib"]
Expand Down
103 changes: 103 additions & 0 deletions program/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//! Vote program errors
use spl_program_error::*;

/// Reasons the vote might have had an error
#[spl_program_error]
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,

#[error("Proposed root is not in slot hashes")]
RootOnDifferentFork,

#[error("Cannot close vote account unless it stopped voting at least one full epoch ago")]
ActiveVoteAccountClose,

#[error("Cannot update commission at this point in the epoch")]
CommissionUpdateTooLate,
}

#[cfg(test)]
mod tests {
use {
super::*,
solana_program::{decode_error::DecodeError, program_error::ProgramError},
};

#[test]
fn test_custom_error_decode_to_program_error() {
use num_traits::FromPrimitive;
fn pretty_err<T>(err: ProgramError) -> String
where
T: 'static + std::error::Error + DecodeError<T> + FromPrimitive,
{
if let ProgramError::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())
)
}
}
1 change: 1 addition & 0 deletions program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#[cfg(all(target_os = "solana", feature = "bpf-entrypoint"))]
mod entrypoint;
pub mod error;

// [Core BPF]: TODO: Program-test will not overwrite existing built-ins.
// See https://github.com/solana-labs/solana/pull/35233.
Expand Down

0 comments on commit 9580741

Please sign in to comment.