lazy_imap.rs 28 KB
Newer Older
1
// Copyright 2018-2021 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::{
16
    CacheCell,
17
    EntryState,
18
    StorageEntry,
19
};
20
use crate::traits::{
21
22
    clear_packed_root,
    pull_packed_root_opt,
23
    ExtKeyPtr,
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
    KeyPtr,
    PackedLayout,
    SpreadLayout,
};
use core::{
    fmt,
    fmt::Debug,
    ptr::NonNull,
};
use ink_prelude::{
    boxed::Box,
    collections::BTreeMap,
};
use ink_primitives::Key;

/// The index type used in the lazy storage chunk.
pub type Index = u32;

/// A lazy storage chunk that spans over a whole chunk of storage cells.
///
/// # Note
///
/// This is mainly used as low-level storage primitives by other high-level
/// storage primitives in order to manage the contract storage for a whole
/// chunk of storage cells.
///
50
/// A chunk of storage cells is a contiguous range of `2^32` storage cells.
51
52
53
54
55
56
57
58
59
60
61
62
pub struct LazyIndexMap<V> {
    /// The offset key for the chunk of cells.
    ///
    /// If the lazy chunk has been initialized during contract initialization
    /// the key will be `None` since there won't be a storage region associated
    /// to the lazy chunk which prevents it from lazily loading elements. This,
    /// however, is only checked at contract runtime. We might incorporate
    /// compile-time checks for this particular use case later on.
    key: Option<Key>,
    /// The subset of currently cached entries of the lazy storage chunk.
    ///
    /// An entry is cached as soon as it is loaded or written.
63
    cached_entries: CacheCell<EntryMap<V>>,
64
65
}

66
struct DebugEntryMap<'a, V>(&'a CacheCell<EntryMap<V>>);
67
68
69
70
71
72

impl<'a, V> Debug for DebugEntryMap<'a, V>
where
    V: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73
        f.debug_map().entries(self.0.as_inner().iter()).finish()
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
    }
}

impl<V> Debug for LazyIndexMap<V>
where
    V: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("LazyIndexMap")
            .field("key", &self.key)
            .field("cached_entries", &DebugEntryMap(&self.cached_entries))
            .finish()
    }
}

#[test]
fn debug_impl_works() {
    let mut imap = <LazyIndexMap<i32>>::new();
    // Empty imap.
    assert_eq!(
        format!("{:?}", &imap),
        "LazyIndexMap { key: None, cached_entries: {} }",
    );
    // Filled imap.
    imap.put(0, Some(1));
    imap.put(42, Some(2));
    imap.put(999, None);
    assert_eq!(
        format!("{:?}", &imap),
        "LazyIndexMap { \
            key: None, \
            cached_entries: {\
                0: Entry { \
                    value: Some(1), \
                    state: Mutated \
                }, \
                42: Entry { \
                    value: Some(2), \
                    state: Mutated \
                }, \
                999: Entry { \
                    value: None, \
                    state: Mutated \
                }\
            } \
        }",
    );
}

impl<V> Default for LazyIndexMap<V> {
    fn default() -> Self {
        Self::new()
    }
}

/// The map for the contract storage entries.
///
/// # Note
///
/// We keep the whole entry in a `Box<T>` in order to prevent pointer
/// invalidation upon updating the cache through `&self` methods as in
/// [`LazyIndexMap::get`].
136
pub type EntryMap<V> = BTreeMap<Index, Box<StorageEntry<V>>>;
137
138
139
140
141
142
143
144
145
146
147

impl<V> LazyIndexMap<V> {
    /// Creates a new empty lazy map.
    ///
    /// # Note
    ///
    /// A lazy map created this way cannot be used to load from the contract storage.
    /// All operations that directly or indirectly load from storage will panic.
    pub fn new() -> Self {
        Self {
            key: None,
148
            cached_entries: CacheCell::new(EntryMap::new()),
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        }
    }

    /// Creates a new empty lazy map positioned at the given key.
    ///
    /// # Note
    ///
    /// This constructor is private and should never need to be called from
    /// outside this module. It is used to construct a lazy index map from a
    /// key that is only useful upon a contract call. Use [`LazyIndexMap::new`]
    /// for construction during contract initialization.
    fn lazy(key: Key) -> Self {
        Self {
            key: Some(key),
163
            cached_entries: CacheCell::new(EntryMap::new()),
164
165
166
167
168
169
170
171
172
173
        }
    }

    /// Returns the offset key of the lazy map if any.
    pub fn key(&self) -> Option<&Key> {
        self.key.as_ref()
    }

    /// Returns a shared reference to the underlying entries.
    fn entries(&self) -> &EntryMap<V> {
174
        self.cached_entries.as_inner()
175
176
177
178
    }

    /// Returns an exclusive reference to the underlying entries.
    fn entries_mut(&mut self) -> &mut EntryMap<V> {
179
        self.cached_entries.as_inner_mut()
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    }

    /// Puts the new value at the given index.
    ///
    /// # Note
    ///
    /// - Use [`LazyIndexMap::put`]`(None)` in order to remove an element.
    /// - Prefer this method over [`LazyIndexMap::put_get`] if you are not interested
    ///   in the old value of the same cell index.
    ///
    /// # Panics
    ///
    /// - If the lazy chunk is in an invalid state that forbids interaction.
    /// - If the decoding of the old element at the given index failed.
    pub fn put(&mut self, index: Index, new_value: Option<V>) {
195
196
197
198
199
200
201
202
        use ink_prelude::collections::btree_map::Entry as BTreeMapEntry;
        match self.entries_mut().entry(index) {
            BTreeMapEntry::Occupied(mut occupied) => {
                // We can re-use the already existing boxed `Entry` and simply
                // swap the underlying values.
                occupied.get_mut().put(new_value);
            }
            BTreeMapEntry::Vacant(vacant) => {
203
204
                vacant
                    .insert(Box::new(StorageEntry::new(new_value, EntryState::Mutated)));
205
206
            }
        }
207
208
209
    }
}

210
211
#[cfg(feature = "std")]
const _: () = {
212
    use crate::traits::StorageLayout;
213
    use ink_metadata::layout::{
214
215
216
217
218
        ArrayLayout,
        CellLayout,
        Layout,
        LayoutKey,
    };
219
    use scale_info::TypeInfo;
220
221
222

    impl<T> StorageLayout for LazyIndexMap<T>
    where
223
        T: TypeInfo + 'static,
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    {
        fn layout(key_ptr: &mut KeyPtr) -> Layout {
            let capacity = u32::MAX;
            Layout::Array(ArrayLayout::new(
                LayoutKey::from(key_ptr.advance_by(capacity as u64)),
                capacity,
                1,
                Layout::Cell(CellLayout::new::<T>(LayoutKey::from(
                    key_ptr.advance_by(0),
                ))),
            ))
        }
    }
};

239
240
241
242
243
244
245
impl<V> SpreadLayout for LazyIndexMap<V>
where
    V: PackedLayout,
{
    const FOOTPRINT: u64 = 1_u64 << 32;

    fn pull_spread(ptr: &mut KeyPtr) -> Self {
246
        Self::lazy(*ExtKeyPtr::next_for::<Self>(ptr))
247
248
249
    }

    fn push_spread(&self, ptr: &mut KeyPtr) {
250
        let offset_key = ExtKeyPtr::next_for::<Self>(ptr);
251
        for (&index, entry) in self.entries().iter() {
252
            let root_key = offset_key + (index as u64);
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
            entry.push_packed_root(&root_key);
        }
    }

    #[inline]
    fn clear_spread(&self, _ptr: &mut KeyPtr) {
        // Low-level lazy abstractions won't perform automated clean-up since
        // they generally are not aware of their entire set of associated
        // elements. The high-level abstractions that build upon them are
        // responsible for cleaning up.
    }
}

impl<V> LazyIndexMap<V>
where
    V: PackedLayout,
{
    /// Clears the underlying storage of the entry at the given index.
    ///
    /// # Safety
    ///
    /// For performance reasons this does not synchronize the lazy index map's
    /// memory-side cache which invalidates future accesses the cleared entry.
    /// Care should be taken when using this API.
    ///
    /// The general use of this API is to streamline `Drop` implementations of
279
    /// high-level abstractions that build upon this low-level data structure.
280
281
282
283
284
285
286
    pub fn clear_packed_at(&self, index: Index) {
        let root_key = self.key_at(index).expect("cannot clear in lazy state");
        if <V as SpreadLayout>::REQUIRES_DEEP_CLEAN_UP {
            // We need to load the entity before we remove its associated contract storage
            // because it requires a deep clean-up which propagates clearing to its fields,
            // for example in the case of `T` being a `storage::Box`.
            let entity = self.get(index).expect("cannot clear a non existing entity");
287
            clear_packed_root::<V>(entity, &root_key);
288
289
290
        } else {
            // The type does not require deep clean-up so we can simply clean-up
            // its associated storage cell and be done without having to load it first.
291
            ink_env::clear_contract_storage(&root_key);
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
        }
    }
}

impl<V> LazyIndexMap<V>
where
    V: PackedLayout,
{
    /// Returns an offset key for the given index.
    pub fn key_at(&self, index: Index) -> Option<Key> {
        let key = self.key?;
        let offset_key = key + index as u64;
        Some(offset_key)
    }

    /// Lazily loads the value at the given index.
    ///
    /// # Note
    ///
    /// Only loads a value if `key` is set and if the value has not been loaded yet.
    /// Returns the freshly loaded or already loaded entry of the value.
    ///
    /// # Safety
    ///
    /// This function has a `&self` receiver while returning an `Option<*mut T>`
    /// which is unsafe in isolation. The caller has to determine how to forward
    /// the returned `*mut T`.
    ///
    /// # Safety
    ///
    /// This is an `unsafe` operation because it has a `&self` receiver but returns
    /// a `*mut Entry<T>` pointer that allows for exclusive access. This is safe
324
325
    /// within internal use only and should never be given outside the lazy entity
    /// for public `&self` methods.
326
    unsafe fn lazily_load(&self, index: Index) -> NonNull<StorageEntry<V>> {
327
328
329
330
331
332
333
334
335
        // SAFETY: We have put the whole `cached_entries` mapping into an
        //         `UnsafeCell` because of this caching functionality. The
        //         trick here is that due to using `Box<T>` internally
        //         we are able to return references to the cached entries
        //         while maintaining the invariant that mutating the caching
        //         `BTreeMap` will never invalidate those references.
        //         By returning a raw pointer we enforce an `unsafe` block at
        //         the caller site to underline that guarantees are given by the
        //         caller.
336
        let cached_entries = &mut *self.cached_entries.get_ptr().as_ptr();
337
338
339
340
341
342
343
344
345
346
347
        use ink_prelude::collections::btree_map::Entry as BTreeMapEntry;
        match cached_entries.entry(index) {
            BTreeMapEntry::Occupied(occupied) => {
                NonNull::from(&mut **occupied.into_mut())
            }
            BTreeMapEntry::Vacant(vacant) => {
                let value = self
                    .key_at(index)
                    .map(|key| pull_packed_root_opt::<V>(&key))
                    .unwrap_or(None);
                NonNull::from(
348
349
350
351
                    &mut **vacant.insert(Box::new(StorageEntry::new(
                        value,
                        EntryState::Preserved,
                    ))),
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
                )
            }
        }
    }

    /// Lazily loads the value at the given index.
    ///
    /// # Note
    ///
    /// Only loads a value if `key` is set and if the value has not been loaded yet.
    /// Returns the freshly loaded or already loaded entry of the value.
    ///
    /// # Panics
    ///
    /// - If the lazy chunk is in an invalid state that forbids interaction.
    /// - If the lazy chunk is not in a state that allows lazy loading.
368
    fn lazily_load_mut(&mut self, index: Index) -> &mut StorageEntry<V> {
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
        // SAFETY:
        // - Returning a `&mut Entry<T>` is safe because entities inside the
        //   cache are stored within a `Box` to not invalidate references into
        //   them upon operating on the outer cache.
        unsafe { &mut *self.lazily_load(index).as_ptr() }
    }

    /// Returns a shared reference to the element at the given index if any.
    ///
    /// # Panics
    ///
    /// - If the lazy chunk is in an invalid state that forbids interaction.
    /// - If the decoding of the element at the given index failed.
    pub fn get(&self, index: Index) -> Option<&V> {
        // SAFETY: Dereferencing the `*mut T` pointer into a `&T` is safe
        //         since this method's receiver is `&self` so we do not
        //         leak non-shared references to the outside.
        unsafe { &*self.lazily_load(index).as_ptr() }.value().into()
    }

    /// Returns an exclusive reference to the element at the given index if any.
    ///
    /// # Panics
    ///
    /// - If the lazy chunk is in an invalid state that forbids interaction.
    /// - If the decoding of the element at the given index failed.
    pub fn get_mut(&mut self, index: Index) -> Option<&mut V> {
        self.lazily_load_mut(index).value_mut().into()
    }

    /// Puts the new value at the given index and returns the old value if any.
    ///
    /// # Note
    ///
    /// - Use [`LazyIndexMap::put_get`]`(None)` in order to remove an element
    ///   and retrieve the old element back.
    ///
    /// # Panics
    ///
    /// - If the lazy chunk is in an invalid state that forbids interaction.
    /// - If the decoding of the old element at the given index failed.
    pub fn put_get(&mut self, index: Index, new_value: Option<V>) -> Option<V> {
        self.lazily_load_mut(index).put(new_value)
    }

    /// Swaps the values at indices `x` and `y`.
    ///
    /// This operation tries to be as efficient as possible and reuse allocations.
    ///
    /// # Panics
    ///
    /// - If the lazy chunk is in an invalid state that forbids interaction.
    /// - If the decoding of one of the elements failed.
    pub fn swap(&mut self, x: Index, y: Index) {
        if x == y {
            // Bail out early if both indices are the same.
            return
        }
        let (loaded_x, loaded_y) =
            // SAFETY: The loaded `x` and `y` entries are distinct from each
            //         other guaranteed by the previous check. Also `lazily_load`
            //         guarantees to return a pointer to a pinned entity
            //         so that the returned references do not conflict with
            //         each other.
            unsafe { (
                &mut *self.lazily_load(x).as_ptr(),
                &mut *self.lazily_load(y).as_ptr(),
            ) };
        if loaded_x.value().is_none() && loaded_y.value().is_none() {
            // Bail out since nothing has to be swapped if both values are `None`.
            return
        }
        // Set the `mutate` flag since at this point at least one of the loaded
        // values is guaranteed to be `Some`.
        loaded_x.replace_state(EntryState::Mutated);
        loaded_y.replace_state(EntryState::Mutated);
        core::mem::swap(loaded_x.value_mut(), loaded_y.value_mut());
    }
}

#[cfg(test)]
mod tests {
    use super::{
        super::{
            EntryState,
454
            StorageEntry,
455
456
457
458
        },
        Index,
        LazyIndexMap,
    };
459
460
461
    use crate::traits::{
        KeyPtr,
        SpreadLayout,
462
463
464
465
    };
    use ink_primitives::Key;

    /// Asserts that the cached entries of the given `imap` is equal to the `expected` slice.
466
467
468
469
    fn assert_cached_entries(
        imap: &LazyIndexMap<u8>,
        expected: &[(Index, StorageEntry<u8>)],
    ) {
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
        assert_eq!(imap.entries().len(), expected.len());
        for (given, expected) in imap
            .entries()
            .iter()
            .map(|(index, boxed_entry)| (*index, &**boxed_entry))
            .zip(expected.iter().map(|(index, entry)| (*index, entry)))
        {
            assert_eq!(given, expected);
        }
    }

    #[test]
    fn new_works() {
        let imap = <LazyIndexMap<u8>>::new();
        // Key must be none.
        assert_eq!(imap.key(), None);
        assert_eq!(imap.key_at(0), None);
        // Cached elements must be empty.
        assert_cached_entries(&imap, &[]);
        // Same as default:
        let default_imap = <LazyIndexMap<u8>>::default();
        assert_eq!(imap.key(), default_imap.key());
        assert_eq!(imap.entries(), default_imap.entries());
    }

    #[test]
    fn lazy_works() {
497
        let key = Key::from([0x42; 32]);
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
        let imap = <LazyIndexMap<u8>>::lazy(key);
        // Key must be none.
        assert_eq!(imap.key(), Some(&key));
        assert_eq!(imap.key_at(0), Some(key));
        assert_eq!(imap.key_at(1), Some(key + 1u64));
        // Cached elements must be empty.
        assert_cached_entries(&imap, &[]);
    }

    #[test]
    fn put_get_works() {
        let mut imap = <LazyIndexMap<u8>>::new();
        // Put some values.
        assert_eq!(imap.put_get(1, Some(b'A')), None);
        assert_eq!(imap.put_get(2, Some(b'B')), None);
        assert_eq!(imap.put_get(4, Some(b'C')), None);
        assert_cached_entries(
            &imap,
            &[
517
518
519
                (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (4, StorageEntry::new(Some(b'C'), EntryState::Mutated)),
520
521
522
523
524
525
526
527
            ],
        );
        // Put none values.
        assert_eq!(imap.put_get(3, None), None);
        assert_eq!(imap.put_get(5, None), None);
        assert_cached_entries(
            &imap,
            &[
528
529
530
531
532
                (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (3, StorageEntry::new(None, EntryState::Preserved)),
                (4, StorageEntry::new(Some(b'C'), EntryState::Mutated)),
                (5, StorageEntry::new(None, EntryState::Preserved)),
533
534
535
536
537
538
539
540
            ],
        );
        // Override some values with none.
        assert_eq!(imap.put_get(2, None), Some(b'B'));
        assert_eq!(imap.put_get(4, None), Some(b'C'));
        assert_cached_entries(
            &imap,
            &[
541
542
543
544
545
                (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (2, StorageEntry::new(None, EntryState::Mutated)),
                (3, StorageEntry::new(None, EntryState::Preserved)),
                (4, StorageEntry::new(None, EntryState::Mutated)),
                (5, StorageEntry::new(None, EntryState::Preserved)),
546
547
548
549
550
551
552
553
            ],
        );
        // Override none values with some.
        assert_eq!(imap.put_get(3, Some(b'X')), None);
        assert_eq!(imap.put_get(5, Some(b'Y')), None);
        assert_cached_entries(
            &imap,
            &[
554
555
556
557
558
                (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (2, StorageEntry::new(None, EntryState::Mutated)),
                (3, StorageEntry::new(Some(b'X'), EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Mutated)),
                (5, StorageEntry::new(Some(b'Y'), EntryState::Mutated)),
559
560
561
562
563
564
565
566
            ],
        );
    }

    #[test]
    fn get_works() {
        let mut imap = <LazyIndexMap<u8>>::new();
        let nothing_changed = &[
567
568
569
570
            (1, StorageEntry::new(None, EntryState::Preserved)),
            (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
            (3, StorageEntry::new(None, EntryState::Preserved)),
            (4, StorageEntry::new(Some(b'D'), EntryState::Mutated)),
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
        ];
        // Put some values.
        assert_eq!(imap.put_get(1, None), None);
        assert_eq!(imap.put_get(2, Some(b'B')), None);
        assert_eq!(imap.put_get(3, None), None);
        assert_eq!(imap.put_get(4, Some(b'D')), None);
        assert_cached_entries(&imap, nothing_changed);
        // `get` works:
        assert_eq!(imap.get(1), None);
        assert_eq!(imap.get(2), Some(&b'B'));
        assert_eq!(imap.get(3), None);
        assert_eq!(imap.get(4), Some(&b'D'));
        assert_cached_entries(&imap, nothing_changed);
        // `get_mut` works:
        assert_eq!(imap.get_mut(1), None);
        assert_eq!(imap.get_mut(2), Some(&mut b'B'));
        assert_eq!(imap.get_mut(3), None);
        assert_eq!(imap.get_mut(4), Some(&mut b'D'));
        assert_cached_entries(&imap, nothing_changed);
        // `get` or `get_mut` without cache:
        assert_eq!(imap.get(5), None);
        assert_eq!(imap.get_mut(5), None);
    }

    #[test]
    fn put_works() {
        let mut imap = <LazyIndexMap<u8>>::new();
        // Put some values.
        imap.put(1, None);
        imap.put(2, Some(b'B'));
        imap.put(4, None);
        // The main difference between `put` and `put_get` is that `put` never
        // loads from storage which also has one drawback: Putting a `None`
        // value always ends-up in `Mutated` state for the entry even if the
        // entry is already `None`.
        assert_cached_entries(
            &imap,
            &[
609
610
611
                (1, StorageEntry::new(None, EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Mutated)),
612
613
614
615
616
617
618
619
            ],
        );
        // Overwrite entries:
        imap.put(1, Some(b'A'));
        imap.put(2, None);
        assert_cached_entries(
            &imap,
            &[
620
621
622
                (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (2, StorageEntry::new(None, EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Mutated)),
623
624
625
626
627
628
629
630
            ],
        );
    }

    #[test]
    fn swap_works() {
        let mut imap = <LazyIndexMap<u8>>::new();
        let nothing_changed = &[
631
632
633
634
            (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
            (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
            (3, StorageEntry::new(None, EntryState::Preserved)),
            (4, StorageEntry::new(None, EntryState::Preserved)),
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
        ];
        // Put some values.
        assert_eq!(imap.put_get(1, Some(b'A')), None);
        assert_eq!(imap.put_get(2, Some(b'B')), None);
        assert_eq!(imap.put_get(3, None), None);
        assert_eq!(imap.put_get(4, None), None);
        assert_cached_entries(&imap, nothing_changed);
        // Swap same indices: Check that nothing has changed.
        for i in 0..4 {
            imap.swap(i, i);
        }
        assert_cached_entries(&imap, nothing_changed);
        // Swap `None` values: Check that nothing has changed.
        imap.swap(3, 4);
        imap.swap(4, 3);
        assert_cached_entries(&imap, nothing_changed);
        // Swap `Some` and `None`:
        imap.swap(1, 3);
        assert_cached_entries(
            &imap,
            &[
656
657
658
659
                (1, StorageEntry::new(None, EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (3, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Preserved)),
660
661
662
663
664
665
666
            ],
        );
        // Swap `Some` and `Some`:
        imap.swap(2, 3);
        assert_cached_entries(
            &imap,
            &[
667
668
669
670
                (1, StorageEntry::new(None, EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (3, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Preserved)),
671
672
673
674
675
676
677
            ],
        );
        // Swap out of bounds: `None` and `None`
        imap.swap(4, 5);
        assert_cached_entries(
            &imap,
            &[
678
679
680
681
682
                (1, StorageEntry::new(None, EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (3, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Preserved)),
                (5, StorageEntry::new(None, EntryState::Preserved)),
683
684
685
686
687
688
689
            ],
        );
        // Swap out of bounds: `Some` and `None`
        imap.swap(3, 6);
        assert_cached_entries(
            &imap,
            &[
690
691
692
693
694
695
                (1, StorageEntry::new(None, EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (3, StorageEntry::new(None, EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Preserved)),
                (5, StorageEntry::new(None, EntryState::Preserved)),
                (6, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
696
697
698
699
700
            ],
        );
    }

    #[test]
701
    fn spread_layout_works() -> ink_env::Result<()> {
702
        ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
703
704
            let mut imap = <LazyIndexMap<u8>>::new();
            let nothing_changed = &[
705
706
707
708
                (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (3, StorageEntry::new(None, EntryState::Preserved)),
                (4, StorageEntry::new(None, EntryState::Preserved)),
709
710
711
712
713
714
715
716
717
718
            ];
            // Put some values.
            assert_eq!(imap.put_get(1, Some(b'A')), None);
            assert_eq!(imap.put_get(2, Some(b'B')), None);
            assert_eq!(imap.put_get(3, None), None);
            assert_eq!(imap.put_get(4, None), None);
            assert_cached_entries(&imap, nothing_changed);
            // Push the lazy index map onto the contract storage and then load
            // another instance of it from the contract stoarge.
            // Then: Compare both instances to be equal.
719
            let root_key = Key::from([0x42; 32]);
720
721
722
723
724
725
726
727
728
729
730
731
            SpreadLayout::push_spread(&imap, &mut KeyPtr::from(root_key));
            let imap2 = <LazyIndexMap<u8> as SpreadLayout>::pull_spread(
                &mut KeyPtr::from(root_key),
            );
            assert_cached_entries(&imap2, &[]);
            assert_eq!(imap2.get(1), Some(&b'A'));
            assert_eq!(imap2.get(2), Some(&b'B'));
            assert_eq!(imap2.get(3), None);
            assert_eq!(imap2.get(4), None);
            assert_cached_entries(
                &imap2,
                &[
732
733
734
735
                    (1, StorageEntry::new(Some(b'A'), EntryState::Preserved)),
                    (2, StorageEntry::new(Some(b'B'), EntryState::Preserved)),
                    (3, StorageEntry::new(None, EntryState::Preserved)),
                    (4, StorageEntry::new(None, EntryState::Preserved)),
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
                ],
            );
            // Clear the first lazy index map instance and reload another instance
            // to check whether the associated storage has actually been freed
            // again:
            SpreadLayout::clear_spread(&imap2, &mut KeyPtr::from(root_key));
            // The above `clear_spread` call is a no-op since lazy index map is
            // generally not aware of its associated elements. So we have to
            // manually clear them from the contract storage which is what the
            // high-level data structures like `storage::Vec` would command:
            imap2.clear_packed_at(1);
            imap2.clear_packed_at(2);
            imap2.clear_packed_at(3); // Not really needed here.
            imap2.clear_packed_at(4); // Not really needed here.
            let imap3 = <LazyIndexMap<u8> as SpreadLayout>::pull_spread(
                &mut KeyPtr::from(root_key),
            );
            assert_cached_entries(&imap3, &[]);
            assert_eq!(imap3.get(1), None);
            assert_eq!(imap3.get(2), None);
            assert_eq!(imap3.get(3), None);
            assert_eq!(imap3.get(4), None);
            assert_cached_entries(
                &imap3,
                &[
761
762
763
764
                    (1, StorageEntry::new(None, EntryState::Preserved)),
                    (2, StorageEntry::new(None, EntryState::Preserved)),
                    (3, StorageEntry::new(None, EntryState::Preserved)),
                    (4, StorageEntry::new(None, EntryState::Preserved)),
765
766
767
768
769
770
                ],
            );
            Ok(())
        })
    }
}