implemented contains_prefix for StorageDoubleMap and StorageNMap (#13232)

* implemented `contains_prefix` for StorageDoubleMap and StorageNMap

Signed-off-by: muraca <mmuraca247@gmail.com>

* match prefix to next_key

Signed-off-by: muraca <mmuraca247@gmail.com>

* warning unexpected behaviour with empty keys

Signed-off-by: muraca <mmuraca247@gmail.com>

* clarifications for unhashed::contains_prefixed_key

Signed-off-by: muraca <mmuraca247@gmail.com>

* added tests for StorageNMap

Signed-off-by: muraca <mmuraca247@gmail.com>

---------

Signed-off-by: muraca <mmuraca247@gmail.com>
This commit is contained in:
Matteo Muraca
2023-02-01 18:11:34 +01:00
committed by GitHub
parent 320a9ce3c9
commit 70a2290251
4 changed files with 70 additions and 1 deletions
@@ -154,6 +154,16 @@ pub fn clear_prefix(
MultiRemovalResults { maybe_cursor, backend: i, unique: i, loops: i }
}
/// Returns `true` if the storage contains any key, which starts with a certain prefix,
/// and is longer than said prefix.
/// This means that a key which equals the prefix will not be counted.
pub fn contains_prefixed_key(prefix: &[u8]) -> bool {
match sp_io::storage::next_key(prefix) {
Some(key) => key.starts_with(prefix),
None => false,
}
}
/// Get a Vec of bytes from storage.
pub fn get_raw(key: &[u8]) -> Option<Vec<u8>> {
sp_io::storage::get(key).map(|value| value.to_vec())