miden_lib/transaction/
errors.rs

1use alloc::{boxed::Box, vec::Vec};
2use core::error::Error;
3
4use miden_objects::{AccountDeltaError, AssetError, Digest, Felt, NoteError, note::NoteMetadata};
5use thiserror::Error;
6
7// TRANSACTION KERNEL ERROR
8// ================================================================================================
9
10#[derive(Debug, Error)]
11pub enum TransactionKernelError {
12    #[error("failed to add asset to account delta")]
13    AccountDeltaAddAssetFailed(#[source] AccountDeltaError),
14    #[error("failed to remove asset to account delta")]
15    AccountDeltaRemoveAssetFailed(#[source] AccountDeltaError),
16    #[error("failed to add asset to note")]
17    FailedToAddAssetToNote(#[source] NoteError),
18    #[error("note input data has hash {actual} but expected hash {expected}")]
19    InvalidNoteInputs { expected: Digest, actual: Digest },
20    #[error(
21        "storage slot index {actual} is invalid, must be smaller than the number of account storage slots {max}"
22    )]
23    InvalidStorageSlotIndex { max: u64, actual: u64 },
24    #[error("failed to push element {0} to advice stack")]
25    FailedToPushAdviceStack(Felt),
26    #[error("failed to generate signature: {0}")]
27    FailedSignatureGeneration(&'static str),
28    #[error("asset data extracted from the stack by event handler `{handler}` is not well formed")]
29    MalformedAssetInEventHandler {
30        handler: &'static str,
31        source: AssetError,
32    },
33    #[error(
34        "note inputs data extracted from the advice map by the event handler is not well formed"
35    )]
36    MalformedNoteInputs(#[source] NoteError),
37    #[error("note metadata created by the event handler is not well formed")]
38    MalformedNoteMetadata(#[source] NoteError),
39    #[error(
40        "note script data `{data:?}` extracted from the advice map by the event handler is not well formed"
41    )]
42    MalformedNoteScript {
43        data: Vec<Felt>,
44        // This is always a DeserializationError, but we can't import it directly here without
45        // adding dependencies, so we make it a trait object instead.
46        source: Box<dyn Error + Send + Sync + 'static>,
47    },
48    #[error("recipient data `{0:?}` in the advice provider is not well formed")]
49    MalformedRecipientData(Vec<Felt>),
50    #[error("cannot add asset to note with index {0}, note does not exist in the advice provider")]
51    MissingNote(u64),
52    #[error(
53        "public note with metadata {0:?} and recipient digest {1} is missing details in the advice provider"
54    )]
55    PublicNoteMissingDetails(NoteMetadata, Digest),
56    #[error("public note has incomplete inputs in the advice provider")]
57    MissingNoteInputs,
58    #[error(
59        "note input data in advice provider contains fewer elements ({actual}) than specified ({specified}) by its inputs length"
60    )]
61    TooFewElementsForNoteInputs { specified: u64, actual: u64 },
62    #[error("account procedure with procedure root {0} is not in the advice provider")]
63    UnknownAccountProcedure(Digest),
64    #[error("code commitment {0} is not in the advice provider")]
65    UnknownCodeCommitment(Digest),
66    #[error("account storage slots number is missing in memory at address {0}")]
67    AccountStorageSlotsNumMissing(u32),
68}
69
70// TRANSACTION EVENT PARSING ERROR
71// ================================================================================================
72
73#[derive(Debug, Error)]
74pub enum TransactionEventError {
75    #[error("event id {0} is not a valid transaction event")]
76    InvalidTransactionEvent(u32),
77    #[error("event id {0} is not a transaction kernel event")]
78    NotTransactionEvent(u32),
79    #[error("event id {0} can only be emitted from the root context")]
80    NotRootContext(u32),
81}
82
83// TRANSACTION TRACE PARSING ERROR
84// ================================================================================================
85
86#[derive(Debug, Error)]
87pub enum TransactionTraceParsingError {
88    #[error("trace id {0} is an unknown transaction kernel trace")]
89    UnknownTransactionTrace(u32),
90}
91
92#[cfg(test)]
93mod error_assertions {
94    use super::*;
95
96    /// Asserts at compile time that the passed error has Send + Sync + 'static bounds.
97    fn _assert_error_is_send_sync_static<E: core::error::Error + Send + Sync + 'static>(_: E) {}
98
99    fn _assert_transaction_kernel_error_bounds(err: TransactionKernelError) {
100        _assert_error_is_send_sync_static(err);
101    }
102}