Skip to main content

zebra_chain/
error.rs

1//! Errors that can occur inside any `zebra-chain` submodule.
2
3use std::{io, sync::Arc};
4use thiserror::Error;
5use zcash_protocol::value::BalanceError;
6
7// TODO: Move all these enums into a common enum at the bottom.
8
9/// Errors related to random bytes generation.
10#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
11pub enum RandError {
12    /// Error of the `try_fill_bytes` function.
13    #[error("failed to generate a secure stream of random bytes")]
14    FillBytes,
15}
16
17/// An error type pertaining to note commitments.
18#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
19pub enum NoteCommitmentError {
20    /// Errors of type `RandError`.
21    #[error("Randomness generation failure")]
22    InsufficientRandomness(#[from] RandError),
23    /// Error of `jubjub::AffinePoint::try_from`.
24    #[error("failed to generate a sapling::NoteCommitment from a diversifier")]
25    InvalidDiversifier,
26}
27
28/// An error type pertaining to key generation, parsing, modification,
29/// randomization.
30#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
31pub enum KeyError {
32    /// Errors of type `RandError`.
33    #[error("Randomness generation failure")]
34    InsufficientRandomness(#[from] RandError),
35}
36
37/// An error type pertaining to payment address generation, parsing,
38/// modification, diversification.
39#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
40pub enum AddressError {
41    /// Errors of type `RandError`.
42    #[error("Randomness generation failure")]
43    InsufficientRandomness(#[from] RandError),
44    /// Errors pertaining to diversifier generation.
45    #[error("Randomness did not hash into the Jubjub group for producing a new diversifier")]
46    DiversifierGenerationFailure,
47}
48
49/// `zebra-chain`'s errors
50#[derive(Clone, Error, Debug)]
51pub enum Error {
52    /// Invalid consensus branch ID.
53    #[error("invalid consensus branch id")]
54    InvalidConsensusBranchId,
55
56    /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and associated traits.
57    #[error(transparent)]
58    Io(#[from] Arc<io::Error>),
59
60    /// The transaction is missing a network upgrade.
61    #[error("the transaction is missing a network upgrade")]
62    MissingNetworkUpgrade,
63
64    /// Invalid amount.
65    #[error(transparent)]
66    Amount(#[from] BalanceError),
67
68    /// Zebra's type could not be converted to its librustzcash equivalent.
69    #[error("Zebra's type could not be converted to its librustzcash equivalent: {0}")]
70    Conversion(String),
71}
72
73/// Allow converting `io::Error` to `Error`; we need this since we
74/// use `Arc<io::Error>` in `Error::Conversion`.
75impl From<io::Error> for Error {
76    fn from(value: io::Error) -> Self {
77        Arc::new(value).into()
78    }
79}
80
81// We need to implement this manually because io::Error does not implement
82// PartialEq.
83impl PartialEq for Error {
84    fn eq(&self, other: &Self) -> bool {
85        match self {
86            Error::InvalidConsensusBranchId => matches!(other, Error::InvalidConsensusBranchId),
87            Error::Io(e) => {
88                if let Error::Io(o) = other {
89                    // Not perfect, but good enough for testing, which
90                    // is the main purpose for our usage of PartialEq for errors
91                    e.to_string() == o.to_string()
92                } else {
93                    false
94                }
95            }
96            Error::MissingNetworkUpgrade => matches!(other, Error::MissingNetworkUpgrade),
97            Error::Amount(e) => matches!(other, Error::Amount(o) if e == o),
98            Error::Conversion(e) => matches!(other, Error::Conversion(o) if e == o),
99        }
100    }
101}
102
103impl Eq for Error {}