Cleanup of the state-machine crate (#3524)

* Start refactoring state-machine crate

* More improvement to state-machine

* Fix tests compilation on master and remove warnings

* Fix compilation

* Apply suggestions from code review

Co-Authored-By: Sergei Pepyakin <sergei@parity.io>

* Update core/state-machine/src/basic.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Line width

* Update core/primitives/src/storage.rs

Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com>

* Update core/state-machine/src/error.rs

Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com>

* Review feedback
This commit is contained in:
Bastian Köcher
2019-09-10 17:00:00 +02:00
committed by GitHub
parent 2beeda1488
commit 9607afd629
37 changed files with 781 additions and 674 deletions
+57 -9
View File
@@ -16,16 +16,15 @@
//! State machine backends. These manage the code and storage of contracts.
use std::{error, fmt};
use std::cmp::Ord;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::{error, fmt, cmp::Ord, collections::HashMap, marker::PhantomData};
use log::warn;
use hash_db::Hasher;
use crate::trie_backend::TrieBackend;
use crate::trie_backend_essence::TrieBackendStorage;
use trie::{TrieMut, MemoryDB, child_trie_root, default_child_trie_root, TrieConfiguration};
use trie::trie_types::{TrieDBMut, Layout};
use trie::{
TrieMut, MemoryDB, child_trie_root, default_child_trie_root, TrieConfiguration,
trie_types::{TrieDBMut, Layout},
};
/// A state backend is used to read state data and can have changes committed
/// to it.
@@ -119,7 +118,9 @@ pub trait Backend<H: Hasher> {
}
/// Try convert into trie backend.
fn as_trie_backend(&mut self) -> Option<&TrieBackend<Self::TrieBackendStorage, H>>;
fn as_trie_backend(&mut self) -> Option<&TrieBackend<Self::TrieBackendStorage, H>> {
None
}
/// Calculate the storage root, with given delta over what is already stored
/// in the backend, and produce a "transaction" that can be used to commit.
@@ -154,7 +155,56 @@ pub trait Backend<H: Hasher> {
txs.consolidate(parent_txs);
(root, txs)
}
}
impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
type Error = T::Error;
type Transaction = T::Transaction;
type TrieBackendStorage = T::TrieBackendStorage;
fn storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
(*self).storage(key)
}
fn child_storage(&self, storage_key: &[u8], key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
(*self).child_storage(storage_key, key)
}
fn for_keys_in_child_storage<F: FnMut(&[u8])>(&self, storage_key: &[u8], f: F) {
(*self).for_keys_in_child_storage(storage_key, f)
}
fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], f: F) {
(*self).for_keys_with_prefix(prefix, f)
}
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(&self, storage_key: &[u8], prefix: &[u8], f: F) {
(*self).for_child_keys_with_prefix(storage_key, prefix, f)
}
fn storage_root<I>(&self, delta: I) -> (H::Out, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
H::Out: Ord,
{
(*self).storage_root(delta)
}
fn child_storage_root<I>(&self, storage_key: &[u8], delta: I) -> (Vec<u8>, bool, Self::Transaction)
where
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
H::Out: Ord,
{
(*self).child_storage_root(storage_key, delta)
}
fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
(*self).pairs()
}
fn for_key_values_with_prefix<F: FnMut(&[u8], &[u8])>(&self, prefix: &[u8], f: F) {
(*self).for_key_values_with_prefix(prefix, f);
}
}
/// Trait that allows consolidate two transactions together.
@@ -298,8 +348,6 @@ impl<H: Hasher> From<Vec<(Option<Vec<u8>>, Vec<u8>, Option<Vec<u8>>)>> for InMem
}
}
impl super::Error for Void {}
impl<H: Hasher> InMemory<H> {
/// child storage key iterator
pub fn child_storage_keys(&self) -> impl Iterator<Item=&[u8]> {