miden-protocol 0.16.0-alpha.4

Core components of the Miden protocol
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use alloc::string::ToString;
use alloc::vec::Vec;

use crate::account::{Account, AccountCode, AccountId, AccountStorage, AccountStoragePatch};
use crate::asset::AssetVault;
use crate::crypto::SequentialCommit;
use crate::errors::{AccountDeltaError, AccountError};
use crate::utils::serde::{
    ByteReader,
    ByteWriter,
    Deserializable,
    DeserializationError,
    Serializable,
};
use crate::{Felt, Word, ZERO};

mod delta_op;
pub use delta_op::AssetDeltaOperation;

mod vault;
pub use vault::{
    AccountVaultDelta,
    FungibleAssetDelta,
    NonFungibleAssetDelta,
    NonFungibleDeltaAction,
};

// ACCOUNT DELTA
// ================================================================================================

/// The [`AccountDelta`] stores the differences between two account states, which can result from
/// one or more transaction.
///
/// The differences are represented as follows:
/// - storage: an [`AccountStoragePatch`] that contains the changes to the account storage.
/// - vault: an [`AccountVaultDelta`] object that contains the changes to the account vault.
/// - nonce: if the nonce of the account has changed, the _delta_ of the nonce is stored, i.e. the
///   value by which the nonce increased.
/// - code: an [`AccountCode`] for new accounts and `None` for others.
///
/// The presence of the code in a delta signals if the delta is a _full state_ or _partial state_
/// delta. A full state delta must be converted into an [`Account`] object, while a partial state
/// delta must be applied to an existing [`Account`]. Because a full state delta reconstructs the
/// account from empty storage, its storage patch may only create slots, never update or remove
/// them; [`AccountDelta::new`] enforces this.
///
/// TODO(code_upgrades): The ability to track account code updates is an outstanding feature. For
/// that reason, the account code is not considered as part of the "nonce must be incremented if
/// state changed" check.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AccountDelta {
    /// The ID of the account to which this delta applies. If the delta is created during
    /// transaction execution, that is the native account of the transaction.
    account_id: AccountId,
    /// The patch of the account's storage.
    storage: AccountStoragePatch,
    /// The delta of the account's asset vault.
    vault: AccountVaultDelta,
    /// The code of a new account (`Some`) or `None` for existing accounts.
    code: Option<AccountCode>,
    /// The value by which the nonce was incremented. Must be greater than zero if storage or vault
    /// are non-empty.
    nonce_delta: Felt,
}

impl AccountDelta {
    // CONSTANTS
    // --------------------------------------------------------------------------------------------

    /// Domain separator for the account delta commitment header.
    const DOMAIN: Felt = Felt::new_unchecked(1);

    // CONSTRUCTOR
    // --------------------------------------------------------------------------------------------

    /// Returns new [AccountDelta] instantiated from the provided components.
    ///
    /// `code` is `Some` for a full state delta (a new account) and `None` otherwise.
    ///
    /// # Errors
    ///
    /// - Returns an error if storage or vault were updated, but the nonce_delta is 0.
    /// - Returns an error if `code` is provided but the storage patch contains an `Update` or
    ///   `Remove` operation. A full state delta must reconstruct the account from empty storage, so
    ///   it may only create slots.
    pub fn new(
        account_id: AccountId,
        storage: AccountStoragePatch,
        vault: AccountVaultDelta,
        code: Option<AccountCode>,
        nonce_delta: Felt,
    ) -> Result<Self, AccountDeltaError> {
        // nonce must be updated if either account storage or vault were updated
        validate_nonce(nonce_delta, &storage, &vault)?;

        // A full state delta (carrying code) must reconstruct the account from empty storage, so it
        // may only create slots. An `Update` or `Remove` assumes the slot already exists and would
        // make reconstruction impossible.
        if code.is_some() && storage.contains_non_create_ops() {
            return Err(AccountDeltaError::FullStateDeltaContainsNonCreateOp);
        }

        Ok(Self {
            account_id,
            storage,
            vault,
            code,
            nonce_delta,
        })
    }

    // PUBLIC MUTATORS
    // --------------------------------------------------------------------------------------------

    /// Returns a mutable reference to the account vault delta.
    pub fn vault_mut(&mut self) -> &mut AccountVaultDelta {
        &mut self.vault
    }

    // PUBLIC ACCESSORS
    // --------------------------------------------------------------------------------------------

    /// Returns true if this account delta does not contain any vault, storage or nonce updates.
    pub fn is_empty(&self) -> bool {
        self.storage.is_empty() && self.vault.is_empty() && self.nonce_delta == ZERO
    }

    /// Returns `true` if this delta is a "full state" delta, `false` otherwise, i.e. if it is a
    /// "partial state" delta.
    ///
    /// See the type-level docs for more on this distinction.
    pub fn is_full_state(&self) -> bool {
        // TODO(code_upgrades): Change this to another detection mechanism once we have code upgrade
        // support, at which point the presence of code may not be enough of an indication
        // that a delta can be converted to a full account.
        //
        // The presence of code alone is sufficient to identify a full state delta: the constructor
        // enforces that a code-carrying delta's storage patch contains only `Create` ops, so it
        // always reconstructs a full account.
        self.code.is_some()
    }

    /// Returns storage updates for this account delta.
    pub fn storage(&self) -> &AccountStoragePatch {
        &self.storage
    }

    /// Returns vault updates for this account delta.
    pub fn vault(&self) -> &AccountVaultDelta {
        &self.vault
    }

    /// Returns the amount by which the nonce was incremented.
    pub fn nonce_delta(&self) -> Felt {
        self.nonce_delta
    }

    /// Returns the account ID to which this delta applies.
    pub fn id(&self) -> AccountId {
        self.account_id
    }

    /// Returns a reference to the account code of this delta, if present.
    pub fn code(&self) -> Option<&AccountCode> {
        self.code.as_ref()
    }

    /// Converts this delta into its individual components.
    pub fn into_parts(self) -> (AccountStoragePatch, AccountVaultDelta, Option<AccountCode>, Felt) {
        (self.storage, self.vault, self.code, self.nonce_delta)
    }

    /// Computes the commitment to the account delta.
    ///
    /// ## Computation
    ///
    /// The delta commitment is a sequential hash over a vector of field elements which starts out
    /// empty and is appended to in the following way. Whenever sorting is expected, it is that
    /// of a [`Word`].
    ///
    /// - Append `[[domain = 1, nonce_delta, account_id_suffix, account_id_prefix], EMPTY_WORD]`,
    ///   where `account_id_{prefix,suffix}` are the prefix and suffix felts of the native account
    ///   id, `nonce_delta` is the value by which the nonce was incremented, and `domain = 1`
    ///   identifies the header as the start of an account delta commitment.
    /// - Asset Delta
    ///   - For each **added** asset, sorted by its asset ID:
    ///     - Append `[ASSET_ID, ASSET_VALUE]`.
    ///   - Append `[domain = 3, delta_op = 1, num_added_assets, 0]` if `num_added_assets != 0`
    ///     where `num_added_assets` is the number of added assets and `delta_op` is set to `1`
    ///     indicating asset addition.
    ///   - For each **removed** asset, sorted by its asset ID:
    ///     - Append `[ASSET_ID, ASSET_VALUE]`.
    ///   - Append `[domain = 3, delta_op = 2, num_removed_assets, 0]` if `num_removed_assets != 0`
    ///     where `num_removed_assets` is the number of removed assets and `delta_op` is set to `2`
    ///     indicating asset removal.
    ///   - Note that the domain is the same independent of asset addition or removal, since the
    ///     `delta_op` sufficiently distinguishes the two domains.
    /// - Storage Slots are sorted by slot ID and are iterated in this order. `patch_op` is the
    ///   [`StoragePatchOperation`](crate::account::StoragePatchOperation) of the slot patch and
    ///   `slot_id_{suffix, prefix}` is the identifier of the slot. For each slot, depending on its
    ///   slot type:
    ///   - Value Slot
    ///     - Append `[[domain = 5, patch_op, slot_id_suffix, slot_id_prefix], NEW_VALUE]` where
    ///       `NEW_VALUE` is the new value of the slot.
    ///   - Map Slot
    ///     - For each key-value pair, sorted by key, whose new value is different from the previous
    ///       value in the map:
    ///       - Append `[KEY, NEW_VALUE]`.
    ///     - The map trailer is constructed as `[[domain = 6, patch_op, slot_id_suffix,
    ///       slot_id_prefix], [num_changed_entries, 0, 0, 0]]`, where `num_changed_entries` is the
    ///       number of key-value pairs appended above. Whether the trailer is included depends on
    ///       `patch_op`:
    ///         - For
    ///           [`StoragePatchOperation::Create`](crate::account::StoragePatchOperation::Create),
    ///           the trailer is always included, since the slot's creation must be committed to even
    ///           when the map is created empty (`num_changed_entries == 0`).
    ///         - For
    ///           [`StoragePatchOperation::Update`](crate::account::StoragePatchOperation::Update),
    ///           the trailer is included only if `num_changed_entries != 0`. An update that changes
    ///           no entries is a no-op and is omitted entirely.
    ///         - For
    ///           [`StoragePatchOperation::Remove`](crate::account::StoragePatchOperation::Remove),
    ///           the trailer is always included with `num_changed_entries` set to zero, since the
    ///           number of removed entries is unknown.
    ///
    /// ## Rationale
    ///
    /// The rationale for this layout is that hashing in the VM should be as efficient as possible
    /// and minimize the number of branches to be as efficient as possible. Every high-level section
    /// in this bullet point list should add an even number of words since the hasher operates
    /// on double words. In the VM, each permutation is done immediately, so adding an uneven
    /// number of words in a given step will result in more difficulty in the MASM implementation.
    ///
    /// ### New Accounts
    ///
    /// The delta for new accounts (a full state delta) must commit to all the created storage slots
    /// of the account, even if these slots contain the default value (e.g. the empty word for value
    /// slots or an empty storage map). This ensures the full state delta commits to the exact
    /// storage slots that are contained in the account.
    ///
    /// ## Security
    ///
    /// The general concern with the commitment is that two distinct deltas must never hash to the
    /// same commitment. E.g. a commitment of a delta that changes a key-value pair in a storage
    /// map slot should be different from a delta that adds a non-fungible asset to the vault.
    /// If not, a delta can be crafted in the VM that sets a map key but a malicious actor
    /// crafts a delta outside the VM that adds a non-fungible asset. To prevent that, a couple
    /// of measures are taken.
    ///
    /// - Because multiple unrelated domains (e.g. vaults and storage slots) are hashed in the same
    ///   hasher, domain separators are used to disambiguate. For each changed asset and each
    ///   changed slot in the delta, a domain separator is hashed into the delta. The domain
    ///   separator is always at the same index in each layout so it cannot be maliciously crafted
    ///   (see below for an example).
    /// - Storage value slots:
    ///   - since value slots are only included in the patch if their value has changed when the
    ///     operation is `Update`, there is no ambiguity between a value slot being set to
    ///     EMPTY_WORD and its value being unchanged.
    /// - Storage map slots:
    ///   - Map slots append a header which summarizes the changes in the slot, in particular the
    ///     slot ID and number of changed entries.
    ///   - Two distinct storage map slots use the same domain but are disambiguated due to
    ///     inclusion of the slot ID.
    ///
    /// ### Domain Separators
    ///
    /// As an example for ambiguity, consider these two deltas:
    ///
    /// ```text
    /// [
    ///   ID_AND_NONCE, EMPTY_WORD,
    ///   [ASSET_ID, ASSET_VALUE],
    ///   [[domain = 3, delta_op = 1, num_added_assets = 1, 0], EMPTY_WORD],
    ///   [/* no removed assets delta */],
    ///   [/* no storage patch */]
    /// ]
    /// ```
    ///
    /// ```text
    /// [
    ///   ID_AND_NONCE, EMPTY_WORD,
    ///   [/* no asset delta */],
    ///   [[domain = 5, patch_op, slot_id_suffix0, slot_id_prefix0], NEW_VALUE]
    ///   [[domain = 5, patch_op, slot_id_suffix1, slot_id_prefix1], NEW_VALUE]
    /// ]
    /// ```
    ///
    /// - `NEW_VALUE` is user-controlled and can be crafted to match `ASSET_VALUE` or `EMPTY_WORD`.
    /// - Slot IDs are user-controlled and can be crafted to match the two most significant elements
    ///   in the asset ID or `num_added_assets` and the fixed 0.
    /// - This leaves only the domain separator and the patch_op to differentiate these two deltas.
    ///
    /// The delta and patch headers further use distinct domain separators (1 and 2 respectively),
    /// so a delta and a patch with otherwise identical bodies can never collide.
    ///
    /// ### Number of Changed Entries
    ///
    /// As an example for ambiguity, consider these two deltas:
    ///
    /// ```text
    /// [
    ///   ID_AND_NONCE, EMPTY_WORD,
    ///   [/* no asset delta */],
    ///   [domain = 6, patch_op, slot_id_suffix = 20, slot_id_prefix = 21, num_changed_entries = 0, 0, 0, 0]
    ///   [domain = 6, patch_op, slot_id_suffix = 42, slot_id_prefix = 43, num_changed_entries = 0, 0, 0, 0]
    /// ]
    /// ```
    ///
    /// ```text
    /// [
    ///   ID_AND_NONCE, EMPTY_WORD,
    ///   [/* no asset delta */],
    ///   [KEY0, VALUE0],
    ///   [domain = 6, patch_op, slot_id_suffix = 42, slot_id_prefix = 43, num_changed_entries = 1, 0, 0, 0]
    /// ]
    /// ```
    ///
    /// The keys and values of map slots are user-controllable so `KEY0` and `VALUE0` could be
    /// crafted to match the first map header in the first delta. So, _without_ having
    /// `num_changed_entries` included in the commitment, these deltas would be ambiguous. A delta
    /// with two empty maps could have the same commitment as a delta with one map entry where one
    /// key-value pair has changed.
    ///
    /// #### New Accounts
    ///
    /// The number of changed entries of a storage map can be validly zero when an empty storage map
    /// is created in account (e.g. at account creation time). In such cases, the number of changed
    /// key-value pairs is 0, but the map must still be committed to, in order to differentiate
    /// between a slot being created as an empty map or not being created at all.
    pub fn to_commitment(&self) -> Word {
        <Self as SequentialCommit>::to_commitment(self)
    }
}

impl TryFrom<&AccountDelta> for Account {
    type Error = AccountError;

    /// Converts an [`AccountDelta`] into an [`Account`].
    ///
    /// Conceptually, this applies the delta onto an empty account.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - If the delta is not a full state delta. See [`AccountDelta`] for details.
    /// - If any vault delta operation removes an asset.
    /// - If any vault delta operation adds an asset that would overflow the maximum representable
    ///   amount.
    /// - If any storage patch update violates account storage constraints.
    fn try_from(delta: &AccountDelta) -> Result<Self, Self::Error> {
        if !delta.is_full_state() {
            return Err(AccountError::PartialStateDeltaToAccount);
        }

        let Some(code) = delta.code().cloned() else {
            return Err(AccountError::PartialStateDeltaToAccount);
        };

        // The asset vault of a new account is empty, so if the delta contains removed assets, the
        // delta is invalid.
        if delta.vault().removed_assets().count() != 0 {
            return Err(AccountError::AssetsRemovedFromNewAccount);
        }

        let mut vault = AssetVault::default();
        for added_asset in delta.vault().added_assets() {
            vault.insert_asset(added_asset).map_err(AccountError::AssetVaultUpdateError)?;
        }

        // A full state delta consists of `Create` slot patches, so applying it to empty storage
        // reconstructs the account's full storage.
        let mut storage = AccountStorage::default();
        storage.apply_patch(delta.storage())?;

        // The nonce of the account is the initial nonce of 0 plus the nonce_delta, so the
        // nonce_delta itself.
        let nonce = delta.nonce_delta();

        Account::new(delta.id(), vault, storage, code, nonce, None)
    }
}

impl SequentialCommit for AccountDelta {
    type Commitment = Word;

    /// Reduces the delta to a sequence of field elements.
    ///
    /// See [AccountDelta::to_commitment()] for more details.
    fn to_elements(&self) -> Vec<Felt> {
        // The commitment to an empty delta is defined as the empty word.
        if self.is_empty() {
            return Vec::new();
        }

        // Minor optimization: At least 24 elements are always added.
        let mut elements = Vec::with_capacity(24);

        // ID and Nonce
        elements.extend_from_slice(&[
            Self::DOMAIN,
            self.nonce_delta,
            self.account_id.suffix(),
            self.account_id.prefix().as_felt(),
        ]);
        elements.extend_from_slice(Word::empty().as_elements());

        // Vault Delta
        self.vault.append_delta_elements(&mut elements);

        // Storage Patch
        self.storage.append_patch_elements(&mut elements);

        debug_assert!(
            elements.len() % (2 * crate::WORD_SIZE) == 0,
            "expected elements to contain an even number of words, but it contained {} elements",
            elements.len()
        );

        elements
    }
}

// SERIALIZATION
// ================================================================================================

impl Serializable for AccountDelta {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        self.account_id.write_into(target);
        self.storage.write_into(target);
        self.vault.write_into(target);
        self.code.write_into(target);
        self.nonce_delta.write_into(target);
    }

    fn get_size_hint(&self) -> usize {
        self.account_id.get_size_hint()
            + self.storage.get_size_hint()
            + self.vault.get_size_hint()
            + self.code.get_size_hint()
            + self.nonce_delta.get_size_hint()
    }
}

impl Deserializable for AccountDelta {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        let account_id = AccountId::read_from(source)?;
        let storage = AccountStoragePatch::read_from(source)?;
        let vault = AccountVaultDelta::read_from(source)?;
        let code = <Option<AccountCode>>::read_from(source)?;
        let nonce_delta = Felt::read_from(source)?;

        validate_nonce(nonce_delta, &storage, &vault)
            .map_err(|err| DeserializationError::InvalidValue(err.to_string()))?;

        Ok(Self {
            account_id,
            storage,
            vault,
            code,
            nonce_delta,
        })
    }
}

// HELPER FUNCTIONS
// ================================================================================================

/// Checks if the nonce was updated correctly given the provided storage and vault deltas.
///
/// # Errors
///
/// Returns an error if:
/// - storage or vault were updated, but the nonce_delta was set to 0.
fn validate_nonce(
    nonce_delta: Felt,
    storage: &AccountStoragePatch,
    vault: &AccountVaultDelta,
) -> Result<(), AccountDeltaError> {
    if (!storage.is_empty() || !vault.is_empty()) && nonce_delta == ZERO {
        return Err(AccountDeltaError::NonEmptyStorageOrVaultDeltaWithZeroNonceDelta);
    }

    Ok(())
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {

    use assert_matches::assert_matches;
    use rstest::rstest;

    use super::{AccountDelta, AccountStoragePatch, AccountVaultDelta};
    use crate::account::{
        Account,
        AccountCode,
        AccountId,
        AccountStorage,
        AccountType,
        StorageMapKey,
        StorageMapPatch,
        StorageSlotName,
    };
    use crate::asset::{
        Asset,
        AssetVault,
        FungibleAsset,
        NonFungibleAsset,
        NonFungibleAssetDetails,
    };
    use crate::errors::AccountDeltaError;
    use crate::testing::account_id::{
        ACCOUNT_ID_PRIVATE_SENDER,
        ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE,
        AccountIdBuilder,
    };
    use crate::utils::serde::Serializable;
    use crate::{ONE, Word, ZERO};

    #[test]
    fn account_delta_nonce_validation() {
        let account_id = AccountId::try_from(ACCOUNT_ID_PRIVATE_SENDER).unwrap();
        // empty delta
        let storage_patch = AccountStoragePatch::new();
        let vault_delta = AccountVaultDelta::default();

        AccountDelta::new(account_id, storage_patch.clone(), vault_delta.clone(), None, ZERO)
            .unwrap();
        AccountDelta::new(account_id, storage_patch.clone(), vault_delta.clone(), None, ONE)
            .unwrap();

        // non-empty delta
        let storage_patch = AccountStoragePatch::from_iters([StorageSlotName::mock(1)], [], []);

        assert_matches!(
            AccountDelta::new(account_id, storage_patch.clone(), vault_delta.clone(), None, ZERO)
                .unwrap_err(),
            AccountDeltaError::NonEmptyStorageOrVaultDeltaWithZeroNonceDelta
        );
        AccountDelta::new(account_id, storage_patch.clone(), vault_delta.clone(), None, ONE)
            .unwrap();
    }

    /// A full state delta (carrying code) must only contain `Create` storage ops, since an `Update`
    /// or `Remove` could not be applied to the empty storage of a new account.
    #[rstest]
    #[case::update(
        AccountStoragePatch::builder().update_value(StorageSlotName::mock(1), Word::empty()).build()
    )]
    #[case::remove(
        AccountStoragePatch::builder().remove_value(StorageSlotName::mock(1)).build()
    )]
    fn account_delta_new_rejects_full_state_with_non_create_op(
        #[case] storage: AccountStoragePatch,
    ) -> anyhow::Result<()> {
        let account_id = AccountId::try_from(ACCOUNT_ID_PRIVATE_SENDER)?;

        let error = AccountDelta::new(
            account_id,
            storage,
            AccountVaultDelta::default(),
            Some(AccountCode::mock()),
            ONE,
        )
        .unwrap_err();
        assert_matches!(error, AccountDeltaError::FullStateDeltaContainsNonCreateOp);

        Ok(())
    }

    /// A full state delta whose storage only creates slots can be reconstructed into an account.
    #[test]
    fn account_delta_full_state_with_create_reconstructs() -> anyhow::Result<()> {
        let account_id = AccountId::try_from(ACCOUNT_ID_PRIVATE_SENDER)?;
        let code = AccountCode::mock();
        let created_slot = StorageSlotName::mock(1);
        let created_value = Word::from([7u32, 0, 0, 0]);

        let storage = AccountStoragePatch::builder()
            .create_value(created_slot.clone(), created_value)
            .build();

        let delta = AccountDelta::new(
            account_id,
            storage,
            AccountVaultDelta::default(),
            Some(code.clone()),
            ONE,
        )?;
        assert!(delta.is_full_state());

        let account = Account::try_from(&delta)?;
        assert_eq!(account.code(), &code);
        assert_eq!(account.storage().get_item(&created_slot)?, created_value);

        Ok(())
    }

    #[test]
    fn account_delta_size_hint() {
        // AccountDelta
        let account_id = AccountId::try_from(ACCOUNT_ID_PRIVATE_SENDER).unwrap();
        let storage_patch = AccountStoragePatch::new();
        let vault_delta = AccountVaultDelta::default();
        assert_eq!(storage_patch.to_bytes().len(), storage_patch.get_size_hint());
        assert_eq!(vault_delta.to_bytes().len(), vault_delta.get_size_hint());

        let account_delta =
            AccountDelta::new(account_id, storage_patch, vault_delta, None, ZERO).unwrap();
        assert_eq!(account_delta.to_bytes().len(), account_delta.get_size_hint());

        let storage_patch = AccountStoragePatch::from_iters(
            [StorageSlotName::mock(1)],
            [
                (StorageSlotName::mock(2), Word::from([1, 1, 1, 1u32])),
                (StorageSlotName::mock(3), Word::from([1, 1, 0, 1u32])),
            ],
            [(
                StorageSlotName::mock(4),
                StorageMapPatch::from_iters(
                    [
                        StorageMapKey::from_array([1, 1, 1, 0]),
                        StorageMapKey::from_array([0, 1, 1, 1]),
                    ],
                    [(StorageMapKey::from_array([1, 1, 1, 1]), Word::from([1, 1, 1, 1u32]))],
                ),
            )],
        );

        let non_fungible: Asset = NonFungibleAsset::new(&NonFungibleAssetDetails::new(
            AccountIdBuilder::new()
                .account_type(AccountType::Public)
                .build_with_rng(&mut rand::rng()),
            vec![6],
        ))
        .into();
        let fungible_2: Asset = FungibleAsset::new(
            AccountIdBuilder::new()
                .account_type(AccountType::Public)
                .build_with_rng(&mut rand::rng()),
            10,
        )
        .unwrap()
        .into();
        let vault_delta = AccountVaultDelta::from_iters([non_fungible], [fungible_2]);

        assert_eq!(storage_patch.to_bytes().len(), storage_patch.get_size_hint());
        assert_eq!(vault_delta.to_bytes().len(), vault_delta.get_size_hint());

        let account_delta =
            AccountDelta::new(account_id, storage_patch, vault_delta, None, ONE).unwrap();
        assert_eq!(account_delta.to_bytes().len(), account_delta.get_size_hint());

        // Account

        let account_id =
            AccountId::try_from(ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE).unwrap();

        let asset_vault = AssetVault::mock();
        assert_eq!(asset_vault.to_bytes().len(), asset_vault.get_size_hint());

        let account_storage = AccountStorage::mock();
        assert_eq!(account_storage.to_bytes().len(), account_storage.get_size_hint());

        let account_code = AccountCode::mock();
        assert_eq!(account_code.to_bytes().len(), account_code.get_size_hint());

        let account =
            Account::new_existing(account_id, asset_vault, account_storage, account_code, ONE);
        assert_eq!(account.to_bytes().len(), account.get_size_hint());
    }
}