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:
Guillaume Thiolliere
2021-05-21 11:07:00 +02:00
committed by GitHub
parent a183031eef
commit 25625a0f0c
10 changed files with 778 additions and 377 deletions
@@ -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()),
);
}
}