casper-contract-sdk 0.1.2

Casper contract sdk package
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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
use core::marker::PhantomData;

use borsh::{BorshDeserialize, BorshSerialize};
use bytes::BufMut;
use casper_executor_wasm_common::keyspace::Keyspace;
use const_fnv1a_hash::fnv1a_hash_64;

use crate::casper::{self, read_into_vec};

/// A pointer that uniquely identifies a value written into the map.
#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Copy, PartialEq)]
pub struct IterableMapPtr {
    /// The key hash
    pub(crate) hash: u64,
    /// In case of a collision, signifies the index of this element
    /// in a bucket
    pub(crate) index: u64,
}

/// Trait for types that can be used as keys in [IterableMap].
/// Must produce a deterministic hash.
///
/// A blanket implementation is provided for all types that implement
/// [BorshSerialize].
pub trait IterableMapHash: PartialEq + BorshSerialize + BorshDeserialize {
    fn compute_hash(&self) -> u64 {
        let mut bytes = Vec::new();
        self.serialize(&mut bytes).unwrap();
        fnv1a_hash_64(&bytes, None)
    }
}

// No blanket IterableMapKey implementation. Explicit impls prevent conflicts with
// user‑provided implementations; a blanket impl would forbid custom hashes.
impl IterableMapHash for u8 {}
impl IterableMapHash for u16 {}
impl IterableMapHash for u32 {}
impl IterableMapHash for u64 {}
impl IterableMapHash for u128 {}
impl IterableMapHash for i8 {}
impl IterableMapHash for i16 {}
impl IterableMapHash for i32 {}
impl IterableMapHash for i64 {}
impl IterableMapHash for i128 {}
impl IterableMapHash for String {}

/// A map over global state that allows iteration. Each entry at key `K_n` stores `(K_{n}, V,
/// K_{n-1})`, where `V` is the value and `K_{n-1}` is the key hash of the previous entry.
///
/// This creates a constant spatial overhead; every entry stores a pointer
/// to the one inserted before it.
///
/// Enables iteration without a guaranteed ordering; updating an existing
/// key does not affect position.
///
/// Under the hood, this is a singly-linked HashMap with linear probing for collision resolution.
/// Supports full traversal, typically in reverse-insertion order.
#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
#[borsh(crate = "crate::serializers::borsh")]
pub struct IterableMap<K, V> {
    pub(crate) prefix: String,

    // Keys are hashed to u128 internally, but K is preserved to enforce type safety.
    // While this map could accept arbitrary u128 keys, requiring a concrete K prevents
    // misuse and clarifies intent at the type level.
    pub(crate) tail_key_hash: Option<IterableMapPtr>,
    _marker: PhantomData<(K, V)>,
}

/// Single entry in `IterableMap`. Stores the value and the hash of the previous entry's key.
#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
#[borsh(crate = "crate::serializers::borsh")]
pub struct IterableMapEntry<K, V> {
    pub(crate) key: K,
    pub(crate) value: Option<V>,
    pub(crate) previous: Option<IterableMapPtr>,
}

impl<K, V> IterableMap<K, V>
where
    K: IterableMapHash,
    V: BorshSerialize + BorshDeserialize,
{
    /// Creates an empty [IterableMap] with the given prefix.
    pub fn new<S: Into<String>>(prefix: S) -> Self {
        Self {
            prefix: prefix.into(),
            tail_key_hash: None,
            _marker: PhantomData,
        }
    }

    /// Inserts a key-value pair into the map.
    ///
    /// If the map did not have this key present, `None` is returned.
    ///
    /// If the map did have this key present, the value is updated, and the old value is returned.
    ///
    /// This has an amortized complexity of O(1), with a worst-case of O(n) when running into
    /// collisions.
    pub fn insert(&mut self, key: K, value: V) -> Option<V> {
        // Find an address we can write to
        let (ptr, at_ptr) = self.get_writable_slot(&key);

        // Either overwrite an existing entry, or create a new one.
        let (entry_to_write, previous) = match at_ptr {
            Some(mut entry) => {
                if entry.value.is_none() {
                    // Reuse tombstone as a new insertion
                    entry.key = key;
                    entry.previous = self.tail_key_hash;
                    entry.value = Some(value);
                    self.tail_key_hash = Some(ptr);
                    (entry, None)
                } else {
                    // Overwrite an existing value
                    let old = entry.value;
                    entry.value = Some(value);
                    (entry, old)
                }
            }
            None => {
                let entry = IterableMapEntry {
                    key,
                    value: Some(value),
                    previous: self.tail_key_hash,
                };

                // Additionally, since this is a new entry, we need to update the tail
                self.tail_key_hash = Some(ptr);

                (entry, None)
            }
        };

        // Write the entry and return previous value if it exists
        let mut entry_bytes = Vec::new();
        entry_to_write.serialize(&mut entry_bytes).unwrap();

        let prefix = self.create_prefix_from_ptr(&ptr);
        let keyspace = Keyspace::Context(&prefix);
        casper::write(keyspace, &entry_bytes).unwrap();

        previous
    }

    /// Returns a value corresponding to the key.
    pub fn get(&self, key: &K) -> Option<V> {
        // If a slot is writable, it implicitly belongs the key
        let (_, at_ptr) = self.get_writable_slot(key);
        at_ptr.and_then(|entry| entry.value)
    }

    /// Removes a key from the map. Returns the associated value if the key exists.
    ///
    /// Has a worst-case runtime of O(n).
    pub fn remove(&mut self, key: &K) -> Option<V> {
        // Find the entry for the key that we're about to remove.
        let (to_remove_ptr, at_remove_ptr) = self.find_slot(key)?;

        let to_remove_prefix = self.create_prefix_from_ptr(&to_remove_ptr);
        let to_remove_context_key = Keyspace::Context(&to_remove_prefix);

        // See if the removed entry is a part of a collision resolution chain
        // by investigating its potential child.
        let to_remove_ptr_child_prefix = self.create_prefix_from_ptr(&IterableMapPtr {
            index: to_remove_ptr.index + 1,
            ..to_remove_ptr
        });
        let to_remove_ptr_child_keyspace = Keyspace::Context(&to_remove_ptr_child_prefix);

        if self.get_entry(to_remove_ptr_child_keyspace).is_some() {
            // A child exists, so we need to retain this element to maintain
            // collision resolution soundness. Instead of purging, mark as
            // tombstone.
            let tombstone = IterableMapEntry {
                value: None,
                ..at_remove_ptr
            };

            // Write the updated value
            let mut entry_bytes = Vec::new();
            tombstone.serialize(&mut entry_bytes).unwrap();
            casper::write(to_remove_context_key, &entry_bytes).unwrap();
        } else {
            // There is no child, so we can safely purge this entry entirely.
            casper::remove(to_remove_context_key).unwrap();
        }

        // Edge case when removing tail
        if self.tail_key_hash == Some(to_remove_ptr) {
            self.tail_key_hash = at_remove_ptr.previous;
            return at_remove_ptr.value;
        }

        // Scan the map, find entry to remove, join adjacent entries
        let mut current_hash = self.tail_key_hash;
        while let Some(key) = current_hash {
            let current_prefix = self.create_prefix_from_ptr(&key);
            let current_context_key = Keyspace::Context(&current_prefix);
            let mut current_entry = self.get_entry(current_context_key).unwrap();

            // If there is no previous entry, then we've finished iterating.
            //
            // This shouldn't happen, as the outer logic prevents from running
            // into such case, ie. we early exit if the entry to remove doesn't
            // exist.
            let Some(next_hash) = current_entry.previous else {
                panic!("Unexpected end of IterableMap");
            };

            // If the next entry is the one to be removed, repoint the current
            // one to the one preceeding the one to remove.
            if next_hash == to_remove_ptr {
                // Advance current past the element to remove
                current_entry.previous = at_remove_ptr.previous;

                // Re-write the updated current entry
                let mut entry_bytes = Vec::new();
                current_entry.serialize(&mut entry_bytes).unwrap();
                casper::write(current_context_key, &entry_bytes).unwrap();

                return at_remove_ptr.value;
            }

            // Advance backwards
            current_hash = current_entry.previous;
        }

        None
    }

    /// Clears the map, removing all key-value pairs.
    pub fn clear(&mut self) {
        for key in self.keys() {
            let prefix = self.create_prefix_from_key(&key);
            {
                let key = Keyspace::Context(&prefix);
                casper::remove(key).unwrap()
            };
        }

        self.tail_key_hash = None;
    }

    /// Returns true if the map contains a value for the specified key.
    pub fn contains_key(&self, key: &K) -> bool {
        self.get(key).is_some()
    }

    /// Creates an iterator visiting all the values in arbitrary order.
    pub fn keys(&self) -> impl Iterator<Item = K> + '_ {
        self.iter().map(|(key, _)| key)
    }

    /// Creates an iterator visiting all the values in arbitrary order.
    pub fn values(&self) -> impl Iterator<Item = V> + '_ {
        self.iter().map(|(_, value)| value)
    }

    // Returns true if the map contains no elements.
    pub fn is_empty(&self) -> bool {
        self.tail_key_hash.is_none()
    }

    /// Returns an iterator over the entries in the map.
    ///
    /// Traverses entries in reverse-insertion order.
    /// Each item is a tuple of the hashed key and the value.
    pub fn iter(&self) -> IterableMapIter<K, V> {
        IterableMapIter {
            prefix: &self.prefix,
            current: self.tail_key_hash,
            _marker: PhantomData,
        }
    }

    /// Returns the number of entries in the map.
    ///
    /// This is an O(n) operation.
    pub fn len(&self) -> usize {
        self.iter().count()
    }

    /// Find the slot containing key, if any.
    fn find_slot(&self, key: &K) -> Option<(IterableMapPtr, IterableMapEntry<K, V>)> {
        let mut bucket_ptr = self.create_root_ptr_from_key(key);

        // Probe until we find either an existing slot, a tombstone or empty space.
        // This should rarely iterate more than once assuming a solid hashing algorithm.
        loop {
            let prefix = self.create_prefix_from_ptr(&bucket_ptr);
            let keyspace = Keyspace::Context(&prefix);

            if let Some(entry) = self.get_entry(keyspace) {
                // Existing value, check if the keys match
                if entry.key == *key && entry.value.is_some() {
                    // We have found a slot where this key lives, return it
                    return Some((bucket_ptr, entry));
                } else {
                    // We found a slot for this key hash, but either the keys mismatch,
                    // or it's vacant, so we need to probe further.
                    bucket_ptr.index += 1;
                    continue;
                }
            } else {
                // We've reached empty address space, so the slot doesn't actually exist.
                return None;
            }
        }
    }

    /// Find the next slot we can safely write to. This is either a slot already owned and
    /// assigned to the key, a vacant tombstone, or empty memory.
    fn get_writable_slot(&self, key: &K) -> (IterableMapPtr, Option<IterableMapEntry<K, V>>) {
        let mut bucket_ptr = self.create_root_ptr_from_key(key);

        // Probe until we find either an existing slot, a tombstone or empty space.
        // This should rarely iterate more than once assuming a solid hashing algorithm.
        loop {
            let prefix = self.create_prefix_from_ptr(&bucket_ptr);
            let keyspace = Keyspace::Context(&prefix);

            if let Some(entry) = self.get_entry(keyspace) {
                // Existing value, check if the keys match
                if entry.key == *key {
                    // We have found an existing slot for that key, return it
                    return (bucket_ptr, Some(entry));
                } else if entry.value.is_none() {
                    // If the value is None, then this is a tombstone, and we
                    // can write over it.
                    return (bucket_ptr, Some(entry));
                } else {
                    // We found a slot for this key hash, but the keys mismatch,
                    // and it's not vacant, so this is a collision and we need to
                    // probe further.
                    bucket_ptr.index += 1;
                    continue;
                }
            } else {
                // We've reached empty address space, so we can write here
                return (bucket_ptr, None);
            }
        }
    }

    fn get_entry(&self, keyspace: Keyspace) -> Option<IterableMapEntry<K, V>> {
        match read_into_vec(keyspace) {
            Ok(Some(vec)) => {
                let entry: IterableMapEntry<K, V> = borsh::from_slice(&vec).unwrap();
                Some(entry)
            }
            Ok(None) => None,
            Err(_) => None,
        }
    }

    fn create_prefix_from_key(&self, key: &K) -> Vec<u8> {
        let ptr = self.create_root_ptr_from_key(key);
        self.create_prefix_from_ptr(&ptr)
    }

    fn create_root_ptr_from_key(&self, key: &K) -> IterableMapPtr {
        IterableMapPtr {
            hash: key.compute_hash(),
            index: 0,
        }
    }

    fn create_prefix_from_ptr(&self, hash: &IterableMapPtr) -> Vec<u8> {
        let mut context_key = Vec::new();
        context_key.extend(self.prefix.as_bytes());
        context_key.extend(b"_");
        context_key.put_u64_le(hash.hash);
        context_key.extend(b"_");
        context_key.put_u64_le(hash.index);
        context_key
    }
}

/// Iterator over entries in an [`IterableMap`].
///
/// Traverses the map in reverse-insertion order, following the internal
/// linked structure via hashed key references [`u128`].
///
/// Yields a tuple (K, V), where the key is the hashed
/// representation of the original key. The original key type `K` is not recoverable.
///
/// Each iteration step deserializes a single entry from storage.
///
/// This iterator performs no allocation beyond internal buffers,
/// and deserialization errors are treated as iteration termination.
pub struct IterableMapIter<'a, K, V> {
    prefix: &'a str,
    current: Option<IterableMapPtr>,
    _marker: PhantomData<(K, V)>,
}

impl<'a, K, V> IntoIterator for &'a IterableMap<K, V>
where
    K: BorshDeserialize,
    V: BorshDeserialize,
{
    type Item = (K, V);
    type IntoIter = IterableMapIter<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        IterableMapIter {
            prefix: &self.prefix,
            current: self.tail_key_hash,
            _marker: PhantomData,
        }
    }
}

impl<K, V> Iterator for IterableMapIter<'_, K, V>
where
    K: BorshDeserialize,
    V: BorshDeserialize,
{
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        let current_hash = self.current?;
        let mut key_bytes = Vec::new();
        key_bytes.extend(self.prefix.as_bytes());
        key_bytes.extend(b"_");
        key_bytes.put_u64_le(current_hash.hash);
        key_bytes.extend(b"_");
        key_bytes.put_u64_le(current_hash.index);

        let context_key = Keyspace::Context(&key_bytes);

        match read_into_vec(context_key) {
            Ok(Some(vec)) => {
                let entry: IterableMapEntry<K, V> = borsh::from_slice(&vec).unwrap();
                self.current = entry.previous;
                Some((
                    entry.key,
                    entry
                        .value
                        .expect("Tombstone values should be unlinked on removal"),
                ))
            }
            Ok(None) => None,
            Err(_) => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::casper::native::dispatch;

    const TEST_MAP_PREFIX: &str = "test_map";

    #[test]
    fn insert_and_get() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            assert_eq!(map.len(), 0);

            assert_eq!(map.get(&1), None);

            map.insert(1, "a".to_string());
            assert_eq!(map.len(), 1);

            assert_eq!(map.get(&1), Some("a".to_string()));

            map.insert(2, "b".to_string());
            assert_eq!(map.len(), 2);

            assert_eq!(map.get(&2), Some("b".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn overwrite_existing_key() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);

            assert_eq!(map.insert(1, "a".to_string()), None);
            assert_eq!(map.insert(1, "b".to_string()), Some("a".to_string()));
            assert_eq!(map.get(&1), Some("b".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn remove_tail_entry() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            assert_eq!(map.len(), 0);
            map.insert(1, "a".to_string());
            assert_eq!(map.len(), 1);
            map.insert(2, "b".to_string());
            assert_eq!(map.len(), 2);
            assert_eq!(map.remove(&2), Some("b".to_string()));
            assert_eq!(map.len(), 1);
            assert_eq!(map.get(&2), None);
            assert_eq!(map.get(&1), Some("a".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn remove_middle_entry() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            assert_eq!(map.len(), 0);

            map.insert(1, "a".to_string());
            assert_eq!(map.len(), 1);

            map.insert(2, "b".to_string());
            assert_eq!(map.len(), 2);

            map.insert(3, "c".to_string());
            assert_eq!(map.len(), 3);

            assert_eq!(map.remove(&2), Some("b".to_string()));
            assert_eq!(map.len(), 2);

            assert_eq!(map.get(&2), None);
            assert_eq!(map.get(&1), Some("a".to_string()));
            assert_eq!(map.get(&3), Some("c".to_string()));

            assert_eq!(map.len(), 2);
        })
        .unwrap();
    }

    #[test]
    fn remove_nonexistent_key_does_nothing() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);

            map.insert(1, "a".to_string());

            assert_eq!(map.remove(&999), None);
            assert_eq!(map.get(&1), Some("a".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn iterates_all_entries_in_reverse_insertion_order() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);

            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            map.insert(3, "c".to_string());

            let values: Vec<_> = map.values().collect();
            assert_eq!(
                values,
                vec!["c".to_string(), "b".to_string(), "a".to_string(),]
            );
        })
        .unwrap();
    }

    #[test]
    fn iteration_skips_deleted_entries() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);

            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            map.insert(3, "c".to_string());

            map.remove(&2);

            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["c".to_string(), "a".to_string(),]);
        })
        .unwrap();
    }

    #[test]
    fn empty_map_behaves_sanely() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);

            assert_eq!(map.get(&1), None);
            assert_eq!(map.remove(&1), None);
            assert_eq!(map.iter().count(), 0);
        })
        .unwrap();
    }

    #[test]
    fn separate_maps_do_not_conflict() {
        dispatch(|| {
            let mut map1 = IterableMap::<u64, String>::new("map1");
            let mut map2 = IterableMap::<u64, String>::new("map2");

            map1.insert(1, "a".to_string());
            map2.insert(1, "b".to_string());

            assert_eq!(map1.get(&1), Some("a".to_string()));
            assert_eq!(map2.get(&1), Some("b".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn insert_same_value_under_different_keys() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);

            map.insert(1, "shared".to_string());
            map.insert(2, "shared".to_string());

            assert_eq!(map.get(&1), Some("shared".to_string()));
            assert_eq!(map.get(&2), Some("shared".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn clear_removes_all_entries() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            map.clear();
            assert!(map.is_empty());
            assert_eq!(map.iter().count(), 0);
        })
        .unwrap();
    }

    #[test]
    fn keys_returns_reverse_insertion_order() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            let hashes: Vec<_> = map.keys().collect();
            assert_eq!(hashes, vec![2, 1]);
        })
        .unwrap();
    }

    #[test]
    fn values_returns_values_in_reverse_insertion_order() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["b".to_string(), "a".to_string()]);
        })
        .unwrap();
    }

    #[test]
    fn contains_key_returns_correctly() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            assert!(!map.contains_key(&1));
            map.insert(1, "a".to_string());
            assert!(map.contains_key(&1));
            map.remove(&1);
            assert!(!map.contains_key(&1));
        })
        .unwrap();
    }

    #[test]
    fn multiple_removals_and_insertions() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            map.insert(3, "c".to_string());
            map.remove(&2);
            assert_eq!(map.get(&2), None);
            assert_eq!(map.get(&1), Some("a".to_string()));
            assert_eq!(map.get(&3), Some("c".to_string()));

            map.insert(4, "d".to_string());
            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["d", "c", "a"]);
        })
        .unwrap();
    }

    #[test]
    fn struct_as_key() {
        #[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq)]
        struct TestKey {
            id: u64,
            name: String,
        }

        impl IterableMapHash for TestKey {}

        dispatch(|| {
            let key1 = TestKey {
                id: 1,
                name: "Key1".to_string(),
            };
            let key2 = TestKey {
                id: 2,
                name: "Key2".to_string(),
            };
            let mut map = IterableMap::<TestKey, String>::new(TEST_MAP_PREFIX);

            map.insert(key1.clone(), "a".to_string());
            map.insert(key2.clone(), "b".to_string());

            assert_eq!(map.get(&key1), Some("a".to_string()));
            assert_eq!(map.get(&key2), Some("b".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn remove_middle_of_long_chain() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            map.insert(3, "c".to_string());
            map.insert(4, "d".to_string());
            map.insert(5, "e".to_string());

            // The order is 5,4,3,2,1
            map.remove(&3); // Remove the middle entry

            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["e", "d", "b", "a"]);

            // Check that entry 4's previous is now 2's hash
            let ptr4 = map.create_root_ptr_from_key(&4u64);
            let prefix = map.create_prefix_from_ptr(&ptr4);
            let entry = map.get_entry(Keyspace::Context(&prefix)).unwrap();
            assert_eq!(entry.previous, Some(map.create_root_ptr_from_key(&2u64)));
        })
        .unwrap();
    }

    #[test]
    fn insert_after_remove_updates_head() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            map.remove(&2);
            map.insert(3, "c".to_string());

            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["c", "a"]);
        })
        .unwrap();
    }

    #[test]
    fn reinsert_removed_key() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            map.insert(1, "a".to_string());
            map.remove(&1);
            map.insert(1, "b".to_string());

            assert_eq!(map.get(&1), Some("b".to_string()));
            assert_eq!(map.iter().next().unwrap().1, "b".to_string());
        })
        .unwrap();
    }

    #[test]
    fn iteration_reflects_modifications() {
        dispatch(|| {
            let mut map = IterableMap::<u64, String>::new(TEST_MAP_PREFIX);
            map.insert(1, "a".to_string());
            map.insert(2, "b".to_string());
            let mut iter = map.iter();
            assert_eq!(iter.next().unwrap().1, "b".to_string());

            map.remove(&2);
            map.insert(3, "c".to_string());
            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["c", "a"]);
        })
        .unwrap();
    }

    #[test]
    fn unit_struct_as_key() {
        #[derive(BorshSerialize, BorshDeserialize, PartialEq)]
        struct UnitKey;

        impl IterableMapHash for UnitKey {}

        dispatch(|| {
            let mut map = IterableMap::<UnitKey, String>::new(TEST_MAP_PREFIX);
            map.insert(UnitKey, "value".to_string());
            assert_eq!(map.get(&UnitKey), Some("value".to_string()));
        })
        .unwrap();
    }

    #[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)]
    struct CollidingKey(u64, u64);

    impl IterableMapHash for CollidingKey {
        fn compute_hash(&self) -> u64 {
            let mut bytes = Vec::new();
            // Only serialize first field for hash computation
            self.0.serialize(&mut bytes).unwrap();
            fnv1a_hash_64(&bytes, None)
        }
    }

    #[test]
    fn basic_collision_handling() {
        dispatch(|| {
            let mut map = IterableMap::<CollidingKey, String>::new(TEST_MAP_PREFIX);

            // Both keys will have same hash but different actual keys
            let k1 = CollidingKey(42, 1);
            let k2 = CollidingKey(42, 2);

            map.insert(k1.clone(), "first".to_string());
            map.insert(k2.clone(), "second".to_string());

            assert_eq!(map.get(&k1), Some("first".to_string()));
            assert_eq!(map.get(&k2), Some("second".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn tombstone_handling() {
        dispatch(|| {
            let mut map = IterableMap::<CollidingKey, String>::new(TEST_MAP_PREFIX);

            let k1 = CollidingKey(42, 1);
            let k2 = CollidingKey(42, 2);
            let k3 = CollidingKey(42, 3);

            map.insert(k1.clone(), "first".to_string());
            map.insert(k2.clone(), "second".to_string());
            map.insert(k3.clone(), "third".to_string());

            // Remove middle entry
            assert_eq!(map.remove(&k2), Some("second".to_string()));

            // Verify tombstone state
            let (_, entry) = map.get_writable_slot(&k2);
            assert!(entry.unwrap().value.is_none());

            // Verify chain integrity
            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["third", "first"]);
        })
        .unwrap();
    }

    #[test]
    fn tombstone_reuse() {
        dispatch(|| {
            let mut map = IterableMap::<CollidingKey, String>::new(TEST_MAP_PREFIX);

            let k1 = CollidingKey(42, 1);
            let k2 = CollidingKey(42, 2);

            map.insert(k1.clone(), "first".to_string());
            map.insert(k2.clone(), "second".to_string());

            // Removing k1 while k2 exists guarantees k1 turns into
            // a tombstone
            map.remove(&k1);

            // Reinsert into tombstone slot
            map.insert(k1.clone(), "reused".to_string());

            assert_eq!(map.get(&k1), Some("reused".to_string()));
            assert_eq!(map.get(&k2), Some("second".to_string()));
        })
        .unwrap();
    }

    #[test]
    fn full_deletion_handling() {
        dispatch(|| {
            let mut map = IterableMap::<CollidingKey, String>::new(TEST_MAP_PREFIX);

            let k1 = CollidingKey(42, 1);
            map.insert(k1.clone(), "lonely".to_string());

            assert_eq!(map.remove(&k1), Some("lonely".to_string()));

            // Verify complete removal
            let (_, entry) = map.get_writable_slot(&k1);
            assert!(entry.is_none());
        })
        .unwrap();
    }

    #[test]
    fn collision_chain_iteration() {
        dispatch(|| {
            let mut map = IterableMap::<CollidingKey, String>::new(TEST_MAP_PREFIX);

            let keys = [
                CollidingKey(42, 1),
                CollidingKey(42, 2),
                CollidingKey(42, 3),
            ];

            for (i, k) in keys.iter().enumerate() {
                map.insert(k.clone(), format!("value-{}", i));
            }

            // Remove middle entry
            map.remove(&keys[1]);

            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["value-2", "value-0"]);
        })
        .unwrap();
    }

    #[test]
    fn complex_collision_chain() {
        dispatch(|| {
            let mut map = IterableMap::<CollidingKey, String>::new(TEST_MAP_PREFIX);

            // Create 5 colliding keys
            let keys: Vec<_> = (0..5).map(|i| CollidingKey(42, i)).collect();

            // Insert all
            for k in &keys {
                map.insert(k.clone(), format!("{}", k.1));
            }

            // Remove even indexes
            for k in keys.iter().step_by(2) {
                map.remove(k);
            }

            // Insert new values
            map.insert(keys[0].clone(), "reinserted".to_string());
            map.insert(CollidingKey(42, 5), "new".to_string());

            // Verify final state
            let expected = vec![
                ("new".to_string(), 5),
                ("reinserted".to_string(), 0),
                ("3".to_string(), 3),
                ("1".to_string(), 1),
            ];

            let results: Vec<_> = map.iter().map(|(k, v)| (v, k.1)).collect();

            assert_eq!(results, expected);
        })
        .unwrap();
    }

    #[test]
    fn cross_bucket_reference() {
        dispatch(|| {
            let mut map = IterableMap::<CollidingKey, String>::new(TEST_MAP_PREFIX);

            // Create keys with different hashes but chained references
            let k1 = CollidingKey(1, 0);
            let k2 = CollidingKey(2, 0);
            let k3 = CollidingKey(1, 1); // Collides with k1

            map.insert(k1.clone(), "first".to_string());
            map.insert(k2.clone(), "second".to_string());
            map.insert(k3.clone(), "third".to_string());

            // Remove k2 which is referenced by k3
            map.remove(&k2);

            // Verify iteration skips removed entry
            let values: Vec<_> = map.values().collect();
            assert_eq!(values, vec!["third", "first"]);
        })
        .unwrap();
    }
}