CountedNMap implementation (#10621)

* add initial CountedDoubleMap implementation

* extend CountedDoubleMap functionality

* add some traits implementation for CountedStorageDoubleMap

* add basic tests for CountedStorageDoubleMap

* add mutate functions implementation

* add additional tests

* add test_option_query test

* add try_append_decode_len_works, append_decode_len_works tests

* add migrate_keys_works, translate_values tests

* add test_iter_drain_translate test

* add test_metadata test

* add remove_prefix implementation,  add test_iter_drain_prefix test

* update

* refactor PrefixIterator usage

* Fix CI build

* fix storage_ensure_span_are_ok_wrong_gen.rs storage_ensure_span_are_ok_wrong_gen_unnamed.rs

* add counted_nmap implementation

* add tests, fixes

* remove counted double map impl

* fix metadata checks

* update clear func

* fix clear, clear with prefix

* fix set function

* update

* final fix

* Update frame/support/src/storage/types/counted_nmap.rs

Co-authored-by: Anton <anton.kalyaev@gmail.com>

* Update frame/support/src/storage/types/counted_nmap.rs

Co-authored-by: Anton <anton.kalyaev@gmail.com>

* Update frame/support/src/storage/types/counted_nmap.rs

Co-authored-by: Anton <anton.kalyaev@gmail.com>

* fix comments

* fix suggestion

* cargo update

* Relocate impl of Sealed for Ref to module root

* fix StorageEntryMetadata type

* Update frame/support/src/storage/types/nmap.rs

Co-authored-by: Guillaume Yu Thiolliere <gui.thiolliere@gmail.com>

* removed StorageNMap and StoragePrefixedMap traits impl

* fix tests

* Update frame/support/src/storage/types/counted_nmap.rs

Co-authored-by: Guillaume Yu Thiolliere <gui.thiolliere@gmail.com>

* extend pallet::storage macro with CountedStorageNMap usage

* fix

* add tests

* fix

* fix

* Add counter_storage_final_key(), map_storage_final_prefix() functions

* update tests

* fix

* fix

* fix

* update tests

* fix fmt

* fix fmt

---------

Co-authored-by: Anton <anton.kalyaev@gmail.com>
Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
Co-authored-by: Guillaume Yu Thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: parity-processbot <>
This commit is contained in:
Alex Pozhylenkov
2023-08-04 18:06:08 +03:00
committed by GitHub
parent 08f680e281
commit ef0238dd29
11 changed files with 1840 additions and 59 deletions
@@ -1428,6 +1428,7 @@ mod private {
impl<K, V, S> Sealed for bounded_btree_map::BoundedBTreeMap<K, V, S> {}
impl<T, S> Sealed for bounded_btree_set::BoundedBTreeSet<T, S> {}
impl<T: Encode> Sealed for BTreeSet<T> {}
impl<'a, T: EncodeLike<U>, U: Encode> Sealed for codec::Ref<'a, T, U> {}
macro_rules! impl_sealed_for_tuple {
($($elem:ident),+) => {
@@ -612,8 +612,9 @@ mod test {
assert_eq!(A::count(), 2);
// Insert an existing key, shouldn't increment counted values.
A::insert(3, 11);
A::insert(3, 12);
assert_eq!(A::try_get(3), Ok(12));
assert_eq!(A::count(), 2);
// Remove non-existing.
@@ -706,17 +707,17 @@ mod test {
// Try succeed mutate existing to existing.
A::try_mutate_exists(1, |query| {
assert_eq!(*query, Some(43));
*query = Some(43);
*query = Some(45);
Result::<(), ()>::Ok(())
})
.unwrap();
assert_eq!(A::try_get(1), Ok(43));
assert_eq!(A::try_get(1), Ok(45));
assert_eq!(A::count(), 4);
// Try succeed mutate existing to non-existing.
A::try_mutate_exists(1, |query| {
assert_eq!(*query, Some(43));
assert_eq!(*query, Some(45));
*query = None;
Result::<(), ()>::Ok(())
})
File diff suppressed because it is too large Load Diff
@@ -37,7 +37,7 @@ pub struct Key<Hasher, KeyType>(core::marker::PhantomData<(Hasher, KeyType)>);
/// A trait that contains the current key as an associated type.
pub trait KeyGenerator {
type Key: EncodeLike<Self::Key> + StaticTypeInfo;
type KArg: Encode;
type KArg: Encode + EncodeLike<Self::KArg>;
type HashFn: FnOnce(&[u8]) -> Vec<u8>;
type HArg;
@@ -196,6 +196,11 @@ impl_encode_like_tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, O, P);
impl_encode_like_tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, O, P, Q);
impl_encode_like_tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, O, P, Q, R);
impl<'a, T: EncodeLike<U> + EncodeLikeTuple<U>, U: Encode> EncodeLikeTuple<U>
for codec::Ref<'a, T, U>
{
}
/// Trait to indicate that a tuple can be converted into an iterator of a vector of encoded bytes.
pub trait TupleToEncodedIter {
fn to_encoded_iter(&self) -> sp_std::vec::IntoIter<Vec<u8>>;
@@ -215,6 +220,15 @@ impl<T: TupleToEncodedIter> TupleToEncodedIter for &T {
}
}
impl<'a, T: EncodeLike<U> + TupleToEncodedIter, U: Encode> TupleToEncodedIter
for codec::Ref<'a, T, U>
{
fn to_encoded_iter(&self) -> sp_std::vec::IntoIter<Vec<u8>> {
use core::ops::Deref as _;
self.deref().to_encoded_iter()
}
}
/// A trait that indicates the hashers for the keys generated are all reversible.
pub trait ReversibleKeyGenerator: KeyGenerator {
type ReversibleHasher;
@@ -23,6 +23,7 @@ use codec::FullCodec;
use sp_std::prelude::*;
mod counted_map;
mod counted_nmap;
mod double_map;
mod key;
mod map;
@@ -30,6 +31,7 @@ mod nmap;
mod value;
pub use counted_map::{CountedStorageMap, CountedStorageMapInstance};
pub use counted_nmap::{CountedStorageNMap, CountedStorageNMapInstance};
pub use double_map::StorageDoubleMap;
pub use key::{
EncodeLikeTuple, HasKeyPrefix, HasReversibleKeyPrefix, Key, KeyGenerator,
@@ -15,8 +15,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! Storage map type. Implements StorageDoubleMap, StorageIterableDoubleMap,
//! StoragePrefixedDoubleMap traits and their methods directly.
//! Storage n-map type. Particularly implements `StorageNMap` and `StoragePrefixedMap`
//! traits and their methods directly.
use crate::{
metadata_ir::{StorageEntryMetadataIR, StorageEntryTypeIR},