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
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
// 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.

//! A lazy storage mapping that stores entries under their SCALE encoded key hashes.

use super::{
    CacheCell,
    EntryState,
    StorageEntry,
};
use crate::traits::{
    clear_packed_root,
    pull_packed_root_opt,
    ExtKeyPtr,
    KeyPtr,
    PackedLayout,
    SpreadAllocate,
    SpreadLayout,
};
use core::{
    borrow::Borrow,
    cmp::{
        Eq,
        Ord,
    },
    fmt,
    fmt::Debug,
    iter::FromIterator,
    marker::PhantomData,
    ptr::NonNull,
};
use ink_env::hash::{
    CryptoHash,
    HashOutput,
};
use ink_prelude::{
    borrow::ToOwned,
    boxed::Box,
    collections::btree_map::{
        BTreeMap,
        Entry as BTreeMapEntry,
        OccupiedEntry as BTreeMapOccupiedEntry,
    },
};
use ink_primitives::Key;

/// 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
/// [`LazyHashMap::get`].
pub type EntryMap<K, V> = BTreeMap<K, Box<StorageEntry<V>>>;

/// A lazy storage mapping that stores entries under their SCALE encoded key hashes.
///
/// # 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
/// mapping of storage cells.
///
/// This storage data structure might store its entries anywhere in the contract
/// storage. It is the users responsibility to keep track of the entries if it
/// is necessary to do so.
pub struct LazyHashMap<K, V, H> {
    /// The offset key for the storage mapping.
    ///
    /// This offsets the mapping for the entries stored in the contract storage
    /// so that all lazy hash map instances store equal entries at different
    /// locations of the contract storage and avoid collisions.
    key: Option<Key>,
    /// The currently cached entries of the lazy storage mapping.
    ///
    /// This normally only represents a subset of the total set of elements.
    /// An entry is cached as soon as it is loaded or written.
    cached_entries: CacheCell<EntryMap<K, V>>,
    /// The used hash builder.
    hash_builder: PhantomData<H>,
}

/// When querying `entry()` there is a case which needs special treatment:
/// In `entry()` we first do a look-up in the cache. If the requested key is
/// in the cache we return the found object.
/// If it is not in the cache we query the storage. If we find the element
/// in storage we insert it into the cache.
///
/// The problem now is that in this case we only have the `Vacant` object
/// which we got from searching in the cache, but we need to return
/// `Occupied` here, since the object is now in the cache. We could do this
/// by querying the cache another time -- but this would be an additional
/// search. So what we do instead is to save a reference to the inserted
/// cache value in the `Occupied`. As a consequence all Entry API operations
/// (`get`, `remove`, …) need to distinguish both cases.
enum EntryOrMutableValue<E, V> {
    /// An occupied `EntryMap` entry that holds a value.
    /// This represents the case where the key was in the cache.
    EntryElementWasInCache(E),
    /// A reference to the mutable value behind a cache entry.
    /// This represents the case where the key was not in the cache, but in storage.
    MutableValueElementWasNotInCache(V),
}

/// An occupied `EntryMap` entry that holds a value.
type OccupiedCache<'a, K, V> = BTreeMapOccupiedEntry<'a, K, Box<StorageEntry<V>>>;

/// An occupied entry that holds the value.
pub struct OccupiedEntry<'a, K, V>
where
    K: Clone,
{
    /// The key stored in this entry.
    key: K,
    /// Either the occupied `EntryMap` entry that holds the value or a mutable reference
    /// to the value behind a cache entry.
    entry: EntryOrMutableValue<OccupiedCache<'a, K, V>, &'a mut Box<StorageEntry<V>>>,
}

/// A vacant entry with previous and next vacant indices.
pub struct VacantEntry<'a, K, V>
where
    K: Ord + Clone + PackedLayout,
    V: PackedLayout,
{
    /// The key stored in this entry.
    key: K,
    /// The entry within the `LazyHashMap`. This entry can be either occupied or vacant.
    /// In an `BTreeMapEntry::Occupied` state the entry has been marked to
    /// be removed (with `None`), but we still want to expose the `VacantEntry` API
    /// to the use.
    /// In an `BTreeMapEntry::Vacant` state the entry is vacant, and we want to expose
    /// the `VacantEntry` API.
    entry: BTreeMapEntry<'a, K, Box<StorageEntry<V>>>,
}

/// An entry within the `LazyHashMap`.
pub enum Entry<'a, K: 'a, V: 'a>
where
    K: Ord + Clone + PackedLayout,
    V: PackedLayout,
{
    /// A vacant entry that holds the index to the next and previous vacant entry.
    Vacant(VacantEntry<'a, K, V>),
    /// An occupied entry that holds the value.
    Occupied(OccupiedEntry<'a, K, V>),
}

struct DebugEntryMap<'a, K, V>(&'a CacheCell<EntryMap<K, V>>);

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

impl<K, V, H> Debug for LazyHashMap<K, V, H>
where
    K: Debug,
    V: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // The `hash_builder` field is not really required or needed for debugging purposes.
        f.debug_struct("LazyHashMap")
            .field("key", &self.key)
            .field("cached_entries", &DebugEntryMap(&self.cached_entries))
            .finish()
    }
}

#[test]
fn debug_impl_works() {
    use ink_env::hash::Blake2x256;
    let mut hmap = <LazyHashMap<char, i32, Blake2x256>>::new();
    // Empty hmap.
    assert_eq!(
        format!("{:?}", &hmap),
        "LazyHashMap { key: None, cached_entries: {} }",
    );
    // Filled hmap.
    hmap.put('A', Some(1));
    hmap.put('B', Some(2));
    hmap.put('C', None);
    assert_eq!(
        format!("{:?}", &hmap),
        "LazyHashMap { \
            key: None, \
            cached_entries: {\
                'A': Entry { \
                    value: Some(1), \
                    state: Mutated \
                }, \
                'B': Entry { \
                    value: Some(2), \
                    state: Mutated \
                }, \
                'C': Entry { \
                    value: None, \
                    state: Mutated \
                }\
            } \
        }",
    );
}

#[cfg(feature = "std")]
const _: () = {
    use crate::traits::{
        LayoutCryptoHasher,
        StorageLayout,
    };
    use ink_metadata::layout::{
        CellLayout,
        HashLayout,
        HashingStrategy,
        Layout,
        LayoutKey,
    };
    use scale_info::TypeInfo;

    impl<K, V, H> StorageLayout for LazyHashMap<K, V, H>
    where
        K: Ord + scale::Encode,
        V: TypeInfo + 'static,
        H: CryptoHash + LayoutCryptoHasher,
        Key: From<<H as HashOutput>::Type>,
    {
        fn layout(key_ptr: &mut KeyPtr) -> Layout {
            Layout::Hash(HashLayout::new(
                LayoutKey::from(key_ptr.advance_by(1)),
                HashingStrategy::new(
                    <H as LayoutCryptoHasher>::crypto_hasher(),
                    b"ink hashmap".to_vec(),
                    Vec::new(),
                ),
                Layout::Cell(CellLayout::new::<V>(LayoutKey::from(
                    key_ptr.advance_by(0),
                ))),
            ))
        }
    }
};

impl<K, V, H> SpreadLayout for LazyHashMap<K, V, H>
where
    K: Ord + scale::Encode,
    V: PackedLayout,
    H: CryptoHash,
    Key: From<<H as HashOutput>::Type>,
{
    const FOOTPRINT: u64 = 1;

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

    fn push_spread(&self, ptr: &mut KeyPtr) {
        let offset_key = ExtKeyPtr::next_for::<Self>(ptr);
        for (index, entry) in self.entries().iter() {
            let root_key = self.to_offset_key(offset_key, index);
            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<K, V, H> SpreadAllocate for LazyHashMap<K, V, H>
where
    K: Ord + scale::Encode,
    V: PackedLayout,
    H: CryptoHash,
    Key: From<<H as HashOutput>::Type>,
{
    #[inline]
    fn allocate_spread(ptr: &mut KeyPtr) -> Self {
        Self::lazy(*ExtKeyPtr::next_for::<Self>(ptr))
    }
}

// # Developer Note
//
// Even thought `LazyHashMap` would require storing just a single key a thus
// be a packable storage entity we cannot really make it one since this could
// allow for overlapping lazy hash map instances.
// An example for this would be a `Pack<(LazyHashMap, LazyHashMap)>` where
// both lazy hash maps would use the same underlying key and thus would apply
// the same underlying key mapping.

impl<K, V, H> Default for LazyHashMap<K, V, H>
where
    K: Ord,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<K, V, H> FromIterator<(K, V)> for LazyHashMap<K, V, H>
where
    K: Ord + Clone + PackedLayout,
    V: PackedLayout,
    H: CryptoHash,
    Key: From<<H as HashOutput>::Type>,
{
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
    {
        let mut hmap = LazyHashMap::new();
        hmap.extend(iter);
        hmap
    }
}

impl<K, V, H> Extend<(K, V)> for LazyHashMap<K, V, H>
where
    K: Ord + Clone + PackedLayout,
    V: PackedLayout,
    H: CryptoHash,
    Key: From<<H as HashOutput>::Type>,
{
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = (K, V)>,
    {
        for (key, value) in iter {
            self.put(key, Some(value));
        }
    }
}

impl<K, V, H> LazyHashMap<K, V, H>
where
    K: Ord,
{
    /// Creates a new empty lazy hash 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,
            cached_entries: CacheCell::new(EntryMap::new()),
            hash_builder: Default::default(),
        }
    }

    /// Creates a new empty lazy hash 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`][`crate::lazy::LazyIndexMap::new`]
    /// for construction during contract initialization.
    fn lazy(key: Key) -> Self {
        Self {
            key: Some(key),
            cached_entries: CacheCell::new(EntryMap::new()),
            hash_builder: Default::default(),
        }
    }

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

    /// Returns the length of the cached entries.
    #[cfg(test)]
    pub(crate) fn len_cached_entries(&self) -> usize {
        self.entries().len()
    }

    /// Returns a shared reference to the underlying entries.
    fn entries(&self) -> &EntryMap<K, V> {
        self.cached_entries.as_inner()
    }

    /// Returns an exclusive reference to the underlying entries.
    fn entries_mut(&mut self) -> &mut EntryMap<K, V> {
        self.cached_entries.as_inner_mut()
    }

    /// Puts the new value under the given key.
    ///
    /// # Note
    ///
    /// - Use [`LazyHashMap::put`]`(None)` in order to remove an element.
    /// - Prefer this method over [`LazyHashMap::put_get`] if you are not interested
    ///   in the old value of the same cell index.
    ///
    /// # Panics
    ///
    /// - If the lazy hash map is in an invalid state that forbids interaction
    ///   with the underlying contract storage.
    /// - If the decoding of the old element at the given index failed.
    pub fn put(&mut self, key: K, new_value: Option<V>) {
        self.entries_mut().insert(
            key,
            Box::new(StorageEntry::new(new_value, EntryState::Mutated)),
        );
    }
}

impl<K, V, H> LazyHashMap<K, V, H>
where
    K: Clone + Ord + PackedLayout,
    V: PackedLayout,
    H: CryptoHash,
    Key: From<<H as HashOutput>::Type>,
{
    /// Gets the given key's corresponding entry in the map for in-place manipulation.
    pub fn entry(&mut self, key: K) -> Entry<K, V> {
        // 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.
        let cached_entries = unsafe { &mut *self.cached_entries.get_ptr().as_ptr() };
        // We have to clone the key here because we do not have access to the unsafe
        // raw entry API for Rust hash maps, yet since it is unstable. We can remove
        // the constraints on `K: Clone` once we have access to this API.
        // Read more about the issue here: https://github.com/rust-lang/rust/issues/56167
        match cached_entries.entry(key.to_owned()) {
            BTreeMapEntry::Occupied(entry) => {
                match entry.get().value() {
                    Some(_) => {
                        Entry::Occupied(OccupiedEntry {
                            key,
                            entry: EntryOrMutableValue::EntryElementWasInCache(entry),
                        })
                    }
                    None => {
                        // value is already marked as to be removed
                        Entry::Vacant(VacantEntry {
                            key,
                            entry: BTreeMapEntry::Occupied(entry),
                        })
                    }
                }
            }
            BTreeMapEntry::Vacant(entry) => {
                let value = self
                    .key_at(&key)
                    .map(|key| pull_packed_root_opt::<V>(&key))
                    .unwrap_or(None);
                match value.is_some() {
                    true => {
                        // The entry was not in the cache, but in the storage. This results in
                        // a problem: We only have `Vacant` here, but need to return `Occupied`,
                        // to reflect this.
                        let v_mut = entry.insert(Box::new(StorageEntry::new(
                            value,
                            EntryState::Preserved,
                        )));
                        Entry::Occupied(OccupiedEntry {
                            key,
                            entry: EntryOrMutableValue::MutableValueElementWasNotInCache(
                                v_mut,
                            ),
                        })
                    }
                    false => {
                        Entry::Vacant(VacantEntry {
                            key,
                            entry: BTreeMapEntry::Vacant(entry),
                        })
                    }
                }
            }
        }
    }
}

impl<K, V, H> LazyHashMap<K, V, H>
where
    K: Ord + scale::Encode,
    H: CryptoHash,
    Key: From<<H as HashOutput>::Type>,
{
    /// Returns an offset key for the given key pair.
    fn to_offset_key<Q>(&self, storage_key: &Key, key: &Q) -> Key
    where
        K: Borrow<Q>,
        Q: scale::Encode,
    {
        #[derive(scale::Encode)]
        struct KeyPair<'a, Q> {
            prefix: [u8; 11],
            storage_key: &'a Key,
            value_key: &'a Q,
        }
        let key_pair = KeyPair {
            prefix: [
                b'i', b'n', b'k', b' ', b'h', b'a', b's', b'h', b'm', b'a', b'p',
            ],
            storage_key,
            value_key: key,
        };
        let mut output = <H as HashOutput>::Type::default();
        ink_env::hash_encoded::<H, KeyPair<Q>>(&key_pair, &mut output);
        output.into()
    }

    /// Returns an offset key for the given key.
    fn key_at<Q>(&self, key: &Q) -> Option<Key>
    where
        K: Borrow<Q>,
        Q: scale::Encode,
    {
        self.key
            .map(|storage_key| self.to_offset_key(&storage_key, key))
    }
}

impl<K, V, H> LazyHashMap<K, V, H>
where
    K: Ord + Eq + scale::Encode,
    V: PackedLayout,
    H: CryptoHash,
    Key: From<<H as HashOutput>::Type>,
{
    /// 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
    /// within internal use only and should never be given outside the lazy entity
    /// for public `&self` methods.
    unsafe fn lazily_load<Q>(&self, key: &Q) -> NonNull<StorageEntry<V>>
    where
        K: Borrow<Q>,
        Q: Ord + scale::Encode + ToOwned<Owned = K>,
    {
        // 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.
        let cached_entries = &mut *self.cached_entries.get_ptr().as_ptr();
        // We have to clone the key here because we do not have access to the unsafe
        // raw entry API for Rust hash maps, yet since it is unstable. We can remove
        // the contraints on `K: Clone` once we have access to this API.
        // Read more about the issue here: https://github.com/rust-lang/rust/issues/56167
        match cached_entries.entry(key.to_owned()) {
            BTreeMapEntry::Occupied(occupied) => {
                NonNull::from(&mut **occupied.into_mut())
            }
            BTreeMapEntry::Vacant(vacant) => {
                let value = self
                    .key_at(key)
                    .map(|key| pull_packed_root_opt::<V>(&key))
                    .unwrap_or(None);
                NonNull::from(
                    &mut **vacant.insert(Box::new(StorageEntry::new(
                        value,
                        EntryState::Preserved,
                    ))),
                )
            }
        }
    }

    /// Lazily loads the value associated with the given key.
    ///
    /// # Note
    ///
    /// Only loads a value if `key` is set and if the value has not been loaded yet.
    /// Returns a pointer to 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.
    fn lazily_load_mut<Q>(&mut self, index: &Q) -> &mut StorageEntry<V>
    where
        K: Borrow<Q>,
        Q: Ord + scale::Encode + ToOwned<Owned = K>,
    {
        // 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() }
    }

    /// 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
    /// high-level abstractions that build upon this low-level data structure.
    pub fn clear_packed_at<Q>(&self, index: &Q)
    where
        K: Borrow<Q>,
        V: PackedLayout,
        Q: Ord + scale::Encode + ToOwned<Owned = K>,
    {
        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");
            clear_packed_root::<V>(entity, &root_key);
        } 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.
            ink_env::clear_contract_storage(&root_key);
        }
    }

    /// Returns a shared reference to the value associated with the given key 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<Q>(&self, index: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Ord + scale::Encode + ToOwned<Owned = K>,
    {
        // 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 value associated with the given key 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<Q>(&mut self, index: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Ord + scale::Encode + ToOwned<Owned = K>,
    {
        self.lazily_load_mut(index).value_mut().into()
    }

    /// Puts the new value under the given key and returns the old value if any.
    ///
    /// # Note
    ///
    /// - Use [`LazyHashMap::put_get`]`(None)` in order to remove an element
    ///   and retrieve the old element back.
    ///
    /// # Panics
    ///
    /// - If the lazy hashmap is in an invalid state that forbids interaction.
    /// - If the decoding of the old element at the given index failed.
    pub fn put_get<Q>(&mut self, key: &Q, new_value: Option<V>) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Ord + scale::Encode + ToOwned<Owned = K>,
    {
        self.lazily_load_mut(key).put(new_value)
    }

    /// Swaps the values at entries with associated keys `x` and `y`.
    ///
    /// This operation tries to be as efficient as possible and reuse allocations.
    ///
    /// # Panics
    ///
    /// - If the lazy hashmap is in an invalid state that forbids interaction.
    /// - If the decoding of one of the elements failed.
    pub fn swap<Q1, Q2>(&mut self, x: &Q1, y: &Q2)
    where
        K: Borrow<Q1> + Borrow<Q2>,
        Q1: Ord + PartialEq<Q2> + scale::Encode + ToOwned<Owned = K>,
        Q2: Ord + PartialEq<Q1> + scale::Encode + ToOwned<Owned = K>,
    {
        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());
    }
}

impl<'a, K, V> Entry<'a, K, V>
where
    K: Ord + Clone + PackedLayout,
    V: PackedLayout + core::fmt::Debug + core::cmp::Eq + Default,
{
    /// Returns a reference to this entry's key.
    pub fn key(&self) -> &K {
        match self {
            Entry::Occupied(entry) => &entry.key,
            Entry::Vacant(entry) => &entry.key,
        }
    }

    /// Ensures a value is in the entry by inserting the default value if empty, and returns
    /// a reference to the value in the entry.
    pub fn or_default(self) -> &'a V {
        match self {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => entry.insert(V::default()),
        }
    }

    /// Ensures a value is in the entry by inserting the default if empty, and returns
    /// a mutable reference to the value in the entry.
    pub fn or_insert(self, default: V) -> &'a mut V {
        match self {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => entry.insert(default),
        }
    }

    /// Ensures a value is in the entry by inserting the result of the default function if empty,
    /// and returns mutable references to the key and value in the entry.
    pub fn or_insert_with<F>(self, default: F) -> &'a mut V
    where
        F: FnOnce() -> V,
    {
        match self {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => entry.insert(default()),
        }
    }

    /// Ensures a value is in the entry by inserting, if empty, the result of the default
    /// function, which takes the key as its argument, and returns a mutable reference to
    /// the value in the entry.
    pub fn or_insert_with_key<F>(self, default: F) -> &'a mut V
    where
        F: FnOnce(&K) -> V,
    {
        match self {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => {
                let value = default(&entry.key);
                entry.insert(value)
            }
        }
    }

    /// Provides in-place mutable access to an occupied entry before any
    /// potential inserts into the map.
    #[must_use]
    pub fn and_modify<F>(self, f: F) -> Self
    where
        F: FnOnce(&mut V),
    {
        match self {
            Entry::Occupied(mut entry) => {
                {
                    let v = entry.get_mut();
                    f(v);
                }
                Entry::Occupied(entry)
            }
            Entry::Vacant(entry) => Entry::Vacant(entry),
        }
    }
}

impl<'a, K, V> VacantEntry<'a, K, V>
where
    K: Ord + Clone + PackedLayout,
    V: PackedLayout,
{
    /// Gets a reference to the key that would be used when inserting a value through the `VacantEntry`.
    pub fn key(&self) -> &K {
        &self.key
    }

    /// Take ownership of the key.
    pub fn into_key(self) -> K {
        self.key
    }

    /// Sets the value of the entry with the `VacantEntry`s key, and returns a mutable reference to it.
    pub fn insert(self, value: V) -> &'a mut V {
        let new = Box::new(StorageEntry::new(Some(value), EntryState::Mutated));
        match self.entry {
            BTreeMapEntry::Vacant(vacant) => {
                vacant
                    .insert(new)
                    .value_mut()
                    .as_mut()
                    .expect("insert was just executed; qed")
            }
            BTreeMapEntry::Occupied(mut occupied) => {
                occupied.insert(new);
                occupied
                    .into_mut()
                    .value_mut()
                    .as_mut()
                    .expect("insert was just executed; qed")
            }
        }
    }
}

impl<'a, K, V> OccupiedEntry<'a, K, V>
where
    K: Ord + Clone + PackedLayout,
    V: PackedLayout,
{
    /// Gets a reference to the key in the entry.
    pub fn key(&self) -> &K {
        &self.key
    }

    /// Take the ownership of the key and value from the map.
    pub fn remove_entry(self) -> (K, V) {
        let old = match self.entry {
            EntryOrMutableValue::EntryElementWasInCache(mut entry) => {
                entry
                    .get_mut()
                    .value_mut()
                    .take()
                    .expect("entry behind `OccupiedEntry` must always exist")
            }
            EntryOrMutableValue::MutableValueElementWasNotInCache(v_mut) => {
                v_mut
                    .value_mut()
                    .take()
                    .expect("entry behind `MutableValue` must always exist")
            }
        };
        (self.key, old)
    }

    /// Gets a reference to the value in the entry.
    pub fn get(&self) -> &V {
        match &self.entry {
            EntryOrMutableValue::EntryElementWasInCache(entry) => {
                entry
                    .get()
                    .value()
                    .as_ref()
                    .expect("entry behind `OccupiedEntry` must always exist")
            }
            EntryOrMutableValue::MutableValueElementWasNotInCache(v_mut) => {
                v_mut
                    .value()
                    .as_ref()
                    .expect("entry behind `MutableValue` must always exist")
            }
        }
    }

    /// Gets a mutable reference to the value in the entry.
    ///
    /// If you need a reference to the `OccupiedEntry` which may outlive the destruction of the
    /// `Entry` value, see `into_mut`.
    pub fn get_mut(&mut self) -> &mut V {
        match &mut self.entry {
            EntryOrMutableValue::EntryElementWasInCache(entry) => {
                entry
                    .get_mut()
                    .value_mut()
                    .as_mut()
                    .expect("entry behind `OccupiedEntry` must always exist")
            }
            EntryOrMutableValue::MutableValueElementWasNotInCache(v_mut) => {
                v_mut
                    .value_mut()
                    .as_mut()
                    .expect("entry behind `MutableValue` must always exist")
            }
        }
    }

    /// Sets the value of the entry, and returns the entry's old value.
    pub fn insert(&mut self, new_value: V) -> V {
        match &mut self.entry {
            EntryOrMutableValue::EntryElementWasInCache(entry) => {
                let new_value =
                    Box::new(StorageEntry::new(Some(new_value), EntryState::Mutated));
                entry
                    .insert(new_value)
                    .into_value()
                    .expect("entry behind `OccupiedEntry` must always exist")
            }
            EntryOrMutableValue::MutableValueElementWasNotInCache(v_mut) => {
                core::mem::replace(v_mut.value_mut(), Some(new_value))
                    .expect("entry behind `MutableValue` must always exist")
            }
        }
    }

    /// Takes the value out of the entry, and returns it.
    pub fn remove(self) -> V {
        self.remove_entry().1
    }

    /// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
    /// with a lifetime bound to the map itself.
    pub fn into_mut(self) -> &'a mut V {
        match self.entry {
            EntryOrMutableValue::EntryElementWasInCache(entry) => {
                entry
                    .into_mut()
                    .value_mut()
                    .as_mut()
                    .expect("entry behind `OccupiedEntry` must always exist")
            }
            EntryOrMutableValue::MutableValueElementWasNotInCache(v_mut) => {
                v_mut
                    .value_mut()
                    .as_mut()
                    .expect("entry behind `MutableValue` must always exist")
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        EntryState,
        LazyHashMap,
        StorageEntry,
    };
    use crate::traits::{
        KeyPtr,
        SpreadLayout,
    };
    use ink_env::hash::{
        Blake2x256,
        Sha2x256,
    };
    use ink_primitives::Key;

    /// Asserts that the cached entries of the given `imap` is equal to the `expected` slice.
    fn assert_cached_entries<H>(
        hmap: &LazyHashMap<i32, u8, H>,
        expected: &[(i32, StorageEntry<u8>)],
    ) {
        assert_eq!(hmap.len_cached_entries(), expected.len());
        for (given, expected) in hmap
            .entries()
            .iter()
            .map(|(index, boxed_entry)| (*index, &**boxed_entry))
            .zip(expected.iter().map(|(index, entry)| (*index, entry)))
        {
            assert_eq!(given, expected);
        }
    }

    fn new_hmap() -> LazyHashMap<i32, u8, Blake2x256> {
        <LazyHashMap<i32, u8, Blake2x256>>::new()
    }

    #[test]
    fn new_works() {
        let hmap = new_hmap();
        // Key must be none.
        assert_eq!(hmap.key(), None);
        assert_eq!(hmap.key_at(&0), None);
        // Cached elements must be empty.
        assert_cached_entries(&hmap, &[]);
        // Same as default:
        let default_hmap = <LazyHashMap<i32, u8, Blake2x256>>::default();
        assert_eq!(hmap.key(), default_hmap.key());
        assert_eq!(hmap.entries(), default_hmap.entries());
    }

    #[test]
    fn key_at_works() {
        let key = Key::from([0x42; 32]);

        // BLAKE-2 256-bit hasher:
        let hmap1 = <LazyHashMap<i32, u8, Blake2x256>>::lazy(key);
        // Key must be some.
        assert_eq!(hmap1.key(), Some(&key));
        // Cached elements must be empty.
        assert_cached_entries(&hmap1, &[]);
        let hmap1_at_0 = b"\
        \x67\x7E\xD3\xA4\x72\x2A\x83\x60\
        \x96\x65\x0E\xCD\x1F\x2C\xE8\x5D\
        \xBF\x7E\xC0\xFF\x16\x40\x8A\xD8\
        \x75\x88\xDE\x52\xF5\x8B\x99\xAF";
        assert_eq!(hmap1.key_at(&0), Some(Key::from(*hmap1_at_0)));
        // Same parameters must yield the same key:
        //
        // This tests an actual regression that happened because the
        // hash accumulator was not reset after a hash finalization.
        assert_cached_entries(&hmap1, &[]);
        assert_eq!(hmap1.key_at(&0), Some(Key::from(*hmap1_at_0)));
        assert_eq!(
            hmap1.key_at(&1),
            Some(Key::from(
                *b"\
                \x9A\x46\x1F\xB3\xA1\xC4\x20\xF8\
                \xA0\xD9\xA7\x79\x2F\x07\xFB\x7D\
                \x49\xDD\xAB\x08\x67\x90\x96\x15\
                \xFB\x85\x36\x3B\x82\x94\x85\x3F"
            ))
        );
        // SHA-2 256-bit hasher:
        let hmap2 = <LazyHashMap<i32, u8, Sha2x256>>::lazy(key);
        // Key must be some.
        assert_eq!(hmap2.key(), Some(&key));
        // Cached elements must be empty.
        assert_cached_entries(&hmap2, &[]);
        assert_eq!(
            hmap1.key_at(&0),
            Some(Key::from(
                *b"\
                \x67\x7E\xD3\xA4\x72\x2A\x83\x60\
                \x96\x65\x0E\xCD\x1F\x2C\xE8\x5D\
                \xBF\x7E\xC0\xFF\x16\x40\x8A\xD8\
                \x75\x88\xDE\x52\xF5\x8B\x99\xAF"
            ))
        );
        assert_eq!(
            hmap1.key_at(&1),
            Some(Key::from(
                *b"\
                \x9A\x46\x1F\xB3\xA1\xC4\x20\xF8\
                \xA0\xD9\xA7\x79\x2F\x07\xFB\x7D\
                \x49\xDD\xAB\x08\x67\x90\x96\x15\
                \xFB\x85\x36\x3B\x82\x94\x85\x3F"
            ))
        );
    }

    #[test]
    fn put_get_works() {
        let mut hmap = new_hmap();
        // Put some values.
        assert_eq!(hmap.put_get(&1, Some(b'A')), None);
        assert_eq!(hmap.put_get(&2, Some(b'B')), None);
        assert_eq!(hmap.put_get(&4, Some(b'C')), None);
        assert_cached_entries(
            &hmap,
            &[
                (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (4, StorageEntry::new(Some(b'C'), EntryState::Mutated)),
            ],
        );
        // Put none values.
        assert_eq!(hmap.put_get(&3, None), None);
        assert_eq!(hmap.put_get(&5, None), None);
        assert_cached_entries(
            &hmap,
            &[
                (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)),
            ],
        );
        // Override some values with none.
        assert_eq!(hmap.put_get(&2, None), Some(b'B'));
        assert_eq!(hmap.put_get(&4, None), Some(b'C'));
        assert_cached_entries(
            &hmap,
            &[
                (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)),
            ],
        );
        // Override none values with some.
        assert_eq!(hmap.put_get(&3, Some(b'X')), None);
        assert_eq!(hmap.put_get(&5, Some(b'Y')), None);
        assert_cached_entries(
            &hmap,
            &[
                (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)),
            ],
        );
    }

    #[test]
    fn get_works() {
        let mut hmap = new_hmap();
        let nothing_changed = &[
            (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)),
        ];
        // Put some values.
        assert_eq!(hmap.put_get(&1, None), None);
        assert_eq!(hmap.put_get(&2, Some(b'B')), None);
        assert_eq!(hmap.put_get(&3, None), None);
        assert_eq!(hmap.put_get(&4, Some(b'D')), None);
        assert_cached_entries(&hmap, nothing_changed);
        // `get` works:
        assert_eq!(hmap.get(&1), None);
        assert_eq!(hmap.get(&2), Some(&b'B'));
        assert_eq!(hmap.get(&3), None);
        assert_eq!(hmap.get(&4), Some(&b'D'));
        assert_cached_entries(&hmap, nothing_changed);
        // `get_mut` works:
        assert_eq!(hmap.get_mut(&1), None);
        assert_eq!(hmap.get_mut(&2), Some(&mut b'B'));
        assert_eq!(hmap.get_mut(&3), None);
        assert_eq!(hmap.get_mut(&4), Some(&mut b'D'));
        assert_cached_entries(&hmap, nothing_changed);
        // `get` or `get_mut` without cache:
        assert_eq!(hmap.get(&5), None);
        assert_eq!(hmap.get_mut(&5), None);
    }

    #[test]
    fn put_works() {
        let mut hmap = new_hmap();
        // Put some values.
        hmap.put(1, None);
        hmap.put(2, Some(b'B'));
        hmap.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(
            &hmap,
            &[
                (1, StorageEntry::new(None, EntryState::Mutated)),
                (2, StorageEntry::new(Some(b'B'), EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Mutated)),
            ],
        );
        // Overwrite entries:
        hmap.put(1, Some(b'A'));
        hmap.put(2, None);
        assert_cached_entries(
            &hmap,
            &[
                (1, StorageEntry::new(Some(b'A'), EntryState::Mutated)),
                (2, StorageEntry::new(None, EntryState::Mutated)),
                (4, StorageEntry::new(None, EntryState::Mutated)),
            ],
        );
    }

    #[test]
    fn swap_works() {
        let mut hmap = new_hmap();
        let nothing_changed = &[
            (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)),
        ];
        // Put some values.
        assert_eq!(hmap.put_get(&1, Some(b'A')), None);
        assert_eq!(hmap.put_get(&2, Some(b'B')), None);
        assert_eq!(hmap.put_get(&3, None), None);
        assert_eq!(hmap.put_get(&4, None), None);
        assert_cached_entries(&hmap, nothing_changed);
        // Swap same indices: Check that nothing has changed.
        for i in 0..4 {
            hmap.swap(&i, &i);
        }
        assert_cached_entries(&hmap, nothing_changed);
        // Swap `None` values: Check that nothing has changed.
        hmap.swap(&3, &4);
        hmap.swap(&4, &3);
        assert_cached_entries(&hmap, nothing_changed);
        // Swap `Some` and `None`:
        hmap.swap(&1, &3);
        assert_cached_entries(
            &hmap,
            &[
                (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)),
            ],
        );
        // Swap `Some` and `Some`:
        hmap.swap(&2, &3);
        assert_cached_entries(
            &hmap,
            &[
                (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)),
            ],
        );
        // Swap out of bounds: `None` and `None`
        hmap.swap(&4, &5);
        assert_cached_entries(
            &hmap,
            &[
                (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)),
            ],
        );
        // Swap out of bounds: `Some` and `None`
        hmap.swap(&3, &6);
        assert_cached_entries(
            &hmap,
            &[
                (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)),
            ],
        );
    }

    #[test]
    fn spread_layout_works() -> ink_env::Result<()> {
        ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
            let mut hmap = new_hmap();
            let nothing_changed = &[
                (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)),
            ];
            // Put some values.
            assert_eq!(hmap.put_get(&1, Some(b'A')), None);
            assert_eq!(hmap.put_get(&2, Some(b'B')), None);
            assert_eq!(hmap.put_get(&3, None), None);
            assert_eq!(hmap.put_get(&4, None), None);
            assert_cached_entries(&hmap, 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.
            let root_key = Key::from([0x42; 32]);
            SpreadLayout::push_spread(&hmap, &mut KeyPtr::from(root_key));
            let hmap2 = <LazyHashMap<i32, u8, Blake2x256> as SpreadLayout>::pull_spread(
                &mut KeyPtr::from(root_key),
            );
            assert_cached_entries(&hmap2, &[]);
            assert_eq!(hmap2.key(), Some(&Key::from([0x42; 32])));
            assert_eq!(hmap2.get(&1), Some(&b'A'));
            assert_eq!(hmap2.get(&2), Some(&b'B'));
            assert_eq!(hmap2.get(&3), None);
            assert_eq!(hmap2.get(&4), None);
            assert_cached_entries(
                &hmap2,
                &[
                    (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)),
                ],
            );
            // 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(&hmap2, &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:
            hmap2.clear_packed_at(&1);
            hmap2.clear_packed_at(&2);
            hmap2.clear_packed_at(&3); // Not really needed here.
            hmap2.clear_packed_at(&4); // Not really needed here.
            let hmap3 = <LazyHashMap<i32, u8, Blake2x256> as SpreadLayout>::pull_spread(
                &mut KeyPtr::from(root_key),
            );
            assert_cached_entries(&hmap3, &[]);
            assert_eq!(hmap3.get(&1), None);
            assert_eq!(hmap3.get(&2), None);
            assert_eq!(hmap3.get(&3), None);
            assert_eq!(hmap3.get(&4), None);
            assert_cached_entries(
                &hmap3,
                &[
                    (1, StorageEntry::new(None, EntryState::Preserved)),
                    (2, StorageEntry::new(None, EntryState::Preserved)),
                    (3, StorageEntry::new(None, EntryState::Preserved)),
                    (4, StorageEntry::new(None, EntryState::Preserved)),
                ],
            );
            Ok(())
        })
    }
}