mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-25 10:41:08 +00:00
Use storage::append in the implementation of the storage types (#5889)
* Start improving `storage_append` * Fix some stuff * Fix compilation * Update docs and add new test * More tests * Test kill + append
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use sp_std::borrow::Borrow;
|
||||
use codec::{Ref, FullCodec, FullEncode, Decode, Encode, EncodeLike, EncodeAppend};
|
||||
use crate::{storage::{self, unhashed}, traits::Len, Never};
|
||||
use codec::{FullCodec, FullEncode, Decode, Encode, EncodeLike};
|
||||
use crate::{storage::{self, unhashed, StorageAppend}, traits::Len, Never};
|
||||
use crate::hash::{StorageHasher, Twox128, ReversibleStorageHasher};
|
||||
|
||||
/// Generator for `StorageDoubleMap` used by `decl_storage`.
|
||||
@@ -245,53 +245,19 @@ impl<K1, K2, V, G> storage::StorageDoubleMap<K1, K2, V> for G where
|
||||
ret
|
||||
}
|
||||
|
||||
fn append<Items, Item, EncodeLikeItem, KArg1, KArg2>(
|
||||
fn append<Item, EncodeLikeItem, KArg1, KArg2>(
|
||||
k1: KArg1,
|
||||
k2: KArg2,
|
||||
items: Items,
|
||||
) -> Result<(), &'static str> where
|
||||
KArg1: EncodeLike<K1>,
|
||||
KArg2: EncodeLike<K2>,
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
V: EncodeAppend<Item=Item>,
|
||||
Items: IntoIterator<Item=EncodeLikeItem>,
|
||||
Items::IntoIter: ExactSizeIterator
|
||||
{
|
||||
let final_key = Self::storage_double_map_final_key(k1, k2);
|
||||
|
||||
let encoded_value = unhashed::get_raw(&final_key)
|
||||
.unwrap_or_else(|| {
|
||||
match G::from_query_to_optional_value(G::from_optional_value_to_query(None)) {
|
||||
Some(value) => value.encode(),
|
||||
None => Vec::new(),
|
||||
}
|
||||
});
|
||||
|
||||
let new_val = V::append_or_new(
|
||||
encoded_value,
|
||||
items,
|
||||
).map_err(|_| "Could not append given item")?;
|
||||
unhashed::put_raw(&final_key, &new_val);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn append_or_insert<Items, Item, EncodeLikeItem, KArg1, KArg2>(
|
||||
k1: KArg1,
|
||||
k2: KArg2,
|
||||
items: Items,
|
||||
item: EncodeLikeItem,
|
||||
) where
|
||||
KArg1: EncodeLike<K1>,
|
||||
KArg2: EncodeLike<K2>,
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
V: EncodeAppend<Item=Item>,
|
||||
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<V>,
|
||||
Items::IntoIter: ExactSizeIterator
|
||||
V: StorageAppend<Item>,
|
||||
{
|
||||
Self::append(Ref::from(&k1), Ref::from(&k2), items.clone())
|
||||
.unwrap_or_else(|_| Self::insert(k1, k2, items));
|
||||
let final_key = Self::storage_double_map_final_key(k1, k2);
|
||||
sp_io::storage::append(&final_key, item.encode());
|
||||
}
|
||||
|
||||
fn decode_len<KArg1, KArg2>(key1: KArg1, key2: KArg2) -> Result<usize, &'static str> where
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
#[cfg(not(feature = "std"))]
|
||||
use sp_std::prelude::*;
|
||||
use sp_std::borrow::Borrow;
|
||||
use codec::{FullCodec, FullEncode, Decode, Encode, EncodeLike, Ref, EncodeAppend};
|
||||
use crate::{storage::{self, unhashed}, traits::Len, Never};
|
||||
use codec::{FullCodec, FullEncode, Decode, Encode, EncodeLike};
|
||||
use crate::{storage::{self, unhashed, StorageAppend}, traits::Len, Never};
|
||||
use crate::hash::{StorageHasher, Twox128, ReversibleStorageHasher};
|
||||
|
||||
/// Generator for `StorageMap` used by `decl_storage`.
|
||||
@@ -276,43 +276,15 @@ impl<K: FullEncode, V: FullCodec, G: StorageMap<K, V>> storage::StorageMap<K, V>
|
||||
G::from_optional_value_to_query(value)
|
||||
}
|
||||
|
||||
fn append<Items, Item, EncodeLikeItem, KeyArg>(key: KeyArg, items: Items) -> Result<(), &'static str>
|
||||
fn append<Item, EncodeLikeItem, EncodeLikeKey>(key: EncodeLikeKey, item: EncodeLikeItem)
|
||||
where
|
||||
KeyArg: EncodeLike<K>,
|
||||
EncodeLikeKey: EncodeLike<K>,
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
V: EncodeAppend<Item=Item>,
|
||||
Items: IntoIterator<Item=EncodeLikeItem>,
|
||||
Items::IntoIter: ExactSizeIterator,
|
||||
V: StorageAppend<Item>,
|
||||
{
|
||||
let key = Self::storage_map_final_key(key);
|
||||
let encoded_value = unhashed::get_raw(key.as_ref())
|
||||
.unwrap_or_else(|| {
|
||||
match G::from_query_to_optional_value(G::from_optional_value_to_query(None)) {
|
||||
Some(value) => value.encode(),
|
||||
None => Vec::new(),
|
||||
}
|
||||
});
|
||||
|
||||
let new_val = V::append_or_new(
|
||||
encoded_value,
|
||||
items,
|
||||
).map_err(|_| "Could not append given item")?;
|
||||
unhashed::put_raw(key.as_ref(), &new_val);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn append_or_insert<Items, Item, EncodeLikeItem, KeyArg>(key: KeyArg, items: Items)
|
||||
where
|
||||
KeyArg: EncodeLike<K>,
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
V: EncodeAppend<Item=Item>,
|
||||
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<V>,
|
||||
Items::IntoIter: ExactSizeIterator,
|
||||
{
|
||||
Self::append(Ref::from(&key), items.clone())
|
||||
.unwrap_or_else(|_| Self::insert(key, items));
|
||||
sp_io::storage::append(&key, item.encode());
|
||||
}
|
||||
|
||||
fn decode_len<KeyArg: EncodeLike<K>>(key: KeyArg) -> Result<usize, &'static str>
|
||||
|
||||
@@ -14,12 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use sp_std::prelude::*;
|
||||
use codec::{FullCodec, Encode, EncodeAppend, EncodeLike, Decode};
|
||||
use codec::{FullCodec, Encode, EncodeLike, Decode};
|
||||
use crate::{
|
||||
Never,
|
||||
storage::{self, unhashed},
|
||||
storage::{self, unhashed, StorageAppend},
|
||||
hash::{Twox128, StorageHasher},
|
||||
traits::Len
|
||||
};
|
||||
@@ -134,55 +132,16 @@ impl<T: FullCodec, G: StorageValue<T>> storage::StorageValue<T> for G {
|
||||
G::from_optional_value_to_query(value)
|
||||
}
|
||||
|
||||
/// Append the given items to the value in the storage.
|
||||
///
|
||||
/// `T` is required to implement `codec::EncodeAppend`.
|
||||
fn append<Items, Item, EncodeLikeItem>(items: Items) -> Result<(), &'static str>
|
||||
fn append<Item, EncodeLikeItem>(item: EncodeLikeItem)
|
||||
where
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
T: EncodeAppend<Item=Item>,
|
||||
Items: IntoIterator<Item=EncodeLikeItem>,
|
||||
Items::IntoIter: ExactSizeIterator,
|
||||
T: StorageAppend<Item>,
|
||||
{
|
||||
let key = Self::storage_value_final_key();
|
||||
let encoded_value = unhashed::get_raw(&key)
|
||||
.unwrap_or_else(|| {
|
||||
match G::from_query_to_optional_value(G::from_optional_value_to_query(None)) {
|
||||
Some(value) => value.encode(),
|
||||
None => Vec::new(),
|
||||
}
|
||||
});
|
||||
|
||||
let new_val = T::append_or_new(
|
||||
encoded_value,
|
||||
items,
|
||||
).map_err(|_| "Could not append given item")?;
|
||||
unhashed::put_raw(&key, &new_val);
|
||||
Ok(())
|
||||
sp_io::storage::append(&key, item.encode());
|
||||
}
|
||||
|
||||
/// Safely append the given items to the value in the storage. If a codec error occurs, then the
|
||||
/// old (presumably corrupt) value is replaced with the given `items`.
|
||||
///
|
||||
/// `T` is required to implement `codec::EncodeAppend`.
|
||||
fn append_or_put<Items, Item, EncodeLikeItem>(items: Items) where
|
||||
Item: Encode,
|
||||
EncodeLikeItem: EncodeLike<Item>,
|
||||
T: EncodeAppend<Item=Item>,
|
||||
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<T>,
|
||||
Items::IntoIter: ExactSizeIterator
|
||||
{
|
||||
Self::append(items.clone()).unwrap_or_else(|_| Self::put(items));
|
||||
}
|
||||
|
||||
/// Read the length of the value in a fast way, without decoding the entire value.
|
||||
///
|
||||
/// `T` is required to implement `Codec::DecodeLength`.
|
||||
///
|
||||
/// Note that `0` is returned as the default value if no encoded value exists at the given key.
|
||||
/// Therefore, this function cannot be used as a sign of _existence_. use the `::exists()`
|
||||
/// function for this purpose.
|
||||
fn decode_len() -> Result<usize, &'static str> where T: codec::DecodeLength, T: Len {
|
||||
let key = Self::storage_value_final_key();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user