Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add typedefs for storage types #4654

Merged
merged 3 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use sp_core::ChangesTrieConfigurationRange;
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::{ChangesTrieState, ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction};
use sp_state_machine::{
ChangesTrieState, ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction,
StorageCollection, ChildStorageCollection,
};
use crate::{
blockchain::{
Backend as BlockchainBackend, well_known_cache_keys
Expand All @@ -45,12 +48,6 @@ pub type TransactionForSB<B, Block> = <B as StateBackend<HasherFor<Block>>>::Tra
/// Extracts the transaction for the given backend.
pub type TransactionFor<B, Block> = TransactionForSB<StateBackendFor<B, Block>, Block>;

/// In memory array of storage values.
pub type StorageCollection = Vec<(Vec<u8>, Option<Vec<u8>>)>;

/// In memory arrays of storage values for multiple child tries.
pub type ChildStorageCollection = Vec<(Vec<u8>, StorageCollection)>;

/// Import operation summary.
///
/// Contains information about the block that just got imported,
Expand Down
7 changes: 4 additions & 3 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use std::collections::HashMap;

use sc_client_api::{execution_extensions::ExecutionExtensions, ForkBlocks, UsageInfo, MemoryInfo, BadBlocks, IoInfo};
use sc_client_api::backend::NewBlockState;
use sc_client_api::backend::{PrunableStateChangesTrieStorage, StorageCollection, ChildStorageCollection};
use sc_client_api::backend::PrunableStateChangesTrieStorage;
use sp_blockchain::{
Result as ClientResult, Error as ClientError,
well_known_cache_keys, HeaderBackend,
Expand All @@ -66,8 +66,9 @@ use sp_runtime::traits::{
};
use sc_executor::RuntimeInfo;
use sp_state_machine::{
DBValue, ChangesTrieTransaction, ChangesTrieCacheAction,
backend::Backend as StateBackend, UsageInfo as StateUsageInfo,
DBValue, ChangesTrieTransaction, ChangesTrieCacheAction, UsageInfo as StateUsageInfo,
StorageCollection, ChildStorageCollection,
backend::Backend as StateBackend,
};
use crate::utils::{DatabaseType, Meta, db_err, meta_keys, read_db, read_meta};
use crate::changes_tries_storage::{DbChangesTrieStorage, DbChangesTrieStorageTransaction};
Expand Down
8 changes: 4 additions & 4 deletions client/db/src/storage_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>;
type ChildStorageKey = (Vec<u8>, Vec<u8>);
type StorageValue = Vec<u8>;

/// Shared canonical state cache.
pub struct Cache<B: BlockT> {
Expand Down
7 changes: 5 additions & 2 deletions client/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ use sp_core::offchain::storage::{
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero, NumberFor, HasherFor};
use sp_runtime::{Justification, Storage};
use sp_state_machine::{ChangesTrieTransaction, InMemoryBackend, Backend as StateBackend};
use sp_state_machine::{
ChangesTrieTransaction, InMemoryBackend, Backend as StateBackend, StorageCollection,
ChildStorageCollection,
};
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
},
Expand Down
5 changes: 3 additions & 2 deletions client/src/light/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use sp_core::ChangesTrieConfiguration;
use sp_core::storage::{well_known_keys, 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};
Expand All @@ -36,7 +37,7 @@ use sp_blockchain::{Error as ClientError, Result as ClientResult};
use sc_client_api::{
backend::{
AuxStore, Backend as ClientBackend, BlockImportOperation, RemoteBackend, NewBlockState,
StorageCollection, ChildStorageCollection, PrunableStateChangesTrieStorage,
PrunableStateChangesTrieStorage,
},
blockchain::{
HeaderBackend as BlockchainHeaderBackend, well_known_cache_keys,
Expand Down
46 changes: 23 additions & 23 deletions primitives/state-machine/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,7 +44,7 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
type TrieBackendStorage: TrieBackendStorage<H>;

/// Get keyed storage or None if there is nothing associated.
fn storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error>;
fn storage(&self, key: &[u8]) -> Result<Option<StorageValue>, Self::Error>;

/// Get keyed storage value hash or None if there is nothing associated.
fn storage_hash(&self, key: &[u8]) -> Result<Option<H::Out>, Self::Error> {
Expand All @@ -57,7 +57,7 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
) -> Result<Option<Vec<u8>>, Self::Error>;
) -> Result<Option<StorageValue>, Self::Error>;

/// Get child keyed storage value hash or None if there is nothing associated.
fn child_storage_hash(
Expand Down Expand Up @@ -85,15 +85,15 @@ pub trait Backend<H: Hasher>: 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<Option<Vec<u8>>, Self::Error>;
fn next_storage_key(&self, key: &[u8]) -> Result<Option<StorageKey>, Self::Error>;

/// 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,
key: &[u8]
) -> Result<Option<Vec<u8>>, Self::Error>;
) -> 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])>(
Expand Down Expand Up @@ -129,7 +129,7 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
/// Does not include child storage updates.
fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
I: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
H::Out: Ord;

/// Calculate the child storage root, with given delta over what is already stored in
Expand All @@ -142,14 +142,14 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
delta: I,
) -> (H::Out, bool, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
I: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
H::Out: Ord;

/// Get all key/value pairs into a Vec.
fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)>;
fn pairs(&self) -> Vec<(StorageKey, StorageValue)>;

/// Get all keys with given prefix
fn keys(&self, prefix: &[u8]) -> Vec<Vec<u8>> {
fn keys(&self, prefix: &[u8]) -> Vec<StorageKey> {
let mut all = Vec::new();
self.for_keys_with_prefix(prefix, |k| all.push(k.to_vec()));
all
Expand All @@ -161,7 +161,7 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
storage_key: &[u8],
child_info: ChildInfo,
prefix: &[u8],
) -> Vec<Vec<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()));
all
Expand All @@ -181,9 +181,9 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
child_deltas: I2)
-> (H::Out, Self::Transaction)
where
I1: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
I2i: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
I2: IntoIterator<Item=(Vec<u8>, I2i, OwnedChildInfo)>,
I1: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
I2i: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
I2: IntoIterator<Item=(StorageKey, I2i, OwnedChildInfo)>,
H::Out: Ord + Encode,
{
let mut txs: Self::Transaction = Default::default();
Expand Down Expand Up @@ -220,7 +220,7 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
type Transaction = T::Transaction;
type TrieBackendStorage = T::TrieBackendStorage;

fn storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
fn storage(&self, key: &[u8]) -> Result<Option<StorageKey>, Self::Error> {
(*self).storage(key)
}

Expand All @@ -229,7 +229,7 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
) -> Result<Option<Vec<u8>>, Self::Error> {
) -> Result<Option<StorageKey>, Self::Error> {
(*self).child_storage(storage_key, child_info, key)
}

Expand All @@ -242,7 +242,7 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
(*self).for_keys_in_child_storage(storage_key, child_info, f)
}

fn next_storage_key(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
fn next_storage_key(&self, key: &[u8]) -> Result<Option<StorageKey>, Self::Error> {
(*self).next_storage_key(key)
}

Expand All @@ -251,7 +251,7 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
storage_key: &[u8],
child_info: ChildInfo,
key: &[u8],
) -> Result<Option<Vec<u8>>, Self::Error> {
) -> Result<Option<StorageKey>, Self::Error> {
(*self).next_child_storage_key(storage_key, child_info, key)
}

Expand All @@ -271,7 +271,7 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {

fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
I: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
H::Out: Ord,
{
(*self).storage_root(delta)
Expand All @@ -284,13 +284,13 @@ impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
delta: I,
) -> (H::Out, bool, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
I: IntoIterator<Item=(StorageKey, Option<StorageValue>)>,
H::Out: Ord,
{
(*self).child_storage_root(storage_key, child_info, delta)
}

fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
fn pairs(&self) -> Vec<(StorageKey, StorageValue)> {
(*self).pairs()
}

Expand All @@ -316,8 +316,8 @@ impl Consolidate for () {
}

impl Consolidate for Vec<(
Option<(Vec<u8>, OwnedChildInfo)>,
Vec<(Vec<u8>, Option<Vec<u8>>)>,
Option<(StorageKey, OwnedChildInfo)>,
StorageCollection,
)> {
fn consolidate(&mut self, mut other: Self) {
self.append(&mut other);
Expand All @@ -334,7 +334,7 @@ impl<H: Hasher, KF: sp_trie::KeyFunction<H>> Consolidate for sp_trie::GenericMem
pub(crate) fn insert_into_memory_db<H, I>(mdb: &mut MemoryDB<H>, input: I) -> Option<H::Out>
where
H: Hasher,
I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>,
I: IntoIterator<Item=(StorageKey, StorageValue)>,
{
let mut root = <H as Hasher>::Out::default();
{
Expand Down
30 changes: 15 additions & 15 deletions primitives/state-machine/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,7 +46,7 @@ impl BasicExternalities {
}

/// Insert key/value
pub fn insert(&mut self, k: Vec<u8>, v: Vec<u8>) -> Option<Vec<u8>> {
pub fn insert(&mut self, k: StorageKey, v: StorageValue) -> Option<StorageValue> {
self.inner.top.insert(k, v)
}

Expand Down Expand Up @@ -89,8 +89,8 @@ impl PartialEq for BasicExternalities {
}
}

impl FromIterator<(Vec<u8>, Vec<u8>)> for BasicExternalities {
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
impl FromIterator<(StorageKey, StorageValue)> for BasicExternalities {
fn from_iter<I: IntoIterator<Item=(StorageKey, StorageValue)>>(iter: I) -> Self {
let mut t = Self::default();
t.inner.top.extend(iter);
t
Expand All @@ -101,8 +101,8 @@ impl Default for BasicExternalities {
fn default() -> Self { Self::new(Default::default()) }
}

impl From<BTreeMap<Vec<u8>, Vec<u8>>> for BasicExternalities {
fn from(hashmap: BTreeMap<Vec<u8>, Vec<u8>>) -> Self {
impl From<BTreeMap<StorageKey, StorageValue>> for BasicExternalities {
fn from(hashmap: BTreeMap<StorageKey, StorageValue>) -> Self {
BasicExternalities { inner: Storage {
top: hashmap,
children: Default::default(),
Expand All @@ -111,15 +111,15 @@ impl From<BTreeMap<Vec<u8>, Vec<u8>>> for BasicExternalities {
}

impl Externalities for BasicExternalities {
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
fn storage(&self, key: &[u8]) -> Option<StorageValue> {
self.inner.top.get(key).cloned()
}

fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
self.storage(key).map(|v| Blake2Hasher::hash(&v).encode())
}

fn original_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
fn original_storage(&self, key: &[u8]) -> Option<StorageValue> {
self.storage(key)
}

Expand All @@ -132,7 +132,7 @@ impl Externalities for BasicExternalities {
storage_key: ChildStorageKey,
_child_info: ChildInfo,
key: &[u8],
) -> Option<Vec<u8>> {
) -> Option<StorageValue> {
self.inner.children.get(storage_key.as_ref()).and_then(|child| child.data.get(key)).cloned()
}

Expand All @@ -159,11 +159,11 @@ impl Externalities for BasicExternalities {
storage_key: ChildStorageKey,
child_info: ChildInfo,
key: &[u8],
) -> Option<Vec<u8>> {
) -> Option<StorageValue> {
Externalities::child_storage(self, storage_key, child_info, key)
}

fn next_storage_key(&self, key: &[u8]) -> Option<Vec<u8>> {
fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
let range = (Bound::Excluded(key), Bound::Unbounded);
self.inner.top.range::<[u8], _>(range).next().map(|(k, _)| k).cloned()
}
Expand All @@ -173,13 +173,13 @@ impl Externalities for BasicExternalities {
storage_key: ChildStorageKey,
_child_info: ChildInfo,
key: &[u8],
) -> Option<Vec<u8>> {
) -> Option<StorageKey> {
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<u8>, maybe_value: Option<Vec<u8>>) {
fn place_storage(&mut self, key: StorageKey, maybe_value: Option<StorageValue>) {
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to set child storage key via main storage");
return;
Expand All @@ -195,8 +195,8 @@ impl Externalities for BasicExternalities {
&mut self,
storage_key: ChildStorageKey,
child_info: ChildInfo,
key: Vec<u8>,
value: Option<Vec<u8>>,
key: StorageKey,
value: Option<StorageValue>,
) {
let child_map = self.inner.children.entry(storage_key.into_owned())
.or_insert_with(|| StorageChild {
Expand Down
Loading