snarkos_errors/storage/
storage.rs

1// Copyright (C) 2019-2020 Aleo Systems Inc.
2// This file is part of the snarkOS library.
3
4// The snarkOS library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The snarkOS library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::{
18    algorithms::MerkleError,
19    objects::{BlockError, TransactionError},
20    parameters::ParametersError,
21};
22
23use bincode;
24use std::fmt::Debug;
25
26#[cfg(feature = "librocksdb")]
27use rocksdb;
28
29#[derive(Debug, Error)]
30pub enum StorageError {
31    #[error("{}: {}", _0, _1)]
32    Crate(&'static str, String),
33
34    #[error("duplicate commitment")]
35    DuplicateCm,
36
37    #[error("duplicate serial number")]
38    DuplicateSn,
39
40    #[error("duplicate transaction memo")]
41    DuplicateMemo,
42
43    #[error("Block has already been committed: {:?}", _0)]
44    ExistingCanonBlock(String),
45
46    #[error("existing record commitment {:?}", _0)]
47    ExistingCm(Vec<u8>),
48
49    #[error("existing transaction memo {:?}", _0)]
50    ExistingMemo(Vec<u8>),
51
52    #[error("existing serial number {:?}", _0)]
53    ExistingSn(Vec<u8>),
54
55    #[error("Can't decommit the genesis block")]
56    InvalidBlockDecommit,
57
58    #[error("Can't remove a canon block with hash")]
59    InvalidBlockRemovalCanon(String),
60
61    #[error("invalid number of blocks to remove {}. There are only {} existing blocks", _0, _1)]
62    InvalidBlockRemovalNum(u32, u32),
63
64    #[error("invalid column family {}", _0)]
65    InvalidColumnFamily(u32),
66
67    #[error("missing outpoint with transaction with id {} and index {}", _0, _1)]
68    InvalidOutpoint(String, usize),
69
70    #[error("missing transaction with id {}", _0)]
71    InvalidTransactionId(String),
72
73    #[error("{}", _0)]
74    Message(String),
75
76    #[error("missing block hash value given block number {}", _0)]
77    MissingBlockHash(u32),
78
79    #[error("missing block header value given block hash {}", _0)]
80    MissingBlockHeader(String),
81
82    #[error("missing block number value given block hash {}", _0)]
83    MissingBlockNumber(String),
84
85    #[error("missing block transactions value for block hash {}", _0)]
86    MissingBlockTransactions(String),
87
88    #[error("missing child block hashes value for block hash {}", _0)]
89    MissingChildBlock(String),
90
91    #[error("missing current commitment index")]
92    MissingCurrentCmIndex,
93
94    #[error("missing current merkle tree digest")]
95    MissingCurrentDigest,
96
97    #[error("missing current memo index")]
98    MissingCurrentMemoIndex,
99
100    #[error("missing current serial number index")]
101    MissingCurrentSnIndex,
102
103    #[error("missing genesis address")]
104    MissingGenesisAccount,
105
106    #[error("missing genesis commitment")]
107    MissingGenesisCm,
108
109    #[error("missing genesis memo")]
110    MissingGenesisMemo,
111
112    #[error("missing genesis program vk bytes")]
113    MissingGenesisProgramVkBytes,
114
115    #[error("missing genesis serial number")]
116    MissingGenesisSn,
117
118    #[error("missing transaction meta value for transaction id {}", _0)]
119    MissingTransactionMeta(String),
120
121    #[error("missing value given key {}", _0)]
122    MissingValue(String),
123
124    #[error("Null Error {:?}", _0)]
125    NullError(()),
126
127    #[error("{}", _0)]
128    BlockError(BlockError),
129
130    #[error("{}", _0)]
131    MerkleError(MerkleError),
132
133    #[error("{}", _0)]
134    ParametersError(ParametersError),
135
136    #[error("{}", _0)]
137    TransactionError(TransactionError),
138}
139
140impl From<bincode::Error> for StorageError {
141    fn from(error: bincode::Error) -> Self {
142        StorageError::Crate("bincode", format!("{:?}", error))
143    }
144}
145
146impl From<hex::FromHexError> for StorageError {
147    fn from(error: hex::FromHexError) -> Self {
148        StorageError::Crate("hex", format!("{:?}", error))
149    }
150}
151
152#[cfg(feature = "librocksdb")]
153impl From<rocksdb::Error> for StorageError {
154    fn from(error: rocksdb::Error) -> Self {
155        StorageError::Crate("rocksdb", format!("{:?}", error))
156    }
157}
158
159impl From<std::io::Error> for StorageError {
160    fn from(error: std::io::Error) -> Self {
161        StorageError::Crate("std::io", format!("{:?}", error))
162    }
163}
164
165impl From<()> for StorageError {
166    fn from(_error: ()) -> Self {
167        StorageError::NullError(())
168    }
169}
170
171impl From<&'static str> for StorageError {
172    fn from(msg: &'static str) -> Self {
173        StorageError::Message(msg.into())
174    }
175}
176
177impl From<BlockError> for StorageError {
178    fn from(error: BlockError) -> Self {
179        StorageError::BlockError(error)
180    }
181}
182
183impl From<MerkleError> for StorageError {
184    fn from(error: MerkleError) -> Self {
185        StorageError::MerkleError(error)
186    }
187}
188
189impl From<ParametersError> for StorageError {
190    fn from(error: ParametersError) -> Self {
191        StorageError::ParametersError(error)
192    }
193}
194
195impl From<TransactionError> for StorageError {
196    fn from(error: TransactionError) -> Self {
197        StorageError::TransactionError(error)
198    }
199}