Skip to main content

solana_message/versions/v1/
error.rs

1use solana_sanitize::SanitizeError;
2
3/// Errors that can occur when working with V1 messages.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum MessageError {
6    /// Input buffer is too small during deserialization.
7    BufferTooSmall,
8    /// Heap size is not a multiple of 1024.
9    InvalidHeapSize,
10    /// Instruction has too many accounts (> 255).
11    InstructionAccountsTooLarge,
12    /// Instruction data is too large (> 65535 bytes).
13    InstructionDataTooLarge,
14    /// Invalid TransactionConfigMask.
15    InvalidConfigMask,
16    /// Instruction account index is out of bounds.
17    InvalidInstructionAccountIndex,
18    /// Program ID index is invalid (out of bounds or fee payer).
19    InvalidProgramIdIndex,
20    /// Invalid or missing version byte (expected 0x81).
21    InvalidVersion,
22    /// Lifetime specifier (blockhash) is required.
23    MissingLifetimeSpecifier,
24    /// Not enough addresses for the number of required signatures.
25    NotEnoughAddressesForSignatures,
26    /// Too many addresses (> 64).
27    TooManyAddresses,
28    /// Too many instructions (> 64).
29    TooManyInstructions,
30    /// Too many signatures (> 12).
31    TooManySignatures,
32    /// Unexpected trailing data after message.
33    TrailingData,
34    /// Transaction exceeds maximum size (4096 bytes).
35    TransactionTooLarge,
36    /// Must have at least one signer (fee payer).
37    ZeroSigners,
38    /// Duplicate addresses found in the message.
39    DuplicateAddresses,
40    /// Invalid configuration value.
41    InvalidConfigValue,
42    /// Not enough account keys provided.
43    NotEnoughAccountKeys,
44}
45
46impl core::fmt::Display for MessageError {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        match self {
49            Self::BufferTooSmall => write!(f, "buffer too small"),
50            Self::InvalidHeapSize => write!(f, "heap size must be a multiple of 1024"),
51            Self::InstructionAccountsTooLarge => {
52                write!(f, "instruction has too many accounts (max 255)")
53            }
54            Self::InstructionDataTooLarge => {
55                write!(f, "instruction data too large (max 65535 bytes)")
56            }
57            Self::InvalidConfigMask => write!(f, "invalid transaction config mask"),
58            Self::InvalidInstructionAccountIndex => {
59                write!(f, "instruction account index out of bounds")
60            }
61            Self::InvalidProgramIdIndex => {
62                write!(f, "program ID index out of bounds or is fee payer")
63            }
64            Self::InvalidVersion => write!(f, "invalid version byte (expected 0x81)"),
65            Self::MissingLifetimeSpecifier => {
66                write!(f, "lifetime specifier (blockhash) is required")
67            }
68            Self::NotEnoughAddressesForSignatures => {
69                write!(f, "not enough addresses for required signatures")
70            }
71            Self::TooManyAddresses => write!(f, "too many addresses (max 64)"),
72            Self::TooManyInstructions => write!(f, "too many instructions (max 64)"),
73            Self::TooManySignatures => write!(f, "too many signatures (max 12)"),
74            Self::TrailingData => write!(f, "unexpected trailing data"),
75            Self::TransactionTooLarge => write!(f, "transaction exceeds max size (4096 bytes)"),
76            Self::ZeroSigners => write!(f, "must have at least one signer (fee payer)"),
77            Self::DuplicateAddresses => write!(f, "duplicate addresses found in message"),
78            Self::InvalidConfigValue => write!(f, "invalid configuration value"),
79            Self::NotEnoughAccountKeys => write!(f, "not enough account keys provided"),
80        }
81    }
82}
83
84impl core::error::Error for MessageError {}
85
86impl From<MessageError> for SanitizeError {
87    fn from(err: MessageError) -> Self {
88        match err {
89            MessageError::BufferTooSmall
90            | MessageError::InvalidHeapSize
91            | MessageError::InstructionAccountsTooLarge
92            | MessageError::InstructionDataTooLarge
93            | MessageError::InvalidConfigMask
94            | MessageError::InvalidVersion
95            | MessageError::MissingLifetimeSpecifier
96            | MessageError::TrailingData
97            | MessageError::TransactionTooLarge
98            | MessageError::ZeroSigners
99            | MessageError::DuplicateAddresses
100            | MessageError::InvalidConfigValue
101            | MessageError::NotEnoughAccountKeys => SanitizeError::InvalidValue,
102            MessageError::InvalidInstructionAccountIndex
103            | MessageError::InvalidProgramIdIndex
104            | MessageError::NotEnoughAddressesForSignatures
105            | MessageError::TooManyAddresses
106            | MessageError::TooManyInstructions
107            | MessageError::TooManySignatures => SanitizeError::IndexOutOfBounds,
108        }
109    }
110}