Child trie api changes BREAKING (#4857)

Co-Authored-By: thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
cheme
2020-04-20 15:21:22 +02:00
committed by GitHub
parent 7d9aa81bfc
commit 4ffcf98d8d
64 changed files with 1514 additions and 1655 deletions
+89 -106
View File
@@ -16,100 +16,90 @@
//! Operation on runtime child storages.
//!
//! This module is a currently only a variant of unhashed with additional `storage_key`.
//! Note that `storage_key` must be unique and strong (strong in the sense of being long enough to
//! avoid collision from a resistant hash function (which unique implies)).
//!
//! A **key collision free** unique id is required as parameter to avoid key collision
//! between child tries.
//! This unique id management and generation responsibility is delegated to pallet module.
// NOTE: could replace unhashed by having only one kind of storage (root being null storage key (storage_key can become Option<&[u8]>).
//! This module is a currently only a variant of unhashed with additional `child_info`.
// NOTE: could replace unhashed by having only one kind of storage (top trie being the child info
// of null length parent storage key).
use crate::sp_std::prelude::*;
use codec::{Codec, Encode, Decode};
pub use sp_core::storage::ChildInfo;
pub use sp_core::storage::{ChildInfo, ChildType};
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
pub fn get<T: Decode + Sized>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> Option<T> {
let (data, child_type) = child_info.info();
sp_io::storage::child_get(
storage_key,
data,
child_type,
key,
).and_then(|v| {
Decode::decode(&mut &v[..]).map(Some).unwrap_or_else(|_| {
// TODO #3700: error should be handleable.
runtime_print!("ERROR: Corrupted state in child trie at {:?}/{:?}", storage_key, key);
None
})
})
match child_info.child_type() {
ChildType::ParentKeyId => {
let storage_key = child_info.storage_key();
sp_io::default_child_storage::get(
storage_key,
key,
).and_then(|v| {
Decode::decode(&mut &v[..]).map(Some).unwrap_or_else(|_| {
// TODO #3700: error should be handleable.
runtime_print!("ERROR: Corrupted state in child trie at {:?}/{:?}", storage_key, key);
None
})
})
},
}
}
/// Return the value of the item in storage under `key`, or the type's default if there is no
/// explicit entry.
pub fn get_or_default<T: Decode + Sized + Default>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> T {
get(storage_key, child_info, key).unwrap_or_else(Default::default)
get(child_info, key).unwrap_or_else(Default::default)
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry.
pub fn get_or<T: Decode + Sized>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
default_value: T,
) -> T {
get(storage_key, child_info, key).unwrap_or(default_value)
get(child_info, key).unwrap_or(default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry.
pub fn get_or_else<T: Decode + Sized, F: FnOnce() -> T>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
default_value: F,
) -> T {
get(storage_key, child_info, key).unwrap_or_else(default_value)
get(child_info, key).unwrap_or_else(default_value)
}
/// Put `value` in storage under `key`.
pub fn put<T: Encode>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
value: &T,
) {
let (data, child_type) = child_info.info();
value.using_encoded(|slice|
sp_io::storage::child_set(
storage_key,
data,
child_type,
key,
slice,
)
);
match child_info.child_type() {
ChildType::ParentKeyId => value.using_encoded(|slice|
sp_io::default_child_storage::set(
child_info.storage_key(),
key,
slice,
)
),
}
}
/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise.
pub fn take<T: Decode + Sized>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> Option<T> {
let r = get(storage_key, child_info, key);
let r = get(child_info, key);
if r.is_some() {
kill(storage_key, child_info, key);
kill(child_info, key);
}
r
}
@@ -117,113 +107,106 @@ pub fn take<T: Decode + Sized>(
/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage,
/// the default for its type.
pub fn take_or_default<T: Codec + Sized + Default>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> T {
take(storage_key, child_info, key).unwrap_or_else(Default::default)
take(child_info, key).unwrap_or_else(Default::default)
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or<T: Codec + Sized>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
default_value: T,
) -> T {
take(storage_key, child_info, key).unwrap_or(default_value)
take(child_info, key).unwrap_or(default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or_else<T: Codec + Sized, F: FnOnce() -> T>(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
default_value: F,
) -> T {
take(storage_key, child_info, key).unwrap_or_else(default_value)
take(child_info, key).unwrap_or_else(default_value)
}
/// Check to see if `key` has an explicit entry in storage.
pub fn exists(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> bool {
let (data, child_type) = child_info.info();
sp_io::storage::child_read(
storage_key, data, child_type,
key, &mut [0;0][..], 0,
).is_some()
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::read(
child_info.storage_key(),
key, &mut [0;0][..], 0,
).is_some(),
}
}
/// Remove all `storage_key` key/values
pub fn kill_storage(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
) {
let (data, child_type) = child_info.info();
sp_io::storage::child_storage_kill(
storage_key,
data,
child_type,
)
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::storage_kill(
child_info.storage_key(),
),
}
}
/// Ensure `key` has no explicit entry in storage.
pub fn kill(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) {
let (data, child_type) = child_info.info();
sp_io::storage::child_clear(
storage_key,
data,
child_type,
key,
);
match child_info.child_type() {
ChildType::ParentKeyId => {
sp_io::default_child_storage::clear(
child_info.storage_key(),
key,
);
},
}
}
/// Get a Vec of bytes from storage.
pub fn get_raw(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> Option<Vec<u8>> {
let (data, child_type) = child_info.info();
sp_io::storage::child_get(
storage_key,
data,
child_type,
key,
)
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::get(
child_info.storage_key(),
key,
),
}
}
/// Put a raw byte slice into storage.
pub fn put_raw(
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
value: &[u8],
) {
let (data, child_type) = child_info.info();
sp_io::storage::child_set(
storage_key,
data,
child_type,
key,
value,
)
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::set(
child_info.storage_key(),
key,
value,
),
}
}
/// Calculate current child root value.
pub fn child_root(
storage_key: &[u8],
pub fn root(
child_info: &ChildInfo,
) -> Vec<u8> {
sp_io::storage::child_root(
storage_key,
)
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::root(
child_info.storage_key(),
),
}
}