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
use alloc::string::{String, ToString};
use core::fmt;

use miden_objects::{
    accounts::AccountId,
    crypto::merkle::MmrError,
    notes::NoteId,
    utils::{DeserializationError, HexParseError},
    AccountError, AssetVaultError, Digest, NoteError, TransactionScriptError, Word,
};
use miden_tx::DataStoreError;

// STORE ERROR
// ================================================================================================

#[derive(Debug)]
pub enum StoreError {
    AssetVaultError(AssetVaultError),
    AccountCodeDataNotFound(Digest),
    AccountDataNotFound(AccountId),
    AccountError(AccountError),
    AccountHashMismatch(AccountId),
    AccountKeyNotFound(Word),
    AccountStorageNotFound(Digest),
    BlockHeaderNotFound(u32),
    ChainMmrNodeNotFound(u64),
    DataDeserializationError(DeserializationError),
    DatabaseError(String),
    HexParseError(HexParseError),
    NoteNotFound(NoteId),
    InputSerializationError(serde_json::Error),
    JsonDataDeserializationError(serde_json::Error),
    MmrError(MmrError),
    NoteInclusionProofError(NoteError),
    NoteTagAlreadyTracked(u64),
    ParsingError(String),
    QueryError(String),
    TransactionScriptError(TransactionScriptError),
    VaultDataNotFound(Digest),
}

impl From<AssetVaultError> for StoreError {
    fn from(value: AssetVaultError) -> Self {
        StoreError::AssetVaultError(value)
    }
}

impl From<AccountError> for StoreError {
    fn from(value: AccountError) -> Self {
        StoreError::AccountError(value)
    }
}

impl From<DeserializationError> for StoreError {
    fn from(value: DeserializationError) -> Self {
        StoreError::DataDeserializationError(value)
    }
}

impl From<HexParseError> for StoreError {
    fn from(value: HexParseError) -> Self {
        StoreError::HexParseError(value)
    }
}

impl From<MmrError> for StoreError {
    fn from(value: MmrError) -> Self {
        StoreError::MmrError(value)
    }
}

impl From<NoteError> for StoreError {
    fn from(value: NoteError) -> Self {
        StoreError::NoteInclusionProofError(value)
    }
}

impl From<TransactionScriptError> for StoreError {
    fn from(value: TransactionScriptError) -> Self {
        StoreError::TransactionScriptError(value)
    }
}

impl fmt::Display for StoreError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use StoreError::*;
        match self {
            AssetVaultError(err) => {
                write!(f, "asset vault with root {} not found", err)
            },
            AccountCodeDataNotFound(root) => {
                write!(f, "account code data with root {} not found", root)
            },
            AccountDataNotFound(account_id) => {
                write!(f, "Account data was not found for Account Id {account_id}")
            },
            AccountError(err) => write!(f, "error instantiating Account: {err}"),
            AccountHashMismatch(account_id) => {
                write!(f, "account hash mismatch for account {account_id}")
            },
            AccountKeyNotFound(pub_key) => {
                write!(f, "error: Public Key {} not found", Digest::from(pub_key))
            },
            AccountStorageNotFound(root) => {
                write!(f, "account storage data with root {} not found", root)
            },
            BlockHeaderNotFound(block_number) => {
                write!(f, "block header for block {} not found", block_number)
            },
            ChainMmrNodeNotFound(node_index) => {
                write!(f, "chain mmr node at index {} not found", node_index)
            },
            DataDeserializationError(err) => {
                write!(f, "error deserializing data from the store: {err}")
            },
            DatabaseError(err) => write!(f, "database-related non-query error: {err}"),
            HexParseError(err) => {
                write!(f, "error parsing hex: {err}")
            },
            NoteNotFound(note_id) => {
                write!(f, "note with note id {} not found", note_id.inner())
            },
            InputSerializationError(err) => {
                write!(f, "error trying to serialize inputs for the store: {err}")
            },
            JsonDataDeserializationError(err) => {
                write!(f, "error deserializing data from JSON from the store: {err}")
            },
            MmrError(err) => write!(f, "error constructing mmr: {err}"),
            NoteTagAlreadyTracked(tag) => write!(f, "note tag {} is already being tracked", tag),
            NoteInclusionProofError(error) => {
                write!(f, "inclusion proof creation error: {}", error)
            },
            ParsingError(err) => {
                write!(f, "failed to parse data retrieved from the database: {err}")
            },
            QueryError(err) => write!(f, "failed to retrieve data from the database: {err}"),
            TransactionScriptError(err) => {
                write!(f, "error instantiating transaction script: {err}")
            },
            VaultDataNotFound(root) => write!(f, "account vault data for root {} not found", root),
        }
    }
}

impl From<StoreError> for DataStoreError {
    fn from(value: StoreError) -> Self {
        match value {
            StoreError::AccountDataNotFound(account_id) => {
                DataStoreError::AccountNotFound(account_id)
            },
            StoreError::BlockHeaderNotFound(block_num) => DataStoreError::BlockNotFound(block_num),
            StoreError::NoteNotFound(note_id) => DataStoreError::NoteNotFound(note_id),
            err => DataStoreError::InternalError(err.to_string()),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for StoreError {}