Add typedefs for storage types (#4654)

* Add typedefs for storage types

* Fix after merge
This commit is contained in:
Stanislav Tkach
2020-01-17 10:20:20 +02:00
committed by Bastian Köcher
parent 20ce6c120c
commit 482ca522cc
19 changed files with 202 additions and 160 deletions
@@ -21,13 +21,17 @@ use std::collections::btree_map::Entry;
use codec::{Decode, Encode};
use hash_db::Hasher;
use num_traits::One;
use crate::backend::Backend;
use crate::overlayed_changes::OverlayedChanges;
use crate::trie_backend_essence::TrieBackendEssence;
use crate::changes_trie::build_iterator::digest_build_iterator;
use crate::changes_trie::input::{InputKey, InputPair, DigestIndex, ExtrinsicIndex};
use crate::changes_trie::{AnchorBlockId, ConfigurationRange, Storage, BlockNumber};
use crate::changes_trie::input::ChildIndex;
use crate::{
StorageKey,
backend::Backend,
overlayed_changes::OverlayedChanges,
trie_backend_essence::TrieBackendEssence,
changes_trie::{
AnchorBlockId, ConfigurationRange, Storage, BlockNumber,
build_iterator::digest_build_iterator,
input::{InputKey, InputPair, DigestIndex, ExtrinsicIndex, ChildIndex},
},
};
/// Prepare input pairs for building a changes trie of given block.
///
@@ -101,7 +105,7 @@ fn prepare_extrinsics_input<'a, B, H, Number>(
Number: BlockNumber,
{
let mut children_keys = BTreeSet::<Vec<u8>>::new();
let mut children_keys = BTreeSet::<StorageKey>::new();
let mut children_result = BTreeMap::new();
for (storage_key, _) in changes.prospective.children.iter()
.chain(changes.committed.children.iter()) {
@@ -126,7 +130,7 @@ fn prepare_extrinsics_input_inner<'a, B, H, Number>(
backend: &'a B,
block: &Number,
changes: &'a OverlayedChanges,
storage_key: Option<Vec<u8>>,
storage_key: Option<StorageKey>,
) -> Result<impl Iterator<Item=InputPair<Number>> + 'a, String>
where
B: Backend<H>,
@@ -231,7 +235,7 @@ fn prepare_digest_input<'a, H, Number>(
let trie_root = storage.root(parent, digest_build_block.clone())?;
let trie_root = trie_root.ok_or_else(|| format!("No changes trie root for block {}", digest_build_block.clone()))?;
let insert_to_map = |map: &mut BTreeMap<_,_>, key: Vec<u8>| {
let insert_to_map = |map: &mut BTreeMap<_,_>, key: StorageKey| {
match map.entry(key.clone()) {
Entry::Vacant(entry) => {
entry.insert((DigestIndex {
@@ -277,7 +281,7 @@ fn prepare_digest_input<'a, H, Number>(
return Ok((map, child_map));
}
let mut children_roots = BTreeMap::<Vec<u8>, _>::new();
let mut children_roots = BTreeMap::<StorageKey, _>::new();
{
let trie_storage = TrieBackendEssence::<_, H>::new(
crate::changes_trie::TrieBackendStorageAdapter(storage),
@@ -18,6 +18,8 @@
use std::collections::{HashMap, HashSet};
use crate::StorageKey;
/// Changes trie build cache.
///
/// Helps to avoid read of changes tries from the database when digest trie
@@ -36,7 +38,7 @@ pub struct BuildCache<H, N> {
/// The `Option<Vec<u8>>` in inner `HashMap` stands for the child storage key.
/// If it is `None`, then the `HashSet` contains keys changed in top-level storage.
/// If it is `Some`, then the `HashSet` contains keys changed in child storage, identified by the key.
changed_keys: HashMap<H, HashMap<Option<Vec<u8>>, HashSet<Vec<u8>>>>,
changed_keys: HashMap<H, HashMap<Option<StorageKey>, HashSet<StorageKey>>>,
}
/// The action to perform when block-with-changes-trie is imported.
@@ -54,7 +56,7 @@ pub struct CachedBuildData<H, N> {
block: N,
trie_root: H,
digest_input_blocks: Vec<N>,
changed_keys: HashMap<Option<Vec<u8>>, HashSet<Vec<u8>>>,
changed_keys: HashMap<Option<StorageKey>, HashSet<StorageKey>>,
}
/// The action to perform when block-with-changes-trie is imported.
@@ -70,7 +72,7 @@ pub(crate) enum IncompleteCacheAction<N> {
#[derive(Debug, PartialEq)]
pub(crate) struct IncompleteCachedBuildData<N> {
digest_input_blocks: Vec<N>,
changed_keys: HashMap<Option<Vec<u8>>, HashSet<Vec<u8>>>,
changed_keys: HashMap<Option<StorageKey>, HashSet<StorageKey>>,
}
impl<H, N> BuildCache<H, N>
@@ -87,7 +89,7 @@ impl<H, N> BuildCache<H, N>
}
/// Get cached changed keys for changes trie with given root.
pub fn get(&self, root: &H) -> Option<&HashMap<Option<Vec<u8>>, HashSet<Vec<u8>>>> {
pub fn get(&self, root: &H) -> Option<&HashMap<Option<StorageKey>, HashSet<StorageKey>>> {
self.changed_keys.get(&root)
}
@@ -96,7 +98,7 @@ impl<H, N> BuildCache<H, N>
pub fn with_changed_keys(
&self,
root: &H,
functor: &mut dyn FnMut(&HashMap<Option<Vec<u8>>, HashSet<Vec<u8>>>),
functor: &mut dyn FnMut(&HashMap<Option<StorageKey>, HashSet<StorageKey>>),
) -> bool {
match self.changed_keys.get(&root) {
Some(changed_keys) => {
@@ -162,8 +164,8 @@ impl<N> IncompleteCacheAction<N> {
/// Insert changed keys of given storage into cached data.
pub(crate) fn insert(
self,
storage_key: Option<Vec<u8>>,
changed_keys: HashSet<Vec<u8>>,
storage_key: Option<StorageKey>,
changed_keys: HashSet<StorageKey>,
) -> Self {
match self {
IncompleteCacheAction::CacheBuildData(build_data) =>
@@ -198,8 +200,8 @@ impl<N> IncompleteCachedBuildData<N> {
fn insert(
mut self,
storage_key: Option<Vec<u8>>,
changed_keys: HashSet<Vec<u8>>,
storage_key: Option<StorageKey>,
changed_keys: HashSet<StorageKey>,
) -> Self {
self.changed_keys.insert(storage_key, changed_keys);
self
@@ -259,4 +261,4 @@ mod tests {
assert_eq!(cache.changed_keys.len(), 0);
}
}
}
@@ -17,7 +17,10 @@
//! Different types of changes trie input pairs.
use codec::{Decode, Encode, Input, Output, Error};
use crate::changes_trie::BlockNumber;
use crate::{
StorageKey, StorageValue,
changes_trie::BlockNumber
};
/// Key of { changed key => set of extrinsic indices } mapping.
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -25,7 +28,7 @@ pub struct ExtrinsicIndex<Number: BlockNumber> {
/// Block at which this key has been inserted in the trie.
pub block: Number,
/// Storage key this node is responsible for.
pub key: Vec<u8>,
pub key: StorageKey,
}
/// Value of { changed key => set of extrinsic indices } mapping.
@@ -37,7 +40,7 @@ pub struct DigestIndex<Number: BlockNumber> {
/// Block at which this key has been inserted in the trie.
pub block: Number,
/// Storage key this node is responsible for.
pub key: Vec<u8>,
pub key: StorageKey,
}
/// Key of { childtrie key => Childchange trie } mapping.
@@ -46,7 +49,7 @@ pub struct ChildIndex<Number: BlockNumber> {
/// Block at which this key has been inserted in the trie.
pub block: Number,
/// Storage key this node is responsible for.
pub storage_key: Vec<u8>,
pub storage_key: StorageKey,
}
/// Value of { changed key => block/digest block numbers } mapping.
@@ -89,8 +92,8 @@ impl<Number: BlockNumber> InputPair<Number> {
}
}
impl<Number: BlockNumber> Into<(Vec<u8>, Vec<u8>)> for InputPair<Number> {
fn into(self) -> (Vec<u8>, Vec<u8>) {
impl<Number: BlockNumber> Into<(StorageKey, StorageValue)> for InputPair<Number> {
fn into(self) -> (StorageKey, StorageValue) {
match self {
InputPair::ExtrinsicIndex(key, value) => (key.encode(), value.encode()),
InputPair::DigestIndex(key, value) => (key.encode(), value.encode()),
@@ -68,15 +68,20 @@ pub use self::prune::prune;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use hash_db::{Hasher, Prefix};
use crate::backend::Backend;
use num_traits::{One, Zero};
use codec::{Decode, Encode};
use sp_core;
use crate::changes_trie::build::prepare_input;
use crate::changes_trie::build_cache::{IncompleteCachedBuildData, IncompleteCacheAction};
use crate::overlayed_changes::OverlayedChanges;
use sp_trie::{MemoryDB, DBValue, TrieMut};
use sp_trie::trie_types::TrieDBMut;
use crate::{
StorageKey,
backend::Backend,
overlayed_changes::OverlayedChanges,
changes_trie::{
build::prepare_input,
build_cache::{IncompleteCachedBuildData, IncompleteCacheAction},
},
};
/// Changes that are made outside of extrinsics are marked with this index;
pub const NO_EXTRINSIC_INDEX: u32 = 0xffffffff;
@@ -151,7 +156,7 @@ pub trait Storage<H: Hasher, Number: BlockNumber>: RootsStorage<H, Number> {
fn with_cached_changed_keys(
&self,
root: &H::Out,
functor: &mut dyn FnMut(&HashMap<Option<Vec<u8>>, HashSet<Vec<u8>>>),
functor: &mut dyn FnMut(&HashMap<Option<StorageKey>, HashSet<StorageKey>>),
) -> bool;
/// Get a trie node.
fn get(&self, key: &H::Out, prefix: Prefix) -> Result<Option<DBValue>, String>;
@@ -21,8 +21,11 @@ use hash_db::{Hasher, Prefix, EMPTY_PREFIX};
use sp_trie::DBValue;
use sp_trie::MemoryDB;
use parking_lot::RwLock;
use crate::changes_trie::{BuildCache, RootsStorage, Storage, AnchorBlockId, BlockNumber};
use crate::trie_backend_essence::TrieBackendStorage;
use crate::{
StorageKey,
trie_backend_essence::TrieBackendStorage,
changes_trie::{BuildCache, RootsStorage, Storage, AnchorBlockId, BlockNumber},
};
#[cfg(test)]
use crate::backend::insert_into_memory_db;
@@ -93,7 +96,7 @@ impl<H: Hasher, Number: BlockNumber> InMemoryStorage<H, Number> {
#[cfg(test)]
pub fn with_inputs(
mut top_inputs: Vec<(Number, Vec<InputPair<Number>>)>,
children_inputs: Vec<(Vec<u8>, Vec<(Number, Vec<InputPair<Number>>)>)>,
children_inputs: Vec<(StorageKey, Vec<(Number, Vec<InputPair<Number>>)>)>,
) -> Self {
let mut mdb = MemoryDB::default();
let mut roots = BTreeMap::new();
@@ -179,7 +182,7 @@ impl<H: Hasher, Number: BlockNumber> Storage<H, Number> for InMemoryStorage<H, N
fn with_cached_changed_keys(
&self,
root: &H::Out,
functor: &mut dyn FnMut(&HashMap<Option<Vec<u8>>, HashSet<Vec<u8>>>),
functor: &mut dyn FnMut(&HashMap<Option<StorageKey>, HashSet<StorageKey>>),
) -> bool {
self.cache.with_changed_keys(root, functor)
}