Fix key collision for child trie (#4162)

* In progress, runtime io must switch to future proof root +
child_specific (unique id) + u32 type.

* Switch interface, sr-io seems ok, rpc could use similar interface to
sr-io, genesis json broken if there is child trie in existing encoding
genesis.

* test from previous implementation.

* fix proving test.

* Restore Keyspacedb from other branch, only apply to child trie.

* Removing unneeded child_info from child root (child info are stored
if things changed, otherwhise the root does not change).

* Switch rpc to use same format as ext: more future proof.

* use root from child info for trie backend essence.

* Breaking long lines.

* Update doc and clean pr a bit.

* fix error type

* Restore removed doc on merge and update sr-io doc.

* Switch child storage api to use directly unique id, if managed id
where to be put in place, the api will change at this time.

* Clean deprecated host interface from child.

* Removing assertion on child info (can fail depending on root
memoization).

* merging child info in the overlay when possible.

* child iteration by prefix using child_info.

* Using ChainInfo in frame support. ChainInfo gets redesign to avoid
buffers allocation on every calls.

* Add length of root to the data of child info.

* comments

* Encode compact.

* Remove child info with root.

* Fix try_update condition.

* Comment Ext child root caching.

* Replace tuples by struct with field

* remove StorageTuple alias.

* Fix doc tests, and remove StorageOverlay and ChildStorageOverlay
aliases.
This commit is contained in:
cheme
2019-12-14 03:11:19 +01:00
committed by Gavin Wood
parent 7121837f84
commit 0ece5d9e17
53 changed files with 2121 additions and 918 deletions
+137 -27
View File
@@ -19,14 +19,29 @@
//! 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 responsability 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]>).
use crate::sp_std::prelude::*;
use codec::{Codec, Encode, Decode};
pub use primitives::storage::ChildInfo;
/// 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], key: &[u8]) -> Option<T> {
sp_io::storage::child_get(storage_key, key).and_then(|v| {
pub fn get<T: Decode + Sized>(
storage_key: &[u8],
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);
@@ -37,83 +52,178 @@ pub fn get<T: Decode + Sized>(storage_key: &[u8], key: &[u8]) -> Option<T> {
/// 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], key: &[u8]) -> T {
get(storage_key, key).unwrap_or_else(Default::default)
pub fn get_or_default<T: Decode + Sized + Default>(
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
) -> T {
get(storage_key, 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], key: &[u8], default_value: T) -> T {
get(storage_key, key).unwrap_or(default_value)
pub fn get_or<T: Decode + Sized>(
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
default_value: T,
) -> T {
get(storage_key, 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,
key: &[u8],
default_value: F,
) -> T {
get(storage_key, key).unwrap_or_else(default_value)
get(storage_key, child_info, key).unwrap_or_else(default_value)
}
/// Put `value` in storage under `key`.
pub fn put<T: Encode>(storage_key: &[u8], key: &[u8], value: &T) {
value.using_encoded(|slice| sp_io::storage::child_set(storage_key, key, slice));
pub fn put<T: Encode>(
storage_key: &[u8],
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,
)
);
}
/// 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], key: &[u8]) -> Option<T> {
let r = get(storage_key, key);
pub fn take<T: Decode + Sized>(
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
) -> Option<T> {
let r = get(storage_key, child_info, key);
if r.is_some() {
kill(storage_key, key);
kill(storage_key, child_info, key);
}
r
}
/// 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], key: &[u8]) -> T {
take(storage_key, key).unwrap_or_else(Default::default)
pub fn take_or_default<T: Codec + Sized + Default>(
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
) -> T {
take(storage_key, 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],key: &[u8], default_value: T) -> T {
take(storage_key, key).unwrap_or(default_value)
pub fn take_or<T: Codec + Sized>(
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
default_value: T,
) -> T {
take(storage_key, 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,
key: &[u8],
default_value: F,
) -> T {
take(storage_key, key).unwrap_or_else(default_value)
take(storage_key, 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], key: &[u8]) -> bool {
sp_io::storage::child_read(storage_key, key, &mut [0;0][..], 0).is_some()
pub fn exists(
storage_key: &[u8],
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()
}
/// Remove all `storage_key` key/values
pub fn kill_storage(storage_key: &[u8]) {
sp_io::storage::child_storage_kill(storage_key)
pub fn kill_storage(
storage_key: &[u8],
child_info: ChildInfo,
) {
let (data, child_type) = child_info.info();
sp_io::storage::child_storage_kill(
storage_key,
data,
child_type,
)
}
/// Ensure `key` has no explicit entry in storage.
pub fn kill(storage_key: &[u8], key: &[u8]) {
sp_io::storage::child_clear(storage_key, key);
pub fn kill(
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
) {
let (data, child_type) = child_info.info();
sp_io::storage::child_clear(
storage_key,
data,
child_type,
key,
);
}
/// Get a Vec of bytes from storage.
pub fn get_raw(storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>> {
sp_io::storage::child_get(storage_key, key)
pub fn get_raw(
storage_key: &[u8],
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,
)
}
/// Put a raw byte slice into storage.
pub fn put_raw(storage_key: &[u8], key: &[u8], value: &[u8]) {
sp_io::storage::child_set(storage_key, key, value)
pub fn put_raw(
storage_key: &[u8],
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,
)
}
/// Calculate current child root value.
pub fn child_root(
storage_key: &[u8],
) -> Vec<u8> {
sp_io::storage::child_root(
storage_key,
)
}