celestia_types/
error.rs

1use crate::consts::appconsts;
2
3/// Alias for a `Result` with the error type [`celestia_types::Error`].
4///
5/// [`celestia_types::Error`]: crate::Error
6pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8/// Representation of all the errors that can occur when interacting with [`celestia_types`].
9///
10/// [`celestia_types`]: crate
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13    /// Unsupported namespace version.
14    #[error("Unsupported namespace version: {0}")]
15    UnsupportedNamespaceVersion(u8),
16
17    /// Unsupported app version.
18    #[error("Unsupported app version: {0}")]
19    UnsupportedAppVersion(u64),
20
21    /// Invalid namespace size.
22    #[error("Invalid namespace size")]
23    InvalidNamespaceSize,
24
25    /// Error propagated from the [`tendermint`].
26    #[error(transparent)]
27    Tendermint(#[from] tendermint::Error),
28
29    /// Error propagated from the [`tendermint_proto`].
30    #[error(transparent)]
31    Protobuf(#[from] tendermint_proto::Error),
32
33    /// Error propagated from the [`cid::multihash`].
34    #[error(transparent)]
35    Multihash(#[from] cid::multihash::Error),
36
37    /// Error returned when trying to compute new or parse existing CID. See [`blockstore::block`]
38    #[error(transparent)]
39    CidError(#[from] blockstore::block::CidError),
40
41    /// Error propagated from the [`leopard_codec`].
42    #[error(transparent)]
43    LeopardCodec(#[from] leopard_codec::LeopardError),
44
45    /// Missing header.
46    #[error("Missing header")]
47    MissingHeader,
48
49    /// Missing commit.
50    #[error("Missing commit")]
51    MissingCommit,
52
53    /// Missing validator set.
54    #[error("Missing validator set")]
55    MissingValidatorSet,
56
57    /// Missing data availability header.
58    #[error("Missing data availability header")]
59    MissingDataAvailabilityHeader,
60
61    /// Missing proof.
62    #[error("Missing proof")]
63    MissingProof,
64
65    /// Missing shares.
66    #[error("Missing shares")]
67    MissingShares,
68
69    /// Missing fee field
70    #[error("Missing fee field")]
71    MissingFee,
72
73    /// Missing sum field
74    #[error("Missing sum field")]
75    MissingSum,
76
77    /// Missing mode info field
78    #[error("Missing mode info field")]
79    MissingModeInfo,
80
81    /// Missing bitarray field
82    #[error("Missing bitarray field")]
83    MissingBitarray,
84
85    /// Bit array too large
86    #[error("Bit array to large")]
87    BitarrayTooLarge,
88
89    /// Malformed CompactBitArray
90    #[error("CompactBitArray malformed")]
91    MalformedCompactBitArray,
92
93    /// Wrong proof type.
94    #[error("Wrong proof type")]
95    WrongProofType,
96
97    /// Unsupported share version.
98    #[error("Unsupported share version: {0}")]
99    UnsupportedShareVersion(u8),
100
101    /// Invalid share size.
102    #[error("Invalid share size: {0}")]
103    InvalidShareSize(usize),
104
105    /// Invalid nmt leaf size.
106    #[error("Invalid nmt leaf size: {0}")]
107    InvalidNmtLeafSize(usize),
108
109    /// Invalid nmt node order.
110    #[error("Invalid nmt node order")]
111    InvalidNmtNodeOrder,
112
113    /// Share sequence length exceeded.
114    #[error(
115        "Sequence len must fit into {} bytes, got value {0}",
116        appconsts::SEQUENCE_LEN_BYTES
117    )]
118    ShareSequenceLenExceeded(usize),
119
120    /// Invalid namespace in version 0.
121    #[error("Invalid namespace v0")]
122    InvalidNamespaceV0,
123
124    /// Invalid namespace in version 255.
125    #[error("Invalid namespace v255")]
126    InvalidNamespaceV255,
127
128    /// Invalid namespaced hash.
129    #[error(transparent)]
130    InvalidNamespacedHash(#[from] nmt_rs::InvalidNamespacedHash),
131
132    /// Invalid index of signature in commit.
133    #[error("Invalid index of signature in commit {0}, height {1}")]
134    InvalidSignatureIndex(usize, u64),
135
136    /// Invalid axis.
137    #[error("Invalid axis type: {0}")]
138    InvalidAxis(i32),
139
140    /// Invalid Shwap proof type in Protobuf.
141    #[error("Invalid proof type: {0}")]
142    InvalidShwapProofType(i32),
143
144    /// Could not deserialise Public Key
145    #[error("Could not deserialize public key")]
146    InvalidPublicKey,
147
148    /// Range proof verification error.
149    #[error("Range proof verification failed: {0:?}")]
150    RangeProofError(nmt_rs::simple_merkle::error::RangeProofError),
151
152    /// Row root computed from shares doesn't match one received in `DataAvailabilityHeaderz
153    #[error("Computed root doesn't match received one")]
154    RootMismatch,
155
156    /// Unexpected signature in absent commit.
157    #[error("Unexpected absent commit signature")]
158    UnexpectedAbsentSignature,
159
160    /// Error that happened during validation.
161    #[error("Validation error: {0}")]
162    Validation(#[from] ValidationError),
163
164    /// Error that happened during verification.
165    #[error("Verification error: {0}")]
166    Verification(#[from] VerificationError),
167
168    /// Max share version exceeded.
169    #[error(
170        "Share version has to be at most {}, got {0}",
171        appconsts::MAX_SHARE_VERSION
172    )]
173    MaxShareVersionExceeded(u8),
174
175    /// An error related to the namespaced merkle tree.
176    #[error("Nmt error: {0}")]
177    Nmt(&'static str),
178
179    /// Invalid address bech32 prefix.
180    #[error("Invalid address prefix: {0}")]
181    InvalidAddressPrefix(String),
182
183    /// Invalid size of the address.
184    #[error("Invalid address size: {0}")]
185    InvalidAddressSize(usize),
186
187    /// Invalid address.
188    #[error("Invalid address: {0}")]
189    InvalidAddress(String),
190
191    /// Invalid balance denomination.
192    #[error("Invalid balance denomination: {0}")]
193    InvalidBalanceDenomination(String),
194
195    /// Invalid balance amount.
196    #[error("Invalid balance amount: {0}")]
197    InvalidBalanceAmount(String),
198
199    /// Invalid coin amount.
200    #[error("Invalid coin amount: {0}")]
201    InvalidCoinAmount(String),
202
203    /// Invalid Public Key
204    #[error("Invalid Public Key")]
205    InvalidPublicKeyType(String),
206
207    /// Unsupported fraud proof type.
208    #[error("Unsupported fraud proof type: {0}")]
209    UnsupportedFraudProofType(String),
210
211    /// Data square index out of range.
212    #[error("Index ({0}) out of range ({1})")]
213    IndexOutOfRange(usize, usize),
214
215    /// Data square index out of range.
216    #[error("Data square index out of range. row: {0}, column: {1}")]
217    EdsIndexOutOfRange(u16, u16),
218
219    /// Could not create EDS, provided number of shares doesn't form a pefect square.
220    #[error("Invalid dimensions of EDS")]
221    EdsInvalidDimentions,
222
223    /// Zero block height.
224    #[error("Invalid zero block height")]
225    ZeroBlockHeight,
226
227    /// Expected first share of a blob
228    #[error("Expected first share of a blob")]
229    ExpectedShareWithSequenceStart,
230
231    /// Unexpected share from reserved namespace.
232    #[error("Unexpected share from reserved namespace")]
233    UnexpectedReservedNamespace,
234
235    /// Unexpected start of a new blob
236    #[error("Unexpected start of a new blob")]
237    UnexpectedSequenceStart,
238
239    /// Metadata mismatch between shares in blob.
240    #[error("Metadata mismatch between shares in blob: {0}")]
241    BlobSharesMetadataMismatch(String),
242
243    /// Blob too large, length must fit u32
244    #[error("Blob too large")]
245    BlobTooLarge,
246
247    /// Invalid comittment length
248    #[error("Invalid committment length")]
249    InvalidComittmentLength,
250
251    /// Missing signer field in blob with share version 1
252    #[error("Missing signer field in blob")]
253    MissingSigner,
254
255    /// Signer is not supported in share version 0
256    #[error("Signer is not supported in share version 0")]
257    SignerNotSupported,
258
259    /// Empty blob list provided when creating MsgPayForBlobs
260    #[error("Empty blob list")]
261    EmptyBlobList,
262}
263
264impl From<prost::DecodeError> for Error {
265    fn from(value: prost::DecodeError) -> Self {
266        Error::Protobuf(tendermint_proto::Error::decode_message(value))
267    }
268}
269
270#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
271impl From<Error> for wasm_bindgen::JsValue {
272    fn from(value: Error) -> Self {
273        js_sys::Error::new(&value.to_string()).into()
274    }
275}
276
277/// Representation of the errors that can occur when validating data.
278///
279/// See [`ValidateBasic`]
280///
281/// [`ValidateBasic`]: crate::ValidateBasic
282#[derive(Debug, thiserror::Error)]
283pub enum ValidationError {
284    /// Not enough voting power for a commit.
285    #[error("Not enought voting power (got {0}, needed {1})")]
286    NotEnoughVotingPower(u64, u64),
287
288    /// Other errors that can happen during validation.
289    #[error("{0}")]
290    Other(String),
291}
292
293/// Representation of the errors that can occur when verifying data.
294///
295/// See [`ExtendedHeader::verify`].
296///
297/// [`ExtendedHeader::verify`]: crate::ExtendedHeader::verify
298#[derive(Debug, thiserror::Error)]
299pub enum VerificationError {
300    /// Not enough voting power for a commit.
301    #[error("Not enought voting power (got {0}, needed {1})")]
302    NotEnoughVotingPower(u64, u64),
303
304    /// Other errors that can happen during verification.
305    #[error("{0}")]
306    Other(String),
307}
308
309macro_rules! validation_error {
310    ($fmt:literal $(,)?) => {
311        $crate::ValidationError::Other(std::format!($fmt))
312    };
313    ($fmt:literal, $($arg:tt)*) => {
314        $crate::ValidationError::Other(std::format!($fmt, $($arg)*))
315    };
316}
317
318macro_rules! bail_validation {
319    ($($arg:tt)*) => {
320        return Err($crate::validation_error!($($arg)*).into())
321    };
322}
323
324macro_rules! verification_error {
325    ($fmt:literal $(,)?) => {
326        $crate::VerificationError::Other(std::format!($fmt))
327    };
328    ($fmt:literal, $($arg:tt)*) => {
329        $crate::VerificationError::Other(std::format!($fmt, $($arg)*))
330    };
331}
332
333macro_rules! bail_verification {
334    ($($arg:tt)*) => {
335        return Err($crate::verification_error!($($arg)*).into())
336    };
337}
338
339// NOTE: This need to be always after the macro definitions
340pub(crate) use bail_validation;
341pub(crate) use bail_verification;
342pub(crate) use validation_error;
343pub(crate) use verification_error;