dusk_core/
error.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7//! Error-type for dusk-core.
8
9use alloc::string::{String, ToString};
10use core::fmt;
11
12/// The dusk-core error type.
13#[derive(Debug, Clone, PartialEq)]
14pub enum Error {
15    /// There is not sufficient balance to cover the transaction costs.
16    InsufficientBalance,
17    /// A transaction input has been used already.
18    Replay,
19    /// The input-note doesn't belong to the given key.
20    PhoenixOwnership,
21    /// The transaction circuit wasn't found or is incorrect.
22    PhoenixCircuit(String),
23    /// The transaction circuit prover wasn't found or couldn't be created.
24    PhoenixProver(String),
25    /// Dusk-bytes `InvalidData` error
26    InvalidData,
27    /// Dusk-bytes `BadLength` error
28    BadLength(usize, usize),
29    /// Dusk-bytes `InvalidChar` error
30    InvalidChar(char, usize),
31    /// Rkyv serialization.
32    Rkyv(String),
33    /// Blob KZG related.
34    Blob(String),
35    /// The provided memo is too large. Contains the memo size used. The max
36    /// size is [`MAX_MEMO_SIZE`].
37    ///
38    /// [`MAX_MEMO_SIZE`]: crate::transfer::data::MAX_MEMO_SIZE
39    MemoTooLarge(usize),
40}
41
42impl fmt::Display for Error {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "Dusk-Core Error: {:?}", &self)
45    }
46}
47
48impl From<phoenix_core::Error> for Error {
49    fn from(core_error: phoenix_core::Error) -> Self {
50        #[allow(clippy::match_same_arms)]
51        match core_error {
52            phoenix_core::Error::InvalidNoteType(_) => Self::InvalidData,
53            phoenix_core::Error::MissingViewKey => Self::PhoenixOwnership,
54            phoenix_core::Error::InvalidEncryption => Self::PhoenixOwnership,
55            phoenix_core::Error::InvalidData => Self::InvalidData,
56            phoenix_core::Error::BadLength(found, expected) => {
57                Self::BadLength(found, expected)
58            }
59            phoenix_core::Error::InvalidChar(ch, index) => {
60                Self::InvalidChar(ch, index)
61            }
62        }
63    }
64}
65
66impl From<dusk_bytes::Error> for Error {
67    fn from(bytes_error: dusk_bytes::Error) -> Self {
68        match bytes_error {
69            dusk_bytes::Error::InvalidData => Self::InvalidData,
70            dusk_bytes::Error::BadLength { found, expected } => {
71                Self::BadLength(found, expected)
72            }
73            dusk_bytes::Error::InvalidChar { ch, index } => {
74                Self::InvalidChar(ch, index)
75            }
76        }
77    }
78}
79
80/// Error type for checking transaction conditions.
81///
82/// This error is used to indicate that a transaction does not meet the
83/// minimum requirements for deployment or blob gas charges.
84#[derive(Debug, Clone, PartialEq)]
85pub enum TxPreconditionError {
86    /// The gas price is too low to deploy a transaction.
87    DeployLowPrice(u64),
88    /// The gas limit is too low to deploy a transaction.
89    DeployLowLimit(u64),
90    /// The gas limit is too low to cover the blob gas charges.
91    BlobLowLimit(u64),
92    /// No blob attached to the transaction.
93    BlobEmpty,
94    /// Too many blobs attached to the transaction.
95    BlobTooMany(usize),
96}
97
98impl TxPreconditionError {
99    /// Return the implementation of toString to be used inside the VM.
100    ///
101    /// Replacing this with the standard display will break the state root
102    /// backward compatibility
103    #[must_use]
104    pub fn legacy_to_string(&self) -> String {
105        match self {
106            TxPreconditionError::DeployLowPrice(_) => {
107                "gas price too low to deploy"
108            }
109            TxPreconditionError::DeployLowLimit(_) => {
110                "not enough gas to deploy"
111            }
112            TxPreconditionError::BlobLowLimit(_) => "not enough gas for blobs",
113            TxPreconditionError::BlobEmpty => {
114                "no blob attached to the transaction"
115            }
116            TxPreconditionError::BlobTooMany(_) => {
117                "too many blobs in the transaction"
118            }
119        }
120        .to_string()
121    }
122}