Rework storage iterators (#13284)

* Rework storage iterators

* Make sure storage iteration is also accounted for when benchmarking

* Use `trie-db` from crates.io

* Appease clippy

* Bump `trie-bench` to 0.35.0

* Fix tests' compilation

* Update comment to clarify how `IterArgs::start_at` works

* Add extra tests

* Fix iterators on `Client` so that they behave as before

* Add extra `unwrap`s in tests

* More clippy fixes

* Come on clippy, give me a break already

* Rename `allow_missing` to `stop_on_incomplete_database`

* Add `#[inline]` to `with_recorder_and_cache`

* Use `with_recorder_and_cache` in `with_trie_db`; add doc comment

* Simplify code: use `with_trie_db` in `next_storage_key_from_root`

* Remove `expect`s in the benchmarking CLI

* Add extra doc comments

* Move `RawIter` before `TrieBackendEssence` (no code changes; just cut-paste)

* Remove a TODO in tests

* Update comment for `StorageIterator::was_complete`

* Update `trie-db` to 0.25.1
This commit is contained in:
Koute
2023-02-22 16:49:25 +09:00
committed by GitHub
parent 236bbbd5ef
commit f8e3bdad3d
27 changed files with 1097 additions and 742 deletions
@@ -21,7 +21,10 @@ use sc_client_api::{StorageProvider, UsageProvider};
use sp_core::storage::{well_known_keys, ChildInfo, Storage, StorageChild, StorageKey, StorageMap};
use sp_runtime::traits::Block as BlockT;
use std::{collections::HashMap, sync::Arc};
use std::{
collections::{BTreeMap, HashMap},
sync::Arc,
};
/// Export the raw state at the given `block`. If `block` is `None`, the
/// best block will be used.
@@ -31,35 +34,30 @@ where
B: BlockT,
BA: sc_client_api::backend::Backend<B>,
{
let empty_key = StorageKey(Vec::new());
let mut top_storage = client.storage_pairs(hash, &empty_key)?;
let mut top = BTreeMap::new();
let mut children_default = HashMap::new();
// Remove all default child storage roots from the top storage and collect the child storage
// pairs.
while let Some(pos) = top_storage
.iter()
.position(|(k, _)| k.0.starts_with(well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX))
{
let (key, _) = top_storage.swap_remove(pos);
let key =
StorageKey(key.0[well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX.len()..].to_vec());
let child_info = ChildInfo::new_default(&key.0);
let keys = client.child_storage_keys(hash, &child_info, &empty_key)?;
let mut pairs = StorageMap::new();
keys.into_iter().try_for_each(|k| {
if let Some(value) = client.child_storage(hash, &child_info, &k)? {
pairs.insert(k.0, value.0);
for (key, value) in client.storage_pairs(hash, None, None)? {
// Remove all default child storage roots from the top storage and collect the child storage
// pairs.
if key.0.starts_with(well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX) {
let child_root_key = StorageKey(
key.0[well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX.len()..].to_vec(),
);
let child_info = ChildInfo::new_default(&child_root_key.0);
let mut pairs = StorageMap::new();
for child_key in client.child_storage_keys(hash, child_info.clone(), None, None)? {
if let Some(child_value) = client.child_storage(hash, &child_info, &child_key)? {
pairs.insert(child_key.0, child_value.0);
}
}
Ok::<_, Error>(())
})?;
children_default.insert(child_root_key.0, StorageChild { child_info, data: pairs });
continue
}
children_default.insert(key.0, StorageChild { child_info, data: pairs });
top.insert(key.0, value.0);
}
let top = top_storage.into_iter().map(|(k, v)| (k.0, v.0)).collect();
Ok(Storage { top, children_default })
}