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
@@ -20,7 +20,7 @@ use log::warn;
use hash_db::Hasher;
use codec::{Decode, Encode};
use sp_core::{traits::RuntimeCode, storage::{ChildInfo, OwnedChildInfo, well_known_keys}};
use sp_core::{traits::RuntimeCode, storage::{ChildInfo, well_known_keys}};
use sp_trie::{TrieMut, MemoryDB, trie_types::TrieDBMut};
use crate::{
@@ -54,19 +54,17 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
/// Get keyed child storage or None if there is nothing associated.
fn child_storage(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> Result<Option<StorageValue>, Self::Error>;
/// Get child keyed storage value hash or None if there is nothing associated.
fn child_storage_hash(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> Result<Option<H::Out>, Self::Error> {
self.child_storage(storage_key, child_info, key).map(|v| v.map(|v| H::hash(&v)))
self.child_storage(child_info, key).map(|v| v.map(|v| H::hash(&v)))
}
/// true if a key exists in storage.
@@ -77,11 +75,10 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
/// true if a key exists in child storage.
fn exists_child_storage(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> Result<bool, Self::Error> {
Ok(self.child_storage(storage_key, child_info, key)?.is_some())
Ok(self.child_storage(child_info, key)?.is_some())
}
/// Return the next key in storage in lexicographic order or `None` if there is no value.
@@ -90,16 +87,14 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
/// Return the next key in child storage in lexicographic order or `None` if there is no value.
fn next_child_storage_key(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8]
) -> Result<Option<StorageKey>, Self::Error>;
/// Retrieve all entries keys of child storage and call `f` for each of those keys.
fn for_keys_in_child_storage<F: FnMut(&[u8])>(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
f: F,
);
@@ -118,8 +113,7 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
/// call `f` for each of those keys.
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
prefix: &[u8],
f: F,
);
@@ -137,8 +131,7 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
/// is true if child storage root equals default storage root.
fn child_storage_root<I>(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
delta: I,
) -> (H::Out, bool, Self::Transaction)
where
@@ -158,12 +151,11 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
/// Get all keys of child storage with given prefix
fn child_keys(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
prefix: &[u8],
) -> Vec<StorageKey> {
let mut all = Vec::new();
self.for_child_keys_with_prefix(storage_key, child_info, prefix, |k| all.push(k.to_vec()));
self.for_child_keys_with_prefix(child_info, prefix, |k| all.push(k.to_vec()));
all
}
@@ -183,20 +175,21 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
where
I1: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
I2i: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
I2: IntoIterator<Item=(StorageKey, I2i, OwnedChildInfo)>,
I2: IntoIterator<Item=(ChildInfo, I2i)>,
H::Out: Ord + Encode,
{
let mut txs: Self::Transaction = Default::default();
let mut child_roots: Vec<_> = Default::default();
// child first
for (storage_key, child_delta, child_info) in child_deltas {
for (child_info, child_delta) in child_deltas {
let (child_root, empty, child_txs) =
self.child_storage_root(&storage_key[..], child_info.as_ref(), child_delta);
self.child_storage_root(&child_info, child_delta);
let prefixed_storage_key = child_info.prefixed_storage_key();
txs.consolidate(child_txs);
if empty {
child_roots.push((storage_key, None));
child_roots.push((prefixed_storage_key.into_inner(), None));
} else {
child_roots.push((storage_key, Some(child_root.encode())));
child_roots.push((prefixed_storage_key.into_inner(), Some(child_root.encode())));
}
}
let (root, parent_txs) = self.storage_root(
@@ -239,20 +232,18 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
fn child_storage(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> Result<Option<StorageKey>, Self::Error> {
(*self).child_storage(storage_key, child_info, key)
(*self).child_storage(child_info, key)
}
fn for_keys_in_child_storage<F: FnMut(&[u8])>(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
f: F,
) {
(*self).for_keys_in_child_storage(storage_key, child_info, f)
(*self).for_keys_in_child_storage(child_info, f)
}
fn next_storage_key(&self, key: &[u8]) -> Result<Option<StorageKey>, Self::Error> {
@@ -261,11 +252,10 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
fn next_child_storage_key(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
key: &[u8],
) -> Result<Option<StorageKey>, Self::Error> {
(*self).next_child_storage_key(storage_key, child_info, key)
(*self).next_child_storage_key(child_info, key)
}
fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], f: F) {
@@ -274,12 +264,11 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
prefix: &[u8],
f: F,
) {
(*self).for_child_keys_with_prefix(storage_key, child_info, prefix, f)
(*self).for_child_keys_with_prefix(child_info, prefix, f)
}
fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
@@ -292,15 +281,14 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
fn child_storage_root<I>(
&self,
storage_key: &[u8],
child_info: ChildInfo,
child_info: &ChildInfo,
delta: I,
) -> (H::Out, bool, Self::Transaction)
where
I: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
H::Out: Ord,
{
(*self).child_storage_root(storage_key, child_info, delta)
(*self).child_storage_root(child_info, delta)
}
fn pairs(&self) -> Vec<(StorageKey, StorageValue)> {
@@ -331,7 +319,7 @@ impl Consolidate for () {
}
impl Consolidate for Vec<(
Option<(StorageKey, OwnedChildInfo)>,
Option<ChildInfo>,
StorageCollection,
)> {
fn consolidate(&mut self, mut other: Self) {