Skip to content

Commit 0129d78

Browse files
authored
Merge pull request #86 from srdtrk/master
Fixed a remove bug in Keymap and Keyset
2 parents 7f0390e + cf4d7a6 commit 0129d78

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Releases.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fixed a remove bug in `Keymap` and `Keyset` ([#86](https://github.com/scrtlabs/secret-toolkit/pull/86)).
6+
57
## v0.8.1
68

79
### Bug fixes
@@ -123,9 +125,9 @@ A full guide to using the new `storage` types can be found
123125
### Breaking
124126

125127
- `secret-toolkit::permit::validate()` Now supports validating any type of Cosmos address.
126-
Interface changes: Now takes a reference to the current token address instead
127-
of taking it by value and an optional hrp string.
128-
In addition, it returns a String and not HumanAddr.
128+
Interface changes: Now takes a reference to the current token address instead
129+
of taking it by value and an optional hrp string.
130+
In addition, it returns a String and not HumanAddr.
129131

130132
- Renamed `secret-toolkit::permit::Permission` to `secret-toolkit::permit::TokenPermission`.
131133
- `secret-toolkit-crypto` now has features `["hash", "rng" and "ecc-secp256k1"]` which are all off by default - enable those you need.
@@ -149,16 +151,16 @@ This version is also the first released to [crates.io](https://crates.io)!
149151
- Added support for SNIP-23 messages (improved Send operations) which broke some interfaces
150152
- Added support for SNIP-24 permits
151153
- Added `Base64Of<S: Serde, T>`, `Base64JsonOf<T>`, and `Base64Bincode2Of<T>`,
152-
which are wrappers that automatically deserializes base64 strings to `T`.
153-
It can be used in message types' fields instead of `Binary` when the contents of the string
154-
should have more specific contents.
154+
which are wrappers that automatically deserializes base64 strings to `T`.
155+
It can be used in message types' fields instead of `Binary` when the contents of the string
156+
should have more specific contents.
155157

156158
- Added `storage::DequeStore` - Similar to `AppendStore` but allows pushing and popping on both ends
157159
- Added the `secret-toolkit::incubator` package intended for experimental features. It contains:
158160
- `CashMap` - A hashmap like storage abstraction
159161
- `GenerationalIndex` - A generational index storage abstraction
160162
- The various subpackages can now be selected using feature flags. The default flags are `["serialization", "snip20", "snip721", "storage", "utils"]`
161-
while `["crypto", "permit", "incubator"]` are left disabled by default.
163+
while `["crypto", "permit", "incubator"]` are left disabled by default.
162164

163165
## v0.1.1
164166

@@ -183,4 +185,4 @@ This is the first release of `secret-toolkit`. It supports:
183185
- `secret-toolkit::utils` - General utilities for writing contract code.
184186
- `padding` - tools for padding queries and responses.
185187
- `calls` - Tools for marking types as messages in queries and callbacks
186-
to other contracts.
188+
to other contracts.

packages/storage/src/keymap.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser:
367367
if len == 0 || len == removed_pos {
368368
indexes.pop();
369369
self.set_indexes_page(storage, page, &indexes)?;
370+
self.remove_impl(storage, &key_vec);
370371
return Ok(());
371372
}
372373

@@ -1498,4 +1499,18 @@ mod tests {
14981499

14991500
Ok(())
15001501
}
1502+
1503+
#[test]
1504+
fn test_add_remove_one() -> StdResult<()> {
1505+
let mut storage = MockStorage::new();
1506+
let keymap: Keymap<i32, i32> = Keymap::new(b"test");
1507+
keymap.insert(&mut storage, &1, &1)?;
1508+
assert_eq!(keymap.get_len(&storage)?, 1);
1509+
keymap.remove(&mut storage, &1)?;
1510+
assert_eq!(keymap.get_len(&storage)?, 0);
1511+
assert!(keymap.get(&storage, &1).is_none());
1512+
keymap.insert(&mut storage, &1, &1)?;
1513+
assert_eq!(keymap.get_len(&storage)?, 1);
1514+
Ok(())
1515+
}
15011516
}

packages/storage/src/keyset.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ impl<'a, K: Serialize + DeserializeOwned, Ser: Serde> Keyset<'a, K, Ser, WithIte
343343
if len == 0 || len == removed_pos {
344344
indexes.pop();
345345
self.set_indexes_page(storage, page, &indexes)?;
346+
storage.remove(&key_vec);
346347
return Ok(());
347348
}
348349

@@ -1047,4 +1048,18 @@ mod tests {
10471048

10481049
Ok(())
10491050
}
1051+
1052+
#[test]
1053+
fn test_add_remove_one() -> StdResult<()> {
1054+
let mut storage = MockStorage::new();
1055+
let keyset: Keyset<i32> = Keyset::new(b"test");
1056+
keyset.insert(&mut storage, &1)?;
1057+
assert_eq!(keyset.get_len(&storage)?, 1);
1058+
keyset.remove(&mut storage, &1)?;
1059+
assert_eq!(keyset.get_len(&storage)?, 0);
1060+
assert!(!keyset.contains(&storage, &1));
1061+
keyset.insert(&mut storage, &1)?;
1062+
assert_eq!(keyset.get_len(&storage)?, 1);
1063+
Ok(())
1064+
}
10501065
}

0 commit comments

Comments
 (0)