mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 08:07:58 +00:00
Introduce WeakBoundedVec, StorageTryAppend, and improve BoundedVec API (#8842)
* fix bounded vec doc and unsafe * fix btree map and set and tests * introduce weak_bounded_vec and StorageTryAppend * fix tests and reorganize tests * improve doc * add doc * Update frame/support/src/storage/weak_bounded_vec.rs Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> * fix inner doc Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
a183031eef
commit
25625a0f0c
@@ -32,11 +32,29 @@ use codec::{Encode, Decode};
|
||||
/// B-Trees represent a fundamental compromise between cache-efficiency and actually minimizing
|
||||
/// the amount of work performed in a search. See [`BTreeMap`] for more details.
|
||||
///
|
||||
/// Unlike a standard `BTreeMap`, there is a static, enforced upper limit to the number of items
|
||||
/// in the map. All internal operations ensure this bound is respected.
|
||||
#[derive(Encode, Decode)]
|
||||
/// Unlike a standard `BTreeMap`, there is an enforced upper limit to the number of items in the
|
||||
/// map. All internal operations ensure this bound is respected.
|
||||
#[derive(Encode)]
|
||||
pub struct BoundedBTreeMap<K, V, S>(BTreeMap<K, V>, PhantomData<S>);
|
||||
|
||||
impl<K, V, S> Decode for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
BTreeMap<K, V>: Decode,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||
let inner = BTreeMap::<K, V>::decode(input)?;
|
||||
if inner.len() > S::get() as usize {
|
||||
return Err("BoundedBTreeMap exceeds its limit".into());
|
||||
}
|
||||
Ok(Self(inner, PhantomData))
|
||||
}
|
||||
|
||||
fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
|
||||
BTreeMap::<K, V>::skip(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
S: Get<u32>,
|
||||
@@ -59,44 +77,6 @@ where
|
||||
BoundedBTreeMap(BTreeMap::new(), PhantomData)
|
||||
}
|
||||
|
||||
/// Create `Self` from a primitive `BTreeMap` without any checks.
|
||||
unsafe fn unchecked_from(map: BTreeMap<K, V>) -> Self {
|
||||
Self(map, Default::default())
|
||||
}
|
||||
|
||||
/// Create `Self` from a primitive `BTreeMap` without any checks.
|
||||
///
|
||||
/// Logs warnings if the bound is not being respected. The scope is mentioned in the log message
|
||||
/// to indicate where overflow is happening.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use sp_std::collections::btree_map::BTreeMap;
|
||||
/// # use frame_support::{parameter_types, storage::bounded_btree_map::BoundedBTreeMap};
|
||||
/// parameter_types! {
|
||||
/// pub const Size: u32 = 5;
|
||||
/// }
|
||||
/// let mut map = BTreeMap::new();
|
||||
/// map.insert("foo", 1);
|
||||
/// map.insert("bar", 2);
|
||||
/// let bounded_map = unsafe {BoundedBTreeMap::<_, _, Size>::force_from(map, "demo")};
|
||||
/// ```
|
||||
pub unsafe fn force_from<Scope>(map: BTreeMap<K, V>, scope: Scope) -> Self
|
||||
where
|
||||
Scope: Into<Option<&'static str>>,
|
||||
{
|
||||
if map.len() > Self::bound() {
|
||||
log::warn!(
|
||||
target: crate::LOG_TARGET,
|
||||
"length of a bounded btreemap in scope {} is not respected.",
|
||||
scope.into().unwrap_or("UNKNOWN"),
|
||||
);
|
||||
}
|
||||
|
||||
Self::unchecked_from(map)
|
||||
}
|
||||
|
||||
/// Consume self, and return the inner `BTreeMap`.
|
||||
///
|
||||
/// This is useful when a mutating API of the inner type is desired, and closure-based mutation
|
||||
@@ -418,4 +398,13 @@ pub mod test {
|
||||
let bounded = boundedmap_from_keys::<u32, Seven>(&[1, 2, 3, 4, 5, 6]);
|
||||
assert_eq!(bounded, map_from_keys(&[1, 2, 3, 4, 5, 6]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_big_fail_to_decode() {
|
||||
let v: Vec<(u32, u32)> = vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)];
|
||||
assert_eq!(
|
||||
BoundedBTreeMap::<u32, u32, Four>::decode(&mut &v.encode()[..]),
|
||||
Err("BoundedBTreeMap exceeds its limit".into()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,11 +32,29 @@ use codec::{Encode, Decode};
|
||||
/// B-Trees represent a fundamental compromise between cache-efficiency and actually minimizing
|
||||
/// the amount of work performed in a search. See [`BTreeSet`] for more details.
|
||||
///
|
||||
/// Unlike a standard `BTreeSet`, there is a static, enforced upper limit to the number of items
|
||||
/// in the set. All internal operations ensure this bound is respected.
|
||||
#[derive(Encode, Decode)]
|
||||
/// Unlike a standard `BTreeSet`, there is an enforced upper limit to the number of items in the
|
||||
/// set. All internal operations ensure this bound is respected.
|
||||
#[derive(Encode)]
|
||||
pub struct BoundedBTreeSet<T, S>(BTreeSet<T>, PhantomData<S>);
|
||||
|
||||
impl<T, S> Decode for BoundedBTreeSet<T, S>
|
||||
where
|
||||
BTreeSet<T>: Decode,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||
let inner = BTreeSet::<T>::decode(input)?;
|
||||
if inner.len() > S::get() as usize {
|
||||
return Err("BoundedBTreeSet exceeds its limit".into());
|
||||
}
|
||||
Ok(Self(inner, PhantomData))
|
||||
}
|
||||
|
||||
fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
|
||||
BTreeSet::<T>::skip(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> BoundedBTreeSet<T, S>
|
||||
where
|
||||
S: Get<u32>,
|
||||
@@ -59,44 +77,6 @@ where
|
||||
BoundedBTreeSet(BTreeSet::new(), PhantomData)
|
||||
}
|
||||
|
||||
/// Create `Self` from a primitive `BTreeSet` without any checks.
|
||||
unsafe fn unchecked_from(set: BTreeSet<T>) -> Self {
|
||||
Self(set, Default::default())
|
||||
}
|
||||
|
||||
/// Create `Self` from a primitive `BTreeSet` without any checks.
|
||||
///
|
||||
/// Logs warnings if the bound is not being respected. The scope is mentioned in the log message
|
||||
/// to indicate where overflow is happening.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use sp_std::collections::btree_set::BTreeSet;
|
||||
/// # use frame_support::{parameter_types, storage::bounded_btree_set::BoundedBTreeSet};
|
||||
/// parameter_types! {
|
||||
/// pub const Size: u32 = 5;
|
||||
/// }
|
||||
/// let mut set = BTreeSet::new();
|
||||
/// set.insert("foo");
|
||||
/// set.insert("bar");
|
||||
/// let bounded_set = unsafe {BoundedBTreeSet::<_, Size>::force_from(set, "demo")};
|
||||
/// ```
|
||||
pub unsafe fn force_from<Scope>(set: BTreeSet<T>, scope: Scope) -> Self
|
||||
where
|
||||
Scope: Into<Option<&'static str>>,
|
||||
{
|
||||
if set.len() > Self::bound() {
|
||||
log::warn!(
|
||||
target: crate::LOG_TARGET,
|
||||
"length of a bounded btreeset in scope {} is not respected.",
|
||||
scope.into().unwrap_or("UNKNOWN"),
|
||||
);
|
||||
}
|
||||
|
||||
Self::unchecked_from(set)
|
||||
}
|
||||
|
||||
/// Consume self, and return the inner `BTreeSet`.
|
||||
///
|
||||
/// This is useful when a mutating API of the inner type is desired, and closure-based mutation
|
||||
@@ -404,4 +384,13 @@ pub mod test {
|
||||
let bounded = boundedmap_from_keys::<u32, Seven>(&[1, 2, 3, 4, 5, 6]);
|
||||
assert_eq!(bounded, map_from_keys(&[1, 2, 3, 4, 5, 6]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_big_fail_to_decode() {
|
||||
let v: Vec<u32> = vec![1, 2, 3, 4, 5];
|
||||
assert_eq!(
|
||||
BoundedBTreeSet::<u32, Four>::decode(&mut &v.encode()[..]),
|
||||
Err("BoundedBTreeSet exceeds its limit".into()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,14 @@
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use sp_std::{convert::TryFrom, fmt, marker::PhantomData};
|
||||
use codec::{FullCodec, Encode, EncodeLike, Decode};
|
||||
use codec::{Encode, Decode};
|
||||
use core::{
|
||||
ops::{Deref, Index, IndexMut},
|
||||
slice::SliceIndex,
|
||||
};
|
||||
use crate::{
|
||||
traits::{Get, MaxEncodedLen},
|
||||
storage::{generator, StorageDecodeLength, StorageValue, StorageMap, StorageDoubleMap},
|
||||
storage::{StorageDecodeLength, StorageTryAppend},
|
||||
};
|
||||
|
||||
/// A bounded vector.
|
||||
@@ -37,12 +37,26 @@ use crate::{
|
||||
///
|
||||
/// As the name suggests, the length of the queue is always bounded. All internal operations ensure
|
||||
/// this bound is respected.
|
||||
#[derive(Encode, Decode)]
|
||||
#[derive(Encode)]
|
||||
pub struct BoundedVec<T, S>(Vec<T>, PhantomData<S>);
|
||||
|
||||
impl<T: Decode, S: Get<u32>> Decode for BoundedVec<T, S> {
|
||||
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||
let inner = Vec::<T>::decode(input)?;
|
||||
if inner.len() > S::get() as usize {
|
||||
return Err("BoundedVec exceeds its limit".into());
|
||||
}
|
||||
Ok(Self(inner, PhantomData))
|
||||
}
|
||||
|
||||
fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
|
||||
Vec::<T>::skip(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> BoundedVec<T, S> {
|
||||
/// Create `Self` from `t` without any checks.
|
||||
unsafe fn unchecked_from(t: Vec<T>) -> Self {
|
||||
fn unchecked_from(t: Vec<T>) -> Self {
|
||||
Self(t, Default::default())
|
||||
}
|
||||
|
||||
@@ -86,21 +100,6 @@ impl<T, S: Get<u32>> BoundedVec<T, S> {
|
||||
S::get() as usize
|
||||
}
|
||||
|
||||
/// Create `Self` from `t` without any checks. Logs warnings if the bound is not being
|
||||
/// respected. The additional scope can be used to indicate where a potential overflow is
|
||||
/// happening.
|
||||
pub unsafe fn force_from(t: Vec<T>, scope: Option<&'static str>) -> Self {
|
||||
if t.len() > Self::bound() {
|
||||
log::warn!(
|
||||
target: crate::LOG_TARGET,
|
||||
"length of a bounded vector in scope {} is not respected.",
|
||||
scope.unwrap_or("UNKNOWN"),
|
||||
);
|
||||
}
|
||||
|
||||
Self::unchecked_from(t)
|
||||
}
|
||||
|
||||
/// Consumes self and mutates self via the given `mutate` function.
|
||||
///
|
||||
/// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is
|
||||
@@ -147,7 +146,7 @@ impl<T, S: Get<u32>> BoundedVec<T, S> {
|
||||
impl<T, S> Default for BoundedVec<T, S> {
|
||||
fn default() -> Self {
|
||||
// the bound cannot be below 0, which is satisfied by an empty vector
|
||||
unsafe { Self::unchecked_from(Vec::default()) }
|
||||
Self::unchecked_from(Vec::default())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,7 +167,7 @@ where
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
// bound is retained
|
||||
unsafe { Self::unchecked_from(self.0.clone()) }
|
||||
Self::unchecked_from(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +176,7 @@ impl<T, S: Get<u32>> TryFrom<Vec<T>> for BoundedVec<T, S> {
|
||||
fn try_from(t: Vec<T>) -> Result<Self, Self::Error> {
|
||||
if t.len() <= Self::bound() {
|
||||
// explicit check just above
|
||||
Ok(unsafe { Self::unchecked_from(t) })
|
||||
Ok(Self::unchecked_from(t))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
@@ -273,114 +272,9 @@ impl<T, S> Eq for BoundedVec<T, S> where T: Eq {}
|
||||
|
||||
impl<T, S> StorageDecodeLength for BoundedVec<T, S> {}
|
||||
|
||||
/// Storage value that is *maybe* capable of [`StorageAppend`](crate::storage::StorageAppend).
|
||||
pub trait TryAppendValue<T: Encode, S: Get<u32>> {
|
||||
/// Try and append the `item` into the storage item.
|
||||
///
|
||||
/// This might fail if bounds are not respected.
|
||||
fn try_append<LikeT: EncodeLike<T>>(item: LikeT) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
/// Storage map that is *maybe* capable of [`StorageAppend`](crate::storage::StorageAppend).
|
||||
pub trait TryAppendMap<K: FullCodec, T: Encode, S: Get<u32>> {
|
||||
/// Try and append the `item` into the storage map at the given `key`.
|
||||
///
|
||||
/// This might fail if bounds are not respected.
|
||||
fn try_append<LikeK: EncodeLike<K> + Clone, LikeT: EncodeLike<T>>(
|
||||
key: LikeK,
|
||||
item: LikeT,
|
||||
) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
/// Storage double map that is *maybe* capable of [`StorageAppend`](crate::storage::StorageAppend).
|
||||
pub trait TryAppendDoubleMap<K1: FullCodec, K2: FullCodec, T: Encode, S: Get<u32>> {
|
||||
/// Try and append the `item` into the storage double map at the given `key`.
|
||||
///
|
||||
/// This might fail if bounds are not respected.
|
||||
fn try_append<
|
||||
LikeK1: EncodeLike<K1> + Clone,
|
||||
LikeK2: EncodeLike<K2> + Clone,
|
||||
LikeT: EncodeLike<T>,
|
||||
>(
|
||||
key1: LikeK1,
|
||||
key2: LikeK2,
|
||||
item: LikeT,
|
||||
) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
impl<T, S, StorageValueT> TryAppendValue<T, S> for StorageValueT
|
||||
where
|
||||
BoundedVec<T, S>: FullCodec,
|
||||
T: Encode,
|
||||
S: Get<u32>,
|
||||
StorageValueT: generator::StorageValue<BoundedVec<T, S>>,
|
||||
{
|
||||
fn try_append<LikeT: EncodeLike<T>>(item: LikeT) -> Result<(), ()> {
|
||||
let bound = BoundedVec::<T, S>::bound();
|
||||
let current = Self::decode_len().unwrap_or_default();
|
||||
if current < bound {
|
||||
// NOTE: we cannot reuse the implementation for `Vec<T>` here because we never want to
|
||||
// mark `BoundedVec<T, S>` as `StorageAppend`.
|
||||
let key = Self::storage_value_final_key();
|
||||
sp_io::storage::append(&key, item.encode());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, T, S, StorageMapT> TryAppendMap<K, T, S> for StorageMapT
|
||||
where
|
||||
K: FullCodec,
|
||||
BoundedVec<T, S>: FullCodec,
|
||||
T: Encode,
|
||||
S: Get<u32>,
|
||||
StorageMapT: generator::StorageMap<K, BoundedVec<T, S>>,
|
||||
{
|
||||
fn try_append<LikeK: EncodeLike<K> + Clone, LikeT: EncodeLike<T>>(
|
||||
key: LikeK,
|
||||
item: LikeT,
|
||||
) -> Result<(), ()> {
|
||||
let bound = BoundedVec::<T, S>::bound();
|
||||
let current = Self::decode_len(key.clone()).unwrap_or_default();
|
||||
if current < bound {
|
||||
let key = Self::storage_map_final_key(key);
|
||||
sp_io::storage::append(&key, item.encode());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K1, K2, T, S, StorageDoubleMapT> TryAppendDoubleMap<K1, K2, T, S> for StorageDoubleMapT
|
||||
where
|
||||
K1: FullCodec,
|
||||
K2: FullCodec,
|
||||
BoundedVec<T, S>: FullCodec,
|
||||
T: Encode,
|
||||
S: Get<u32>,
|
||||
StorageDoubleMapT: generator::StorageDoubleMap<K1, K2, BoundedVec<T, S>>,
|
||||
{
|
||||
fn try_append<
|
||||
LikeK1: EncodeLike<K1> + Clone,
|
||||
LikeK2: EncodeLike<K2> + Clone,
|
||||
LikeT: EncodeLike<T>,
|
||||
>(
|
||||
key1: LikeK1,
|
||||
key2: LikeK2,
|
||||
item: LikeT,
|
||||
) -> Result<(), ()> {
|
||||
let bound = BoundedVec::<T, S>::bound();
|
||||
let current = Self::decode_len(key1.clone(), key2.clone()).unwrap_or_default();
|
||||
if current < bound {
|
||||
let double_map_key = Self::storage_double_map_final_key(key1, key2);
|
||||
sp_io::storage::append(&double_map_key, item.encode());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
impl<T, S: Get<u32>> StorageTryAppend<T> for BoundedVec<T, S> {
|
||||
fn bound() -> usize {
|
||||
S::get() as usize
|
||||
}
|
||||
}
|
||||
|
||||
@@ -405,7 +299,7 @@ pub mod test {
|
||||
use super::*;
|
||||
use sp_io::TestExternalities;
|
||||
use sp_std::convert::TryInto;
|
||||
use crate::{assert_ok, Twox128};
|
||||
use crate::Twox128;
|
||||
|
||||
crate::parameter_types! {
|
||||
pub const Seven: u32 = 7;
|
||||
@@ -419,6 +313,11 @@ pub mod test {
|
||||
FooDoubleMap => DoubleMap<(u32, Twox128), (u32, Twox128), BoundedVec<u32, Seven>>
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_append_is_correct() {
|
||||
assert_eq!(BoundedVec::<u32, Seven>::bound(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_len_works() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
@@ -445,66 +344,6 @@ pub mod test {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_append_works() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: BoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
Foo::put(bounded);
|
||||
assert_ok!(Foo::try_append(4));
|
||||
assert_ok!(Foo::try_append(5));
|
||||
assert_ok!(Foo::try_append(6));
|
||||
assert_ok!(Foo::try_append(7));
|
||||
assert_eq!(Foo::decode_len().unwrap(), 7);
|
||||
assert!(Foo::try_append(8).is_err());
|
||||
});
|
||||
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: BoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
FooMap::insert(1, bounded);
|
||||
|
||||
assert_ok!(FooMap::try_append(1, 4));
|
||||
assert_ok!(FooMap::try_append(1, 5));
|
||||
assert_ok!(FooMap::try_append(1, 6));
|
||||
assert_ok!(FooMap::try_append(1, 7));
|
||||
assert_eq!(FooMap::decode_len(1).unwrap(), 7);
|
||||
assert!(FooMap::try_append(1, 8).is_err());
|
||||
|
||||
// append to a non-existing
|
||||
assert!(FooMap::get(2).is_none());
|
||||
assert_ok!(FooMap::try_append(2, 4));
|
||||
assert_eq!(FooMap::get(2).unwrap(), unsafe {
|
||||
BoundedVec::<u32, Seven>::unchecked_from(vec![4])
|
||||
});
|
||||
assert_ok!(FooMap::try_append(2, 5));
|
||||
assert_eq!(FooMap::get(2).unwrap(), unsafe {
|
||||
BoundedVec::<u32, Seven>::unchecked_from(vec![4, 5])
|
||||
});
|
||||
});
|
||||
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: BoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
FooDoubleMap::insert(1, 1, bounded);
|
||||
|
||||
assert_ok!(FooDoubleMap::try_append(1, 1, 4));
|
||||
assert_ok!(FooDoubleMap::try_append(1, 1, 5));
|
||||
assert_ok!(FooDoubleMap::try_append(1, 1, 6));
|
||||
assert_ok!(FooDoubleMap::try_append(1, 1, 7));
|
||||
assert_eq!(FooDoubleMap::decode_len(1, 1).unwrap(), 7);
|
||||
assert!(FooDoubleMap::try_append(1, 1, 8).is_err());
|
||||
|
||||
// append to a non-existing
|
||||
assert!(FooDoubleMap::get(2, 1).is_none());
|
||||
assert_ok!(FooDoubleMap::try_append(2, 1, 4));
|
||||
assert_eq!(FooDoubleMap::get(2, 1).unwrap(), unsafe {
|
||||
BoundedVec::<u32, Seven>::unchecked_from(vec![4])
|
||||
});
|
||||
assert_ok!(FooDoubleMap::try_append(2, 1, 5));
|
||||
assert_eq!(FooDoubleMap::get(2, 1).unwrap(), unsafe {
|
||||
BoundedVec::<u32, Seven>::unchecked_from(vec![4, 5])
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_insert_works() {
|
||||
let mut bounded: BoundedVec<u32, Four> = vec![1, 2, 3].try_into().unwrap();
|
||||
@@ -559,4 +398,13 @@ pub mod test {
|
||||
let bounded: BoundedVec<u32, Seven> = vec![1, 2, 3, 4, 5, 6].try_into().unwrap();
|
||||
assert_eq!(bounded, vec![1, 2, 3, 4, 5, 6]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_big_vec_fail_to_decode() {
|
||||
let v: Vec<u32> = vec![1, 2, 3, 4, 5];
|
||||
assert_eq!(
|
||||
BoundedVec::<u32, Four>::decode(&mut &v.encode()[..]),
|
||||
Err("BoundedVec exceeds its limit".into()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ pub mod hashed;
|
||||
pub mod bounded_btree_map;
|
||||
pub mod bounded_btree_set;
|
||||
pub mod bounded_vec;
|
||||
pub mod weak_bounded_vec;
|
||||
pub mod child;
|
||||
#[doc(hidden)]
|
||||
pub mod generator;
|
||||
@@ -965,12 +966,14 @@ pub trait StorageDecodeLength: private::Sealed + codec::DecodeLength {
|
||||
mod private {
|
||||
use super::*;
|
||||
use bounded_vec::BoundedVec;
|
||||
use weak_bounded_vec::WeakBoundedVec;
|
||||
|
||||
pub trait Sealed {}
|
||||
|
||||
impl<T: Encode> Sealed for Vec<T> {}
|
||||
impl<Hash: Encode> Sealed for Digest<Hash> {}
|
||||
impl<T, S> Sealed for BoundedVec<T, S> {}
|
||||
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> {}
|
||||
|
||||
@@ -1010,13 +1013,132 @@ impl<T: Encode> StorageDecodeLength for Vec<T> {}
|
||||
/// format ever changes, we need to remove this here.
|
||||
impl<Hash: Encode> StorageAppend<DigestItem<Hash>> for Digest<Hash> {}
|
||||
|
||||
/// Marker trait that is implemented for types that support the `storage::append` api with a limit
|
||||
/// on the number of element.
|
||||
///
|
||||
/// This trait is sealed.
|
||||
pub trait StorageTryAppend<Item>: StorageDecodeLength + private::Sealed {
|
||||
fn bound() -> usize;
|
||||
}
|
||||
|
||||
/// Storage value that is capable of [`StorageTryAppend`](crate::storage::StorageTryAppend).
|
||||
pub trait TryAppendValue<T: StorageTryAppend<I>, I: Encode> {
|
||||
/// Try and append the `item` into the storage item.
|
||||
///
|
||||
/// This might fail if bounds are not respected.
|
||||
fn try_append<LikeI: EncodeLike<I>>(item: LikeI) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
impl<T, I, StorageValueT> TryAppendValue<T, I> for StorageValueT
|
||||
where
|
||||
I: Encode,
|
||||
T: FullCodec + StorageTryAppend<I>,
|
||||
StorageValueT: generator::StorageValue<T>,
|
||||
{
|
||||
fn try_append<LikeI: EncodeLike<I>>(item: LikeI) -> Result<(), ()> {
|
||||
let bound = T::bound();
|
||||
let current = Self::decode_len().unwrap_or_default();
|
||||
if current < bound {
|
||||
// NOTE: we cannot reuse the implementation for `Vec<T>` here because we never want to
|
||||
// mark `BoundedVec<T, S>` as `StorageAppend`.
|
||||
let key = Self::storage_value_final_key();
|
||||
sp_io::storage::append(&key, item.encode());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Storage map that is capable of [`StorageTryAppend`](crate::storage::StorageTryAppend).
|
||||
pub trait TryAppendMap<K: Encode, T: StorageTryAppend<I>, I: Encode> {
|
||||
/// Try and append the `item` into the storage map at the given `key`.
|
||||
///
|
||||
/// This might fail if bounds are not respected.
|
||||
fn try_append<LikeK: EncodeLike<K> + Clone, LikeI: EncodeLike<I>>(
|
||||
key: LikeK,
|
||||
item: LikeI,
|
||||
) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
impl<K, T, I, StorageMapT> TryAppendMap<K, T, I> for StorageMapT
|
||||
where
|
||||
K: FullCodec,
|
||||
T: FullCodec + StorageTryAppend<I>,
|
||||
I: Encode,
|
||||
StorageMapT: generator::StorageMap<K, T>,
|
||||
{
|
||||
fn try_append<LikeK: EncodeLike<K> + Clone, LikeI: EncodeLike<I>>(
|
||||
key: LikeK,
|
||||
item: LikeI,
|
||||
) -> Result<(), ()> {
|
||||
let bound = T::bound();
|
||||
let current = Self::decode_len(key.clone()).unwrap_or_default();
|
||||
if current < bound {
|
||||
let key = Self::storage_map_final_key(key);
|
||||
sp_io::storage::append(&key, item.encode());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Storage double map that is capable of [`StorageTryAppend`](crate::storage::StorageTryAppend).
|
||||
pub trait TryAppendDoubleMap<K1: Encode, K2: Encode, T: StorageTryAppend<I>, I: Encode> {
|
||||
/// Try and append the `item` into the storage double map at the given `key`.
|
||||
///
|
||||
/// This might fail if bounds are not respected.
|
||||
fn try_append<
|
||||
LikeK1: EncodeLike<K1> + Clone,
|
||||
LikeK2: EncodeLike<K2> + Clone,
|
||||
LikeI: EncodeLike<I>,
|
||||
>(
|
||||
key1: LikeK1,
|
||||
key2: LikeK2,
|
||||
item: LikeI,
|
||||
) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
impl<K1, K2, T, I, StorageDoubleMapT> TryAppendDoubleMap<K1, K2, T, I> for StorageDoubleMapT
|
||||
where
|
||||
K1: FullCodec,
|
||||
K2: FullCodec,
|
||||
T: FullCodec + StorageTryAppend<I>,
|
||||
I: Encode,
|
||||
StorageDoubleMapT: generator::StorageDoubleMap<K1, K2, T>,
|
||||
{
|
||||
fn try_append<
|
||||
LikeK1: EncodeLike<K1> + Clone,
|
||||
LikeK2: EncodeLike<K2> + Clone,
|
||||
LikeI: EncodeLike<I>,
|
||||
>(
|
||||
key1: LikeK1,
|
||||
key2: LikeK2,
|
||||
item: LikeI,
|
||||
) -> Result<(), ()> {
|
||||
let bound = T::bound();
|
||||
let current = Self::decode_len(key1.clone(), key2.clone()).unwrap_or_default();
|
||||
if current < bound {
|
||||
let double_map_key = Self::storage_double_map_final_key(key1, key2);
|
||||
sp_io::storage::append(&double_map_key, item.encode());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use sp_core::hashing::twox_128;
|
||||
use crate::hash::Identity;
|
||||
use crate::{hash::Identity, assert_ok};
|
||||
use sp_io::TestExternalities;
|
||||
use generator::StorageValue as _;
|
||||
use bounded_vec::BoundedVec;
|
||||
use weak_bounded_vec::WeakBoundedVec;
|
||||
use core::convert::{TryFrom, TryInto};
|
||||
|
||||
#[test]
|
||||
fn prefixed_map_works() {
|
||||
@@ -1225,4 +1347,80 @@ mod test {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
crate::parameter_types! {
|
||||
pub const Seven: u32 = 7;
|
||||
pub const Four: u32 = 4;
|
||||
}
|
||||
|
||||
crate::generate_storage_alias! { Prefix, Foo => Value<WeakBoundedVec<u32, Seven>> }
|
||||
crate::generate_storage_alias! { Prefix, FooMap => Map<(u32, Twox128), BoundedVec<u32, Seven>> }
|
||||
crate::generate_storage_alias! {
|
||||
Prefix,
|
||||
FooDoubleMap => DoubleMap<(u32, Twox128), (u32, Twox128), BoundedVec<u32, Seven>>
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_append_works() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: WeakBoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
Foo::put(bounded);
|
||||
assert_ok!(Foo::try_append(4));
|
||||
assert_ok!(Foo::try_append(5));
|
||||
assert_ok!(Foo::try_append(6));
|
||||
assert_ok!(Foo::try_append(7));
|
||||
assert_eq!(Foo::decode_len().unwrap(), 7);
|
||||
assert!(Foo::try_append(8).is_err());
|
||||
});
|
||||
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: BoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
FooMap::insert(1, bounded);
|
||||
|
||||
assert_ok!(FooMap::try_append(1, 4));
|
||||
assert_ok!(FooMap::try_append(1, 5));
|
||||
assert_ok!(FooMap::try_append(1, 6));
|
||||
assert_ok!(FooMap::try_append(1, 7));
|
||||
assert_eq!(FooMap::decode_len(1).unwrap(), 7);
|
||||
assert!(FooMap::try_append(1, 8).is_err());
|
||||
|
||||
// append to a non-existing
|
||||
assert!(FooMap::get(2).is_none());
|
||||
assert_ok!(FooMap::try_append(2, 4));
|
||||
assert_eq!(
|
||||
FooMap::get(2).unwrap(),
|
||||
BoundedVec::<u32, Seven>::try_from(vec![4]).unwrap(),
|
||||
);
|
||||
assert_ok!(FooMap::try_append(2, 5));
|
||||
assert_eq!(
|
||||
FooMap::get(2).unwrap(),
|
||||
BoundedVec::<u32, Seven>::try_from(vec![4, 5]).unwrap(),
|
||||
);
|
||||
});
|
||||
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: BoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
FooDoubleMap::insert(1, 1, bounded);
|
||||
|
||||
assert_ok!(FooDoubleMap::try_append(1, 1, 4));
|
||||
assert_ok!(FooDoubleMap::try_append(1, 1, 5));
|
||||
assert_ok!(FooDoubleMap::try_append(1, 1, 6));
|
||||
assert_ok!(FooDoubleMap::try_append(1, 1, 7));
|
||||
assert_eq!(FooDoubleMap::decode_len(1, 1).unwrap(), 7);
|
||||
assert!(FooDoubleMap::try_append(1, 1, 8).is_err());
|
||||
|
||||
// append to a non-existing
|
||||
assert!(FooDoubleMap::get(2, 1).is_none());
|
||||
assert_ok!(FooDoubleMap::try_append(2, 1, 4));
|
||||
assert_eq!(
|
||||
FooDoubleMap::get(2, 1).unwrap(),
|
||||
BoundedVec::<u32, Seven>::try_from(vec![4]).unwrap(),
|
||||
);
|
||||
assert_ok!(FooDoubleMap::try_append(2, 1, 5));
|
||||
assert_eq!(
|
||||
FooDoubleMap::get(2, 1).unwrap(),
|
||||
BoundedVec::<u32, Seven>::try_from(vec![4, 5]).unwrap(),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
use codec::{Decode, Encode, EncodeLike, FullCodec};
|
||||
use crate::{
|
||||
storage::{
|
||||
StorageAppend, StorageDecodeLength, StoragePrefixedMap,
|
||||
bounded_vec::BoundedVec,
|
||||
StorageAppend, StorageTryAppend, StorageDecodeLength, StoragePrefixedMap,
|
||||
types::{OptionQuery, QueryKindTrait, OnEmptyGetter},
|
||||
},
|
||||
traits::{GetDefault, StorageInstance, Get, MaxEncodedLen, StorageInfo},
|
||||
@@ -116,52 +115,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<Prefix, Hasher1, Key1, Hasher2, Key2, QueryKind, OnEmpty, MaxValues, VecValue, VecBound>
|
||||
StorageDoubleMap<
|
||||
Prefix,
|
||||
Hasher1,
|
||||
Key1,
|
||||
Hasher2,
|
||||
Key2,
|
||||
BoundedVec<VecValue, VecBound>,
|
||||
QueryKind,
|
||||
OnEmpty,
|
||||
MaxValues,
|
||||
> where
|
||||
Prefix: StorageInstance,
|
||||
Hasher1: crate::hash::StorageHasher,
|
||||
Hasher2: crate::hash::StorageHasher,
|
||||
Key1: FullCodec,
|
||||
Key2: FullCodec,
|
||||
QueryKind: QueryKindTrait<BoundedVec<VecValue, VecBound>, OnEmpty>,
|
||||
OnEmpty: Get<QueryKind::Query> + 'static,
|
||||
MaxValues: Get<Option<u32>>,
|
||||
VecValue: FullCodec,
|
||||
VecBound: Get<u32>,
|
||||
{
|
||||
/// Try and append the given item to the double map in the storage.
|
||||
///
|
||||
/// Is only available if `Value` of the map is [`BoundedVec`].
|
||||
pub fn try_append<EncodeLikeItem, EncodeLikeKey1, EncodeLikeKey2>(
|
||||
key1: EncodeLikeKey1,
|
||||
key2: EncodeLikeKey2,
|
||||
item: EncodeLikeItem,
|
||||
) -> Result<(), ()>
|
||||
where
|
||||
EncodeLikeKey1: EncodeLike<Key1> + Clone,
|
||||
EncodeLikeKey2: EncodeLike<Key2> + Clone,
|
||||
EncodeLikeItem: EncodeLike<VecValue>,
|
||||
{
|
||||
<
|
||||
Self
|
||||
as
|
||||
crate::storage::bounded_vec::TryAppendDoubleMap<Key1, Key2, VecValue, VecBound>
|
||||
>::try_append(
|
||||
key1, key2, item,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Prefix, Hasher1, Key1, Hasher2, Key2, Value, QueryKind, OnEmpty, MaxValues>
|
||||
StorageDoubleMap<Prefix, Hasher1, Key1, Hasher2, Key2, Value, QueryKind, OnEmpty, MaxValues>
|
||||
where
|
||||
@@ -390,6 +343,26 @@ where
|
||||
pub fn translate_values<OldValue: Decode, F: FnMut(OldValue) -> Option<Value>>(f: F) {
|
||||
<Self as crate::storage::StoragePrefixedMap<Value>>::translate_values(f)
|
||||
}
|
||||
|
||||
/// Try and append the given item to the value in the storage.
|
||||
///
|
||||
/// Is only available if `Value` of the storage implements [`StorageTryAppend`].
|
||||
pub fn try_append<KArg1, KArg2, Item, EncodeLikeItem>(
|
||||
key1: KArg1,
|
||||
key2: KArg2,
|
||||
item: EncodeLikeItem,
|
||||
) -> Result<(), ()>
|
||||
where
|
||||
KArg1: EncodeLike<Key1> + Clone,
|
||||
KArg2: EncodeLike<Key2> + Clone,
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
Value: StorageTryAppend<Item>,
|
||||
{
|
||||
<
|
||||
Self as crate::storage::TryAppendDoubleMap<Key1, Key2, Value, Item>
|
||||
>::try_append(key1, key2, item)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Prefix, Hasher1, Key1, Hasher2, Key2, Value, QueryKind, OnEmpty, MaxValues>
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
use codec::{FullCodec, Decode, EncodeLike, Encode};
|
||||
use crate::{
|
||||
storage::{
|
||||
StorageAppend, StorageDecodeLength, StoragePrefixedMap,
|
||||
bounded_vec::BoundedVec,
|
||||
StorageAppend, StorageTryAppend, StorageDecodeLength, StoragePrefixedMap,
|
||||
types::{OptionQuery, QueryKindTrait, OnEmptyGetter},
|
||||
},
|
||||
traits::{GetDefault, StorageInstance, Get, MaxEncodedLen, StorageInfo},
|
||||
@@ -98,35 +97,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<Prefix, Hasher, Key, QueryKind, OnEmpty, MaxValues, VecValue, VecBound>
|
||||
StorageMap<Prefix, Hasher, Key, BoundedVec<VecValue, VecBound>, QueryKind, OnEmpty, MaxValues>
|
||||
where
|
||||
Prefix: StorageInstance,
|
||||
Hasher: crate::hash::StorageHasher,
|
||||
Key: FullCodec,
|
||||
QueryKind: QueryKindTrait<BoundedVec<VecValue, VecBound>, OnEmpty>,
|
||||
OnEmpty: Get<QueryKind::Query> + 'static,
|
||||
MaxValues: Get<Option<u32>>,
|
||||
VecValue: FullCodec,
|
||||
VecBound: Get<u32>,
|
||||
{
|
||||
/// Try and append the given item to the map in the storage.
|
||||
///
|
||||
/// Is only available if `Value` of the map is [`BoundedVec`].
|
||||
pub fn try_append<EncodeLikeItem, EncodeLikeKey>(
|
||||
key: EncodeLikeKey,
|
||||
item: EncodeLikeItem,
|
||||
) -> Result<(), ()>
|
||||
where
|
||||
EncodeLikeKey: EncodeLike<Key> + Clone,
|
||||
EncodeLikeItem: EncodeLike<VecValue>,
|
||||
{
|
||||
<Self as crate::storage::bounded_vec::TryAppendMap<Key, VecValue, VecBound>>::try_append(
|
||||
key, item,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Prefix, Hasher, Key, Value, QueryKind, OnEmpty, MaxValues>
|
||||
StorageMap<Prefix, Hasher, Key, Value, QueryKind, OnEmpty, MaxValues>
|
||||
where
|
||||
@@ -289,6 +259,24 @@ where
|
||||
pub fn translate_values<OldValue: Decode, F: FnMut(OldValue) -> Option<Value>>(f: F) {
|
||||
<Self as crate::storage::StoragePrefixedMap<Value>>::translate_values(f)
|
||||
}
|
||||
|
||||
/// Try and append the given item to the value in the storage.
|
||||
///
|
||||
/// Is only available if `Value` of the storage implements [`StorageTryAppend`].
|
||||
pub fn try_append<KArg, Item, EncodeLikeItem>(
|
||||
key: KArg,
|
||||
item: EncodeLikeItem,
|
||||
) -> Result<(), ()>
|
||||
where
|
||||
KArg: EncodeLike<Key> + Clone,
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
Value: StorageTryAppend<Item>,
|
||||
{
|
||||
<
|
||||
Self as crate::storage::TryAppendMap<Key, Value, Item>
|
||||
>::try_append(key, item)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Prefix, Hasher, Key, Value, QueryKind, OnEmpty, MaxValues>
|
||||
|
||||
@@ -20,11 +20,10 @@
|
||||
use codec::{FullCodec, Decode, EncodeLike, Encode};
|
||||
use crate::{
|
||||
storage::{
|
||||
StorageAppend, StorageDecodeLength,
|
||||
bounded_vec::BoundedVec,
|
||||
StorageAppend, StorageTryAppend, StorageDecodeLength,
|
||||
types::{OptionQuery, QueryKindTrait, OnEmptyGetter},
|
||||
},
|
||||
traits::{GetDefault, StorageInstance, Get, MaxEncodedLen, StorageInfo},
|
||||
traits::{GetDefault, StorageInstance, MaxEncodedLen, StorageInfo},
|
||||
};
|
||||
use frame_metadata::{DefaultByteGetter, StorageEntryModifier};
|
||||
use sp_arithmetic::traits::SaturatedConversion;
|
||||
@@ -63,26 +62,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<Prefix, QueryKind, OnEmpty, VecValue, VecBound>
|
||||
StorageValue<Prefix, BoundedVec<VecValue, VecBound>, QueryKind, OnEmpty>
|
||||
where
|
||||
Prefix: StorageInstance,
|
||||
QueryKind: QueryKindTrait<BoundedVec<VecValue, VecBound>, OnEmpty>,
|
||||
OnEmpty: crate::traits::Get<QueryKind::Query> + 'static,
|
||||
VecValue: FullCodec,
|
||||
VecBound: Get<u32>,
|
||||
{
|
||||
/// Try and append the given item to the value in the storage.
|
||||
///
|
||||
/// Is only available if `Value` of the storage is [`BoundedVec`].
|
||||
pub fn try_append<EncodeLikeItem>(item: EncodeLikeItem) -> Result<(), ()>
|
||||
where
|
||||
EncodeLikeItem: EncodeLike<VecValue>,
|
||||
{
|
||||
<Self as crate::storage::bounded_vec::TryAppendValue<VecValue, VecBound>>::try_append(item)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Prefix, Value, QueryKind, OnEmpty> StorageValue<Prefix, Value, QueryKind, OnEmpty>
|
||||
where
|
||||
Prefix: StorageInstance,
|
||||
@@ -192,6 +171,18 @@ where
|
||||
pub fn decode_len() -> Option<usize> where Value: StorageDecodeLength {
|
||||
<Self as crate::storage::StorageValue<Value>>::decode_len()
|
||||
}
|
||||
|
||||
/// Try and append the given item to the value in the storage.
|
||||
///
|
||||
/// Is only available if `Value` of the storage implements [`StorageTryAppend`].
|
||||
pub fn try_append<Item, EncodeLikeItem>(item: EncodeLikeItem) -> Result<(), ()>
|
||||
where
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
Value: StorageTryAppend<Item>,
|
||||
{
|
||||
<Self as crate::storage::TryAppendValue<Value, Item>>::try_append(item)
|
||||
}
|
||||
}
|
||||
|
||||
/// Part of storage metadata for storage value.
|
||||
|
||||
@@ -0,0 +1,420 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Traits, types and structs to support putting a bounded vector into storage, as a raw value, map
|
||||
//! or a double map.
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use sp_std::{convert::TryFrom, fmt, marker::PhantomData};
|
||||
use codec::{Encode, Decode};
|
||||
use core::{
|
||||
ops::{Deref, Index, IndexMut},
|
||||
slice::SliceIndex,
|
||||
};
|
||||
use crate::{
|
||||
traits::{Get, MaxEncodedLen},
|
||||
storage::{StorageDecodeLength, StorageTryAppend},
|
||||
};
|
||||
|
||||
/// A weakly bounded vector.
|
||||
///
|
||||
/// It has implementations for efficient append and length decoding, as with a normal `Vec<_>`, once
|
||||
/// put into storage as a raw value, map or double-map.
|
||||
///
|
||||
/// The length of the vec is not strictly bounded. Decoding a vec with more element that the bound
|
||||
/// is accepted, and some method allow to bypass the restriction with warnings.
|
||||
#[derive(Encode)]
|
||||
pub struct WeakBoundedVec<T, S>(Vec<T>, PhantomData<S>);
|
||||
|
||||
impl<T: Decode, S: Get<u32>> Decode for WeakBoundedVec<T, S> {
|
||||
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||
let inner = Vec::<T>::decode(input)?;
|
||||
Ok(Self::force_from(inner, Some("decode")))
|
||||
}
|
||||
|
||||
fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
|
||||
Vec::<T>::skip(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> WeakBoundedVec<T, S> {
|
||||
/// Create `Self` from `t` without any checks.
|
||||
fn unchecked_from(t: Vec<T>) -> Self {
|
||||
Self(t, Default::default())
|
||||
}
|
||||
|
||||
/// Consume self, and return the inner `Vec`. Henceforth, the `Vec<_>` can be altered in an
|
||||
/// arbitrary way. At some point, if the reverse conversion is required, `TryFrom<Vec<_>>` can
|
||||
/// be used.
|
||||
///
|
||||
/// This is useful for cases if you need access to an internal API of the inner `Vec<_>` which
|
||||
/// is not provided by the wrapper `WeakBoundedVec`.
|
||||
pub fn into_inner(self) -> Vec<T> {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::remove`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `index` is out of bounds.
|
||||
pub fn remove(&mut self, index: usize) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::swap_remove`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `index` is out of bounds.
|
||||
pub fn swap_remove(&mut self, index: usize) {
|
||||
self.0.swap_remove(index);
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::retain`].
|
||||
pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F) {
|
||||
self.0.retain(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: Get<u32>> WeakBoundedVec<T, S> {
|
||||
/// Get the bound of the type in `usize`.
|
||||
pub fn bound() -> usize {
|
||||
S::get() as usize
|
||||
}
|
||||
|
||||
/// Create `Self` from `t` without any checks. Logs warnings if the bound is not being
|
||||
/// respected. The additional scope can be used to indicate where a potential overflow is
|
||||
/// happening.
|
||||
pub fn force_from(t: Vec<T>, scope: Option<&'static str>) -> Self {
|
||||
if t.len() > Self::bound() {
|
||||
log::warn!(
|
||||
target: crate::LOG_TARGET,
|
||||
"length of a bounded vector in scope {} is not respected.",
|
||||
scope.unwrap_or("UNKNOWN"),
|
||||
);
|
||||
}
|
||||
|
||||
Self::unchecked_from(t)
|
||||
}
|
||||
|
||||
/// Consumes self and mutates self via the given `mutate` function.
|
||||
///
|
||||
/// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is
|
||||
/// returned.
|
||||
///
|
||||
/// This is essentially a *consuming* shorthand [`Self::into_inner`] -> `...` ->
|
||||
/// [`Self::try_from`].
|
||||
pub fn try_mutate(mut self, mut mutate: impl FnMut(&mut Vec<T>)) -> Option<Self> {
|
||||
mutate(&mut self.0);
|
||||
(self.0.len() <= Self::bound()).then(move || self)
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::insert`], but returns an `Err` (and is a noop) if the
|
||||
/// new length of the vector exceeds `S`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `index > len`.
|
||||
pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), ()> {
|
||||
if self.len() < Self::bound() {
|
||||
self.0.insert(index, element);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::push`], but returns an `Err` (and is a noop) if the
|
||||
/// new length of the vector exceeds `S`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the new capacity exceeds isize::MAX bytes.
|
||||
pub fn try_push(&mut self, element: T) -> Result<(), ()> {
|
||||
if self.len() < Self::bound() {
|
||||
self.0.push(element);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Default for WeakBoundedVec<T, S> {
|
||||
fn default() -> Self {
|
||||
// the bound cannot be below 0, which is satisfied by an empty vector
|
||||
Self::unchecked_from(Vec::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T, S> fmt::Debug for WeakBoundedVec<T, S>
|
||||
where
|
||||
T: fmt::Debug,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_tuple("WeakBoundedVec").field(&self.0).field(&Self::bound()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Clone for WeakBoundedVec<T, S>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
// bound is retained
|
||||
Self::unchecked_from(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: Get<u32>> TryFrom<Vec<T>> for WeakBoundedVec<T, S> {
|
||||
type Error = ();
|
||||
fn try_from(t: Vec<T>) -> Result<Self, Self::Error> {
|
||||
if t.len() <= Self::bound() {
|
||||
// explicit check just above
|
||||
Ok(Self::unchecked_from(t))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// It is okay to give a non-mutable reference of the inner vec to anyone.
|
||||
impl<T, S> AsRef<Vec<T>> for WeakBoundedVec<T, S> {
|
||||
fn as_ref(&self) -> &Vec<T> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> AsRef<[T]> for WeakBoundedVec<T, S> {
|
||||
fn as_ref(&self) -> &[T] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> AsMut<[T]> for WeakBoundedVec<T, S> {
|
||||
fn as_mut(&mut self) -> &mut [T] {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
// will allow for immutable all operations of `Vec<T>` on `WeakBoundedVec<T>`.
|
||||
impl<T, S> Deref for WeakBoundedVec<T, S> {
|
||||
type Target = Vec<T>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
// Allows for indexing similar to a normal `Vec`. Can panic if out of bound.
|
||||
impl<T, S, I> Index<I> for WeakBoundedVec<T, S>
|
||||
where
|
||||
I: SliceIndex<[T]>,
|
||||
{
|
||||
type Output = I::Output;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: I) -> &Self::Output {
|
||||
self.0.index(index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, I> IndexMut<I> for WeakBoundedVec<T, S>
|
||||
where
|
||||
I: SliceIndex<[T]>,
|
||||
{
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: I) -> &mut Self::Output {
|
||||
self.0.index_mut(index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> sp_std::iter::IntoIterator for WeakBoundedVec<T, S> {
|
||||
type Item = T;
|
||||
type IntoIter = sp_std::vec::IntoIter<T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> codec::DecodeLength for WeakBoundedVec<T, S> {
|
||||
fn len(self_encoded: &[u8]) -> Result<usize, codec::Error> {
|
||||
// `WeakBoundedVec<T, _>` stored just a `Vec<T>`, thus the length is at the beginning in
|
||||
// `Compact` form, and same implementation as `Vec<T>` can be used.
|
||||
<Vec<T> as codec::DecodeLength>::len(self_encoded)
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: we could also implement this as:
|
||||
// impl<T: Value, S1: Get<u32>, S2: Get<u32>> PartialEq<WeakBoundedVec<T, S2>> for WeakBoundedVec<T, S1>
|
||||
// to allow comparison of bounded vectors with different bounds.
|
||||
impl<T, S> PartialEq for WeakBoundedVec<T, S>
|
||||
where
|
||||
T: PartialEq,
|
||||
{
|
||||
fn eq(&self, rhs: &Self) -> bool {
|
||||
self.0 == rhs.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq, S: Get<u32>> PartialEq<Vec<T>> for WeakBoundedVec<T, S> {
|
||||
fn eq(&self, other: &Vec<T>) -> bool {
|
||||
&self.0 == other
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Eq for WeakBoundedVec<T, S> where T: Eq {}
|
||||
|
||||
impl<T, S> StorageDecodeLength for WeakBoundedVec<T, S> {}
|
||||
|
||||
impl<T, S: Get<u32>> StorageTryAppend<T> for WeakBoundedVec<T, S> {
|
||||
fn bound() -> usize {
|
||||
S::get() as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> MaxEncodedLen for WeakBoundedVec<T, S>
|
||||
where
|
||||
T: MaxEncodedLen,
|
||||
S: Get<u32>,
|
||||
WeakBoundedVec<T, S>: Encode,
|
||||
{
|
||||
fn max_encoded_len() -> usize {
|
||||
// WeakBoundedVec<T, S> encodes like Vec<T> which encodes like [T], which is a compact u32
|
||||
// plus each item in the slice:
|
||||
// https://substrate.dev/rustdocs/v3.0.0/src/parity_scale_codec/codec.rs.html#798-808
|
||||
codec::Compact(S::get())
|
||||
.encoded_size()
|
||||
.saturating_add(Self::bound().saturating_mul(T::max_encoded_len()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use super::*;
|
||||
use sp_io::TestExternalities;
|
||||
use sp_std::convert::TryInto;
|
||||
use crate::Twox128;
|
||||
|
||||
crate::parameter_types! {
|
||||
pub const Seven: u32 = 7;
|
||||
pub const Four: u32 = 4;
|
||||
}
|
||||
|
||||
crate::generate_storage_alias! { Prefix, Foo => Value<WeakBoundedVec<u32, Seven>> }
|
||||
crate::generate_storage_alias! { Prefix, FooMap => Map<(u32, Twox128), WeakBoundedVec<u32, Seven>> }
|
||||
crate::generate_storage_alias! {
|
||||
Prefix,
|
||||
FooDoubleMap => DoubleMap<(u32, Twox128), (u32, Twox128), WeakBoundedVec<u32, Seven>>
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_append_is_correct() {
|
||||
assert_eq!(WeakBoundedVec::<u32, Seven>::bound(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_len_works() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: WeakBoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
Foo::put(bounded);
|
||||
assert_eq!(Foo::decode_len().unwrap(), 3);
|
||||
});
|
||||
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: WeakBoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
FooMap::insert(1, bounded);
|
||||
assert_eq!(FooMap::decode_len(1).unwrap(), 3);
|
||||
assert!(FooMap::decode_len(0).is_none());
|
||||
assert!(FooMap::decode_len(2).is_none());
|
||||
});
|
||||
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let bounded: WeakBoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
FooDoubleMap::insert(1, 1, bounded);
|
||||
assert_eq!(FooDoubleMap::decode_len(1, 1).unwrap(), 3);
|
||||
assert!(FooDoubleMap::decode_len(2, 1).is_none());
|
||||
assert!(FooDoubleMap::decode_len(1, 2).is_none());
|
||||
assert!(FooDoubleMap::decode_len(2, 2).is_none());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_insert_works() {
|
||||
let mut bounded: WeakBoundedVec<u32, Four> = vec![1, 2, 3].try_into().unwrap();
|
||||
bounded.try_insert(1, 0).unwrap();
|
||||
assert_eq!(*bounded, vec![1, 0, 2, 3]);
|
||||
|
||||
assert!(bounded.try_insert(0, 9).is_err());
|
||||
assert_eq!(*bounded, vec![1, 0, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "insertion index (is 9) should be <= len (is 3)")]
|
||||
fn try_inert_panics_if_oob() {
|
||||
let mut bounded: WeakBoundedVec<u32, Four> = vec![1, 2, 3].try_into().unwrap();
|
||||
bounded.try_insert(9, 0).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_push_works() {
|
||||
let mut bounded: WeakBoundedVec<u32, Four> = vec![1, 2, 3].try_into().unwrap();
|
||||
bounded.try_push(0).unwrap();
|
||||
assert_eq!(*bounded, vec![1, 2, 3, 0]);
|
||||
|
||||
assert!(bounded.try_push(9).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deref_coercion_works() {
|
||||
let bounded: WeakBoundedVec<u32, Seven> = vec![1, 2, 3].try_into().unwrap();
|
||||
// these methods come from deref-ed vec.
|
||||
assert_eq!(bounded.len(), 3);
|
||||
assert!(bounded.iter().next().is_some());
|
||||
assert!(!bounded.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_mutate_works() {
|
||||
let bounded: WeakBoundedVec<u32, Seven> = vec![1, 2, 3, 4, 5, 6].try_into().unwrap();
|
||||
let bounded = bounded.try_mutate(|v| v.push(7)).unwrap();
|
||||
assert_eq!(bounded.len(), 7);
|
||||
assert!(bounded.try_mutate(|v| v.push(8)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice_indexing_works() {
|
||||
let bounded: WeakBoundedVec<u32, Seven> = vec![1, 2, 3, 4, 5, 6].try_into().unwrap();
|
||||
assert_eq!(&bounded[0..=2], &[1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vec_eq_works() {
|
||||
let bounded: WeakBoundedVec<u32, Seven> = vec![1, 2, 3, 4, 5, 6].try_into().unwrap();
|
||||
assert_eq!(bounded, vec![1, 2, 3, 4, 5, 6]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_big_succeed_to_decode() {
|
||||
let v: Vec<u32> = vec![1, 2, 3, 4, 5];
|
||||
let w = WeakBoundedVec::<u32, Four>::decode(&mut &v.encode()[..]).unwrap();
|
||||
assert_eq!(v, *w);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user