Skip to content

Commit

Permalink
implement storage decode length for BTreeSet (paritytech#12503)
Browse files Browse the repository at this point in the history
* implement storage decode length for BTreeSet

* Orderly moving of things around

* include test for append and decode_len

* fix cargo clippy issue
  • Loading branch information
dharjeezy authored and ark0f committed Feb 27, 2023
1 parent e71cf5b commit 7a02c94
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion frame/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
use codec::{Decode, Encode, EncodeLike, FullCodec, FullEncode};
use sp_core::storage::ChildInfo;
use sp_runtime::generic::{Digest, DigestItem};
use sp_std::{marker::PhantomData, prelude::*};
use sp_std::{collections::btree_set::BTreeSet, marker::PhantomData, prelude::*};

pub use self::{
transactional::{
Expand Down Expand Up @@ -1303,6 +1303,7 @@ mod private {
impl<T, S> Sealed for WeakBoundedVec<T, S> {}
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> {}

macro_rules! impl_sealed_for_tuple {
($($elem:ident),+) => {
Expand Down Expand Up @@ -1335,6 +1336,9 @@ mod private {
impl<T: Encode> StorageAppend<T> for Vec<T> {}
impl<T: Encode> StorageDecodeLength for Vec<T> {}

impl<T: Encode> StorageAppend<T> for BTreeSet<T> {}
impl<T: Encode> StorageDecodeLength for BTreeSet<T> {}

/// We abuse the fact that SCALE does not put any marker into the encoding, i.e. we only encode the
/// internal vec and we can append to this vec. We have a test that ensures that if the `Digest`
/// format ever changes, we need to remove this here.
Expand Down Expand Up @@ -1832,4 +1836,22 @@ mod test {
);
});
}

#[crate::storage_alias]
type FooSet = StorageValue<Prefix, BTreeSet<u32>>;

#[test]
fn btree_set_append_and_decode_len_works() {
TestExternalities::default().execute_with(|| {
let btree = BTreeSet::from([1, 2, 3]);
FooSet::put(btree);

FooSet::append(4);
FooSet::append(5);
FooSet::append(6);
FooSet::append(7);

assert_eq!(FooSet::decode_len().unwrap(), 7);
});
}
}

0 comments on commit 7a02c94

Please sign in to comment.