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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
// Copyright 2018-2022 Parity Technologies (UK) Ltd.
//
// 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::{
    CacheCell,
    EntryState,
    StorageEntry,
};
use crate::traits::{
    clear_spread_root_opt,
    pull_spread_root_opt,
    ExtKeyPtr,
    KeyPtr,
    SpreadAllocate,
    SpreadLayout,
};
use core::{
    fmt,
    fmt::Debug,
    ptr::NonNull,
};
use ink_primitives::Key;

/// A lazy storage entity.
///
/// This loads its value from storage upon first use.
///
/// # Note
///
/// Use this if the storage field does not need to be loaded in some or most cases.
pub struct LazyCell<T>
where
    T: SpreadLayout,
{
    /// The key to lazily load the value from.
    ///
    /// # Note
    ///
    /// This can be `None` on contract initialization where a `LazyCell` is
    /// normally initialized given a concrete value.
    key: Option<Key>,
    /// The low-level cache for the lazily loaded storage value.
    ///
    /// # Developer Note: Safety
    ///
    /// We use `UnsafeCell` instead of `RefCell` because
    /// the intended use-case is to hand out references (`&` and `&mut`)
    /// to the callers of `Lazy`. This cannot be done without `unsafe`
    /// code even with `RefCell`. Also `RefCell` has a larger memory footprint
    /// and has additional overhead that we can avoid by the interface
    /// and the fact that ink! code is always run single-threaded.
    /// Being efficient is important here because this is intended to be
    /// a low-level primitive with lots of dependencies.
    cache: CacheCell<Option<StorageEntry<T>>>,
}

impl<T> Debug for LazyCell<T>
where
    T: Debug + SpreadLayout,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("LazyCell")
            .field("key", &self.key)
            .field("cache", self.cache.as_inner())
            .finish()
    }
}

#[test]
fn debug_impl_works() -> ink_env::Result<()> {
    ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
        let c1 = <LazyCell<i32>>::new(None);
        assert_eq!(
            format!("{:?}", &c1),
            "LazyCell { key: None, cache: Some(Entry { value: None, state: Mutated }) }",
        );
        let c2 = <LazyCell<i32>>::new(Some(42));
        assert_eq!(
            format!("{:?}", &c2),
            "LazyCell { key: None, cache: Some(Entry { value: Some(42), state: Mutated }) }",
        );
        let c3 = <LazyCell<i32>>::lazy(Key::from([0x00; 32]));
        assert_eq!(
            format!("{:?}", &c3),
            "LazyCell { \
            key: Some(Key(0x_\
                00000000_00000000_\
                00000000_00000000_\
                00000000_00000000_\
                00000000_00000000)\
            ), \
            cache: None \
        }",
        );
        Ok(())
    })
}

impl<T> Drop for LazyCell<T>
where
    T: SpreadLayout,
{
    fn drop(&mut self) {
        if let Some(root_key) = self.key() {
            match self.entry() {
                Some(entry) => {
                    // The inner cell needs to be cleared, no matter if it has
                    // been loaded or not. Otherwise there might be leftovers.
                    // Load from storage and then clear:
                    clear_spread_root_opt::<T, _>(root_key, || entry.value().into())
                }
                None => {
                    // The value is not yet in the cache. we need it in there
                    // though in order to properly clean up.
                    if <T as SpreadLayout>::REQUIRES_DEEP_CLEAN_UP {
                        // The inner cell needs to be cleared, no matter if it has
                        // been loaded or not. Otherwise there might be leftovers.
                        // Load from storage and then clear:
                        clear_spread_root_opt::<T, _>(root_key, || self.get())
                    } else {
                        // Clear without loading from storage:
                        let footprint = <T as SpreadLayout>::FOOTPRINT;
                        assert_footprint_threshold(footprint);
                        let mut key_ptr = KeyPtr::from(*root_key);
                        for _ in 0..footprint {
                            ink_env::clear_contract_storage(key_ptr.advance_by(1));
                        }
                    }
                }
            }
        }
    }
}

#[cfg(feature = "std")]
const _: () = {
    use crate::traits::StorageLayout;
    use ink_metadata::layout::Layout;

    impl<T> StorageLayout for LazyCell<T>
    where
        T: StorageLayout + SpreadLayout,
    {
        fn layout(key_ptr: &mut KeyPtr) -> Layout {
            <T as StorageLayout>::layout(key_ptr)
        }
    }
};

impl<T> SpreadLayout for LazyCell<T>
where
    T: SpreadLayout,
{
    const FOOTPRINT: u64 = <T as SpreadLayout>::FOOTPRINT;

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

    fn push_spread(&self, ptr: &mut KeyPtr) {
        let root_key = ExtKeyPtr::next_for::<Self>(ptr);
        if let Some(entry) = self.entry() {
            entry.push_spread_root(root_key)
        }
    }

    fn clear_spread(&self, ptr: &mut KeyPtr) {
        let root_key = ExtKeyPtr::next_for::<Self>(ptr);
        match <T as SpreadLayout>::REQUIRES_DEEP_CLEAN_UP {
            true => {
                // The inner cell needs to be cleared, no matter if it has
                // been loaded or not. Otherwise there might be leftovers.
                // Load from storage and then clear:
                clear_spread_root_opt::<T, _>(root_key, || self.get())
            }
            false => {
                // Clear without loading from storage:
                let footprint = <T as SpreadLayout>::FOOTPRINT;
                assert_footprint_threshold(footprint);
                let mut key_ptr = KeyPtr::from(*root_key);
                for _ in 0..footprint {
                    ink_env::clear_contract_storage(key_ptr.advance_by(1));
                }
            }
        }
    }
}

impl<T> SpreadAllocate for LazyCell<T>
where
    T: SpreadLayout,
{
    #[inline]
    fn allocate_spread(ptr: &mut KeyPtr) -> Self {
        Self::lazy(*ExtKeyPtr::next_for::<Self>(ptr))
    }
}

// # Developer Note
//
// Implementing PackedLayout for LazyCell is not useful since that would
// potentially allow overlapping distinct LazyCell instances by pulling
// from the same underlying storage cell.
//
// If a user wants a packed LazyCell they can instead pack its inner type.

impl<T> From<T> for LazyCell<T>
where
    T: SpreadLayout,
{
    fn from(value: T) -> Self {
        Self::new(Some(value))
    }
}

impl<T> Default for LazyCell<T>
where
    T: Default + SpreadLayout,
{
    fn default() -> Self {
        Self::new(Some(Default::default()))
    }
}

impl<T> LazyCell<T>
where
    T: SpreadLayout,
{
    /// Creates an already populated lazy storage cell.
    ///
    /// # Note
    ///
    /// Since this already has a value it will never actually load from
    /// the contract storage.
    #[must_use]
    pub fn new(value: Option<T>) -> Self {
        Self {
            key: None,
            cache: CacheCell::new(Some(StorageEntry::new(value, EntryState::Mutated))),
        }
    }

    /// Creates a lazy storage cell for the given key.
    ///
    /// # Note
    ///
    /// This will actually lazily load from the associated storage cell
    /// upon access.
    #[must_use]
    pub fn lazy(key: Key) -> Self {
        Self {
            key: Some(key),
            cache: CacheCell::new(None),
        }
    }

    /// Returns the lazy key if any.
    ///
    /// # Note
    ///
    /// The key is `None` if the `LazyCell` has been initialized as a value.
    /// This generally only happens in ink! constructors.
    fn key(&self) -> Option<&Key> {
        self.key.as_ref()
    }

    /// Returns the cached entry.
    fn entry(&self) -> Option<&StorageEntry<T>> {
        self.cache.as_inner().as_ref()
    }
}

impl<T> LazyCell<T>
where
    T: SpreadLayout,
{
    /// Loads the storage entry.
    ///
    /// Tries to load the entry from cache and falls back to lazily load the
    /// entry from the contract storage.
    unsafe fn load_through_cache(&self) -> NonNull<StorageEntry<T>> {
        // SAFETY: This is critical because we mutably access the entry.
        //         However, we mutate the entry only if it is vacant.
        //         If the entry is occupied by a value we return early.
        //         This way we do not invalidate pointers to this value.
        let cache = &mut *self.cache.get_ptr().as_ptr();
        if cache.is_none() {
            // Load value from storage and then return the cached entry.
            let value = self
                .key
                .map(|key| pull_spread_root_opt::<T>(&key))
                .unwrap_or(None);
            *cache = Some(StorageEntry::new(value, EntryState::Preserved));
        }
        debug_assert!(cache.is_some());
        NonNull::from(cache.as_mut().expect("unpopulated cache entry"))
    }

    /// Returns a shared reference to the entry.
    fn load_entry(&self) -> &StorageEntry<T> {
        // SAFETY: We load the entry either from cache of from contract storage.
        //
        //         This is safe because we are just returning a shared reference
        //         from within a `&self` method. This also cannot change the
        //         loaded value and thus cannot change the `mutate` flag of the
        //         entry. Aliases using this method are safe since ink! is
        //         single-threaded.
        unsafe { &*self.load_through_cache().as_ptr() }
    }

    /// Returns an exclusive reference to the entry.
    fn load_entry_mut(&mut self) -> &mut StorageEntry<T> {
        // SAFETY: We load the entry either from cache of from contract storage.
        //
        //         This is safe because we are just returning an exclusive reference
        //         from within a `&mut self` method. This may change the
        //         loaded value and thus the `mutate` flag of the entry is set.
        //         Aliases cannot happen through this method since ink! is
        //         single-threaded.
        let entry = unsafe { &mut *self.load_through_cache().as_ptr() };
        entry.replace_state(EntryState::Mutated);
        entry
    }

    /// Returns a shared reference to the value.
    ///
    /// # Note
    ///
    /// This eventually lazily loads the value from the contract storage.
    ///
    /// # Panics
    ///
    /// If decoding the loaded value to `T` failed.
    #[must_use]
    pub fn get(&self) -> Option<&T> {
        self.load_entry().value().into()
    }

    /// Returns an exclusive reference to the value.
    ///
    /// # Note
    ///
    /// This eventually lazily loads the value from the contract storage.
    ///
    /// # Panics
    ///
    /// If decoding the loaded value to `T` failed.
    #[must_use]
    pub fn get_mut(&mut self) -> Option<&mut T> {
        self.load_entry_mut().value_mut().into()
    }

    /// Sets the value in this cell to `value`, without executing any reads.
    ///
    /// # Note
    ///
    /// No reads from contract storage will be executed.
    ///
    /// This method should be preferred over dereferencing or `get_mut`
    /// in case the returned value is of no interest to the caller.
    ///
    /// # Panics
    ///
    /// If accessing the inner value fails.
    #[inline]
    pub fn set(&mut self, new_value: T) {
        // SAFETY: This is critical because we mutably access the entry.
        let cache = unsafe { &mut *self.cache.get_ptr().as_ptr() };
        if let Some(cache) = cache.as_mut() {
            //  Cache is already populated we simply overwrite its already existing value.
            cache.put(Some(new_value));
        } else {
            // Cache is empty, so we simply set the cache to the value.
            // The key does not need to exist for this to work, we only need to
            // write the value into the cache and are done. Writing to contract
            // storage happens during setup/teardown of a contract.
            *cache = Some(StorageEntry::new(Some(new_value), EntryState::Mutated));
        }
        debug_assert!(cache.is_some());
    }
}

/// Asserts that the given `footprint` is below `FOOTPRINT_CLEANUP_THRESHOLD`.
fn assert_footprint_threshold(footprint: u64) {
    let footprint_threshold = crate::traits::FOOTPRINT_CLEANUP_THRESHOLD;
    assert!(
        footprint <= footprint_threshold,
        "cannot clean-up a storage entity with a footprint of {}. maximum threshold for clean-up is {}.",
        footprint,
        footprint_threshold,
    );
}

#[cfg(test)]
mod tests {
    use super::{
        EntryState,
        LazyCell,
        StorageEntry,
    };
    use crate::{
        traits::{
            KeyPtr,
            SpreadLayout,
        },
        Lazy,
    };
    use ink_env::test::run_test;
    use ink_primitives::Key;

    #[test]
    fn new_works() {
        // Initialized via some value:
        let mut a = <LazyCell<u8>>::new(Some(b'A'));
        assert_eq!(a.key(), None);
        assert_eq!(
            a.entry(),
            Some(&StorageEntry::new(Some(b'A'), EntryState::Mutated))
        );
        assert_eq!(a.get(), Some(&b'A'));
        assert_eq!(a.get_mut(), Some(&mut b'A'));
        // Initialized as none:
        let mut b = <LazyCell<u8>>::new(None);
        assert_eq!(b.key(), None);
        assert_eq!(
            b.entry(),
            Some(&StorageEntry::new(None, EntryState::Mutated))
        );
        assert_eq!(b.get(), None);
        assert_eq!(b.get_mut(), None);
        // Same as default or from:
        let default_lc = <LazyCell<u8>>::default();
        let from_lc = LazyCell::from(u8::default());
        let new_lc = LazyCell::new(Some(u8::default()));
        assert_eq!(default_lc.get(), from_lc.get());
        assert_eq!(from_lc.get(), new_lc.get());
        assert_eq!(new_lc.get(), Some(&u8::default()));
    }

    #[test]
    fn lazy_works() -> ink_env::Result<()> {
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
            let root_key = Key::from([0x42; 32]);
            let cell = <LazyCell<u8>>::lazy(root_key);
            assert_eq!(cell.key(), Some(&root_key));
            Ok(())
        })
    }

    #[test]
    fn lazy_get_works() -> ink_env::Result<()> {
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
            let cell = <LazyCell<u8>>::lazy(Key::from([0x42; 32]));
            let value = cell.get();
            // We do the normally unreachable check in order to have an easier
            // time finding the issue if the above execution did not panic.
            assert_eq!(value, None);
            Ok(())
        })
    }

    #[test]
    fn get_mut_works() {
        let mut cell = <LazyCell<i32>>::new(Some(1));
        assert_eq!(cell.get(), Some(&1));
        *cell.get_mut().unwrap() += 1;
        assert_eq!(cell.get(), Some(&2));
    }

    #[test]
    fn spread_layout_works() -> ink_env::Result<()> {
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
            let cell_a0 = <LazyCell<u8>>::new(Some(b'A'));
            assert_eq!(cell_a0.get(), Some(&b'A'));
            // Push `cell_a0` to the contract storage.
            // Then, pull `cell_a1` from the contract storage and check if it is
            // equal to `cell_a0`.
            let root_key = Key::from([0x42; 32]);
            SpreadLayout::push_spread(&cell_a0, &mut KeyPtr::from(root_key));
            let cell_a1 =
                <LazyCell<u8> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
            assert_eq!(cell_a1.get(), cell_a0.get());
            assert_eq!(cell_a1.get(), Some(&b'A'));
            assert_eq!(
                cell_a1.entry(),
                Some(&StorageEntry::new(Some(b'A'), EntryState::Preserved))
            );
            // Also test if a lazily instantiated cell works:
            let cell_a2 = <LazyCell<u8>>::lazy(root_key);
            assert_eq!(cell_a2.get(), cell_a0.get());
            assert_eq!(cell_a2.get(), Some(&b'A'));
            assert_eq!(
                cell_a2.entry(),
                Some(&StorageEntry::new(Some(b'A'), EntryState::Preserved))
            );
            // Test if clearing works:
            SpreadLayout::clear_spread(&cell_a1, &mut KeyPtr::from(root_key));
            let cell_a3 = <LazyCell<u8>>::lazy(root_key);
            assert_eq!(cell_a3.get(), None);
            assert_eq!(
                cell_a3.entry(),
                Some(&StorageEntry::new(None, EntryState::Preserved))
            );
            Ok(())
        })
    }

    #[test]
    fn set_works() {
        let mut cell = <LazyCell<i32>>::new(Some(1));
        cell.set(23);
        assert_eq!(cell.get(), Some(&23));
    }

    #[test]
    fn lazy_set_works() -> ink_env::Result<()> {
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
            let mut cell = <LazyCell<u8>>::lazy(Key::from([0x42; 32]));
            let value = cell.get();
            assert_eq!(value, None);

            cell.set(13);
            assert_eq!(cell.get(), Some(&13));
            Ok(())
        })
    }

    #[test]
    fn lazy_set_works_with_spread_layout_push_pull() -> ink_env::Result<()> {
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
            type MaybeValue = Option<u8>;

            // Initialize a LazyCell with None and push it to `k`
            let k = Key::from([0x00; 32]);
            let val: MaybeValue = None;
            SpreadLayout::push_spread(&Lazy::new(val), &mut KeyPtr::from(k));

            // Pull another instance `v` from `k`, check that it is `None`
            let mut v =
                <Lazy<MaybeValue> as SpreadLayout>::pull_spread(&mut KeyPtr::from(k));
            assert_eq!(*v, None);

            // Set `v` using `set` to an actual value
            let actual_value: MaybeValue = Some(13);
            Lazy::set(&mut v, actual_value);

            // Push `v` to `k`
            SpreadLayout::push_spread(&v, &mut KeyPtr::from(k));

            // Load `v2` from `k`
            let v2 =
                <Lazy<MaybeValue> as SpreadLayout>::pull_spread(&mut KeyPtr::from(k));

            // Check that V2 is the set value
            assert_eq!(*v2, Some(13));

            Ok(())
        })
    }

    #[test]
    fn regression_test_for_issue_528() -> ink_env::Result<()> {
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
            let root_key = Key::from([0x00; 32]);
            {
                // Step 1: Push a valid pair onto the contract storage.
                let pair = (LazyCell::new(Some(1i32)), 2i32);
                SpreadLayout::push_spread(&pair, &mut KeyPtr::from(root_key));
            }
            {
                // Step 2: Pull the pair from the step before.
                //
                // 1. Change the second `i32` value of the pair.
                // 2. Push the pair again to contract storage.
                //
                // We prevent the intermediate instance from clearing the storage preemtively by wrapping
                // it inside `ManuallyDrop`. The third step will clean up the same storage region afterwards.
                //
                // We explicitly do not touch or assert the value of `pulled_pair.0` in order to trigger
                // the bug.
                let pulled_pair: (LazyCell<i32>, i32) =
                    SpreadLayout::pull_spread(&mut KeyPtr::from(root_key));
                let mut pulled_pair = core::mem::ManuallyDrop::new(pulled_pair);
                assert_eq!(pulled_pair.1, 2i32);
                pulled_pair.1 = 3i32;
                SpreadLayout::push_spread(&*pulled_pair, &mut KeyPtr::from(root_key));
            }
            {
                // Step 3: Pull the pair again from the storage.
                //
                // If the bug with `Lazy` that has been fixed in PR #528 has been fixed we should be
                // able to inspect the correct values for both pair entries which is: `(Some(1), 3)`
                let pulled_pair: (LazyCell<i32>, i32) =
                    SpreadLayout::pull_spread(&mut KeyPtr::from(root_key));
                assert_eq!(pulled_pair.0.get(), Some(&1i32));
                assert_eq!(pulled_pair.1, 3i32);
            }
            Ok(())
        })
    }

    #[test]
    fn regression_test_for_issue_570() -> ink_env::Result<()> {
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
            let root_key = Key::from([0x00; 32]);
            {
                // Step 1: Push two valid values one after the other to contract storage.
                // The first value needs to be an `Option::None` value, since the bug was
                // then messing up following pointers.
                let v1: Option<u32> = None;
                let v2: u32 = 13;
                let mut ptr = KeyPtr::from(root_key);

                SpreadLayout::push_spread(&v1, &mut ptr);
                SpreadLayout::push_spread(&v2, &mut ptr);
            }
            {
                // Step 2: Pull the values from the step before.
                //
                // 1. Change the first values `None` to `Some(...)`.
                // 2. Push the first value again to contract storage.
                //
                // We prevent the intermediate instance from clearing the storage preemptively
                // by wrapping it inside `ManuallyDrop`. The third step will clean up the same
                // storage region afterwards.
                let mut ptr = KeyPtr::from(root_key);
                let pulled_v1: Option<u32> = SpreadLayout::pull_spread(&mut ptr);
                let mut pulled_v1 = core::mem::ManuallyDrop::new(pulled_v1);

                let pulled_v2: u32 = SpreadLayout::pull_spread(&mut ptr);
                let pulled_v2 = core::mem::ManuallyDrop::new(pulled_v2);

                assert_eq!(*pulled_v1, None);
                assert_eq!(*pulled_v2, 13);

                *pulled_v1 = Some(99u32);
                SpreadLayout::push_spread(&*pulled_v1, &mut KeyPtr::from(root_key));
            }
            {
                // Step 3: Pull the values again from the storage.
                //
                // If the bug with `Option` has been fixed in PR #520 we must be able to inspect
                // the correct values for both entries.
                let mut ptr = KeyPtr::from(root_key);
                let pulled_v1: Option<u32> = SpreadLayout::pull_spread(&mut ptr);
                let pulled_v2: u32 = SpreadLayout::pull_spread(&mut ptr);

                assert_eq!(pulled_v1, Some(99));
                assert_eq!(pulled_v2, 13);
            }
            Ok(())
        })
    }

    #[test]
    fn second_regression_test_for_issue_570() -> ink_env::Result<()> {
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
            // given
            let root_key = Key::from([0x00; 32]);
            let none: Option<u32> = None;
            let some: Option<u32> = Some(13);

            // when
            let mut ptr_push_none = KeyPtr::from(root_key);
            SpreadLayout::push_spread(&none, &mut ptr_push_none);
            let mut ptr_pull_none = KeyPtr::from(root_key);
            let v1: Option<u32> = SpreadLayout::pull_spread(&mut ptr_pull_none);
            assert!(v1.is_none());
            let mut ptr_clear_none = KeyPtr::from(root_key);
            SpreadLayout::clear_spread(&none, &mut ptr_clear_none);

            let mut ptr_push_some = KeyPtr::from(root_key);
            SpreadLayout::push_spread(&some, &mut ptr_push_some);
            let mut ptr_pull_some = KeyPtr::from(root_key);
            let v2: Option<u32> = SpreadLayout::pull_spread(&mut ptr_pull_some);
            assert!(v2.is_some());
            let mut ptr_clear_some = KeyPtr::from(root_key);
            SpreadLayout::clear_spread(&some, &mut ptr_clear_some);

            // then
            // the bug which we observed was that the pointer after push/pull/clear
            // was set so a different value if the `Option` was `None` vs. if it was
            // `Some`.
            //
            // if the bug has been fixed the pointer must be the same for `None`
            // and `Some` after push/pull/clear. otherwise subsequent operations using
            // the pointer will break as soon as the `Option` is changed to it's
            // opposite (`None` -> `Some`, `Some` -> `None`).
            let mut expected_post_op_ptr = KeyPtr::from(root_key);
            // advance one time after the cell containing `self.is_some() as u8` has been read
            expected_post_op_ptr.advance_by(1);
            // advance another time after the cell containing the inner `Option` value
            // has either been skipped (in case of the previous cell being `None`) or
            // read (in case of `Some`).
            expected_post_op_ptr.advance_by(1);

            assert_eq!(expected_post_op_ptr, ptr_push_none);
            assert_eq!(ptr_push_none, ptr_push_some);

            assert_eq!(expected_post_op_ptr, ptr_pull_none);
            assert_eq!(ptr_pull_none, ptr_pull_some);

            assert_eq!(expected_post_op_ptr, ptr_clear_none);
            assert_eq!(ptr_clear_none, ptr_clear_some);

            Ok(())
        })
    }

    #[test]
    #[should_panic(expected = "encountered empty storage cell")]
    #[cfg(not(feature = "ink-experimental-engine"))]
    fn nested_lazies_are_cleared_completely_after_pull() {
        ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
            // given
            let root_key = Key::from([0x42; 32]);
            let nested_lazy: Lazy<Lazy<u32>> = Lazy::new(Lazy::new(13u32));
            SpreadLayout::push_spread(&nested_lazy, &mut KeyPtr::from(root_key));
            let pulled_lazy = <Lazy<Lazy<u32>> as SpreadLayout>::pull_spread(
                &mut KeyPtr::from(root_key),
            );

            // when
            SpreadLayout::clear_spread(&pulled_lazy, &mut KeyPtr::from(root_key));

            // then
            let contract_id = ink_env::test::get_current_contract_account_id::<
                ink_env::DefaultEnvironment,
            >()
            .expect("Cannot get contract id");
            let used_cells = ink_env::test::count_used_storage_cells::<
                ink_env::DefaultEnvironment,
            >(&contract_id)
            .expect("used cells must be returned");
            assert_eq!(used_cells, 0);
            let _ = *<Lazy<Lazy<u32>> as SpreadLayout>::pull_spread(&mut KeyPtr::from(
                root_key,
            ));
            Ok(())
        })
        .unwrap()
    }

    #[test]
    #[should_panic(expected = "encountered empty storage cell")]
    #[cfg(not(feature = "ink-experimental-engine"))]
    fn lazy_drop_works() {
        ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
            // given
            let root_key = Key::from([0x42; 32]);

            // when
            let setup_result = std::panic::catch_unwind(|| {
                let lazy: Lazy<u32> = Lazy::new(13u32);
                SpreadLayout::push_spread(&lazy, &mut KeyPtr::from(root_key));
                let _pulled_lazy =
                    <Lazy<u32> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
                // lazy is dropped which should clear the cells
            });
            assert!(setup_result.is_ok(), "setup should not panic");

            // then
            let contract_id = ink_env::test::get_current_contract_account_id::<
                ink_env::DefaultEnvironment,
            >()
            .expect("Cannot get contract id");
            let used_cells = ink_env::test::count_used_storage_cells::<
                ink_env::DefaultEnvironment,
            >(&contract_id)
            .expect("used cells must be returned");
            assert_eq!(used_cells, 0);
            let _ =
                *<Lazy<u32> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
            Ok(())
        })
        .unwrap()
    }

    #[test]
    #[should_panic(expected = "encountered empty storage cell")]
    #[cfg(not(feature = "ink-experimental-engine"))]
    fn lazy_drop_works_with_greater_footprint() {
        ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
            // given
            let root_key = Key::from([0x42; 32]);

            // when
            let setup_result = std::panic::catch_unwind(|| {
                let lazy: Lazy<[u32; 5]> = Lazy::new([13, 14, 15, 16, 17]);
                SpreadLayout::push_spread(&lazy, &mut KeyPtr::from(root_key));
                let _pulled_lazy = <Lazy<[u32; 5]> as SpreadLayout>::pull_spread(
                    &mut KeyPtr::from(root_key),
                );
                // lazy is dropped which should clear the cells
            });
            assert!(setup_result.is_ok(), "setup should not panic");

            // then
            let contract_id = ink_env::test::get_current_contract_account_id::<
                ink_env::DefaultEnvironment,
            >()
            .expect("Cannot get contract id");
            let used_cells = ink_env::test::count_used_storage_cells::<
                ink_env::DefaultEnvironment,
            >(&contract_id)
            .expect("used cells must be returned");
            assert_eq!(used_cells, 0);
            let _ =
                *<Lazy<u32> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
            Ok(())
        })
        .unwrap()
    }

    #[test]
    #[should_panic(expected = "encountered empty storage cell")]
    #[cfg(feature = "ink-experimental-engine")]
    fn nested_lazies_are_cleared_completely_after_pull() {
        ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
            // given
            let root_key = Key::from([0x42; 32]);
            let nested_lazy: Lazy<Lazy<u32>> = Lazy::new(Lazy::new(13u32));
            SpreadLayout::push_spread(&nested_lazy, &mut KeyPtr::from(root_key));
            let pulled_lazy = <Lazy<Lazy<u32>> as SpreadLayout>::pull_spread(
                &mut KeyPtr::from(root_key),
            );

            // when
            SpreadLayout::clear_spread(&pulled_lazy, &mut KeyPtr::from(root_key));

            // then
            let contract_id = ink_env::test::callee::<ink_env::DefaultEnvironment>();
            let used_cells = ink_env::test::count_used_storage_cells::<
                ink_env::DefaultEnvironment,
            >(&contract_id)
            .expect("used cells must be returned");
            assert_eq!(used_cells, 0);
            let _ = *<Lazy<Lazy<u32>> as SpreadLayout>::pull_spread(&mut KeyPtr::from(
                root_key,
            ));
            Ok(())
        })
        .unwrap()
    }

    #[test]
    #[should_panic(expected = "encountered empty storage cell")]
    #[cfg(feature = "ink-experimental-engine")]
    fn lazy_drop_works() {
        ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
            // given
            let root_key = Key::from([0x42; 32]);

            // when
            let setup_result = std::panic::catch_unwind(|| {
                let lazy: Lazy<u32> = Lazy::new(13u32);
                SpreadLayout::push_spread(&lazy, &mut KeyPtr::from(root_key));
                let _pulled_lazy =
                    <Lazy<u32> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
                // lazy is dropped which should clear the cells
            });
            assert!(setup_result.is_ok(), "setup should not panic");

            // then
            let contract_id = ink_env::test::callee::<ink_env::DefaultEnvironment>();
            let used_cells = ink_env::test::count_used_storage_cells::<
                ink_env::DefaultEnvironment,
            >(&contract_id)
            .expect("used cells must be returned");
            assert_eq!(used_cells, 0);
            let _ =
                *<Lazy<u32> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
            Ok(())
        })
        .unwrap()
    }

    #[test]
    #[should_panic(expected = "encountered empty storage cell")]
    #[cfg(feature = "ink-experimental-engine")]
    fn lazy_drop_works_with_greater_footprint() {
        ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
            // given
            let root_key = Key::from([0x42; 32]);

            // when
            let setup_result = std::panic::catch_unwind(|| {
                let lazy: Lazy<[u32; 5]> = Lazy::new([13, 14, 15, 16, 17]);
                SpreadLayout::push_spread(&lazy, &mut KeyPtr::from(root_key));
                let _pulled_lazy = <Lazy<[u32; 5]> as SpreadLayout>::pull_spread(
                    &mut KeyPtr::from(root_key),
                );
                // lazy is dropped which should clear the cells
            });
            assert!(setup_result.is_ok(), "setup should not panic");

            // then
            let contract_id = ink_env::test::callee::<ink_env::DefaultEnvironment>();
            let used_cells = ink_env::test::count_used_storage_cells::<
                ink_env::DefaultEnvironment,
            >(&contract_id)
            .expect("used cells must be returned");
            assert_eq!(used_cells, 0);
            let _ =
                *<Lazy<u32> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key));
            Ok(())
        })
        .unwrap()
    }
}