From ef3310d21e662289b67c8d53e5cf77038ff538a6 Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Thu, 16 Jan 2020 23:55:14 +0200 Subject: [PATCH 1/2] Add typedefs for storage types --- client/api/src/backend.rs | 11 ++-- client/db/src/lib.rs | 4 +- client/db/src/storage_cache.rs | 8 +-- client/src/in_mem.rs | 4 +- client/src/light/backend.rs | 4 +- primitives/state-machine/src/backend.rs | 46 ++++++++--------- primitives/state-machine/src/basic.rs | 30 +++++------ .../state-machine/src/changes_trie/build.rs | 26 ++++++---- .../src/changes_trie/build_cache.rs | 22 ++++---- .../state-machine/src/changes_trie/input.rs | 15 +++--- .../state-machine/src/changes_trie/mod.rs | 15 ++++-- .../state-machine/src/changes_trie/storage.rs | 11 ++-- primitives/state-machine/src/ext.rs | 23 +++++---- .../state-machine/src/in_memory_backend.rs | 44 ++++++++-------- primitives/state-machine/src/lib.rs | 5 +- .../state-machine/src/overlayed_changes.rs | 50 ++++++++++++------- 16 files changed, 176 insertions(+), 142 deletions(-) diff --git a/client/api/src/backend.rs b/client/api/src/backend.rs index 292031a4cb9de..61cc527b4e8ab 100644 --- a/client/api/src/backend.rs +++ b/client/api/src/backend.rs @@ -22,7 +22,10 @@ use sp_core::ChangesTrieConfiguration; use sp_core::offchain::OffchainStorage; use sp_runtime::{generic::BlockId, Justification, Storage}; use sp_runtime::traits::{Block as BlockT, NumberFor, HasherFor}; -use sp_state_machine::{ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction}; +use sp_state_machine::{ + ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction, StorageCollection, + ChildStorageCollection, +}; use crate::{ blockchain::{ Backend as BlockchainBackend, well_known_cache_keys @@ -45,12 +48,6 @@ pub type TransactionForSB = >>::Tra /// Extracts the transaction for the given backend. pub type TransactionFor = TransactionForSB, Block>; -/// In memory array of storage values. -pub type StorageCollection = Vec<(Vec, Option>)>; - -/// In memory arrays of storage values for multiple child tries. -pub type ChildStorageCollection = Vec<(Vec, StorageCollection)>; - /// Import operation summary. /// /// Contains information about the block that just got imported, diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index b4eae2db2b49f..21522975df1df 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -42,7 +42,6 @@ use std::collections::{HashMap, HashSet}; use sc_client_api::{execution_extensions::ExecutionExtensions, ForkBlocks, UsageInfo, MemoryInfo, BadBlocks, IoInfo}; use sc_client_api::backend::NewBlockState; -use sc_client_api::backend::{StorageCollection, ChildStorageCollection}; use sp_blockchain::{ Result as ClientResult, Error as ClientError, well_known_cache_keys, HeaderBackend, @@ -64,7 +63,8 @@ use sp_runtime::traits::{ use sc_executor::RuntimeInfo; use sp_state_machine::{ DBValue, ChangesTrieTransaction, ChangesTrieCacheAction, ChangesTrieBuildCache, - backend::Backend as StateBackend, UsageInfo as StateUsageInfo, + backend::Backend as StateBackend, UsageInfo as StateUsageInfo, StorageCollection, + ChildStorageCollection, }; use crate::utils::{Meta, db_err, meta_keys, read_db, read_meta}; use sc_client::leaves::{LeafSet, FinalizationDisplaced}; diff --git a/client/db/src/storage_cache.rs b/client/db/src/storage_cache.rs index 4f7e906a0ae28..fd85a899b628e 100644 --- a/client/db/src/storage_cache.rs +++ b/client/db/src/storage_cache.rs @@ -24,17 +24,17 @@ use hash_db::Hasher; use sp_runtime::traits::{Block as BlockT, Header, HasherFor, NumberFor}; use sp_core::hexdisplay::HexDisplay; use sp_core::storage::ChildInfo; -use sp_state_machine::{backend::Backend as StateBackend, TrieBackend}; +use sp_state_machine::{ + backend::Backend as StateBackend, TrieBackend, StorageKey, StorageValue, + StorageCollection, ChildStorageCollection, +}; use log::trace; -use sc_client_api::backend::{StorageCollection, ChildStorageCollection}; use std::hash::Hash as StdHash; use crate::stats::StateUsageStats; const STATE_CACHE_BLOCKS: usize = 12; -type StorageKey = Vec; type ChildStorageKey = (Vec, Vec); -type StorageValue = Vec; /// Shared canonical state cache. pub struct Cache { diff --git a/client/src/in_mem.rs b/client/src/in_mem.rs index 5d0d14c1cb1d7..68d73f03f2852 100644 --- a/client/src/in_mem.rs +++ b/client/src/in_mem.rs @@ -28,14 +28,14 @@ use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero, NumberFor, Ha use sp_runtime::{Justification, Storage}; use sp_state_machine::{ InMemoryChangesTrieStorage, ChangesTrieAnchorBlockId, ChangesTrieTransaction, - InMemoryBackend, Backend as StateBackend, + InMemoryBackend, Backend as StateBackend, StorageCollection, ChildStorageCollection, }; use hash_db::Prefix; use sp_trie::MemoryDB; use sp_blockchain::{CachedHeaderMetadata, HeaderMetadata}; use sc_client_api::{ - backend::{self, NewBlockState, StorageCollection, ChildStorageCollection}, + backend::{self, NewBlockState}, blockchain::{ self, BlockStatus, HeaderBackend, well_known_cache_keys::Id as CacheKeyId }, diff --git a/client/src/light/backend.rs b/client/src/light/backend.rs index 65fd94e32395c..8a9ae9922a73d 100644 --- a/client/src/light/backend.rs +++ b/client/src/light/backend.rs @@ -24,7 +24,8 @@ use parking_lot::RwLock; use sp_core::storage::{ChildInfo, OwnedChildInfo}; use sp_core::offchain::storage::InMemOffchainStorage; use sp_state_machine::{ - Backend as StateBackend, TrieBackend, InMemoryBackend, ChangesTrieTransaction + Backend as StateBackend, TrieBackend, InMemoryBackend, ChangesTrieTransaction, + StorageCollection, ChildStorageCollection, }; use sp_runtime::{generic::BlockId, Justification, Storage}; use sp_runtime::traits::{Block as BlockT, NumberFor, Zero, Header, HasherFor}; @@ -33,7 +34,6 @@ use sp_blockchain::{Error as ClientError, Result as ClientResult}; use sc_client_api::{ backend::{ AuxStore, Backend as ClientBackend, BlockImportOperation, RemoteBackend, NewBlockState, - StorageCollection, ChildStorageCollection, }, blockchain::{ HeaderBackend as BlockchainHeaderBackend, well_known_cache_keys, diff --git a/primitives/state-machine/src/backend.rs b/primitives/state-machine/src/backend.rs index 75733d68b3ffa..4ef9b970ae21d 100644 --- a/primitives/state-machine/src/backend.rs +++ b/primitives/state-machine/src/backend.rs @@ -26,7 +26,7 @@ use sp_trie::{TrieMut, MemoryDB, trie_types::TrieDBMut}; use crate::{ trie_backend::TrieBackend, trie_backend_essence::TrieBackendStorage, - UsageInfo, + UsageInfo, StorageKey, StorageValue, StorageCollection, }; /// A state backend is used to read state data and can have changes committed @@ -44,7 +44,7 @@ pub trait Backend: std::fmt::Debug { type TrieBackendStorage: TrieBackendStorage; /// Get keyed storage or None if there is nothing associated. - fn storage(&self, key: &[u8]) -> Result>, Self::Error>; + fn storage(&self, key: &[u8]) -> Result, Self::Error>; /// Get keyed storage value hash or None if there is nothing associated. fn storage_hash(&self, key: &[u8]) -> Result, Self::Error> { @@ -57,7 +57,7 @@ pub trait Backend: std::fmt::Debug { storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, Self::Error>; + ) -> Result, Self::Error>; /// Get child keyed storage value hash or None if there is nothing associated. fn child_storage_hash( @@ -85,7 +85,7 @@ pub trait Backend: std::fmt::Debug { } /// Return the next key in storage in lexicographic order or `None` if there is no value. - fn next_storage_key(&self, key: &[u8]) -> Result>, Self::Error>; + fn next_storage_key(&self, key: &[u8]) -> Result, Self::Error>; /// Return the next key in child storage in lexicographic order or `None` if there is no value. fn next_child_storage_key( @@ -93,7 +93,7 @@ pub trait Backend: std::fmt::Debug { storage_key: &[u8], child_info: ChildInfo, key: &[u8] - ) -> Result>, Self::Error>; + ) -> Result, Self::Error>; /// Retrieve all entries keys of child storage and call `f` for each of those keys. fn for_keys_in_child_storage( @@ -129,7 +129,7 @@ pub trait Backend: std::fmt::Debug { /// Does not include child storage updates. fn storage_root(&self, delta: I) -> (H::Out, Self::Transaction) where - I: IntoIterator, Option>)>, + I: IntoIterator)>, H::Out: Ord; /// Calculate the child storage root, with given delta over what is already stored in @@ -142,14 +142,14 @@ pub trait Backend: std::fmt::Debug { delta: I, ) -> (H::Out, bool, Self::Transaction) where - I: IntoIterator, Option>)>, + I: IntoIterator)>, H::Out: Ord; /// Get all key/value pairs into a Vec. - fn pairs(&self) -> Vec<(Vec, Vec)>; + fn pairs(&self) -> Vec<(StorageKey, StorageValue)>; /// Get all keys with given prefix - fn keys(&self, prefix: &[u8]) -> Vec> { + fn keys(&self, prefix: &[u8]) -> Vec { let mut all = Vec::new(); self.for_keys_with_prefix(prefix, |k| all.push(k.to_vec())); all @@ -161,7 +161,7 @@ pub trait Backend: std::fmt::Debug { storage_key: &[u8], child_info: ChildInfo, prefix: &[u8], - ) -> Vec> { + ) -> Vec { let mut all = Vec::new(); self.for_child_keys_with_prefix(storage_key, child_info, prefix, |k| all.push(k.to_vec())); all @@ -181,9 +181,9 @@ pub trait Backend: std::fmt::Debug { child_deltas: I2) -> (H::Out, Self::Transaction) where - I1: IntoIterator, Option>)>, - I2i: IntoIterator, Option>)>, - I2: IntoIterator, I2i, OwnedChildInfo)>, + I1: IntoIterator)>, + I2i: IntoIterator)>, + I2: IntoIterator, H::Out: Ord + Encode, { let mut txs: Self::Transaction = Default::default(); @@ -220,7 +220,7 @@ impl<'a, T: Backend, H: Hasher> Backend for &'a T { type Transaction = T::Transaction; type TrieBackendStorage = T::TrieBackendStorage; - fn storage(&self, key: &[u8]) -> Result>, Self::Error> { + fn storage(&self, key: &[u8]) -> Result, Self::Error> { (*self).storage(key) } @@ -229,7 +229,7 @@ impl<'a, T: Backend, H: Hasher> Backend for &'a T { storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, Self::Error> { + ) -> Result, Self::Error> { (*self).child_storage(storage_key, child_info, key) } @@ -242,7 +242,7 @@ impl<'a, T: Backend, H: Hasher> Backend for &'a T { (*self).for_keys_in_child_storage(storage_key, child_info, f) } - fn next_storage_key(&self, key: &[u8]) -> Result>, Self::Error> { + fn next_storage_key(&self, key: &[u8]) -> Result, Self::Error> { (*self).next_storage_key(key) } @@ -251,7 +251,7 @@ impl<'a, T: Backend, H: Hasher> Backend for &'a T { storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, Self::Error> { + ) -> Result, Self::Error> { (*self).next_child_storage_key(storage_key, child_info, key) } @@ -271,7 +271,7 @@ impl<'a, T: Backend, H: Hasher> Backend for &'a T { fn storage_root(&self, delta: I) -> (H::Out, Self::Transaction) where - I: IntoIterator, Option>)>, + I: IntoIterator)>, H::Out: Ord, { (*self).storage_root(delta) @@ -284,13 +284,13 @@ impl<'a, T: Backend, H: Hasher> Backend for &'a T { delta: I, ) -> (H::Out, bool, Self::Transaction) where - I: IntoIterator, Option>)>, + I: IntoIterator)>, H::Out: Ord, { (*self).child_storage_root(storage_key, child_info, delta) } - fn pairs(&self) -> Vec<(Vec, Vec)> { + fn pairs(&self) -> Vec<(StorageKey, StorageValue)> { (*self).pairs() } @@ -316,8 +316,8 @@ impl Consolidate for () { } impl Consolidate for Vec<( - Option<(Vec, OwnedChildInfo)>, - Vec<(Vec, Option>)>, + Option<(StorageKey, OwnedChildInfo)>, + StorageCollection, )> { fn consolidate(&mut self, mut other: Self) { self.append(&mut other); @@ -334,7 +334,7 @@ impl> Consolidate for sp_trie::GenericMem pub(crate) fn insert_into_memory_db(mdb: &mut MemoryDB, input: I) -> Option where H: Hasher, - I: IntoIterator, Vec)>, + I: IntoIterator, { let mut root = ::Out::default(); { diff --git a/primitives/state-machine/src/basic.rs b/primitives/state-machine/src/basic.rs index d06aedfc4b6ee..d905657737a8a 100644 --- a/primitives/state-machine/src/basic.rs +++ b/primitives/state-machine/src/basic.rs @@ -19,7 +19,7 @@ use std::{ collections::BTreeMap, any::{TypeId, Any}, iter::FromIterator, ops::Bound }; -use crate::{Backend, InMemoryBackend}; +use crate::{Backend, InMemoryBackend, StorageKey, StorageValue}; use hash_db::Hasher; use sp_trie::{TrieConfiguration, default_child_trie_root}; use sp_trie::trie_types::Layout; @@ -46,7 +46,7 @@ impl BasicExternalities { } /// Insert key/value - pub fn insert(&mut self, k: Vec, v: Vec) -> Option> { + pub fn insert(&mut self, k: StorageKey, v: StorageValue) -> Option { self.inner.top.insert(k, v) } @@ -89,8 +89,8 @@ impl PartialEq for BasicExternalities { } } -impl FromIterator<(Vec, Vec)> for BasicExternalities { - fn from_iter, Vec)>>(iter: I) -> Self { +impl FromIterator<(StorageKey, StorageValue)> for BasicExternalities { + fn from_iter>(iter: I) -> Self { let mut t = Self::default(); t.inner.top.extend(iter); t @@ -101,8 +101,8 @@ impl Default for BasicExternalities { fn default() -> Self { Self::new(Default::default()) } } -impl From, Vec>> for BasicExternalities { - fn from(hashmap: BTreeMap, Vec>) -> Self { +impl From> for BasicExternalities { + fn from(hashmap: BTreeMap) -> Self { BasicExternalities { inner: Storage { top: hashmap, children: Default::default(), @@ -111,7 +111,7 @@ impl From, Vec>> for BasicExternalities { } impl Externalities for BasicExternalities { - fn storage(&self, key: &[u8]) -> Option> { + fn storage(&self, key: &[u8]) -> Option { self.inner.top.get(key).cloned() } @@ -119,7 +119,7 @@ impl Externalities for BasicExternalities { self.storage(key).map(|v| Blake2Hasher::hash(&v).encode()) } - fn original_storage(&self, key: &[u8]) -> Option> { + fn original_storage(&self, key: &[u8]) -> Option { self.storage(key) } @@ -132,7 +132,7 @@ impl Externalities for BasicExternalities { storage_key: ChildStorageKey, _child_info: ChildInfo, key: &[u8], - ) -> Option> { + ) -> Option { self.inner.children.get(storage_key.as_ref()).and_then(|child| child.data.get(key)).cloned() } @@ -159,11 +159,11 @@ impl Externalities for BasicExternalities { storage_key: ChildStorageKey, child_info: ChildInfo, key: &[u8], - ) -> Option> { + ) -> Option { Externalities::child_storage(self, storage_key, child_info, key) } - fn next_storage_key(&self, key: &[u8]) -> Option> { + fn next_storage_key(&self, key: &[u8]) -> Option { let range = (Bound::Excluded(key), Bound::Unbounded); self.inner.top.range::<[u8], _>(range).next().map(|(k, _)| k).cloned() } @@ -173,13 +173,13 @@ impl Externalities for BasicExternalities { storage_key: ChildStorageKey, _child_info: ChildInfo, key: &[u8], - ) -> Option> { + ) -> Option { let range = (Bound::Excluded(key), Bound::Unbounded); self.inner.children.get(storage_key.as_ref()) .and_then(|child| child.data.range::<[u8], _>(range).next().map(|(k, _)| k).cloned()) } - fn place_storage(&mut self, key: Vec, maybe_value: Option>) { + fn place_storage(&mut self, key: StorageKey, maybe_value: Option) { if is_child_storage_key(&key) { warn!(target: "trie", "Refuse to set child storage key via main storage"); return; @@ -195,8 +195,8 @@ impl Externalities for BasicExternalities { &mut self, storage_key: ChildStorageKey, child_info: ChildInfo, - key: Vec, - value: Option>, + key: StorageKey, + value: Option, ) { let child_map = self.inner.children.entry(storage_key.into_owned()) .or_insert_with(|| StorageChild { diff --git a/primitives/state-machine/src/changes_trie/build.rs b/primitives/state-machine/src/changes_trie/build.rs index ded8e4f6d19ed..bcc6502731217 100644 --- a/primitives/state-machine/src/changes_trie/build.rs +++ b/primitives/state-machine/src/changes_trie/build.rs @@ -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::>::new(); + let mut children_keys = BTreeSet::::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>, + storage_key: Option, ) -> Result> + 'a, String> where B: Backend, @@ -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| { + 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::, _>::new(); + let mut children_roots = BTreeMap::::new(); { let trie_storage = TrieBackendEssence::<_, H>::new( crate::changes_trie::TrieBackendStorageAdapter(storage), diff --git a/primitives/state-machine/src/changes_trie/build_cache.rs b/primitives/state-machine/src/changes_trie/build_cache.rs index 76e3652e16681..9d0dbb4c1f310 100644 --- a/primitives/state-machine/src/changes_trie/build_cache.rs +++ b/primitives/state-machine/src/changes_trie/build_cache.rs @@ -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 { /// The `Option>` 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>, HashSet>>>, + changed_keys: HashMap, HashSet>>, } /// The action to perform when block-with-changes-trie is imported. @@ -54,7 +56,7 @@ pub struct CachedBuildData { block: N, trie_root: H, digest_input_blocks: Vec, - changed_keys: HashMap>, HashSet>>, + changed_keys: HashMap, HashSet>, } /// The action to perform when block-with-changes-trie is imported. @@ -70,7 +72,7 @@ pub(crate) enum IncompleteCacheAction { #[derive(Debug, PartialEq)] pub(crate) struct IncompleteCachedBuildData { digest_input_blocks: Vec, - changed_keys: HashMap>, HashSet>>, + changed_keys: HashMap, HashSet>, } impl BuildCache @@ -87,7 +89,7 @@ impl BuildCache } /// Get cached changed keys for changes trie with given root. - pub fn get(&self, root: &H) -> Option<&HashMap>, HashSet>>> { + pub fn get(&self, root: &H) -> Option<&HashMap, HashSet>> { self.changed_keys.get(&root) } @@ -96,7 +98,7 @@ impl BuildCache pub fn with_changed_keys( &self, root: &H, - functor: &mut dyn FnMut(&HashMap>, HashSet>>), + functor: &mut dyn FnMut(&HashMap, HashSet>), ) -> bool { match self.changed_keys.get(&root) { Some(changed_keys) => { @@ -162,8 +164,8 @@ impl IncompleteCacheAction { /// Insert changed keys of given storage into cached data. pub(crate) fn insert( self, - storage_key: Option>, - changed_keys: HashSet>, + storage_key: Option, + changed_keys: HashSet, ) -> Self { match self { IncompleteCacheAction::CacheBuildData(build_data) => @@ -198,8 +200,8 @@ impl IncompleteCachedBuildData { fn insert( mut self, - storage_key: Option>, - changed_keys: HashSet>, + storage_key: Option, + changed_keys: HashSet, ) -> Self { self.changed_keys.insert(storage_key, changed_keys); self @@ -259,4 +261,4 @@ mod tests { assert_eq!(cache.changed_keys.len(), 0); } -} \ No newline at end of file +} diff --git a/primitives/state-machine/src/changes_trie/input.rs b/primitives/state-machine/src/changes_trie/input.rs index 9b3341ca5842b..4a1420f8486f9 100644 --- a/primitives/state-machine/src/changes_trie/input.rs +++ b/primitives/state-machine/src/changes_trie/input.rs @@ -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 { /// Block at which this key has been inserted in the trie. pub block: Number, /// Storage key this node is responsible for. - pub key: Vec, + pub key: StorageKey, } /// Value of { changed key => set of extrinsic indices } mapping. @@ -37,7 +40,7 @@ pub struct DigestIndex { /// Block at which this key has been inserted in the trie. pub block: Number, /// Storage key this node is responsible for. - pub key: Vec, + pub key: StorageKey, } /// Key of { childtrie key => Childchange trie } mapping. @@ -46,7 +49,7 @@ pub struct ChildIndex { /// 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, + pub storage_key: StorageKey, } /// Value of { changed key => block/digest block numbers } mapping. @@ -89,8 +92,8 @@ impl InputPair { } } -impl Into<(Vec, Vec)> for InputPair { - fn into(self) -> (Vec, Vec) { +impl Into<(StorageKey, StorageValue)> for InputPair { + fn into(self) -> (StorageKey, StorageValue) { match self { InputPair::ExtrinsicIndex(key, value) => (key.encode(), value.encode()), InputPair::DigestIndex(key, value) => (key.encode(), value.encode()), diff --git a/primitives/state-machine/src/changes_trie/mod.rs b/primitives/state-machine/src/changes_trie/mod.rs index cb19d157dd80a..532e58bd58e78 100644 --- a/primitives/state-machine/src/changes_trie/mod.rs +++ b/primitives/state-machine/src/changes_trie/mod.rs @@ -68,15 +68,20 @@ pub use self::prune::{prune, oldest_non_pruned_trie}; 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; @@ -139,7 +144,7 @@ pub trait Storage: RootsStorage { fn with_cached_changed_keys( &self, root: &H::Out, - functor: &mut dyn FnMut(&HashMap>, HashSet>>), + functor: &mut dyn FnMut(&HashMap, HashSet>), ) -> bool; /// Get a trie node. fn get(&self, key: &H::Out, prefix: Prefix) -> Result, String>; diff --git a/primitives/state-machine/src/changes_trie/storage.rs b/primitives/state-machine/src/changes_trie/storage.rs index 163ea7f412251..7fb418672872b 100644 --- a/primitives/state-machine/src/changes_trie/storage.rs +++ b/primitives/state-machine/src/changes_trie/storage.rs @@ -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 InMemoryStorage { #[cfg(test)] pub fn with_inputs( mut top_inputs: Vec<(Number, Vec>)>, - children_inputs: Vec<(Vec, Vec<(Number, Vec>)>)>, + children_inputs: Vec<(StorageKey, Vec<(Number, Vec>)>)>, ) -> Self { let mut mdb = MemoryDB::default(); let mut roots = BTreeMap::new(); @@ -179,7 +182,7 @@ impl Storage for InMemoryStorage>, HashSet>>), + functor: &mut dyn FnMut(&HashMap, HashSet>), ) -> bool { self.cache.with_changed_keys(root, functor) } diff --git a/primitives/state-machine/src/ext.rs b/primitives/state-machine/src/ext.rs index 6fc4312d1079d..e353dde9cf232 100644 --- a/primitives/state-machine/src/ext.rs +++ b/primitives/state-machine/src/ext.rs @@ -17,7 +17,8 @@ //! Concrete externalities implementation. use crate::{ - backend::Backend, OverlayedChanges, StorageTransactionCache, + StorageKey, StorageValue, OverlayedChanges, StorageTransactionCache, + backend::Backend, changes_trie::Storage as ChangesTrieStorage, }; @@ -132,7 +133,7 @@ where T: 'a + ChangesTrieStorage, N: crate::changes_trie::BlockNumber, { - pub fn storage_pairs(&self) -> Vec<(Vec, Vec)> { + pub fn storage_pairs(&self) -> Vec<(StorageKey, StorageValue)> { use std::collections::HashMap; self.backend.pairs().iter() @@ -154,7 +155,7 @@ where T: 'a + ChangesTrieStorage, N: crate::changes_trie::BlockNumber, { - fn storage(&self, key: &[u8]) -> Option> { + fn storage(&self, key: &[u8]) -> Option { let _guard = sp_panic_handler::AbortGuard::force_abort(); let result = self.overlay.storage(key).map(|x| x.map(|x| x.to_vec())).unwrap_or_else(|| self.backend.storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL)); @@ -181,7 +182,7 @@ where result.map(|r| r.encode()) } - fn original_storage(&self, key: &[u8]) -> Option> { + fn original_storage(&self, key: &[u8]) -> Option { let _guard = sp_panic_handler::AbortGuard::force_abort(); let result = self.backend.storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL); @@ -210,7 +211,7 @@ where storage_key: ChildStorageKey, child_info: ChildInfo, key: &[u8], - ) -> Option> { + ) -> Option { let _guard = sp_panic_handler::AbortGuard::force_abort(); let result = self.overlay .child_storage(storage_key.as_ref(), key) @@ -259,7 +260,7 @@ where storage_key: ChildStorageKey, child_info: ChildInfo, key: &[u8], - ) -> Option> { + ) -> Option { let _guard = sp_panic_handler::AbortGuard::force_abort(); let result = self.backend .child_storage(storage_key.as_ref(), child_info, key) @@ -335,7 +336,7 @@ where result } - fn next_storage_key(&self, key: &[u8]) -> Option> { + fn next_storage_key(&self, key: &[u8]) -> Option { let next_backend_key = self.backend.next_storage_key(key).expect(EXT_NOT_ALLOWED_TO_FAIL); let next_overlay_key_change = self.overlay.next_storage_key_change(key); @@ -355,7 +356,7 @@ where storage_key: ChildStorageKey, child_info: ChildInfo, key: &[u8], - ) -> Option> { + ) -> Option { let next_backend_key = self.backend .next_child_storage_key(storage_key.as_ref(), child_info, key) .expect(EXT_NOT_ALLOWED_TO_FAIL); @@ -379,7 +380,7 @@ where } } - fn place_storage(&mut self, key: Vec, value: Option>) { + fn place_storage(&mut self, key: StorageKey, value: Option) { trace!(target: "state-trace", "{:04x}: Put {}={:?}", self.id, HexDisplay::from(&key), @@ -399,8 +400,8 @@ where &mut self, storage_key: ChildStorageKey, child_info: ChildInfo, - key: Vec, - value: Option>, + key: StorageKey, + value: Option, ) { trace!(target: "state-trace", "{:04x}: PutChild({}) {}={:?}", self.id, diff --git a/primitives/state-machine/src/in_memory_backend.rs b/primitives/state-machine/src/in_memory_backend.rs index ae1a214a72c92..0a29468bbc4ef 100644 --- a/primitives/state-machine/src/in_memory_backend.rs +++ b/primitives/state-machine/src/in_memory_backend.rs @@ -16,7 +16,11 @@ //! State machine in memory backend. -use crate::{trie_backend::TrieBackend, backend::{Backend, insert_into_memory_db}}; +use crate::{ + StorageKey, StorageValue, StorageCollection, + trie_backend::TrieBackend, + backend::{Backend, insert_into_memory_db}, +}; use std::{error, fmt, collections::{BTreeMap, HashMap}, marker::PhantomData, ops}; use hash_db::Hasher; use sp_trie::{ @@ -43,7 +47,7 @@ impl error::Error for Void { /// In-memory backend. Fully recomputes tries each time `as_trie_backend` is called but useful for /// tests and proof checking. pub struct InMemory { - inner: HashMap, OwnedChildInfo)>, BTreeMap, Vec>>, + inner: HashMap, BTreeMap>, // This field is only needed for returning reference in `as_trie_backend`. trie: Option, H>>, _hasher: PhantomData, @@ -84,7 +88,7 @@ impl PartialEq for InMemory { impl InMemory { /// Copy the state, with applied updates pub fn update< - T: IntoIterator, OwnedChildInfo)>, Vec<(Vec, Option>)>)> + T: IntoIterator, StorageCollection)> >( &self, changes: T, @@ -103,10 +107,10 @@ impl InMemory { } } -impl From, OwnedChildInfo)>, BTreeMap, Vec>>> +impl From, BTreeMap>> for InMemory { - fn from(inner: HashMap, OwnedChildInfo)>, BTreeMap, Vec>>) -> Self { + fn from(inner: HashMap, BTreeMap>) -> Self { InMemory { inner, trie: None, @@ -117,7 +121,7 @@ impl From, OwnedChildInfo)>, BTreeMap impl From for InMemory { fn from(inners: Storage) -> Self { - let mut inner: HashMap, OwnedChildInfo)>, BTreeMap, Vec>> + let mut inner: HashMap, BTreeMap> = inners.children.into_iter().map(|(k, c)| (Some((k, c.child_info)), c.data)).collect(); inner.insert(None, inners.top); InMemory { @@ -128,8 +132,8 @@ impl From for InMemory { } } -impl From, Vec>> for InMemory { - fn from(inner: BTreeMap, Vec>) -> Self { +impl From> for InMemory { + fn from(inner: BTreeMap) -> Self { let mut expanded = HashMap::new(); expanded.insert(None, inner); InMemory { @@ -140,12 +144,12 @@ impl From, Vec>> for InMemory { } } -impl From, OwnedChildInfo)>, Vec<(Vec, Option>)>)>> +impl From, StorageCollection)>> for InMemory { fn from( - inner: Vec<(Option<(Vec, OwnedChildInfo)>, Vec<(Vec, Option>)>)>, + inner: Vec<(Option<(StorageKey, OwnedChildInfo)>, StorageCollection)>, ) -> Self { - let mut expanded: HashMap, OwnedChildInfo)>, BTreeMap, Vec>> + let mut expanded: HashMap, BTreeMap> = HashMap::new(); for (child_info, key_values) in inner { let entry = expanded.entry(child_info).or_default(); @@ -171,12 +175,12 @@ impl InMemory { impl Backend for InMemory where H::Out: Codec { type Error = Void; type Transaction = Vec<( - Option<(Vec, OwnedChildInfo)>, - Vec<(Vec, Option>)>, + Option<(StorageKey, OwnedChildInfo)>, + StorageCollection, )>; type TrieBackendStorage = MemoryDB; - fn storage(&self, key: &[u8]) -> Result>, Self::Error> { + fn storage(&self, key: &[u8]) -> Result, Self::Error> { Ok(self.inner.get(&None).and_then(|map| map.get(key).map(Clone::clone))) } @@ -185,7 +189,7 @@ impl Backend for InMemory where H::Out: Codec { storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, Self::Error> { + ) -> Result, Self::Error> { Ok(self.inner.get(&Some((storage_key.to_vec(), child_info.to_owned()))) .and_then(|map| map.get(key).map(Clone::clone))) } @@ -279,7 +283,7 @@ impl Backend for InMemory where H::Out: Codec { (root, is_default, vec![(child_info, full_transaction)]) } - fn next_storage_key(&self, key: &[u8]) -> Result>, Self::Error> { + fn next_storage_key(&self, key: &[u8]) -> Result, Self::Error> { let range = (ops::Bound::Excluded(key), ops::Bound::Unbounded); let next_key = self.inner.get(&None) .and_then(|map| map.range::<[u8], _>(range).next().map(|(k, _)| k).cloned()); @@ -292,7 +296,7 @@ impl Backend for InMemory where H::Out: Codec { storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, Self::Error> { + ) -> Result, Self::Error> { let range = (ops::Bound::Excluded(key), ops::Bound::Unbounded); let next_key = self.inner.get(&Some((storage_key.to_vec(), child_info.to_owned()))) .and_then(|map| map.range::<[u8], _>(range).next().map(|(k, _)| k).cloned()); @@ -300,14 +304,14 @@ impl Backend for InMemory where H::Out: Codec { Ok(next_key) } - fn pairs(&self) -> Vec<(Vec, Vec)> { + fn pairs(&self) -> Vec<(StorageKey, StorageValue)> { self.inner.get(&None) .into_iter() .flat_map(|map| map.iter().map(|(k, v)| (k.clone(), v.clone()))) .collect() } - fn keys(&self, prefix: &[u8]) -> Vec> { + fn keys(&self, prefix: &[u8]) -> Vec { self.inner.get(&None) .into_iter() .flat_map(|map| map.keys().filter(|k| k.starts_with(prefix)).cloned()) @@ -319,7 +323,7 @@ impl Backend for InMemory where H::Out: Codec { storage_key: &[u8], child_info: ChildInfo, prefix: &[u8], - ) -> Vec> { + ) -> Vec { self.inner.get(&Some((storage_key.to_vec(), child_info.to_owned()))) .into_iter() .flat_map(|map| map.keys().filter(|k| k.starts_with(prefix)).cloned()) diff --git a/primitives/state-machine/src/lib.rs b/primitives/state-machine/src/lib.rs index 55f2eb4941f90..dff467b761d94 100644 --- a/primitives/state-machine/src/lib.rs +++ b/primitives/state-machine/src/lib.rs @@ -60,7 +60,10 @@ pub use changes_trie::{ oldest_non_pruned_trie as oldest_non_pruned_changes_trie, BlockNumber as ChangesTrieBlockNumber, }; -pub use overlayed_changes::{OverlayedChanges, StorageChanges, StorageTransactionCache}; +pub use overlayed_changes::{ + OverlayedChanges, StorageChanges, StorageTransactionCache, StorageKey, StorageValue, + StorageCollection, ChildStorageCollection, +}; pub use proving_backend::{ create_proof_check_backend, create_proof_check_backend_storage, merge_storage_proofs, ProofRecorder, ProvingBackend, ProvingBackendRecorder, StorageProof, diff --git a/primitives/state-machine/src/overlayed_changes.rs b/primitives/state-machine/src/overlayed_changes.rs index eafd31241038b..7b7c231909939 100644 --- a/primitives/state-machine/src/overlayed_changes.rs +++ b/primitives/state-machine/src/overlayed_changes.rs @@ -33,6 +33,18 @@ use std::{mem, ops}; use hash_db::Hasher; +/// Storage key. +pub type StorageKey = Vec; + +/// Storage value. +pub type StorageValue = Vec; + +/// In memory array of storage values. +pub type StorageCollection = Vec<(StorageKey, Option)>; + +/// In memory arrays of storage values for multiple child tries. +pub type ChildStorageCollection = Vec<(StorageKey, StorageCollection)>; + /// The overlayed changes to state to be queried on top of the backend. /// /// A transaction shares all prospective changes within an inner overlay @@ -53,7 +65,7 @@ pub struct OverlayedChanges { #[cfg_attr(test, derive(PartialEq))] pub struct OverlayedValue { /// Current value. None if value has been deleted. - pub value: Option>, + pub value: Option, /// The set of extinsic indices where the values has been changed. /// Is filled only if runtime has announced changes trie support. pub extrinsics: Option>, @@ -64,9 +76,9 @@ pub struct OverlayedValue { #[cfg_attr(test, derive(PartialEq))] pub struct OverlayedChangeSet { /// Top level storage changes. - pub top: BTreeMap, OverlayedValue>, + pub top: BTreeMap, /// Child storage changes. - pub children: HashMap, (BTreeMap, OverlayedValue>, OwnedChildInfo)>, + pub children: HashMap, OwnedChildInfo)>, } /// A storage changes structure that can be generated by the data collected in [`OverlayedChanges`]. @@ -77,9 +89,9 @@ pub struct StorageChanges { /// All changes to the main storage. /// /// A value of `None` means that it was deleted. - pub main_storage_changes: Vec<(Vec, Option>)>, + pub main_storage_changes: StorageCollection, /// All changes to the child storages. - pub child_storage_changes: Vec<(Vec, Vec<(Vec, Option>)>)>, + pub child_storage_changes: ChildStorageCollection, /// A transaction for the backend that contains all changes from /// [`main_storage_changes`](Self::main_storage_changes) and from /// [`child_storage_changes`](Self::child_storage_changes). @@ -95,8 +107,8 @@ pub struct StorageChanges { impl StorageChanges { /// Deconstruct into the inner values pub fn into_inner(self) -> ( - Vec<(Vec, Option>)>, - Vec<(Vec, Vec<(Vec, Option>)>)>, + StorageCollection, + ChildStorageCollection, Transaction, H::Out, Option>, @@ -156,8 +168,8 @@ impl Default for StorageChanges } #[cfg(test)] -impl FromIterator<(Vec, OverlayedValue)> for OverlayedChangeSet { - fn from_iter, OverlayedValue)>>(iter: T) -> Self { +impl FromIterator<(StorageKey, OverlayedValue)> for OverlayedChangeSet { + fn from_iter>(iter: T) -> Self { Self { top: iter.into_iter().collect(), children: Default::default(), @@ -231,7 +243,7 @@ impl OverlayedChanges { /// Inserts the given key-value pair into the prospective change set. /// /// `None` can be used to delete a value specified by the given key. - pub(crate) fn set_storage(&mut self, key: Vec, val: Option>) { + pub(crate) fn set_storage(&mut self, key: StorageKey, val: Option) { let extrinsic_index = self.extrinsic_index(); let entry = self.prospective.top.entry(key).or_default(); entry.value = val; @@ -247,10 +259,10 @@ impl OverlayedChanges { /// `None` can be used to delete a value specified by the given key. pub(crate) fn set_child_storage( &mut self, - storage_key: Vec, + storage_key: StorageKey, child_info: ChildInfo, - key: Vec, - val: Option>, + key: StorageKey, + val: Option, ) { let extrinsic_index = self.extrinsic_index(); let map_entry = self.prospective.children.entry(storage_key) @@ -429,8 +441,8 @@ impl OverlayedChanges { /// Panics: /// Will panic if there are any uncommitted prospective changes. pub fn into_committed(self) -> ( - impl Iterator, Option>)>, - impl Iterator, (impl Iterator, Option>)>, OwnedChildInfo))>, + impl Iterator)>, + impl Iterator)>, OwnedChildInfo))>, ){ assert!(self.prospective.is_empty()); ( @@ -643,8 +655,8 @@ impl OverlayedChanges { } #[cfg(test)] -impl From>> for OverlayedValue { - fn from(value: Option>) -> OverlayedValue { +impl From> for OverlayedValue { + fn from(value: Option) -> OverlayedValue { OverlayedValue { value, ..Default::default() } } } @@ -660,8 +672,8 @@ mod tests { use crate::ext::Ext; use super::*; - fn strip_extrinsic_index(map: &BTreeMap, OverlayedValue>) - -> BTreeMap, OverlayedValue> + fn strip_extrinsic_index(map: &BTreeMap) + -> BTreeMap { let mut clone = map.clone(); clone.remove(&EXTRINSIC_INDEX.to_vec()); From c8d0725f6ebde07afe106e92a7a9874c2843fa2d Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Fri, 17 Jan 2020 00:56:51 +0200 Subject: [PATCH 2/2] Fix after merge --- primitives/state-machine/src/ext.rs | 2 +- primitives/state-machine/src/testing.rs | 3 ++- primitives/state-machine/src/trie_backend.rs | 22 ++++++++++--------- .../state-machine/src/trie_backend_essence.rs | 12 +++++----- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/primitives/state-machine/src/ext.rs b/primitives/state-machine/src/ext.rs index 43a2bb5c2ff63..f293ae9f51615 100644 --- a/primitives/state-machine/src/ext.rs +++ b/primitives/state-machine/src/ext.rs @@ -19,7 +19,7 @@ use crate::{ StorageKey, StorageValue, OverlayedChanges, StorageTransactionCache, backend::Backend, - changes_trie::Storage as ChangesTrieState, + changes_trie::State as ChangesTrieState, }; use hash_db::Hasher; diff --git a/primitives/state-machine/src/testing.rs b/primitives/state-machine/src/testing.rs index 450919088a4ca..39a34509b720b 100644 --- a/primitives/state-machine/src/testing.rs +++ b/primitives/state-machine/src/testing.rs @@ -21,6 +21,7 @@ use codec::Decode; use hash_db::Hasher; use crate::{ backend::Backend, OverlayedChanges, StorageTransactionCache, ext::Ext, InMemoryBackend, + StorageKey, StorageValue, changes_trie::{ Configuration as ChangesTrieConfiguration, InMemoryStorage as ChangesTrieInMemoryStorage, @@ -104,7 +105,7 @@ impl TestExternalities } /// Insert key/value into backend - pub fn insert(&mut self, k: Vec, v: Vec) { + pub fn insert(&mut self, k: StorageKey, v: StorageValue) { self.backend = self.backend.update(vec![(None, vec![(k, Some(v))])]); } diff --git a/primitives/state-machine/src/trie_backend.rs b/primitives/state-machine/src/trie_backend.rs index 1920ea40a88b5..dbaae323c09f2 100644 --- a/primitives/state-machine/src/trie_backend.rs +++ b/primitives/state-machine/src/trie_backend.rs @@ -20,10 +20,12 @@ use log::{warn, debug}; use hash_db::Hasher; use sp_trie::{Trie, delta_trie_root, default_child_trie_root, child_delta_trie_root}; use sp_trie::trie_types::{TrieDB, TrieError, Layout}; -use crate::trie_backend_essence::{TrieBackendEssence, TrieBackendStorage, Ephemeral}; -use crate::Backend; use sp_core::storage::ChildInfo; use codec::{Codec, Decode}; +use crate::{ + StorageKey, StorageValue, Backend, + trie_backend_essence::{TrieBackendEssence, TrieBackendStorage, Ephemeral}, +}; /// Patricia trie-based backend. Transaction type is an overlay of changes to commit. pub struct TrieBackend, H: Hasher> { @@ -72,7 +74,7 @@ impl, H: Hasher> Backend for TrieBackend where type Transaction = S::Overlay; type TrieBackendStorage = S; - fn storage(&self, key: &[u8]) -> Result>, Self::Error> { + fn storage(&self, key: &[u8]) -> Result, Self::Error> { self.essence.storage(key) } @@ -81,11 +83,11 @@ impl, H: Hasher> Backend for TrieBackend where storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, Self::Error> { + ) -> Result, Self::Error> { self.essence.child_storage(storage_key, child_info, key) } - fn next_storage_key(&self, key: &[u8]) -> Result>, Self::Error> { + fn next_storage_key(&self, key: &[u8]) -> Result, Self::Error> { self.essence.next_storage_key(key) } @@ -94,7 +96,7 @@ impl, H: Hasher> Backend for TrieBackend where storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, Self::Error> { + ) -> Result, Self::Error> { self.essence.next_child_storage_key(storage_key, child_info, key) } @@ -125,7 +127,7 @@ impl, H: Hasher> Backend for TrieBackend where self.essence.for_child_keys_with_prefix(storage_key, child_info, prefix, f) } - fn pairs(&self) -> Vec<(Vec, Vec)> { + fn pairs(&self) -> Vec<(StorageKey, StorageValue)> { let mut read_overlay = S::Overlay::default(); let eph = Ephemeral::new(self.essence.backend_storage(), &mut read_overlay); @@ -149,7 +151,7 @@ impl, H: Hasher> Backend for TrieBackend where } } - fn keys(&self, prefix: &[u8]) -> Vec> { + fn keys(&self, prefix: &[u8]) -> Vec { let mut read_overlay = S::Overlay::default(); let eph = Ephemeral::new(self.essence.backend_storage(), &mut read_overlay); @@ -170,7 +172,7 @@ impl, H: Hasher> Backend for TrieBackend where } fn storage_root(&self, delta: I) -> (H::Out, S::Overlay) - where I: IntoIterator, Option>)> + where I: IntoIterator)> { let mut write_overlay = S::Overlay::default(); let mut root = *self.essence.root(); @@ -197,7 +199,7 @@ impl, H: Hasher> Backend for TrieBackend where delta: I, ) -> (H::Out, bool, Self::Transaction) where - I: IntoIterator, Option>)>, + I: IntoIterator)>, H::Out: Ord, { let default_root = default_child_trie_root::>(storage_key); diff --git a/primitives/state-machine/src/trie_backend_essence.rs b/primitives/state-machine/src/trie_backend_essence.rs index f7f5006f41dab..2598682ae0668 100644 --- a/primitives/state-machine/src/trie_backend_essence.rs +++ b/primitives/state-machine/src/trie_backend_essence.rs @@ -25,7 +25,7 @@ use sp_trie::{Trie, MemoryDB, PrefixedMemoryDB, DBValue, default_child_trie_root, read_trie_value, read_child_trie_value, for_keys_in_child_trie, KeySpacedDB}; use sp_trie::trie_types::{TrieDB, TrieError, Layout}; -use crate::backend::Consolidate; +use crate::{backend::Consolidate, StorageKey, StorageValue}; use sp_core::storage::ChildInfo; use codec::Encode; @@ -67,7 +67,7 @@ impl, H: Hasher> TrieBackendEssence where H::Out: /// Return the next key in the trie i.e. the minimum key that is strictly superior to `key` in /// lexicographic order. - pub fn next_storage_key(&self, key: &[u8]) -> Result>, String> { + pub fn next_storage_key(&self, key: &[u8]) -> Result, String> { self.next_storage_key_from_root(&self.root, None, key) } @@ -78,7 +78,7 @@ impl, H: Hasher> TrieBackendEssence where H::Out: storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, String> { + ) -> Result, String> { let child_root = match self.storage(storage_key)? { Some(child_root) => child_root, None => return Ok(None), @@ -101,7 +101,7 @@ impl, H: Hasher> TrieBackendEssence where H::Out: root: &H::Out, child_info: Option, key: &[u8], - ) -> Result>, String> { + ) -> Result, String> { let mut read_overlay = S::Overlay::default(); let eph = Ephemeral { storage: &self.storage, @@ -146,7 +146,7 @@ impl, H: Hasher> TrieBackendEssence where H::Out: } /// Get the value of storage at given key. - pub fn storage(&self, key: &[u8]) -> Result>, String> { + pub fn storage(&self, key: &[u8]) -> Result, String> { let mut read_overlay = S::Overlay::default(); let eph = Ephemeral { storage: &self.storage, @@ -164,7 +164,7 @@ impl, H: Hasher> TrieBackendEssence where H::Out: storage_key: &[u8], child_info: ChildInfo, key: &[u8], - ) -> Result>, String> { + ) -> Result, String> { let root = self.storage(storage_key)? .unwrap_or(default_child_trie_root::>(storage_key).encode());