lazy_cell.rs 33.1 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_spread_root_opt,
    pull_spread_root_opt,
23
    ExtKeyPtr,
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    KeyPtr,
    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
///
40
/// Use this if the storage field does not need to be loaded in some or most cases.
41
42
43
44
45
46
47
48
49
50
51
52
53
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.
    ///
54
    /// # Developer Note: Safety
55
56
57
58
59
60
61
62
63
    ///
    /// 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.
64
    cache: CacheCell<Option<StorageEntry<T>>>,
65
66
67
68
69
70
71
72
73
}

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)
74
            .field("cache", self.cache.as_inner())
75
76
77
78
79
            .finish()
    }
}

#[test]
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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 { \
96
97
98
99
100
101
            key: Some(Key(0x_\
                0000000000000000_\
                0000000000000000_\
                0000000000000000_\
                0000000000000000)\
            ), \
102
103
            cache: None \
        }",
104
105
106
        );
        Ok(())
    })
107
108
109
110
111
112
113
}

impl<T> Drop for LazyCell<T>
where
    T: SpreadLayout,
{
    fn drop(&mut self) {
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
        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));
                        }
                    }
                }
140
141
142
143
144
            }
        }
    }
}

145
146
#[cfg(feature = "std")]
const _: () = {
147
    use crate::traits::StorageLayout;
148
    use ink_metadata::layout::Layout;
149
150
151
152
153
154
155
156
157
158
159

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

160
161
162
163
164
165
166
impl<T> SpreadLayout for LazyCell<T>
where
    T: SpreadLayout,
{
    const FOOTPRINT: u64 = <T as SpreadLayout>::FOOTPRINT;

    fn pull_spread(ptr: &mut KeyPtr) -> Self {
Hero Bird's avatar
Hero Bird committed
167
168
        let root_key = ExtKeyPtr::next_for::<Self>(ptr);
        Self::lazy(*root_key)
169
170
171
    }

    fn push_spread(&self, ptr: &mut KeyPtr) {
Hero Bird's avatar
Hero Bird committed
172
        let root_key = ExtKeyPtr::next_for::<Self>(ptr);
173
        if let Some(entry) = self.entry() {
Hero Bird's avatar
Hero Bird committed
174
            entry.push_spread_root(root_key)
175
176
177
178
        }
    }

    fn clear_spread(&self, ptr: &mut KeyPtr) {
Hero Bird's avatar
Hero Bird committed
179
        let root_key = ExtKeyPtr::next_for::<Self>(ptr);
180
181
182
183
184
185
186
187
188
189
        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;
190
                assert_footprint_threshold(footprint);
191
192
193
194
195
                let mut key_ptr = KeyPtr::from(*root_key);
                for _ in 0..footprint {
                    ink_env::clear_contract_storage(key_ptr.advance_by(1));
                }
            }
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
        }
    }
}

// # 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,
240
            cache: CacheCell::new(Some(StorageEntry::new(value, EntryState::Mutated))),
241
242
243
244
245
246
247
248
249
250
251
252
253
        }
    }

    /// 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),
254
            cache: CacheCell::new(None),
255
256
257
258
259
260
261
262
263
264
265
266
267
268
        }
    }

    /// 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.
269
    fn entry(&self) -> Option<&StorageEntry<T>> {
270
        self.cache.as_inner().as_ref()
271
272
273
274
275
276
277
278
279
280
281
    }
}

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.
282
    unsafe fn load_through_cache(&self) -> NonNull<StorageEntry<T>> {
283
284
285
286
        // 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.
287
        let cache = &mut *self.cache.get_ptr().as_ptr();
288
289
290
291
292
293
        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);
294
            *cache = Some(StorageEntry::new(value, EntryState::Preserved));
295
296
297
298
299
300
        }
        debug_assert!(cache.is_some());
        NonNull::from(cache.as_mut().expect("unpopulated cache entry"))
    }

    /// Returns a shared reference to the entry.
301
    fn load_entry(&self) -> &StorageEntry<T> {
302
303
304
305
306
307
308
309
310
311
312
        // 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.
313
    fn load_entry_mut(&mut self) -> &mut StorageEntry<T> {
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
        // 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()
    }
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

    /// 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.
378
            *cache = Some(StorageEntry::new(Some(new_value), EntryState::Mutated));
379
380
381
        }
        debug_assert!(cache.is_some());
    }
382
383
}

384
385
386
387
388
389
390
391
392
393
394
/// 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,
    );
}

395
396
397
398
399
#[cfg(test)]
mod tests {
    use super::{
        EntryState,
        LazyCell,
400
        StorageEntry,
401
402
    };
    use crate::{
403
404
405
        traits::{
            KeyPtr,
            SpreadLayout,
406
        },
407
        Lazy,
408
    };
409
    use ink_env::test::run_test;
410
411
412
413
414
415
416
417
418
    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(),
419
            Some(&StorageEntry::new(Some(b'A'), EntryState::Mutated))
420
421
422
423
424
425
        );
        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);
426
427
428
429
        assert_eq!(
            b.entry(),
            Some(&StorageEntry::new(None, EntryState::Mutated))
        );
430
431
432
433
434
435
436
437
438
439
440
441
        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]
442
443
444
445
446
447
448
    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(())
        })
449
450
451
    }

    #[test]
452
    fn lazy_get_works() -> ink_env::Result<()> {
453
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
454
            let cell = <LazyCell<u8>>::lazy(Key::from([0x42; 32]));
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
            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]
472
    fn spread_layout_works() -> ink_env::Result<()> {
473
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
474
475
476
477
478
            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`.
479
            let root_key = Key::from([0x42; 32]);
480
481
482
483
484
485
486
            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(),
487
                Some(&StorageEntry::new(Some(b'A'), EntryState::Preserved))
488
489
490
491
492
493
494
            );
            // 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(),
495
                Some(&StorageEntry::new(Some(b'A'), EntryState::Preserved))
496
497
498
499
500
501
502
            );
            // 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(),
503
                Some(&StorageEntry::new(None, EntryState::Preserved))
504
505
506
507
            );
            Ok(())
        })
    }
508
509
510
511
512
513
514
515
516

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

    #[test]
517
    fn lazy_set_works() -> ink_env::Result<()> {
518
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
519
520
521
522
523
524
525
526
527
528
529
            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]
530
    fn lazy_set_works_with_spread_layout_push_pull() -> ink_env::Result<()> {
531
        run_test::<ink_env::DefaultEnvironment, _>(|_| {
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
            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(())
        })
    }
Hero Bird's avatar
Hero Bird committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

    #[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.
                //
580
                // We explicitly do not touch or assert the value of `pulled_pair.0` in order to trigger
Hero Bird's avatar
Hero Bird committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
                // 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(())
        })
    }
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

    #[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(())
        })
    }
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

    #[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(())
        })
    }
710
711
712

    #[test]
    #[should_panic(expected = "encountered empty storage cell")]
713
    #[cfg(not(feature = "ink-experimental-engine"))]
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
    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,
            >()
731
            .expect("Cannot get contract id");
732
733
734
735
736
737
738
739
740
741
742
743
            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()
    }
744
745
746

    #[test]
    #[should_panic(expected = "encountered empty storage cell")]
747
    #[cfg(not(feature = "ink-experimental-engine"))]
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
    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")]
782
    #[cfg(not(feature = "ink-experimental-engine"))]
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
    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()
    }
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

    #[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()
    }
911
}