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