diff --git a/packages/permit/src/state.rs b/packages/permit/src/state.rs index 9a47e93..7c37035 100644 --- a/packages/permit/src/state.rs +++ b/packages/permit/src/state.rs @@ -4,14 +4,14 @@ pub struct RevokedPermits; impl RevokedPermits { pub fn is_permit_revoked( - storgae: &dyn Storage, + storage: &dyn Storage, storage_prefix: &str, account: &str, permit_name: &str, ) -> bool { let storage_key = storage_prefix.to_string() + account + permit_name; - storgae.get(storage_key.as_bytes()).is_some() + storage.get(storage_key.as_bytes()).is_some() } pub fn revoke_permit( diff --git a/packages/storage/Readme.md b/packages/storage/Readme.md index 187017a..7eaa2df 100644 --- a/packages/storage/Readme.md +++ b/packages/storage/Readme.md @@ -337,7 +337,7 @@ pub static JSON_VOTE: Keymap = You can find more examples of using keymaps in the unit tests of Keymap in `keymap.rs`. -To insert, remove, read from the keymap, do the following: +To save to, remove, and read from the keymap, do the following: ```rust # use secret_toolkit_storage::{Keymap, KeymapBuilder}; @@ -357,9 +357,9 @@ let foo = Foo { votes: 1111, }; -ADDR_VOTE.insert(&mut deps.storage, &user_addr, &foo)?; +ADDR_VOTE.save(&mut deps.storage, &user_addr, &foo)?; // Compiler knows that this is Foo -let read_foo = ADDR_VOTE.get(deps.as_ref().storage, &user_addr).unwrap(); +let read_foo = ADDR_VOTE.load(deps.as_ref().storage, &user_addr).unwrap(); assert_eq!(read_foo, foo); ADDR_VOTE.remove(&mut deps.storage, &user_addr)?; assert_eq!(ADDR_VOTE.get_len(deps.as_ref().storage)?, 0); @@ -397,8 +397,8 @@ fn test_keymap_iter_keys() -> StdResult<()> { let key1 = "key1".to_string(); let key2 = "key2".to_string(); - keymap.insert(&mut storage, &key1, &foo1)?; - keymap.insert(&mut storage, &key2, &foo2)?; + keymap.save(&mut storage, &key1, &foo1)?; + keymap.save(&mut storage, &key2, &foo2)?; let mut x = keymap.iter_keys(&storage)?; let (len, _) = x.size_hint(); @@ -432,8 +432,8 @@ fn test_keymap_iter() -> StdResult<()> { number: 1111, }; - keymap.insert(&mut storage, &b"key1".to_vec(), &foo1)?; - keymap.insert(&mut storage, &b"key2".to_vec(), &foo2)?; + keymap.save(&mut storage, &b"key1".to_vec(), &foo1)?; + keymap.save(&mut storage, &b"key2".to_vec(), &foo2)?; let mut x = keymap.iter(&storage)?; let (len, _) = x.size_hint(); diff --git a/packages/storage/src/keymap.rs b/packages/storage/src/keymap.rs index df39c12..8736d63 100644 --- a/packages/storage/src/keymap.rs +++ b/packages/storage/src/keymap.rs @@ -198,13 +198,13 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser: Ser::serialize(key) } - /// user facing get function - pub fn get(&self, storage: &dyn Storage, key: &K) -> Option { - self.get_from_key(storage, key).ok() + /// user facing load function + pub fn load(&self, storage: &dyn Storage, key: &K) -> Option { + self.load_from_key(storage, key).ok() } - /// internal item get function - fn get_from_key(&self, storage: &dyn Storage, key: &K) -> StdResult { + /// internal item load function + fn load_from_key(&self, storage: &dyn Storage, key: &K) -> StdResult { let key_vec = self.serialize_key(key)?; self.load_impl(storage, &key_vec) } @@ -217,16 +217,16 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser: Ok(()) } - /// user facing insert function - pub fn insert(&self, storage: &mut dyn Storage, key: &K, item: &T) -> StdResult<()> { + /// user facing save function + pub fn save(&self, storage: &mut dyn Storage, key: &K, item: &T) -> StdResult<()> { let key_vec = self.serialize_key(key)?; self.save_impl(storage, &key_vec, item) } /// user facing method that checks if any item is stored with this key. - pub fn contains(&self, storage: &dyn Storage, key: &K) -> bool { + pub fn has(&self, storage: &dyn Storage, key: &K) -> bool { match self.serialize_key(key) { - Ok(key_vec) => self.contains_impl(storage, &key_vec), + Ok(key_vec) => self.has_impl(storage, &key_vec), Err(_) => false, } } @@ -277,7 +277,7 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser: Ok(self.get_len(storage)? == 0) } - /// set length of the map + /// set length of the keymap fn set_len(&self, storage: &mut dyn Storage, len: u32) -> StdResult<()> { let len_key = [self.as_slice(), MAP_LENGTH].concat(); storage.set(&len_key, &len.to_be_bytes()); @@ -326,17 +326,17 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser: Ok(()) } - /// user facing get function - pub fn get(&self, storage: &dyn Storage, key: &K) -> Option { - if let Ok(internal_item) = self.get_from_key(storage, key) { + /// user facing load function + pub fn load(&self, storage: &dyn Storage, key: &K) -> Option { + if let Ok(internal_item) = self.load_from_key(storage, key) { internal_item.get_item().ok() } else { None } } - /// internal item get function - fn get_from_key(&self, storage: &dyn Storage, key: &K) -> StdResult> { + /// internal item load function + fn load_from_key(&self, storage: &dyn Storage, key: &K) -> StdResult> { let key_vec = self.serialize_key(key)?; self.load_impl(storage, &key_vec) } @@ -345,7 +345,7 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser: pub fn remove(&self, storage: &mut dyn Storage, key: &K) -> StdResult<()> { let key_vec = self.serialize_key(key)?; - let removed_pos = self.get_from_key(storage, key)?.index_pos.unwrap(); + let removed_pos = self.load_from_key(storage, key)?.index_pos.unwrap(); let page = self.page_from_position(removed_pos); @@ -404,8 +404,8 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser: Ok(()) } - /// user facing insert function - pub fn insert(&self, storage: &mut dyn Storage, key: &K, item: &T) -> StdResult<()> { + /// user facing save function + pub fn save(&self, storage: &mut dyn Storage, key: &K, item: &T) -> StdResult<()> { let key_vec = self.serialize_key(key)?; match self.may_load_impl(storage, &key_vec)? { @@ -431,9 +431,9 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser: } /// user facing method that checks if any item is stored with this key. - pub fn contains(&self, storage: &dyn Storage, key: &K) -> bool { + pub fn has(&self, storage: &dyn Storage, key: &K) -> bool { match self.serialize_key(key) { - Ok(key_vec) => self.contains_impl(storage, &key_vec), + Ok(key_vec) => self.has_impl(storage, &key_vec), Err(_) => false, } } @@ -813,7 +813,7 @@ where self.start += 1; // turn key into pair let pair = match key { - Ok(k) => match self.keymap.get_from_key(self.storage, &k) { + Ok(k) => match self.keymap.load_from_key(self.storage, &k) { Ok(internal_item) => match internal_item.get_item() { Ok(item) => Ok((k, item)), Err(e) => Err(e), @@ -875,7 +875,7 @@ where } // turn key into pair let pair = match key { - Ok(k) => match self.keymap.get_from_key(self.storage, &k) { + Ok(k) => match self.keymap.load_from_key(self.storage, &k) { Ok(internal_item) => match internal_item.get_item() { Ok(item) => Ok((k, item)), Err(e) => Err(e), @@ -917,7 +917,7 @@ trait PrefixedTypedStorage { /// /// * `storage` - a reference to the storage this item is in /// * `key` - a byte slice representing the key to access the stored item - fn contains_impl(&self, storage: &dyn Storage, key: &[u8]) -> bool { + fn has_impl(&self, storage: &dyn Storage, key: &[u8]) -> bool { let prefixed_key = [self.as_slice(), key].concat(); storage.get(&prefixed_key).is_some() } @@ -993,7 +993,7 @@ mod tests { number: i32, } #[test] - fn test_keymap_perf_insert() -> StdResult<()> { + fn keymap_perf_save() -> StdResult<()> { let mut storage = MockStorage::new(); let total_items = 1000; @@ -1002,7 +1002,7 @@ mod tests { for i in 0..total_items { let key: Vec = (i as i32).to_be_bytes().to_vec(); - keymap.insert(&mut storage, &key, &i)?; + keymap.save(&mut storage, &key, &i)?; } assert_eq!(keymap.get_len(&storage)?, 1000); @@ -1011,7 +1011,7 @@ mod tests { } #[test] - fn test_keymap_perf_insert_remove() -> StdResult<()> { + fn test_keymap_perf_save_remove() -> StdResult<()> { let mut storage = MockStorage::new(); let total_items = 100; @@ -1019,7 +1019,7 @@ mod tests { let keymap: Keymap = Keymap::new(b"test"); for i in 0..total_items { - keymap.insert(&mut storage, &i, &i)?; + keymap.save(&mut storage, &i, &i)?; } for i in 0..total_items { @@ -1041,7 +1041,7 @@ mod tests { for i in 0..total_items { let key: Vec = (i as i32).to_be_bytes().to_vec(); - keymap.insert(&mut storage, &key, &i)?; + keymap.save(&mut storage, &key, &i)?; } for i in 0..((total_items / page_size) - 1) { @@ -1069,7 +1069,7 @@ mod tests { let keymap: Keymap = Keymap::new(b"test"); for i in 0..total_items { - keymap.insert(&mut storage, &(i as i32), &i)?; + keymap.save(&mut storage, &(i as i32), &i)?; } let values = keymap.paging_keys(&storage, 0, page_size)?; @@ -1084,7 +1084,7 @@ mod tests { } #[test] - fn test_keymap_insert_multiple() -> StdResult<()> { + fn test_keymap_save_multiple() -> StdResult<()> { let mut storage = MockStorage::new(); let keymap: Keymap, Foo> = Keymap::new(b"test"); @@ -1097,11 +1097,11 @@ mod tests { number: 1111, }; - keymap.insert(&mut storage, &b"key1".to_vec(), &foo1)?; - keymap.insert(&mut storage, &b"key2".to_vec(), &foo2)?; + keymap.save(&mut storage, &b"key1".to_vec(), &foo1)?; + keymap.save(&mut storage, &b"key2".to_vec(), &foo2)?; - let read_foo1 = keymap.get(&storage, &b"key1".to_vec()).unwrap(); - let read_foo2 = keymap.get(&storage, &b"key2".to_vec()).unwrap(); + let read_foo1 = keymap.load(&storage, &b"key1".to_vec()).unwrap(); + let read_foo2 = keymap.load(&storage, &b"key2".to_vec()).unwrap(); assert_eq!(foo1, read_foo1); assert_eq!(foo2, read_foo2); @@ -1109,7 +1109,7 @@ mod tests { } #[test] - fn test_keymap_contains() -> StdResult<()> { + fn test_keymap_has() -> StdResult<()> { let mut storage = MockStorage::new(); let keymap: Keymap, Foo> = Keymap::new(b"test"); @@ -1118,10 +1118,10 @@ mod tests { number: 1111, }; - keymap.insert(&mut storage, &b"key1".to_vec(), &foo1)?; - let contains_k1 = keymap.contains(&storage, &b"key1".to_vec()); + keymap.save(&mut storage, &b"key1".to_vec(), &foo1)?; + let has_k1 = keymap.has(&storage, &b"key1".to_vec()); - assert!(contains_k1); + assert!(has_k1); Ok(()) } @@ -1140,8 +1140,8 @@ mod tests { number: 1111, }; - keymap.insert(&mut storage, &b"key1".to_vec(), &foo1)?; - keymap.insert(&mut storage, &b"key2".to_vec(), &foo2)?; + keymap.save(&mut storage, &b"key1".to_vec(), &foo1)?; + keymap.save(&mut storage, &b"key2".to_vec(), &foo2)?; let mut x = keymap.iter(&storage)?; let (len, _) = x.size_hint(); @@ -1171,8 +1171,8 @@ mod tests { let key1 = "key1".to_string(); let key2 = "key2".to_string(); - keymap.insert(&mut storage, &key1, &foo1)?; - keymap.insert(&mut storage, &key2, &foo2)?; + keymap.save(&mut storage, &key1, &foo1)?; + keymap.save(&mut storage, &key2, &foo2)?; let mut x = keymap.iter_keys(&storage)?; let (len, _) = x.size_hint(); @@ -1199,10 +1199,10 @@ mod tests { number: 2222, }; - keymap.insert(&mut storage, &b"key1".to_vec(), &foo1)?; - keymap.insert(&mut storage, &b"key1".to_vec(), &foo2)?; + keymap.save(&mut storage, &b"key1".to_vec(), &foo1)?; + keymap.save(&mut storage, &b"key1".to_vec(), &foo2)?; - let foo3 = keymap.get(&storage, &b"key1".to_vec()).unwrap(); + let foo3 = keymap.load(&storage, &b"key1".to_vec()).unwrap(); assert_eq!(foo3, foo2); @@ -1223,11 +1223,11 @@ mod tests { string: "string one".to_string(), number: 1111, }; - keymap.insert(&mut storage, &"key1".to_string(), &foo1)?; - keymap.insert(&mut storage, &"key2".to_string(), &foo2)?; + keymap.save(&mut storage, &"key1".to_string(), &foo1)?; + keymap.save(&mut storage, &"key2".to_string(), &foo2)?; - let read_foo1 = keymap.get(&storage, &"key1".to_string()).unwrap(); - let read_foo2 = keymap.get(&storage, &"key2".to_string()).unwrap(); + let read_foo1 = keymap.load(&storage, &"key1".to_string()).unwrap(); + let read_foo2 = keymap.load(&storage, &"key2".to_string()).unwrap(); assert_eq!(original_keymap.get_len(&storage)?, 0); assert_eq!(foo1, read_foo1); @@ -1239,16 +1239,16 @@ mod tests { assert!(alt_same_suffix.is_empty(&storage)?); // show that it loads foo1 before removal - let before_remove_foo1 = keymap.get(&storage, &"key1".to_string()); + let before_remove_foo1 = keymap.load(&storage, &"key1".to_string()); assert!(before_remove_foo1.is_some()); assert_eq!(foo1, before_remove_foo1.unwrap()); // and returns None after removal keymap.remove(&mut storage, &"key1".to_string())?; - let removed_foo1 = keymap.get(&storage, &"key1".to_string()); + let removed_foo1 = keymap.load(&storage, &"key1".to_string()); assert!(removed_foo1.is_none()); // show what happens when reading from keys that have not been set yet. - assert!(keymap.get(&storage, &"key3".to_string()).is_none()); + assert!(keymap.load(&storage, &"key3".to_string()).is_none()); Ok(()) } @@ -1283,12 +1283,12 @@ mod tests { let key1 = "k1".to_string(); let key2 = "k2".to_string(); - keymap.insert(&mut storage, &key1, &foo1)?; + keymap.save(&mut storage, &key1, &foo1)?; assert_eq!(keymap.get_len(&storage)?, 1); assert!(keymap.length.lock().unwrap().eq(&Some(1))); // add another item - keymap.insert(&mut storage, &key2, &foo2)?; + keymap.save(&mut storage, &key2, &foo2)?; assert_eq!(keymap.get_len(&storage)?, 2); assert!(keymap.length.lock().unwrap().eq(&Some(2))); @@ -1298,7 +1298,7 @@ mod tests { assert!(keymap.length.lock().unwrap().eq(&Some(1))); // override item (should not change length) - keymap.insert(&mut storage, &key2, &foo1)?; + keymap.save(&mut storage, &key2, &foo1)?; assert_eq!(keymap.get_len(&storage)?, 1); assert!(keymap.length.lock().unwrap().eq(&Some(1))); @@ -1334,20 +1334,20 @@ mod tests { string: "string one".to_string(), number: 1111, }; - keymap.insert(&mut storage, &"key1".to_string(), &foo1)?; - keymap.insert(&mut storage, &"key2".to_string(), &foo2)?; + keymap.save(&mut storage, &"key1".to_string(), &foo1)?; + keymap.save(&mut storage, &"key2".to_string(), &foo2)?; - let read_foo1 = keymap.get(&storage, &"key1".to_string()).unwrap(); - let read_foo2 = keymap.get(&storage, &"key2".to_string()).unwrap(); + let read_foo1 = keymap.load(&storage, &"key1".to_string()).unwrap(); + let read_foo2 = keymap.load(&storage, &"key2".to_string()).unwrap(); assert_eq!(foo1, read_foo1); assert_eq!(foo2, read_foo2); - assert!(keymap.contains(&storage, &"key1".to_string())); + assert!(keymap.has(&storage, &"key1".to_string())); keymap.remove(&mut storage, &"key1".to_string())?; - let read_foo1 = keymap.get(&storage, &"key1".to_string()); - let read_foo2 = keymap.get(&storage, &"key2".to_string()).unwrap(); + let read_foo1 = keymap.load(&storage, &"key1".to_string()); + let read_foo2 = keymap.load(&storage, &"key2".to_string()).unwrap(); assert!(read_foo1.is_none()); assert_eq!(foo2, read_foo2); @@ -1365,7 +1365,7 @@ mod tests { for i in 0..total_items { let key: Vec = (i as i32).to_be_bytes().to_vec(); - keymap.insert(&mut storage, &key, &i)?; + keymap.save(&mut storage, &key, &i)?; } for i in 0..((total_items / page_size) - 1) { @@ -1393,7 +1393,7 @@ mod tests { let keymap: Keymap = KeymapBuilder::new(b"test").with_page_size(3).build(); for i in 0..total_items { - keymap.insert(&mut storage, &(i as i32), &i)?; + keymap.save(&mut storage, &(i as i32), &i)?; } let values = keymap.paging_keys(&storage, 0, page_size)?; @@ -1425,9 +1425,9 @@ mod tests { number: 1111, }; - keymap.insert(&mut storage, &b"key1".to_vec(), &foo1)?; - keymap.insert(&mut storage, &b"key2".to_vec(), &foo2)?; - keymap.insert(&mut storage, &b"key3".to_vec(), &foo3)?; + keymap.save(&mut storage, &b"key1".to_vec(), &foo1)?; + keymap.save(&mut storage, &b"key2".to_vec(), &foo2)?; + keymap.save(&mut storage, &b"key3".to_vec(), &foo3)?; let mut x = keymap.iter(&storage)?; let (len, _) = x.size_hint(); @@ -1458,10 +1458,10 @@ mod tests { let keymap: Keymap = KeymapBuilder::new(b"test") .with_page_size(page_size) .build(); - keymap.insert(&mut storage, &1234, &1234)?; - keymap.insert(&mut storage, &2143, &2143)?; - keymap.insert(&mut storage, &3412, &3412)?; - keymap.insert(&mut storage, &4321, &4321)?; + keymap.save(&mut storage, &1234, &1234)?; + keymap.save(&mut storage, &2143, &2143)?; + keymap.save(&mut storage, &3412, &3412)?; + keymap.save(&mut storage, &4321, &4321)?; let mut iter = keymap.iter(&storage)?.rev(); assert_eq!(iter.next(), Some(Ok((4321, 4321)))); @@ -1508,7 +1508,7 @@ mod tests { let keymap: Keymap = KeymapBuilder::new(b"test") .with_page_size(page_size) .build(); - keymap.insert(&mut storage, &1234, &1234)?; + keymap.save(&mut storage, &1234, &1234)?; let page_key = [keymap.as_slice(), INDEXES, &0_u32.to_be_bytes()].concat(); if keymap.page_size == 1 { @@ -1526,7 +1526,7 @@ mod tests { let json_keymap: Keymap = KeymapBuilder::new(b"test2") .with_page_size(page_size) .build(); - json_keymap.insert(&mut storage, &1234, &1234)?; + json_keymap.save(&mut storage, &1234, &1234)?; let key = [json_keymap.as_slice(), INDEXES, &0_u32.to_be_bytes()].concat(); if json_keymap.page_size == 1 {