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
@@ -33,6 +33,18 @@ use std::{mem, ops};
use hash_db::Hasher;
/// Storage key.
pub type StorageKey = Vec<u8>;
/// Storage value.
pub type StorageValue = Vec<u8>;
/// In memory array of storage values.
pub type StorageCollection = Vec<(StorageKey, Option<StorageValue>)>;
/// 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
@@ -52,7 +64,7 @@ pub struct OverlayedChanges {
#[cfg_attr(test, derive(PartialEq))]
pub struct OverlayedValue {
/// Current value. None if value has been deleted.
pub value: Option<Vec<u8>>,
pub value: Option<StorageValue>,
/// The set of extinsic indices where the values has been changed.
/// Is filled only if runtime has announced changes trie support.
pub extrinsics: Option<BTreeSet<u32>>,
@@ -63,9 +75,9 @@ pub struct OverlayedValue {
#[cfg_attr(test, derive(PartialEq))]
pub struct OverlayedChangeSet {
/// Top level storage changes.
pub top: BTreeMap<Vec<u8>, OverlayedValue>,
pub top: BTreeMap<StorageKey, OverlayedValue>,
/// Child storage changes.
pub children: HashMap<Vec<u8>, (BTreeMap<Vec<u8>, OverlayedValue>, OwnedChildInfo)>,
pub children: HashMap<StorageKey, (BTreeMap<StorageKey, OverlayedValue>, OwnedChildInfo)>,
}
/// A storage changes structure that can be generated by the data collected in [`OverlayedChanges`].
@@ -76,9 +88,9 @@ pub struct StorageChanges<Transaction, H: Hasher, N: BlockNumber> {
/// All changes to the main storage.
///
/// A value of `None` means that it was deleted.
pub main_storage_changes: Vec<(Vec<u8>, Option<Vec<u8>>)>,
pub main_storage_changes: StorageCollection,
/// All changes to the child storages.
pub child_storage_changes: Vec<(Vec<u8>, Vec<(Vec<u8>, Option<Vec<u8>>)>)>,
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).
@@ -94,8 +106,8 @@ pub struct StorageChanges<Transaction, H: Hasher, N: BlockNumber> {
impl<Transaction, H: Hasher, N: BlockNumber> StorageChanges<Transaction, H, N> {
/// Deconstruct into the inner values
pub fn into_inner(self) -> (
Vec<(Vec<u8>, Option<Vec<u8>>)>,
Vec<(Vec<u8>, Vec<(Vec<u8>, Option<Vec<u8>>)>)>,
StorageCollection,
ChildStorageCollection,
Transaction,
H::Out,
Option<ChangesTrieTransaction<H, N>>,
@@ -155,8 +167,8 @@ impl<Transaction: Default, H: Hasher, N: BlockNumber> Default for StorageChanges
}
#[cfg(test)]
impl FromIterator<(Vec<u8>, OverlayedValue)> for OverlayedChangeSet {
fn from_iter<T: IntoIterator<Item = (Vec<u8>, OverlayedValue)>>(iter: T) -> Self {
impl FromIterator<(StorageKey, OverlayedValue)> for OverlayedChangeSet {
fn from_iter<T: IntoIterator<Item = (StorageKey, OverlayedValue)>>(iter: T) -> Self {
Self {
top: iter.into_iter().collect(),
children: Default::default(),
@@ -219,7 +231,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<u8>, val: Option<Vec<u8>>) {
pub(crate) fn set_storage(&mut self, key: StorageKey, val: Option<StorageValue>) {
let extrinsic_index = self.extrinsic_index();
let entry = self.prospective.top.entry(key).or_default();
entry.value = val;
@@ -235,10 +247,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<u8>,
storage_key: StorageKey,
child_info: ChildInfo,
key: Vec<u8>,
val: Option<Vec<u8>>,
key: StorageKey,
val: Option<StorageValue>,
) {
let extrinsic_index = self.extrinsic_index();
let map_entry = self.prospective.children.entry(storage_key)
@@ -417,8 +429,8 @@ impl OverlayedChanges {
/// Panics:
/// Will panic if there are any uncommitted prospective changes.
pub fn into_committed(self) -> (
impl Iterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
impl Iterator<Item=(Vec<u8>, (impl Iterator<Item=(Vec<u8>, Option<Vec<u8>>)>, OwnedChildInfo))>,
impl Iterator<Item=(StorageKey, Option<StorageValue>)>,
impl Iterator<Item=(StorageKey, (impl Iterator<Item=(StorageKey, Option<StorageValue>)>, OwnedChildInfo))>,
){
assert!(self.prospective.is_empty());
(
@@ -631,8 +643,8 @@ impl OverlayedChanges {
}
#[cfg(test)]
impl From<Option<Vec<u8>>> for OverlayedValue {
fn from(value: Option<Vec<u8>>) -> OverlayedValue {
impl From<Option<StorageValue>> for OverlayedValue {
fn from(value: Option<StorageValue>) -> OverlayedValue {
OverlayedValue { value, ..Default::default() }
}
}
@@ -647,8 +659,8 @@ mod tests {
use crate::ext::Ext;
use super::*;
fn strip_extrinsic_index(map: &BTreeMap<Vec<u8>, OverlayedValue>)
-> BTreeMap<Vec<u8>, OverlayedValue>
fn strip_extrinsic_index(map: &BTreeMap<StorageKey, OverlayedValue>)
-> BTreeMap<StorageKey, OverlayedValue>
{
let mut clone = map.clone();
clone.remove(&EXTRINSIC_INDEX.to_vec());