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

use miden_objects::{
    accounts::AccountStorage, notes::NoteMetadata, AccountError, AssetError, Digest, Felt,
    NoteError,
};

// TRANSACTION KERNEL ERROR
// ================================================================================================

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TransactionKernelError {
    FailedToAddAssetToNote(NoteError),
    InvalidNoteInputs {
        expected: Digest,
        got: Digest,
        data: Option<Vec<Felt>>,
    },
    InvalidStorageSlotIndex(u64),
    MalformedAccountId(AccountError),
    MalformedAsset(AssetError),
    MalformedAssetOnAccountVaultUpdate(AssetError),
    MalformedNoteInputs(NoteError),
    MalformedNoteMetadata(NoteError),
    MalformedNoteScript(Vec<Felt>),
    MalformedNoteType(NoteError),
    MalformedRecipientData(Vec<Felt>),
    MalformedTag(Felt),
    MissingNote(String),
    MissingNoteDetails(NoteMetadata, Digest),
    MissingNoteInputs,
    MissingStorageSlotValue(u8, String),
    TooFewElementsForNoteInputs,
    UnknownAccountProcedure(Digest),
}

impl fmt::Display for TransactionKernelError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TransactionKernelError::FailedToAddAssetToNote(err) => {
                write!(f, "failed to add asset to note: {err}")
            },
            TransactionKernelError::InvalidNoteInputs { expected, got, data } => {
                write!(
                    f,
                    "The note input data does not match its hash, expected: {} got: {} data {:?}",
                    expected, got, data
                )
            },
            TransactionKernelError::InvalidStorageSlotIndex(index) => {
                let num_slots = AccountStorage::NUM_STORAGE_SLOTS;
                write!(f, "storage slot index {index} is invalid, must be smaller than {num_slots}")
            },
            TransactionKernelError::MalformedAccountId(err) => {
                write!( f, "Account id data extracted from the stack by the event handler is not well formed {err}")
            },
            TransactionKernelError::MalformedAsset(err) => {
                write!(f, "Asset data extracted from the stack by the event handler is not well formed {err}")
            },
            TransactionKernelError::MalformedAssetOnAccountVaultUpdate(err) => {
                write!(f, "malformed asset during account vault update: {err}")
            },
            TransactionKernelError::MalformedNoteInputs(err) => {
                write!( f, "Note inputs data extracted from the advice map by the event handler is not well formed {err}")
            },
            TransactionKernelError::MalformedNoteMetadata(err) => {
                write!(f, "Note metadata created by the event handler is not well formed {err}")
            },
            TransactionKernelError::MalformedNoteScript(data) => {
                write!( f, "Note script data extracted from the advice map by the event handler is not well formed {data:?}")
            },
            TransactionKernelError::MalformedNoteType(err) => {
                write!( f, "Note type data extracted from the stack by the event handler is not well formed {err}")
            },
            TransactionKernelError::MalformedRecipientData(data) => {
                write!(f, "Recipient data in the advice provider is not well formed {data:?}")
            },
            TransactionKernelError::MalformedTag(tag) => {
                write!( f, "Tag data extracted from the stack by the event handler is not well formed {tag}")
            },
            TransactionKernelError::MissingNote(note_idx) => {
                write!(f, "Cannot add asset to note with index {note_idx}, note does not exist in the advice provider")
            },
            TransactionKernelError::MissingNoteDetails(metadata, recipient) => {
                write!( f, "Public note missing the details in the advice provider. metadata: {metadata:?}, recipient: {recipient:?}")
            },
            TransactionKernelError::MissingNoteInputs => {
                write!(f, "Public note missing or incomplete inputs in the advice provider")
            },
            TransactionKernelError::MissingStorageSlotValue(index, err) => {
                write!(f, "value for storage slot {index} could not be found: {err}")
            },
            TransactionKernelError::TooFewElementsForNoteInputs => {
                write!(
                    f,
                    "note input data in advice provider contains fewer elements than specified by its inputs length"
                )
            },
            TransactionKernelError::UnknownAccountProcedure(proc_root) => {
                write!(f, "account procedure with root {proc_root} is not in the advice provider")
            },
        }
    }
}

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

// TRANSACTION EVENT PARSING ERROR
// ================================================================================================

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TransactionEventParsingError {
    InvalidTransactionEvent(u32),
    NotTransactionEvent(u32),
}

impl fmt::Display for TransactionEventParsingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidTransactionEvent(event_id) => {
                write!(f, "event {event_id} is not a valid transaction kernel event")
            },
            Self::NotTransactionEvent(event_id) => {
                write!(f, "event {event_id} is not a transaction kernel event")
            },
        }
    }
}

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

// TRANSACTION TRACE PARSING ERROR
// ================================================================================================

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TransactionTraceParsingError {
    InvalidTransactionTrace(u32),
    NotTransactionTrace(u32),
}

impl fmt::Display for TransactionTraceParsingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidTransactionTrace(trace_id) => {
                write!(f, "trace {trace_id} is invalid")
            },
            Self::NotTransactionTrace(trace_id) => {
                write!(f, "trace {trace_id} is not a transaction kernel trace")
            },
        }
    }
}

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