use solana_sanitize::SanitizeError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MessageError {
BufferTooSmall,
InvalidHeapSize,
InstructionAccountsTooLarge,
InstructionDataTooLarge,
InvalidConfigMask,
InvalidInstructionAccountIndex,
InvalidProgramIdIndex,
InvalidVersion,
MissingLifetimeSpecifier,
NotEnoughAddressesForSignatures,
TooManyAddresses,
TooManyInstructions,
TooManySignatures,
TrailingData,
TransactionTooLarge,
ZeroSigners,
DuplicateAddresses,
InvalidConfigValue,
NotEnoughAccountKeys,
}
impl core::fmt::Display for MessageError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::BufferTooSmall => write!(f, "buffer too small"),
Self::InvalidHeapSize => write!(f, "heap size must be a multiple of 1024"),
Self::InstructionAccountsTooLarge => {
write!(f, "instruction has too many accounts (max 255)")
}
Self::InstructionDataTooLarge => {
write!(f, "instruction data too large (max 65535 bytes)")
}
Self::InvalidConfigMask => write!(f, "invalid transaction config mask"),
Self::InvalidInstructionAccountIndex => {
write!(f, "instruction account index out of bounds")
}
Self::InvalidProgramIdIndex => {
write!(f, "program ID index out of bounds or is fee payer")
}
Self::InvalidVersion => write!(f, "invalid version byte (expected 0x81)"),
Self::MissingLifetimeSpecifier => {
write!(f, "lifetime specifier (blockhash) is required")
}
Self::NotEnoughAddressesForSignatures => {
write!(f, "not enough addresses for required signatures")
}
Self::TooManyAddresses => write!(f, "too many addresses (max 64)"),
Self::TooManyInstructions => write!(f, "too many instructions (max 64)"),
Self::TooManySignatures => write!(f, "too many signatures (max 12)"),
Self::TrailingData => write!(f, "unexpected trailing data"),
Self::TransactionTooLarge => write!(f, "transaction exceeds max size (4096 bytes)"),
Self::ZeroSigners => write!(f, "must have at least one signer (fee payer)"),
Self::DuplicateAddresses => write!(f, "duplicate addresses found in message"),
Self::InvalidConfigValue => write!(f, "invalid configuration value"),
Self::NotEnoughAccountKeys => write!(f, "not enough account keys provided"),
}
}
}
impl core::error::Error for MessageError {}
impl From<MessageError> for SanitizeError {
fn from(err: MessageError) -> Self {
match err {
MessageError::BufferTooSmall
| MessageError::InvalidHeapSize
| MessageError::InstructionAccountsTooLarge
| MessageError::InstructionDataTooLarge
| MessageError::InvalidConfigMask
| MessageError::InvalidVersion
| MessageError::MissingLifetimeSpecifier
| MessageError::TrailingData
| MessageError::TransactionTooLarge
| MessageError::ZeroSigners
| MessageError::DuplicateAddresses
| MessageError::InvalidConfigValue
| MessageError::NotEnoughAccountKeys => SanitizeError::InvalidValue,
MessageError::InvalidInstructionAccountIndex
| MessageError::InvalidProgramIdIndex
| MessageError::NotEnoughAddressesForSignatures
| MessageError::TooManyAddresses
| MessageError::TooManyInstructions
| MessageError::TooManySignatures => SanitizeError::IndexOutOfBounds,
}
}
}