1use crate::consts::appconsts;
2
3pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8#[derive(Debug, thiserror::Error)]
12pub enum Error {
13 #[error("Unsupported namespace version: {0}")]
15 UnsupportedNamespaceVersion(u8),
16
17 #[error("Unsupported app version: {0}")]
19 UnsupportedAppVersion(u64),
20
21 #[error("Invalid namespace size")]
23 InvalidNamespaceSize,
24
25 #[error(transparent)]
27 Tendermint(#[from] tendermint::Error),
28
29 #[error(transparent)]
31 Protobuf(#[from] tendermint_proto::Error),
32
33 #[error(transparent)]
35 Multihash(#[from] cid::multihash::Error),
36
37 #[error(transparent)]
39 CidError(#[from] blockstore::block::CidError),
40
41 #[error(transparent)]
43 LeopardCodec(#[from] leopard_codec::LeopardError),
44
45 #[error("Missing header")]
47 MissingHeader,
48
49 #[error("Missing commit")]
51 MissingCommit,
52
53 #[error("Missing validator set")]
55 MissingValidatorSet,
56
57 #[error("Missing data availability header")]
59 MissingDataAvailabilityHeader,
60
61 #[error("Missing proof")]
63 MissingProof,
64
65 #[error("Missing shares")]
67 MissingShares,
68
69 #[error("Missing fee field")]
71 MissingFee,
72
73 #[error("Missing sum field")]
75 MissingSum,
76
77 #[error("Missing mode info field")]
79 MissingModeInfo,
80
81 #[error("Missing bitarray field")]
83 MissingBitarray,
84
85 #[error("Bit array to large")]
87 BitarrayTooLarge,
88
89 #[error("CompactBitArray malformed")]
91 MalformedCompactBitArray,
92
93 #[error("Wrong proof type")]
95 WrongProofType,
96
97 #[error("Unsupported share version: {0}")]
99 UnsupportedShareVersion(u8),
100
101 #[error("Invalid share size: {0}")]
103 InvalidShareSize(usize),
104
105 #[error("Invalid nmt leaf size: {0}")]
107 InvalidNmtLeafSize(usize),
108
109 #[error("Invalid nmt node order")]
111 InvalidNmtNodeOrder,
112
113 #[error(
115 "Sequence len must fit into {} bytes, got value {0}",
116 appconsts::SEQUENCE_LEN_BYTES
117 )]
118 ShareSequenceLenExceeded(usize),
119
120 #[error("Invalid namespace v0")]
122 InvalidNamespaceV0,
123
124 #[error("Invalid namespace v255")]
126 InvalidNamespaceV255,
127
128 #[error(transparent)]
130 InvalidNamespacedHash(#[from] nmt_rs::InvalidNamespacedHash),
131
132 #[error("Invalid index of signature in commit {0}, height {1}")]
134 InvalidSignatureIndex(usize, u64),
135
136 #[error("Invalid axis type: {0}")]
138 InvalidAxis(i32),
139
140 #[error("Invalid proof type: {0}")]
142 InvalidShwapProofType(i32),
143
144 #[error("Could not deserialize public key")]
146 InvalidPublicKey,
147
148 #[error("Range proof verification failed: {0:?}")]
150 RangeProofError(nmt_rs::simple_merkle::error::RangeProofError),
151
152 #[error("Computed root doesn't match received one")]
154 RootMismatch,
155
156 #[error("Unexpected absent commit signature")]
158 UnexpectedAbsentSignature,
159
160 #[error("Validation error: {0}")]
162 Validation(#[from] ValidationError),
163
164 #[error("Verification error: {0}")]
166 Verification(#[from] VerificationError),
167
168 #[error(
170 "Share version has to be at most {}, got {0}",
171 appconsts::MAX_SHARE_VERSION
172 )]
173 MaxShareVersionExceeded(u8),
174
175 #[error("Nmt error: {0}")]
177 Nmt(&'static str),
178
179 #[error("Invalid address prefix: {0}")]
181 InvalidAddressPrefix(String),
182
183 #[error("Invalid address size: {0}")]
185 InvalidAddressSize(usize),
186
187 #[error("Invalid address: {0}")]
189 InvalidAddress(String),
190
191 #[error("Invalid balance denomination: {0}")]
193 InvalidBalanceDenomination(String),
194
195 #[error("Invalid balance amount: {0}")]
197 InvalidBalanceAmount(String),
198
199 #[error("Invalid coin amount: {0}")]
201 InvalidCoinAmount(String),
202
203 #[error("Invalid Public Key")]
205 InvalidPublicKeyType(String),
206
207 #[error("Unsupported fraud proof type: {0}")]
209 UnsupportedFraudProofType(String),
210
211 #[error("Index ({0}) out of range ({1})")]
213 IndexOutOfRange(usize, usize),
214
215 #[error("Data square index out of range. row: {0}, column: {1}")]
217 EdsIndexOutOfRange(u16, u16),
218
219 #[error("Invalid dimensions of EDS")]
221 EdsInvalidDimentions,
222
223 #[error("Invalid zero block height")]
225 ZeroBlockHeight,
226
227 #[error("Expected first share of a blob")]
229 ExpectedShareWithSequenceStart,
230
231 #[error("Unexpected share from reserved namespace")]
233 UnexpectedReservedNamespace,
234
235 #[error("Unexpected start of a new blob")]
237 UnexpectedSequenceStart,
238
239 #[error("Metadata mismatch between shares in blob: {0}")]
241 BlobSharesMetadataMismatch(String),
242
243 #[error("Blob too large")]
245 BlobTooLarge,
246
247 #[error("Invalid committment length")]
249 InvalidComittmentLength,
250
251 #[error("Missing signer field in blob")]
253 MissingSigner,
254
255 #[error("Signer is not supported in share version 0")]
257 SignerNotSupported,
258
259 #[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#[derive(Debug, thiserror::Error)]
283pub enum ValidationError {
284 #[error("Not enought voting power (got {0}, needed {1})")]
286 NotEnoughVotingPower(u64, u64),
287
288 #[error("{0}")]
290 Other(String),
291}
292
293#[derive(Debug, thiserror::Error)]
299pub enum VerificationError {
300 #[error("Not enought voting power (got {0}, needed {1})")]
302 NotEnoughVotingPower(u64, u64),
303
304 #[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
339pub(crate) use bail_validation;
341pub(crate) use bail_verification;
342pub(crate) use validation_error;
343pub(crate) use verification_error;