Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
match prefix to next_key
Browse files Browse the repository at this point in the history
Signed-off-by: muraca <mmuraca247@gmail.com>
  • Loading branch information
muraca committed Jan 24, 2023
1 parent b26116e commit 7a08a6a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions frame/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,9 @@ mod test {
#[crate::storage_alias]
type FooDoubleMap =
StorageDoubleMap<Prefix, Twox128, u32, Twox128, u32, BoundedVec<u32, ConstU32<7>>>;
#[crate::storage_alias]
type FooDoubleMapWithEmptyArrays =
StorageDoubleMap<Prefix, Identity, [u8; 0], Identity, [u8; 0], [u8; 0]>;

#[test]
fn contains_prefix_works() {
Expand All @@ -1792,10 +1795,17 @@ mod test {
assert_eq!(FooDoubleMap::contains_prefix(0), false);

assert_ok!(FooDoubleMap::try_append(1, 1, 4));
assert_ok!(FooDoubleMap::try_append(2, 1, 4));
assert!(FooDoubleMap::iter_prefix_values(1).next().is_some());
assert!(FooDoubleMap::contains_prefix(1));
FooDoubleMap::remove(1, 1);
assert_eq!(FooDoubleMap::contains_prefix(1), false);

assert_eq!(FooDoubleMapWithEmptyArrays::contains_prefix::<[u8; 0]>([]), false);
FooDoubleMapWithEmptyArrays::insert::<[u8; 0], [u8; 1], [u8; 0]>([], [2], []);
assert!(FooDoubleMapWithEmptyArrays::contains_prefix::<[u8; 0]>([]));
FooDoubleMapWithEmptyArrays::remove::<[u8; 0], [u8; 1]>([], [2]);
assert_eq!(FooDoubleMapWithEmptyArrays::contains_prefix::<[u8; 0]>([]), false);
});
}

Expand Down
5 changes: 4 additions & 1 deletion frame/support/src/storage/unhashed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ pub fn clear_prefix(
}

pub fn contains_prefix(prefix: &[u8]) -> bool {
sp_io::storage::next_key(prefix).is_some()
match sp_io::storage::next_key(prefix) {
Some(key) => key.starts_with(prefix),
None => false,
}
}

/// Get a Vec of bytes from storage.
Expand Down

0 comments on commit 7a08a6a

Please sign in to comment.