miden-protocol 0.14.5

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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
use alloc::collections::BTreeMap;
use alloc::collections::btree_map::Entry;
use alloc::vec::Vec;

use super::{
    AccountDeltaError,
    ByteReader,
    ByteWriter,
    Deserializable,
    DeserializationError,
    Serializable,
    Word,
};
use crate::account::{
    StorageMap,
    StorageMapKey,
    StorageSlotContent,
    StorageSlotName,
    StorageSlotType,
};
use crate::{EMPTY_WORD, Felt, ZERO};

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

/// The [`AccountStorageDelta`] stores the differences between two states of account storage.
///
/// The delta consists of a map from [`StorageSlotName`] to [`StorageSlotDelta`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AccountStorageDelta {
    /// The updates to the slots of the account.
    deltas: BTreeMap<StorageSlotName, StorageSlotDelta>,
}

impl AccountStorageDelta {
    /// Creates a new, empty storage delta.
    pub fn new() -> Self {
        Self { deltas: BTreeMap::new() }
    }

    /// Creates a new storage delta from the provided slot deltas.
    pub fn from_raw(deltas: BTreeMap<StorageSlotName, StorageSlotDelta>) -> Self {
        Self { deltas }
    }

    /// Returns the delta for the provided slot name, or `None` if no delta exists.
    pub fn get(&self, slot_name: &StorageSlotName) -> Option<&StorageSlotDelta> {
        self.deltas.get(slot_name)
    }

    /// Returns an iterator over the slot deltas.
    pub(crate) fn slots(&self) -> impl Iterator<Item = (&StorageSlotName, &StorageSlotDelta)> {
        self.deltas.iter()
    }

    /// Returns an iterator over the updated values in this storage delta.
    pub fn values(&self) -> impl Iterator<Item = (&StorageSlotName, &Word)> {
        self.deltas.iter().filter_map(|(slot_name, slot_delta)| match slot_delta {
            StorageSlotDelta::Value(word) => Some((slot_name, word)),
            StorageSlotDelta::Map(_) => None,
        })
    }

    /// Returns an iterator over the updated maps in this storage delta.
    pub fn maps(&self) -> impl Iterator<Item = (&StorageSlotName, &StorageMapDelta)> {
        self.deltas.iter().filter_map(|(slot_name, slot_delta)| match slot_delta {
            StorageSlotDelta::Value(_) => None,
            StorageSlotDelta::Map(map_delta) => Some((slot_name, map_delta)),
        })
    }

    /// Returns true if storage delta contains no updates.
    pub fn is_empty(&self) -> bool {
        self.deltas.is_empty()
    }

    /// Tracks a slot change.
    ///
    /// This does not (and cannot) validate that the slot name _exists_ or that it points to a
    /// _value_ slot in the corresponding account.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - the slot name points to an existing slot that is not of type value.
    pub fn set_item(
        &mut self,
        slot_name: StorageSlotName,
        new_slot_value: Word,
    ) -> Result<(), AccountDeltaError> {
        if !self.deltas.get(&slot_name).map(StorageSlotDelta::is_value).unwrap_or(true) {
            return Err(AccountDeltaError::StorageSlotUsedAsDifferentTypes(slot_name));
        }

        self.deltas.insert(slot_name, StorageSlotDelta::Value(new_slot_value));

        Ok(())
    }

    /// Tracks a map item change.
    ///
    /// This does not (and cannot) validate that the slot name _exists_ or that it points to a
    /// _map_ slot in the corresponding account.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - the slot name points to an existing slot that is not of type map.
    pub fn set_map_item(
        &mut self,
        slot_name: StorageSlotName,
        key: StorageMapKey,
        new_value: Word,
    ) -> Result<(), AccountDeltaError> {
        match self
            .deltas
            .entry(slot_name.clone())
            .or_insert(StorageSlotDelta::Map(StorageMapDelta::default()))
        {
            StorageSlotDelta::Value(_) => {
                return Err(AccountDeltaError::StorageSlotUsedAsDifferentTypes(slot_name));
            },
            StorageSlotDelta::Map(storage_map_delta) => {
                storage_map_delta.insert(key, new_value);
            },
        };

        Ok(())
    }

    /// Inserts an empty storage map delta for the provided slot name.
    ///
    /// This is useful for full state deltas to represent an empty map in the delta.
    ///
    /// This overwrites the existing slot delta, if any.
    pub fn insert_empty_map_delta(&mut self, slot_name: StorageSlotName) {
        self.deltas.insert(slot_name, StorageSlotDelta::with_empty_map());
    }

    /// Merges another delta into this one, overwriting any existing values.
    pub fn merge(&mut self, other: Self) -> Result<(), AccountDeltaError> {
        for (slot_name, slot_delta) in other.deltas {
            match self.deltas.entry(slot_name.clone()) {
                Entry::Vacant(vacant_entry) => {
                    vacant_entry.insert(slot_delta);
                },
                Entry::Occupied(mut occupied_entry) => {
                    occupied_entry.get_mut().merge(slot_delta).ok_or_else(|| {
                        AccountDeltaError::StorageSlotUsedAsDifferentTypes(slot_name)
                    })?;
                },
            }
        }

        Ok(())
    }

    /// Returns an iterator of all the cleared storage slots.
    fn cleared_values(&self) -> impl Iterator<Item = &StorageSlotName> {
        self.values().filter_map(
            |(slot_name, slot_value)| {
                if slot_value.is_empty() { Some(slot_name) } else { None }
            },
        )
    }

    /// Returns an iterator of all the updated storage slots.
    fn updated_values(&self) -> impl Iterator<Item = (&StorageSlotName, &Word)> {
        self.values().filter_map(|(slot_name, slot_value)| {
            if !slot_value.is_empty() {
                Some((slot_name, slot_value))
            } else {
                None
            }
        })
    }

    /// Appends the storage slots delta to the given `elements` from which the delta commitment will
    /// be computed.
    pub(super) fn append_delta_elements(&self, elements: &mut Vec<Felt>) {
        const DOMAIN_VALUE: Felt = Felt::new(2);
        const DOMAIN_MAP: Felt = Felt::new(3);

        for (slot_name, slot_delta) in self.deltas.iter() {
            let slot_id = slot_name.id();

            match slot_delta {
                StorageSlotDelta::Value(new_value) => {
                    elements.extend_from_slice(&[
                        DOMAIN_VALUE,
                        ZERO,
                        slot_id.suffix(),
                        slot_id.prefix(),
                    ]);
                    elements.extend_from_slice(new_value.as_elements());
                },
                StorageSlotDelta::Map(map_delta) => {
                    for (key, value) in map_delta.entries() {
                        elements.extend_from_slice(key.as_elements());
                        elements.extend_from_slice(value.as_elements());
                    }

                    let num_changed_entries = Felt::try_from(map_delta.num_entries() as u64)
                        .expect(
                            "number of changed entries should not exceed max representable felt",
                        );

                    elements.extend_from_slice(&[
                        DOMAIN_MAP,
                        num_changed_entries,
                        slot_id.suffix(),
                        slot_id.prefix(),
                    ]);
                    elements.extend_from_slice(EMPTY_WORD.as_elements());
                },
            }
        }
    }

    /// Consumes self and returns the underlying map of the storage delta.
    pub fn into_map(self) -> BTreeMap<StorageSlotName, StorageSlotDelta> {
        self.deltas
    }
}

impl Default for AccountStorageDelta {
    fn default() -> Self {
        Self::new()
    }
}

impl Serializable for AccountStorageDelta {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        let num_cleared_values = self.cleared_values().count();
        let num_cleared_values =
            u8::try_from(num_cleared_values).expect("number of slots should fit in u8");
        let cleared_values = self.cleared_values();

        let num_updated_values = self.updated_values().count();
        let num_updated_values =
            u8::try_from(num_updated_values).expect("number of slots should fit in u8");
        let updated_values = self.updated_values();

        let num_maps = self.maps().count();
        let num_maps = u8::try_from(num_maps).expect("number of slots should fit in u8");
        let maps = self.maps();

        target.write_u8(num_cleared_values);
        target.write_many(cleared_values);

        target.write_u8(num_updated_values);
        target.write_many(updated_values);

        target.write_u8(num_maps);
        target.write_many(maps);
    }

    fn get_size_hint(&self) -> usize {
        let u8_size = 0u8.get_size_hint();

        let mut storage_map_delta_size = 0;
        for (slot_name, storage_map_delta) in self.maps() {
            // The serialized size of each entry is the combination of slot (key) and the delta
            // (value).
            storage_map_delta_size += slot_name.get_size_hint() + storage_map_delta.get_size_hint();
        }

        // Length Prefixes
        u8_size * 3 +
        // Cleared Values
        self.cleared_values().fold(0, |acc, slot_name| acc + slot_name.get_size_hint()) +
        // Updated Values
        self.updated_values().fold(0, |acc, (slot_name, slot_value)| {
            acc + slot_name.get_size_hint() + slot_value.get_size_hint()
        }) +
        // Storage Map Delta
        storage_map_delta_size
    }
}

impl Deserializable for AccountStorageDelta {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        let mut deltas = BTreeMap::new();

        let num_cleared_values = source.read_u8()?;
        for _ in 0..num_cleared_values {
            let cleared_value: StorageSlotName = source.read()?;
            deltas.insert(cleared_value, StorageSlotDelta::with_empty_value());
        }

        let num_updated_values = source.read_u8()?;
        for _ in 0..num_updated_values {
            let (updated_slot, updated_value) = source.read()?;
            deltas.insert(updated_slot, StorageSlotDelta::Value(updated_value));
        }

        let num_maps = source.read_u8()? as usize;
        for read_result in source.read_many_iter::<(StorageSlotName, StorageMapDelta)>(num_maps)? {
            let (slot_name, map_delta) = read_result?;
            deltas.insert(slot_name, StorageSlotDelta::Map(map_delta));
        }

        Ok(Self::from_raw(deltas))
    }
}

// STORAGE SLOT DELTA
// ================================================================================================

/// The delta of a single storage slot.
///
/// - [`StorageSlotDelta::Value`] contains the value to which a value slot is updated.
/// - [`StorageSlotDelta::Map`] contains the [`StorageMapDelta`] which contains the key-value pairs
///   that were updated in a map slot.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StorageSlotDelta {
    Value(Word),
    Map(StorageMapDelta),
}

impl StorageSlotDelta {
    // CONSTANTS
    // ----------------------------------------------------------------------------------------

    /// The type byte for value slot deltas.
    const VALUE: u8 = 0;

    /// The type byte for map slot deltas.
    const MAP: u8 = 1;

    // CONSTRUCTORS
    // ----------------------------------------------------------------------------------------

    /// Returns a new [`StorageSlotDelta::Value`] with an empty value.
    pub fn with_empty_value() -> Self {
        Self::Value(Word::empty())
    }

    /// Returns a new [`StorageSlotDelta::Map`] with an empty map delta.
    pub fn with_empty_map() -> Self {
        Self::Map(StorageMapDelta::default())
    }

    // ACCESSORS
    // ----------------------------------------------------------------------------------------

    /// Returns the [`StorageSlotType`] of this slot delta.
    pub fn slot_type(&self) -> StorageSlotType {
        match self {
            StorageSlotDelta::Value(_) => StorageSlotType::Value,
            StorageSlotDelta::Map(_) => StorageSlotType::Map,
        }
    }

    /// Returns `true` if the slot delta is of type [`StorageSlotDelta::Value`], `false` otherwise.
    pub fn is_value(&self) -> bool {
        matches!(self, Self::Value(_))
    }

    /// Returns `true` if the slot delta is of type [`StorageSlotDelta::Map`], `false` otherwise.
    pub fn is_map(&self) -> bool {
        matches!(self, Self::Map(_))
    }

    // MUTATORS
    // ----------------------------------------------------------------------------------------

    /// Unwraps a value slot delta into a [`Word`].
    ///
    /// # Panics
    ///
    /// Panics if:
    /// - `self` is not of type [`StorageSlotDelta::Value`].
    pub fn unwrap_value(self) -> Word {
        match self {
            StorageSlotDelta::Value(value) => value,
            StorageSlotDelta::Map(_) => panic!("called unwrap_value on a map slot delta"),
        }
    }

    /// Unwraps a map slot delta into a [`StorageMapDelta`].
    ///
    /// # Panics
    ///
    /// Panics if:
    /// - `self` is not of type [`StorageSlotDelta::Map`].
    pub fn unwrap_map(self) -> StorageMapDelta {
        match self {
            StorageSlotDelta::Value(_) => panic!("called unwrap_map on a value slot delta"),
            StorageSlotDelta::Map(map_delta) => map_delta,
        }
    }

    /// Merges `other` into `self`.
    ///
    /// # Errors
    ///
    /// Returns `None` if:
    /// - merging failed due to a slot type mismatch.
    #[must_use]
    fn merge(&mut self, other: Self) -> Option<()> {
        match (self, other) {
            (StorageSlotDelta::Value(current_value), StorageSlotDelta::Value(new_value)) => {
                *current_value = new_value;
            },
            (StorageSlotDelta::Map(current_map_delta), StorageSlotDelta::Map(new_map_delta)) => {
                current_map_delta.merge(new_map_delta);
            },
            (..) => {
                return None;
            },
        }

        Some(())
    }
}

impl From<StorageSlotContent> for StorageSlotDelta {
    fn from(content: StorageSlotContent) -> Self {
        match content {
            StorageSlotContent::Value(word) => StorageSlotDelta::Value(word),
            StorageSlotContent::Map(storage_map) => {
                StorageSlotDelta::Map(StorageMapDelta::from(storage_map))
            },
        }
    }
}

impl Serializable for StorageSlotDelta {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        match self {
            StorageSlotDelta::Value(value) => {
                target.write_u8(Self::VALUE);
                target.write(value);
            },
            StorageSlotDelta::Map(storage_map_delta) => {
                target.write_u8(Self::MAP);
                target.write(storage_map_delta);
            },
        }
    }
}

impl Deserializable for StorageSlotDelta {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        match source.read_u8()? {
            Self::VALUE => {
                let value = source.read()?;
                Ok(Self::Value(value))
            },
            Self::MAP => {
                let map_delta = source.read()?;
                Ok(Self::Map(map_delta))
            },
            other => Err(DeserializationError::InvalidValue(format!(
                "unknown storage slot delta variant {other}"
            ))),
        }
    }
}

// STORAGE MAP DELTA
// ================================================================================================

/// [StorageMapDelta] stores the differences between two states of account storage maps.
///
/// The differences are represented as leaf updates: a map of updated item key ([Word]) to
/// value ([Word]). For cleared items the value is [EMPTY_WORD].
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct StorageMapDelta(BTreeMap<StorageMapKey, Word>);

impl StorageMapDelta {
    /// Creates a new storage map delta from the provided leaves.
    pub fn new(map: BTreeMap<StorageMapKey, Word>) -> Self {
        Self(map)
    }

    /// Returns the number of changed entries in this map delta.
    pub fn num_entries(&self) -> usize {
        self.0.len()
    }

    /// Returns a reference to the updated entries in this storage map delta.
    ///
    /// Note that the returned key is the [`StorageMapKey`].
    pub fn entries(&self) -> &BTreeMap<StorageMapKey, Word> {
        &self.0
    }

    /// Inserts an item into the storage map delta.
    pub fn insert(&mut self, key: StorageMapKey, value: Word) {
        self.0.insert(key, value);
    }

    /// Returns true if storage map delta contains no updates.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Merge `other` into this delta, giving precedence to `other`.
    pub fn merge(&mut self, other: Self) {
        // Aggregate the changes into a map such that `other` overwrites self.
        self.0.extend(other.0);
    }

    /// Returns a mutable reference to the underlying map.
    pub fn as_map_mut(&mut self) -> &mut BTreeMap<StorageMapKey, Word> {
        &mut self.0
    }

    /// Returns an iterator of all the cleared keys in the storage map.
    fn cleared_keys(&self) -> impl Iterator<Item = &StorageMapKey> + '_ {
        self.0.iter().filter(|&(_, value)| value.is_empty()).map(|(key, _)| key)
    }

    /// Returns an iterator of all the updated entries in the storage map.
    fn updated_entries(&self) -> impl Iterator<Item = (&StorageMapKey, &Word)> + '_ {
        self.0.iter().filter_map(
            |(key, value)| {
                if !value.is_empty() { Some((key, value)) } else { None }
            },
        )
    }
}

#[cfg(any(feature = "testing", test))]
impl StorageMapDelta {
    /// Creates a new [StorageMapDelta] from the provided iterators.
    pub fn from_iters(
        cleared_leaves: impl IntoIterator<Item = StorageMapKey>,
        updated_leaves: impl IntoIterator<Item = (StorageMapKey, Word)>,
    ) -> Self {
        Self(BTreeMap::from_iter(
            cleared_leaves.into_iter().map(|key| (key, EMPTY_WORD)).chain(updated_leaves),
        ))
    }

    /// Consumes self and returns the underlying map.
    pub fn into_map(self) -> BTreeMap<StorageMapKey, Word> {
        self.0
    }
}

/// Converts a [StorageMap] into a [StorageMapDelta] for initial delta construction.
impl From<StorageMap> for StorageMapDelta {
    fn from(map: StorageMap) -> Self {
        StorageMapDelta::new(map.into_entries().into_iter().collect())
    }
}

impl Serializable for StorageMapDelta {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        let cleared: Vec<&StorageMapKey> = self.cleared_keys().collect();
        let updated: Vec<(&StorageMapKey, &Word)> = self.updated_entries().collect();

        target.write_usize(cleared.len());
        target.write_many(cleared.iter());

        target.write_usize(updated.len());
        target.write_many(updated.iter());
    }

    fn get_size_hint(&self) -> usize {
        let cleared_keys_count = self.cleared_keys().count();
        let updated_entries_count = self.updated_entries().count();

        // Cleared Keys
        cleared_keys_count.get_size_hint() +
        cleared_keys_count * StorageMapKey::SERIALIZED_SIZE +

        // Updated Entries
        updated_entries_count.get_size_hint() +
        updated_entries_count * (StorageMapKey::SERIALIZED_SIZE + Word::SERIALIZED_SIZE)
    }
}

impl Deserializable for StorageMapDelta {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        let mut map = BTreeMap::new();

        let cleared_count = source.read_usize()?;
        for _ in 0..cleared_count {
            let cleared_key = source.read()?;
            map.insert(cleared_key, EMPTY_WORD);
        }

        let updated_count = source.read_usize()?;
        for _ in 0..updated_count {
            let (updated_key, updated_value) = source.read()?;
            map.insert(updated_key, updated_value);
        }

        Ok(Self::new(map))
    }
}

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

#[cfg(test)]
mod tests {
    use anyhow::Context;
    use assert_matches::assert_matches;

    use super::{AccountStorageDelta, Deserializable, Serializable};
    use crate::account::{StorageMapDelta, StorageMapKey, StorageSlotDelta, StorageSlotName};
    use crate::errors::AccountDeltaError;
    use crate::{ONE, Word};

    #[test]
    fn account_storage_delta_returns_err_on_slot_type_mismatch() {
        let value_slot_name = StorageSlotName::mock(1);
        let map_slot_name = StorageSlotName::mock(2);

        let mut delta = AccountStorageDelta::from_iters(
            [value_slot_name.clone()],
            [],
            [(map_slot_name.clone(), StorageMapDelta::default())],
        );

        let err = delta
            .set_map_item(value_slot_name.clone(), StorageMapKey::empty(), Word::empty())
            .unwrap_err();
        assert_matches!(err, AccountDeltaError::StorageSlotUsedAsDifferentTypes(slot_name) => {
            assert_eq!(value_slot_name, slot_name)
        });

        let err = delta.set_item(map_slot_name.clone(), Word::empty()).unwrap_err();
        assert_matches!(err, AccountDeltaError::StorageSlotUsedAsDifferentTypes(slot_name) => {
            assert_eq!(map_slot_name, slot_name)
        });
    }

    #[test]
    fn test_is_empty() {
        let storage_delta = AccountStorageDelta::new();
        assert!(storage_delta.is_empty());

        let storage_delta = AccountStorageDelta::from_iters([StorageSlotName::mock(1)], [], []);
        assert!(!storage_delta.is_empty());

        let storage_delta = AccountStorageDelta::from_iters(
            [],
            [(StorageSlotName::mock(2), Word::from([ONE, ONE, ONE, ONE]))],
            [],
        );
        assert!(!storage_delta.is_empty());

        let storage_delta = AccountStorageDelta::from_iters(
            [],
            [],
            [(StorageSlotName::mock(3), StorageMapDelta::default())],
        );
        assert!(!storage_delta.is_empty());
    }

    #[test]
    fn test_serde_account_storage_delta() {
        let storage_delta = AccountStorageDelta::new();
        let serialized = storage_delta.to_bytes();
        let deserialized = AccountStorageDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, storage_delta);
        assert_eq!(storage_delta.get_size_hint(), serialized.len());

        let storage_delta = AccountStorageDelta::from_iters([StorageSlotName::mock(1)], [], []);
        let serialized = storage_delta.to_bytes();
        let deserialized = AccountStorageDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, storage_delta);
        assert_eq!(storage_delta.get_size_hint(), serialized.len());

        let storage_delta = AccountStorageDelta::from_iters(
            [],
            [(StorageSlotName::mock(2), Word::from([ONE, ONE, ONE, ONE]))],
            [],
        );
        let serialized = storage_delta.to_bytes();
        let deserialized = AccountStorageDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, storage_delta);
        assert_eq!(storage_delta.get_size_hint(), serialized.len());

        let storage_delta = AccountStorageDelta::from_iters(
            [],
            [],
            [(StorageSlotName::mock(3), StorageMapDelta::default())],
        );
        let serialized = storage_delta.to_bytes();
        let deserialized = AccountStorageDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, storage_delta);
        assert_eq!(storage_delta.get_size_hint(), serialized.len());
    }

    #[test]
    fn test_serde_storage_map_delta() {
        let storage_map_delta = StorageMapDelta::default();
        let serialized = storage_map_delta.to_bytes();
        let deserialized = StorageMapDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, storage_map_delta);

        let storage_map_delta =
            StorageMapDelta::from_iters([StorageMapKey::from_array([1, 1, 1, 1])], []);
        let serialized = storage_map_delta.to_bytes();
        let deserialized = StorageMapDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, storage_map_delta);

        let storage_map_delta = StorageMapDelta::from_iters(
            [],
            [(StorageMapKey::empty(), Word::from([ONE, ONE, ONE, ONE]))],
        );
        let serialized = storage_map_delta.to_bytes();
        let deserialized = StorageMapDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, storage_map_delta);
    }

    #[test]
    fn test_serde_storage_slot_value_delta() {
        let slot_delta = StorageSlotDelta::with_empty_value();
        let serialized = slot_delta.to_bytes();
        let deserialized = StorageSlotDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, slot_delta);

        let slot_delta = StorageSlotDelta::Value(Word::from([1, 2, 3, 4u32]));
        let serialized = slot_delta.to_bytes();
        let deserialized = StorageSlotDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, slot_delta);
    }

    #[test]
    fn test_serde_storage_slot_map_delta() {
        let slot_delta = StorageSlotDelta::with_empty_map();
        let serialized = slot_delta.to_bytes();
        let deserialized = StorageSlotDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, slot_delta);

        let map_delta = StorageMapDelta::from_iters(
            [StorageMapKey::from_array([1, 2, 3, 4])],
            [(StorageMapKey::from_array([5, 6, 7, 8]), Word::from([3, 4, 5, 6u32]))],
        );
        let slot_delta = StorageSlotDelta::Map(map_delta);
        let serialized = slot_delta.to_bytes();
        let deserialized = StorageSlotDelta::read_from_bytes(&serialized).unwrap();
        assert_eq!(deserialized, slot_delta);
    }

    #[rstest::rstest]
    #[case::some_some(Some(1), Some(2), Some(2))]
    #[case::none_some(None, Some(2), Some(2))]
    #[case::some_none(Some(1), None, None)]
    #[test]
    fn merge_items(
        #[case] x: Option<u32>,
        #[case] y: Option<u32>,
        #[case] expected: Option<u32>,
    ) -> anyhow::Result<()> {
        /// Creates a delta containing the item as an update if Some, else with the item cleared.
        fn create_delta(item: Option<u32>) -> AccountStorageDelta {
            let slot_name = StorageSlotName::mock(123);
            let item = item.map(|x| (slot_name.clone(), Word::from([x, 0, 0, 0])));

            AccountStorageDelta::new()
                .add_cleared_items(item.is_none().then_some(slot_name.clone()))
                .add_updated_values(item)
        }

        let mut delta_x = create_delta(x);
        let delta_y = create_delta(y);
        let expected = create_delta(expected);

        delta_x.merge(delta_y).context("failed to merge deltas")?;

        assert_eq!(delta_x, expected);

        Ok(())
    }

    #[rstest::rstest]
    #[case::some_some(Some(1), Some(2), Some(2))]
    #[case::none_some(None, Some(2), Some(2))]
    #[case::some_none(Some(1), None, None)]
    #[test]
    fn merge_maps(#[case] x: Option<u32>, #[case] y: Option<u32>, #[case] expected: Option<u32>) {
        fn create_delta(value: Option<u32>) -> StorageMapDelta {
            let key = StorageMapKey::from_array([10, 0, 0, 0]);
            match value {
                Some(value) => {
                    StorageMapDelta::from_iters([], [(key, Word::from([value, 0, 0, 0]))])
                },
                None => StorageMapDelta::from_iters([key], []),
            }
        }

        let mut delta_x = create_delta(x);
        let delta_y = create_delta(y);
        let expected = create_delta(expected);

        delta_x.merge(delta_y);

        assert_eq!(delta_x, expected);
    }
}