1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the snarkVM library.

// The snarkVM library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkVM library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the snarkVM library. If not, see <https://www.gnu.org/licenses/>.

use crate::errors::{BlockError, TransactionError};
use snarkvm_algorithms::errors::MerkleError;
use snarkvm_parameters::errors::ParameterError;

use std::fmt::Debug;

#[derive(Debug, Error)]
pub enum StorageError {
    #[error("{}: {}", _0, _1)]
    Crate(&'static str, String),

    #[error("duplicate commitment")]
    DuplicateCm,

    #[error("duplicate serial number")]
    DuplicateSn,

    #[error("duplicate transaction memo")]
    DuplicateMemo,

    #[error("Block has already been committed: {:?}", _0)]
    ExistingCanonBlock(String),

    #[error("existing record commitment {:?}", _0)]
    ExistingCm(Vec<u8>),

    #[error("existing transaction memo {:?}", _0)]
    ExistingMemo(Vec<u8>),

    #[error("existing serial number {:?}", _0)]
    ExistingSn(Vec<u8>),

    #[error("Can't decommit the genesis block")]
    InvalidBlockDecommit,

    #[error("Can't remove a canon block with hash")]
    InvalidBlockRemovalCanon(String),

    #[error("invalid number of blocks to remove {}. There are only {} existing blocks", _0, _1)]
    InvalidBlockRemovalNum(u32, u32),

    #[error("invalid column family {}", _0)]
    InvalidColumnFamily(u32),

    #[error("missing outpoint with transaction with id {} and index {}", _0, _1)]
    InvalidOutpoint(String, usize),

    #[error("missing transaction with id {}", _0)]
    InvalidTransactionId(String),

    #[error("{}", _0)]
    Message(String),

    #[error("missing block hash value given block number {}", _0)]
    MissingBlockHash(u32),

    #[error("missing block header value given block hash {}", _0)]
    MissingBlockHeader(String),

    #[error("missing block number value given block hash {}", _0)]
    MissingBlockNumber(String),

    #[error("missing block transactions value for block hash {}", _0)]
    MissingBlockTransactions(String),

    #[error("missing child block hashes value for block hash {}", _0)]
    MissingChildBlock(String),

    #[error("missing current commitment index")]
    MissingCurrentCmIndex,

    #[error("missing current merkle tree digest")]
    MissingCurrentDigest,

    #[error("missing current memo index")]
    MissingCurrentMemoIndex,

    #[error("missing current serial number index")]
    MissingCurrentSnIndex,

    #[error("missing genesis address")]
    MissingGenesisAccount,

    #[error("missing genesis commitment")]
    MissingGenesisCm,

    #[error("missing genesis memo")]
    MissingGenesisMemo,

    #[error("missing genesis program vk bytes")]
    MissingGenesisProgramVkBytes,

    #[error("missing genesis serial number")]
    MissingGenesisSn,

    #[error("missing transaction meta value for transaction id {}", _0)]
    MissingTransactionMeta(String),

    #[error("missing value given key {}", _0)]
    MissingValue(String),

    #[error("Null Error {:?}", _0)]
    NullError(()),

    #[error("{}", _0)]
    BlockError(BlockError),

    #[error("{}", _0)]
    MerkleError(MerkleError),

    #[error("{}", _0)]
    ParameterError(ParameterError),

    #[error("{}", _0)]
    TransactionError(TransactionError),
}

impl From<bincode::Error> for StorageError {
    fn from(error: bincode::Error) -> Self {
        StorageError::Crate("bincode", error.to_string())
    }
}

impl From<std::io::Error> for StorageError {
    fn from(error: std::io::Error) -> Self {
        StorageError::Crate("std::io", format!("{:?}", error))
    }
}

impl From<()> for StorageError {
    fn from(_error: ()) -> Self {
        StorageError::NullError(())
    }
}

impl From<&'static str> for StorageError {
    fn from(msg: &'static str) -> Self {
        StorageError::Message(msg.into())
    }
}

impl From<BlockError> for StorageError {
    fn from(error: BlockError) -> Self {
        StorageError::BlockError(error)
    }
}

impl From<MerkleError> for StorageError {
    fn from(error: MerkleError) -> Self {
        StorageError::MerkleError(error)
    }
}

impl From<ParameterError> for StorageError {
    fn from(error: ParameterError) -> Self {
        StorageError::ParameterError(error)
    }
}

impl From<TransactionError> for StorageError {
    fn from(error: TransactionError) -> Self {
        StorageError::TransactionError(error)
    }
}