miden-tx 0.14.5

Miden blockchain transaction executor and prover
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use core::error::Error;

use miden_processor::ExecutionError;
use miden_processor::serde::DeserializationError;
use miden_protocol::account::auth::PublicKeyCommitment;
use miden_protocol::account::{AccountId, StorageMapKey};
use miden_protocol::assembly::diagnostics::reporting::PrintDiagnostic;
use miden_protocol::asset::AssetVaultKey;
use miden_protocol::block::BlockNumber;
use miden_protocol::crypto::merkle::smt::SmtProofError;
use miden_protocol::errors::{
    AccountDeltaError,
    AccountError,
    AssetError,
    NoteError,
    OutputNoteError,
    ProvenTransactionError,
    TransactionInputError,
    TransactionInputsExtractionError,
    TransactionOutputError,
};
use miden_protocol::note::{NoteId, NoteMetadata};
use miden_protocol::transaction::TransactionSummary;
use miden_protocol::{Felt, Word};
use miden_verifier::VerificationError;
use thiserror::Error;

// NOTE EXECUTION ERROR
// ================================================================================================

#[derive(Debug, Error)]
pub enum NoteCheckerError {
    #[error("invalid input note count {0} is out of range)")]
    InputNoteCountOutOfRange(usize),
    #[error("transaction preparation failed: {0}")]
    TransactionPreparation(#[source] TransactionExecutorError),
    #[error("transaction execution prologue failed: {0}")]
    PrologueExecution(#[source] TransactionExecutorError),
}

// TRANSACTION CHECKER ERROR
// ================================================================================================

#[derive(Debug, Error)]
pub(crate) enum TransactionCheckerError {
    #[error("transaction preparation failed: {0}")]
    TransactionPreparation(#[source] TransactionExecutorError),
    #[error("transaction execution prologue failed: {0}")]
    PrologueExecution(#[source] TransactionExecutorError),
    #[error("transaction execution epilogue failed: {0}")]
    EpilogueExecution(#[source] TransactionExecutorError),
    #[error("transaction note execution failed on note index {failed_note_index}: {error}")]
    NoteExecution {
        failed_note_index: usize,
        error: TransactionExecutorError,
    },
}

impl From<TransactionCheckerError> for TransactionExecutorError {
    fn from(error: TransactionCheckerError) -> Self {
        match error {
            TransactionCheckerError::TransactionPreparation(error) => error,
            TransactionCheckerError::PrologueExecution(error) => error,
            TransactionCheckerError::EpilogueExecution(error) => error,
            TransactionCheckerError::NoteExecution { error, .. } => error,
        }
    }
}

// TRANSACTION EXECUTOR ERROR
// ================================================================================================

#[derive(Debug, Error)]
pub enum TransactionExecutorError {
    #[error("failed to read fee asset from transaction inputs")]
    FeeAssetRetrievalFailed(#[source] TransactionInputsExtractionError),
    #[error("failed to fetch transaction inputs from the data store")]
    FetchTransactionInputsFailed(#[source] DataStoreError),
    #[error("failed to fetch asset witnesses from the data store")]
    FetchAssetWitnessFailed(#[source] DataStoreError),
    #[error("fee asset must be fungible but was non-fungible")]
    FeeAssetMustBeFungible,
    #[error("foreign account inputs for ID {0} are not anchored on reference block")]
    ForeignAccountNotAnchoredInReference(AccountId),
    #[error(
        "execution options' cycles must be between {min_cycles} and {max_cycles}, but found {actual}"
    )]
    InvalidExecutionOptionsCycles {
        min_cycles: u32,
        max_cycles: u32,
        actual: u32,
    },
    #[error("failed to create transaction inputs")]
    InvalidTransactionInputs(#[source] TransactionInputError),
    #[error("failed to process account update commitment: {0}")]
    AccountUpdateCommitment(&'static str),
    #[error(
        "account delta commitment computed in transaction kernel ({in_kernel_commitment}) does not match account delta computed via the host ({host_commitment})"
    )]
    InconsistentAccountDeltaCommitment {
        in_kernel_commitment: Word,
        host_commitment: Word,
    },
    #[error("failed to remove the fee asset from the pre-fee account delta")]
    RemoveFeeAssetFromDelta(#[source] AccountDeltaError),
    #[error("input account ID {input_id} does not match output account ID {output_id}")]
    InconsistentAccountId {
        input_id: AccountId,
        output_id: AccountId,
    },
    #[error("expected account nonce delta to be {expected}, found {actual}")]
    InconsistentAccountNonceDelta { expected: Felt, actual: Felt },
    #[error(
        "native asset amount {account_balance} in the account vault is not sufficient to cover the transaction fee of {tx_fee}"
    )]
    InsufficientFee { account_balance: u64, tx_fee: u64 },
    #[error("account witness provided for account ID {0} is invalid")]
    InvalidAccountWitness(AccountId, #[source] SmtProofError),
    #[error(
        "input note {0} was created in a block past the transaction reference block number ({1})"
    )]
    NoteBlockPastReferenceBlock(NoteId, BlockNumber),
    #[error("failed to construct transaction outputs")]
    TransactionOutputConstructionFailed(#[source] TransactionOutputError),
    // Print the diagnostic directly instead of returning the source error. In the source error
    // case, the diagnostic is lost if the execution error is not explicitly unwrapped.
    #[error("failed to execute transaction kernel program:\n{}", PrintDiagnostic::new(.0))]
    TransactionProgramExecutionFailed(ExecutionError),
    /// This variant can be matched on to get the summary of a transaction for signing purposes.
    // It is boxed to avoid triggering clippy::result_large_err for functions that return this type.
    #[error("transaction is unauthorized with summary {0:?}")]
    Unauthorized(Box<TransactionSummary>),
    #[error(
        "failed to respond to signature requested since no authenticator is assigned to the host"
    )]
    MissingAuthenticator,
}

// TRANSACTION PROVER ERROR
// ================================================================================================

#[derive(Debug, Error)]
pub enum TransactionProverError {
    #[error("failed to apply account delta")]
    AccountDeltaApplyFailed(#[source] AccountError),
    #[error("failed to remove the fee asset from the pre-fee account delta")]
    RemoveFeeAssetFromDelta(#[source] AccountDeltaError),
    #[error("failed to construct transaction outputs")]
    TransactionOutputConstructionFailed(#[source] TransactionOutputError),
    #[error("failed to shrink output note")]
    OutputNoteShrinkFailed(#[source] OutputNoteError),
    #[error("failed to build proven transaction")]
    ProvenTransactionBuildFailed(#[source] ProvenTransactionError),
    // Print the diagnostic directly instead of returning the source error. In the source error
    // case, the diagnostic is lost if the execution error is not explicitly unwrapped.
    #[error("failed to execute transaction kernel program:\n{}", PrintDiagnostic::new(.0))]
    TransactionProgramExecutionFailed(ExecutionError),
    /// Custom error variant for errors not covered by the other variants.
    #[error("{error_msg}")]
    Other {
        error_msg: Box<str>,
        // thiserror will return this when calling Error::source on DataStoreError.
        source: Option<Box<dyn Error + Send + Sync + 'static>>,
    },
}

impl TransactionProverError {
    /// Creates a custom error using the [`TransactionProverError::Other`] variant from an error
    /// message.
    pub fn other(message: impl Into<String>) -> Self {
        let message: String = message.into();
        Self::Other { error_msg: message.into(), source: None }
    }

    /// Creates a custom error using the [`TransactionProverError::Other`] variant from an error
    /// message and a source error.
    pub fn other_with_source(
        message: impl Into<String>,
        source: impl Error + Send + Sync + 'static,
    ) -> Self {
        let message: String = message.into();
        Self::Other {
            error_msg: message.into(),
            source: Some(Box::new(source)),
        }
    }
}

// TRANSACTION VERIFIER ERROR
// ================================================================================================

#[derive(Debug, Error)]
pub enum TransactionVerifierError {
    #[error("failed to verify transaction")]
    TransactionVerificationFailed(#[source] VerificationError),
    #[error("transaction proof security level is {actual} but must be at least {expected_minimum}")]
    InsufficientProofSecurityLevel { actual: u32, expected_minimum: u32 },
}

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

#[derive(Debug, Error)]
pub enum TransactionKernelError {
    #[error("failed to add asset to account delta")]
    AccountDeltaAddAssetFailed(#[source] AccountDeltaError),
    #[error("failed to remove asset from account delta")]
    AccountDeltaRemoveAssetFailed(#[source] AccountDeltaError),
    #[error("failed to add asset to note")]
    FailedToAddAssetToNote(#[source] NoteError),
    #[error("note storage has commitment {actual} but expected commitment {expected}")]
    InvalidNoteStorage { expected: Word, actual: Word },
    #[error(
        "failed to respond to signature requested since no authenticator is assigned to the host"
    )]
    MissingAuthenticator,
    #[error("failed to generate signature")]
    SignatureGenerationFailed(#[source] AuthenticationError),
    #[error("transaction returned unauthorized event but a commitment did not match: {0}")]
    TransactionSummaryCommitmentMismatch(#[source] Box<dyn Error + Send + Sync + 'static>),
    #[error("failed to construct transaction summary")]
    TransactionSummaryConstructionFailed(#[source] Box<dyn Error + Send + Sync + 'static>),
    #[error("asset data extracted from the stack by event handler `{handler}` is not well formed")]
    MalformedAssetInEventHandler {
        handler: &'static str,
        source: AssetError,
    },
    #[error(
        "note storage data extracted from the advice map by the event handler is not well formed"
    )]
    MalformedNoteStorage(#[source] NoteError),
    #[error(
        "note script data `{data:?}` extracted from the advice map by the event handler is not well formed"
    )]
    MalformedNoteScript {
        data: Vec<Felt>,
        source: DeserializationError,
    },
    #[error("recipient data `{0:?}` in the advice provider is not well formed")]
    MalformedRecipientData(Vec<Felt>),
    #[error("cannot add asset to note with index {0}, note does not exist in the advice provider")]
    MissingNote(usize),
    #[error(
        "public note with metadata {0:?} and recipient digest {1} is missing details in the advice provider"
    )]
    PublicNoteMissingDetails(NoteMetadata, Word),
    #[error("attachment provided to set_attachment must be empty when attachment kind is None")]
    NoteAttachmentNoneIsNotEmpty,
    #[error(
        "commitment of note attachment {actual} does not match attachment {provided} provided to set_attachment"
    )]
    NoteAttachmentArrayMismatch { actual: Word, provided: Word },
    #[error(
        "note storage in advice provider contains fewer items ({actual}) than specified ({specified}) by its number of storage items"
    )]
    TooFewElementsForNoteStorage { specified: u64, actual: u64 },
    #[error("account procedure with procedure root {0} is not in the account procedure index map")]
    UnknownAccountProcedure(Word),
    #[error("code commitment {0} is not in the account procedure index map")]
    UnknownCodeCommitment(Word),
    #[error("account storage slots number is missing in memory at address {0}")]
    AccountStorageSlotsNumMissing(u32),
    #[error("account nonce can only be incremented once")]
    NonceCanOnlyIncrementOnce,
    #[error("failed to convert fee asset into fungible asset")]
    FailedToConvertFeeAsset(#[source] AssetError),
    #[error(
        "failed to get inputs for foreign account {foreign_account_id} from data store at reference block {ref_block}"
    )]
    GetForeignAccountInputs {
        foreign_account_id: AccountId,
        ref_block: BlockNumber,
        // thiserror will return this when calling Error::source on TransactionKernelError.
        source: DataStoreError,
    },
    #[error(
        "failed to get vault asset witness from data store for vault root {vault_root} and vault_key {asset_key}"
    )]
    GetVaultAssetWitness {
        vault_root: Word,
        asset_key: AssetVaultKey,
        // thiserror will return this when calling Error::source on TransactionKernelError.
        source: DataStoreError,
    },
    #[error(
        "failed to get storage map witness from data store for map root {map_root} and map_key {map_key}"
    )]
    GetStorageMapWitness {
        map_root: Word,
        map_key: StorageMapKey,
        // thiserror will return this when calling Error::source on TransactionKernelError.
        source: DataStoreError,
    },
    #[error(
        "native asset amount {account_balance} in the account vault is not sufficient to cover the transaction fee of {tx_fee}"
    )]
    InsufficientFee { account_balance: u64, tx_fee: u64 },
    /// This variant signals that a signature over the contained commitments is required, but
    /// missing.
    #[error("transaction requires a signature")]
    Unauthorized(Box<TransactionSummary>),
    /// A generic error returned when the transaction kernel did not behave as expected.
    #[error("{message}")]
    Other {
        message: Box<str>,
        // thiserror will return this when calling Error::source on TransactionKernelError.
        source: Option<Box<dyn Error + Send + Sync + 'static>>,
    },
}

impl TransactionKernelError {
    /// Creates a custom error using the [`TransactionKernelError::Other`] variant from an error
    /// message.
    pub fn other(message: impl Into<String>) -> Self {
        let message: String = message.into();
        Self::Other { message: message.into(), source: None }
    }

    /// Creates a custom error using the [`TransactionKernelError::Other`] variant from an error
    /// message and a source error.
    pub fn other_with_source(
        message: impl Into<String>,
        source: impl Error + Send + Sync + 'static,
    ) -> Self {
        let message: String = message.into();
        Self::Other {
            message: message.into(),
            source: Some(Box::new(source)),
        }
    }
}

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

#[derive(Debug, Error)]
pub enum DataStoreError {
    #[error("account with id {0} not found in data store")]
    AccountNotFound(AccountId),
    #[error("block with number {0} not found in data store")]
    BlockNotFound(BlockNumber),
    /// Custom error variant for implementors of the [`DataStore`](crate::executor::DataStore)
    /// trait.
    #[error("{error_msg}")]
    Other {
        error_msg: Box<str>,
        // thiserror will return this when calling Error::source on DataStoreError.
        source: Option<Box<dyn Error + Send + Sync + 'static>>,
    },
}

impl DataStoreError {
    /// Creates a custom error using the [`DataStoreError::Other`] variant from an error message.
    pub fn other(message: impl Into<String>) -> Self {
        let message: String = message.into();
        Self::Other { error_msg: message.into(), source: None }
    }

    /// Creates a custom error using the [`DataStoreError::Other`] variant from an error message and
    /// a source error.
    pub fn other_with_source(
        message: impl Into<String>,
        source: impl Error + Send + Sync + 'static,
    ) -> Self {
        let message: String = message.into();
        Self::Other {
            error_msg: message.into(),
            source: Some(Box::new(source)),
        }
    }
}

// AUTHENTICATION ERROR
// ================================================================================================

#[derive(Debug, Error)]
pub enum AuthenticationError {
    #[error("signature rejected: {0}")]
    RejectedSignature(String),
    #[error("public key `{0}` is not contained in the authenticator's keys")]
    UnknownPublicKey(PublicKeyCommitment),
    /// Custom error variant for implementors of the
    /// [`TransactionAuthenticator`](crate::auth::TransactionAuthenticator) trait.
    #[error("{error_msg}")]
    Other {
        error_msg: Box<str>,
        // thiserror will return this when calling Error::source on DataStoreError.
        source: Option<Box<dyn Error + Send + Sync + 'static>>,
    },
}

impl AuthenticationError {
    /// Creates a custom error using the [`AuthenticationError::Other`] variant from an error
    /// message.
    pub fn other(message: impl Into<String>) -> Self {
        let message: String = message.into();
        Self::Other { error_msg: message.into(), source: None }
    }

    /// Creates a custom error using the [`AuthenticationError::Other`] variant from an error
    /// message and a source error.
    pub fn other_with_source(
        message: impl Into<String>,
        source: impl Error + Send + Sync + 'static,
    ) -> Self {
        let message: String = message.into();
        Self::Other {
            error_msg: message.into(),
            source: Some(Box::new(source)),
        }
    }
}

#[cfg(test)]
mod error_assertions {
    use super::*;

    /// Asserts at compile time that the passed error has Send + Sync + 'static bounds.
    fn _assert_error_is_send_sync_static<E: core::error::Error + Send + Sync + 'static>(_: E) {}

    fn _assert_data_store_error_bounds(err: DataStoreError) {
        _assert_error_is_send_sync_static(err);
    }

    fn _assert_authentication_error_bounds(err: AuthenticationError) {
        _assert_error_is_send_sync_static(err);
    }

    fn _assert_transaction_kernel_error_bounds(err: TransactionKernelError) {
        _assert_error_is_send_sync_static(err);
    }
}