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]> {
+16 -14
View File
@@ -22,9 +22,10 @@ use crate::backend::{Backend, InMemory};
use hash_db::Hasher;
use trie::{TrieConfiguration, default_child_trie_root};
use trie::trie_types::Layout;
use primitives::offchain;
use primitives::storage::well_known_keys::is_child_storage_key;
use super::{ChildStorageKey, Externalities};
use primitives::{
storage::well_known_keys::is_child_storage_key, child_storage_key::ChildStorageKey, offchain,
traits::Externalities,
};
use log::warn;
/// Simple HashMap-based Externalities impl.
@@ -35,7 +36,6 @@ pub struct BasicExternalities {
}
impl BasicExternalities {
/// Create a new instance of `BasicExternalities`
pub fn new(
top: HashMap<Vec<u8>, Vec<u8>>,
@@ -97,11 +97,11 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
Externalities::<H>::storage(self, key)
}
fn child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
fn child_storage(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<Vec<u8>> {
self.children.get(storage_key.as_ref()).and_then(|child| child.get(key)).cloned()
}
fn original_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
fn original_child_storage(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<Vec<u8>> {
Externalities::<H>::child_storage(self, storage_key, key)
}
@@ -119,9 +119,9 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
fn place_child_storage(
&mut self,
storage_key: ChildStorageKey<H>,
storage_key: ChildStorageKey,
key: Vec<u8>,
value: Option<Vec<u8>>
value: Option<Vec<u8>>,
) {
let child_map = self.children.entry(storage_key.into_owned()).or_default();
if let Some(value) = value {
@@ -131,7 +131,7 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
}
}
fn kill_child_storage(&mut self, storage_key: ChildStorageKey<H>) {
fn kill_child_storage(&mut self, storage_key: ChildStorageKey) {
self.children.remove(storage_key.as_ref());
}
@@ -147,7 +147,7 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
self.top.retain(|key, _| !key.starts_with(prefix));
}
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey<H>, prefix: &[u8]) {
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey, prefix: &[u8]) {
if let Some(child) = self.children.get_mut(storage_key.as_ref()) {
child.retain(|key, _| !key.starts_with(prefix));
}
@@ -163,9 +163,10 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
// type of child trie support.
let empty_hash = default_child_trie_root::<Layout<H>>(&[]);
for storage_key in keys {
let child_root = self.child_storage_root(
ChildStorageKey::<H>::from_slice(storage_key.as_slice())
.expect("Map only feed by valid keys; qed")
let child_root = Externalities::<H>::child_storage_root(
self,
ChildStorageKey::from_slice(storage_key.as_slice())
.expect("Map only feed by valid keys; qed"),
);
if &empty_hash[..] == &child_root[..] {
top.remove(&storage_key);
@@ -173,10 +174,11 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
top.insert(storage_key, child_root);
}
}
Layout::<H>::trie_root(self.top.clone())
}
fn child_storage_root(&mut self, storage_key: ChildStorageKey<H>) -> Vec<u8> {
fn child_storage_root(&mut self, storage_key: ChildStorageKey) -> Vec<u8> {
if let Some(child) = self.children.get(storage_key.as_ref()) {
let delta = child.clone().into_iter().map(|(k, v)| (k, Some(v)));
+47
View File
@@ -0,0 +1,47 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
/// State Machine Errors
use std::fmt;
/// State Machine Error bound.
///
/// This should reflect Wasm error type bound for future compatibility.
pub trait Error: 'static + fmt::Debug + fmt::Display + Send {}
impl<T: 'static + fmt::Debug + fmt::Display + Send> Error for T {}
/// Externalities Error.
///
/// Externalities are not really allowed to have errors, since it's assumed that dependent code
/// would not be executed unless externalities were available. This is included for completeness,
/// and as a transition away from the pre-existing framework.
#[derive(Debug, Eq, PartialEq)]
pub enum ExecutionError {
/// Backend error.
Backend(String),
/// The entry `:code` doesn't exist in storage so there's no way we can execute anything.
CodeEntryDoesNotExist,
/// Backend is incompatible with execution proof generation process.
UnableToGenerateProof,
/// Invalid execution proof.
InvalidProof,
}
impl fmt::Display for ExecutionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Externalities Error") }
}
+18 -15
View File
@@ -18,14 +18,17 @@
use std::{error, fmt, cmp::Ord};
use log::warn;
use crate::backend::Backend;
use crate::changes_trie::{
Storage as ChangesTrieStorage, CacheAction as ChangesTrieCacheAction,
build_changes_trie,
use crate::{
backend::Backend, OverlayedChanges,
changes_trie::{
Storage as ChangesTrieStorage, CacheAction as ChangesTrieCacheAction, build_changes_trie,
},
};
use crate::{Externalities, OverlayedChanges, ChildStorageKey};
use hash_db::Hasher;
use primitives::{offchain, storage::well_known_keys::is_child_storage_key, traits::BareCryptoStorePtr};
use primitives::{
offchain, storage::well_known_keys::is_child_storage_key,
traits::{BareCryptoStorePtr, Externalities}, child_storage_key::ChildStorageKey,
};
use trie::{MemoryDB, default_child_trie_root};
use trie::trie_types::Layout;
@@ -201,24 +204,24 @@ where
self.backend.storage_hash(key).expect(EXT_NOT_ALLOWED_TO_FAIL)
}
fn child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
fn child_storage(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<Vec<u8>> {
let _guard = panic_handler::AbortGuard::force_abort();
self.overlay.child_storage(storage_key.as_ref(), key).map(|x| x.map(|x| x.to_vec())).unwrap_or_else(||
self.backend.child_storage(storage_key.as_ref(), key).expect(EXT_NOT_ALLOWED_TO_FAIL))
}
fn child_storage_hash(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<H::Out> {
fn child_storage_hash(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<H::Out> {
let _guard = panic_handler::AbortGuard::force_abort();
self.overlay.child_storage(storage_key.as_ref(), key).map(|x| x.map(|x| H::hash(x))).unwrap_or_else(||
self.backend.storage_hash(key).expect(EXT_NOT_ALLOWED_TO_FAIL))
}
fn original_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
fn original_child_storage(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<Vec<u8>> {
let _guard = panic_handler::AbortGuard::force_abort();
self.backend.child_storage(storage_key.as_ref(), key).expect(EXT_NOT_ALLOWED_TO_FAIL)
}
fn original_child_storage_hash(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<H::Out> {
fn original_child_storage_hash(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<H::Out> {
let _guard = panic_handler::AbortGuard::force_abort();
self.backend.child_storage_hash(storage_key.as_ref(), key).expect(EXT_NOT_ALLOWED_TO_FAIL)
}
@@ -231,7 +234,7 @@ where
}
}
fn exists_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> bool {
fn exists_child_storage(&self, storage_key: ChildStorageKey, key: &[u8]) -> bool {
let _guard = panic_handler::AbortGuard::force_abort();
match self.overlay.child_storage(storage_key.as_ref(), key) {
@@ -251,14 +254,14 @@ where
self.overlay.set_storage(key, value);
}
fn place_child_storage(&mut self, storage_key: ChildStorageKey<H>, key: Vec<u8>, value: Option<Vec<u8>>) {
fn place_child_storage(&mut self, storage_key: ChildStorageKey, key: Vec<u8>, value: Option<Vec<u8>>) {
let _guard = panic_handler::AbortGuard::force_abort();
self.mark_dirty();
self.overlay.set_child_storage(storage_key.into_owned(), key, value);
}
fn kill_child_storage(&mut self, storage_key: ChildStorageKey<H>) {
fn kill_child_storage(&mut self, storage_key: ChildStorageKey) {
let _guard = panic_handler::AbortGuard::force_abort();
self.mark_dirty();
@@ -282,7 +285,7 @@ where
});
}
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey<H>, prefix: &[u8]) {
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey, prefix: &[u8]) {
let _guard = panic_handler::AbortGuard::force_abort();
self.mark_dirty();
@@ -323,7 +326,7 @@ where
root
}
fn child_storage_root(&mut self, storage_key: ChildStorageKey<H>) -> Vec<u8> {
fn child_storage_root(&mut self, storage_key: ChildStorageKey) -> Vec<u8> {
let _guard = panic_handler::AbortGuard::force_abort();
if self.storage_transaction.is_some() {
self
+189 -485
View File
@@ -19,17 +19,17 @@
#![warn(missing_docs)]
use std::{fmt, panic::UnwindSafe, result, marker::PhantomData};
use std::borrow::Cow;
use log::warn;
use hash_db::Hasher;
use codec::{Decode, Encode};
use primitives::{
storage::well_known_keys, NativeOrEncoded, NeverNativeValue, offchain,
traits::BareCryptoStorePtr,
storage::well_known_keys, NativeOrEncoded, NeverNativeValue, offchain::{self, NeverOffchainExt},
traits::{BareCryptoStorePtr, CodeExecutor},
};
pub mod backend;
mod changes_trie;
mod error;
mod ext;
mod testing;
mod basic;
@@ -64,6 +64,11 @@ pub use proving_backend::{
};
pub use trie_backend_essence::{TrieBackendStorage, Storage};
pub use trie_backend::TrieBackend;
pub use error::{Error, ExecutionError};
type CallResult<R, E> = Result<NativeOrEncoded<R>, E>;
type DefaultHandler<R, E> = fn(CallResult<R, E>, CallResult<R, E>) -> CallResult<R, E>;
/// Type of changes trie transaction.
pub type ChangesTrieTransaction<H, N> = (
@@ -71,321 +76,6 @@ pub type ChangesTrieTransaction<H, N> = (
ChangesTrieCacheAction<<H as Hasher>::Out, N>,
);
/// A wrapper around a child storage key.
///
/// This wrapper ensures that the child storage key is correct and properly used. It is
/// impossible to create an instance of this struct without providing a correct `storage_key`.
pub struct ChildStorageKey<'a, H: Hasher> {
storage_key: Cow<'a, [u8]>,
_hasher: PhantomData<H>,
}
impl<'a, H: Hasher> ChildStorageKey<'a, H> {
fn new(storage_key: Cow<'a, [u8]>) -> Option<Self> {
if !trie::is_child_trie_key_valid::<Layout<H>>(&storage_key) {
return None;
}
Some(ChildStorageKey {
storage_key,
_hasher: PhantomData,
})
}
/// Create a new `ChildStorageKey` from a vector.
///
/// `storage_key` has should start with `:child_storage:default:`
/// See `is_child_trie_key_valid` for more details.
pub fn from_vec(key: Vec<u8>) -> Option<Self> {
Self::new(Cow::Owned(key))
}
/// Create a new `ChildStorageKey` from a slice.
///
/// `storage_key` has should start with `:child_storage:default:`
/// See `is_child_trie_key_valid` for more details.
pub fn from_slice(key: &'a [u8]) -> Option<Self> {
Self::new(Cow::Borrowed(key))
}
/// Get access to the byte representation of the storage key.
///
/// This key is guaranteed to be correct.
pub fn as_ref(&self) -> &[u8] {
&*self.storage_key
}
/// Destruct this instance into an owned vector that represents the storage key.
///
/// This key is guaranteed to be correct.
pub fn into_owned(self) -> Vec<u8> {
self.storage_key.into_owned()
}
}
/// State Machine Error bound.
///
/// This should reflect WASM error type bound for future compatibility.
pub trait Error: 'static + fmt::Debug + fmt::Display + Send {}
impl Error for ExecutionError {}
/// Externalities Error.
///
/// Externalities are not really allowed to have errors, since it's assumed that dependent code
/// would not be executed unless externalities were available. This is included for completeness,
/// and as a transition away from the pre-existing framework.
#[derive(Debug, Eq, PartialEq)]
pub enum ExecutionError {
/// Backend error.
Backend(String),
/// The entry `:code` doesn't exist in storage so there's no way we can execute anything.
CodeEntryDoesNotExist,
/// Backend is incompatible with execution proof generation process.
UnableToGenerateProof,
/// Invalid execution proof.
InvalidProof,
}
impl fmt::Display for ExecutionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Externalities Error") }
}
type CallResult<R, E> = Result<NativeOrEncoded<R>, E>;
/// Externalities: pinned to specific active address.
pub trait Externalities<H: Hasher> {
/// Read runtime storage.
fn storage(&self, key: &[u8]) -> Option<Vec<u8>>;
/// Get storage value hash. This may be optimized for large values.
fn storage_hash(&self, key: &[u8]) -> Option<H::Out> {
self.storage(key).map(|v| H::hash(&v))
}
/// Get child storage value hash. This may be optimized for large values.
fn child_storage_hash(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<H::Out> {
self.child_storage(storage_key, key).map(|v| H::hash(&v))
}
/// Read original runtime storage, ignoring any overlayed changes.
fn original_storage(&self, key: &[u8]) -> Option<Vec<u8>>;
/// Read original runtime child storage, ignoring any overlayed changes.
fn original_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>>;
/// Get original storage value hash, ignoring any overlayed changes.
/// This may be optimized for large values.
fn original_storage_hash(&self, key: &[u8]) -> Option<H::Out> {
self.original_storage(key).map(|v| H::hash(&v))
}
/// Get original child storage value hash, ignoring any overlayed changes.
/// This may be optimized for large values.
fn original_child_storage_hash(
&self,
storage_key: ChildStorageKey<H>,
key: &[u8],
) -> Option<H::Out> {
self.original_child_storage(storage_key, key).map(|v| H::hash(&v))
}
/// Read child runtime storage.
fn child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>>;
/// Set storage entry `key` of current contract being called (effective immediately).
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
self.place_storage(key, Some(value));
}
/// Set child storage entry `key` of current contract being called (effective immediately).
fn set_child_storage(&mut self, storage_key: ChildStorageKey<H>, key: Vec<u8>, value: Vec<u8>) {
self.place_child_storage(storage_key, key, Some(value))
}
/// Clear a storage entry (`key`) of current contract being called (effective immediately).
fn clear_storage(&mut self, key: &[u8]) {
self.place_storage(key.to_vec(), None);
}
/// Clear a child storage entry (`key`) of current contract being called (effective immediately).
fn clear_child_storage(&mut self, storage_key: ChildStorageKey<H>, key: &[u8]) {
self.place_child_storage(storage_key, key.to_vec(), None)
}
/// Whether a storage entry exists.
fn exists_storage(&self, key: &[u8]) -> bool {
self.storage(key).is_some()
}
/// Whether a child storage entry exists.
fn exists_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> bool {
self.child_storage(storage_key, key).is_some()
}
/// Clear an entire child storage.
fn kill_child_storage(&mut self, storage_key: ChildStorageKey<H>);
/// Clear storage entries which keys start with the given prefix.
fn clear_prefix(&mut self, prefix: &[u8]);
/// Clear child storage entries which keys start with the given prefix.
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey<H>, prefix: &[u8]);
/// Set or clear a storage entry (`key`) of current contract being called (effective immediately).
fn place_storage(&mut self, key: Vec<u8>, value: Option<Vec<u8>>);
/// Set or clear a child storage entry. Return whether the operation succeeds.
fn place_child_storage(&mut self, storage_key: ChildStorageKey<H>, key: Vec<u8>, value: Option<Vec<u8>>);
/// Get the identity of the chain.
fn chain_id(&self) -> u64;
/// Get the trie root of the current storage map. This will also update all child storage keys in the top-level storage map.
fn storage_root(&mut self) -> H::Out where H::Out: Ord;
/// Get the trie root of a child storage map. This will also update the value of the child
/// storage keys in the top-level storage map.
/// If the storage root equals the default hash as defined by the trie, the key in the top-level
/// storage map will be removed.
fn child_storage_root(&mut self, storage_key: ChildStorageKey<H>) -> Vec<u8>;
/// Get the change trie root of the current storage overlay at a block with given parent.
fn storage_changes_root(&mut self, parent: H::Out) -> Result<Option<H::Out>, ()> where H::Out: Ord;
/// Returns offchain externalities extension if present.
fn offchain(&mut self) -> Option<&mut dyn offchain::Externalities>;
/// Returns the keystore.
fn keystore(&self) -> Option<BareCryptoStorePtr>;
}
/// An implementation of offchain extensions that should never be triggered.
pub enum NeverOffchainExt {}
impl NeverOffchainExt {
/// Create new offchain extensions.
pub fn new<'a>() -> Option<&'a mut Self> {
None
}
}
impl offchain::Externalities for NeverOffchainExt {
fn is_validator(&self) -> bool {
unreachable!()
}
fn submit_transaction(&mut self, _extrinsic: Vec<u8>) -> Result<(), ()> {
unreachable!()
}
fn network_state(
&self,
) -> Result<offchain::OpaqueNetworkState, ()> {
unreachable!()
}
fn timestamp(&mut self) -> offchain::Timestamp {
unreachable!()
}
fn sleep_until(&mut self, _deadline: offchain::Timestamp) {
unreachable!()
}
fn random_seed(&mut self) -> [u8; 32] {
unreachable!()
}
fn local_storage_set(&mut self, _kind: offchain::StorageKind, _key: &[u8], _value: &[u8]) {
unreachable!()
}
fn local_storage_compare_and_set(
&mut self,
_kind: offchain::StorageKind,
_key: &[u8],
_old_value: Option<&[u8]>,
_new_value: &[u8],
) -> bool {
unreachable!()
}
fn local_storage_get(&mut self, _kind: offchain::StorageKind, _key: &[u8]) -> Option<Vec<u8>> {
unreachable!()
}
fn http_request_start(
&mut self,
_method: &str,
_uri: &str,
_meta: &[u8]
) -> Result<offchain::HttpRequestId, ()> {
unreachable!()
}
fn http_request_add_header(
&mut self,
_request_id: offchain::HttpRequestId,
_name: &str,
_value: &str
) -> Result<(), ()> {
unreachable!()
}
fn http_request_write_body(
&mut self,
_request_id: offchain::HttpRequestId,
_chunk: &[u8],
_deadline: Option<offchain::Timestamp>
) -> Result<(), offchain::HttpError> {
unreachable!()
}
fn http_response_wait(
&mut self,
_ids: &[offchain::HttpRequestId],
_deadline: Option<offchain::Timestamp>
) -> Vec<offchain::HttpRequestStatus> {
unreachable!()
}
fn http_response_headers(
&mut self,
_request_id: offchain::HttpRequestId
) -> Vec<(Vec<u8>, Vec<u8>)> {
unreachable!()
}
fn http_response_read_body(
&mut self,
_request_id: offchain::HttpRequestId,
_buffer: &mut [u8],
_deadline: Option<offchain::Timestamp>
) -> Result<usize, offchain::HttpError> {
unreachable!()
}
}
/// Code execution engine.
pub trait CodeExecutor<H: Hasher>: Sized + Send + Sync {
/// Externalities error type.
type Error: Error;
/// Call a given method in the runtime. Returns a tuple of the result (either the output data
/// or an execution error) together with a `bool`, which is true if native execution was used.
fn call<
E: Externalities<H>, R: Encode + Decode + PartialEq, NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe
>(
&self,
ext: &mut E,
method: &str,
data: &[u8],
use_native: bool,
native_call: Option<NC>,
) -> (CallResult<R, Self::Error>, bool);
}
/// Strategy for executing a call into the runtime.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum ExecutionStrategy {
@@ -399,11 +89,6 @@ pub enum ExecutionStrategy {
NativeElseWasm,
}
type DefaultHandler<R, E> = fn(
CallResult<R, E>,
CallResult<R, E>,
) -> CallResult<R, E>;
/// Like `ExecutionStrategy` only it also stores a handler in case of consensus failure.
#[derive(Clone)]
pub enum ExecutionManager<F> {
@@ -430,7 +115,9 @@ impl<'a, F> From<&'a ExecutionManager<F>> for ExecutionStrategy {
impl ExecutionStrategy {
/// Gets the corresponding manager for the execution strategy.
pub fn get_manager<E: std::fmt::Debug, R: Decode + Encode>(self) -> ExecutionManager<DefaultHandler<R, E>> {
pub fn get_manager<E: fmt::Debug, R: Decode + Encode>(
self,
) -> ExecutionManager<DefaultHandler<R, E>> {
match self {
ExecutionStrategy::AlwaysWasm => ExecutionManager::AlwaysWasm,
ExecutionStrategy::NativeWhenPossible => ExecutionManager::NativeWhenPossible,
@@ -447,49 +134,14 @@ impl ExecutionStrategy {
}
}
/// Evaluate to ExecutionManager::NativeWhenPossible, without having to figure out the type.
pub fn native_when_possible<E, R: Decode>() -> ExecutionManager<DefaultHandler<R, E>> {
ExecutionManager::NativeWhenPossible
}
/// Evaluate to ExecutionManager::NativeElseWasm, without having to figure out the type.
pub fn native_else_wasm<E, R: Decode>() -> ExecutionManager<DefaultHandler<R, E>> {
ExecutionManager::NativeElseWasm
}
/// Evaluate to ExecutionManager::NativeWhenPossible, without having to figure out the type.
pub fn always_wasm<E, R: Decode>() -> ExecutionManager<DefaultHandler<R, E>> {
ExecutionManager::AlwaysWasm
}
/// Creates new substrate state machine.
pub fn new<'a, H, N, B, T, O, Exec>(
backend: &'a B,
changes_trie_storage: Option<&'a T>,
offchain_ext: Option<&'a mut O>,
overlay: &'a mut OverlayedChanges,
exec: &'a Exec,
method: &'a str,
call_data: &'a [u8],
keystore: Option<BareCryptoStorePtr>,
) -> StateMachine<'a, H, N, B, T, O, Exec> {
StateMachine {
backend,
changes_trie_storage,
offchain_ext,
overlay,
exec,
method,
call_data,
keystore,
_hasher: PhantomData,
}
}
/// The substrate state machine.
pub struct StateMachine<'a, H, N, B, T, O, Exec> {
backend: &'a B,
pub struct StateMachine<'a, B, H, N, T, O, Exec> {
backend: B,
changes_trie_storage: Option<&'a T>,
offchain_ext: Option<&'a mut O>,
overlay: &'a mut OverlayedChanges,
@@ -500,7 +152,7 @@ pub struct StateMachine<'a, H, N, B, T, O, Exec> {
_hasher: PhantomData<(H, N)>,
}
impl<'a, H, N, B, T, O, Exec> StateMachine<'a, H, N, B, T, O, Exec> where
impl<'a, B, H, N, T, O, Exec> StateMachine<'a, B, H, N, T, O, Exec> where
H: Hasher,
Exec: CodeExecutor<H>,
B: Backend<H>,
@@ -509,6 +161,30 @@ impl<'a, H, N, B, T, O, Exec> StateMachine<'a, H, N, B, T, O, Exec> where
H::Out: Ord + 'static,
N: crate::changes_trie::BlockNumber,
{
/// Creates new substrate state machine.
pub fn new(
backend: B,
changes_trie_storage: Option<&'a T>,
offchain_ext: Option<&'a mut O>,
overlay: &'a mut OverlayedChanges,
exec: &'a Exec,
method: &'a str,
call_data: &'a [u8],
keystore: Option<BareCryptoStorePtr>,
) -> Self {
Self {
backend,
changes_trie_storage,
offchain_ext,
overlay,
exec,
method,
call_data,
keystore,
_hasher: PhantomData,
}
}
/// Execute a call using the given state backend, overlayed changes, and call executor.
/// Produces a state-backend-specific "transaction" which can be used to apply the changes
/// to the backing store, such as the disk.
@@ -551,7 +227,7 @@ impl<'a, H, N, B, T, O, Exec> StateMachine<'a, H, N, B, T, O, Exec> where
{
let mut externalities = ext::Ext::new(
self.overlay,
self.backend,
&self.backend,
self.changes_trie_storage,
self.offchain_ext.as_mut().map(|x| &mut **x),
self.keystore.clone(),
@@ -586,11 +262,19 @@ impl<'a, H, N, B, T, O, Exec> StateMachine<'a, H, N, B, T, O, Exec> where
CallResult<R, Exec::Error>
) -> CallResult<R, Exec::Error>
{
let (result, was_native, storage_delta, changes_delta) = self.execute_aux(compute_tx, true, native_call.take());
let (result, was_native, storage_delta, changes_delta) = self.execute_aux(
compute_tx,
true,
native_call.take(),
);
if was_native {
self.overlay.prospective = orig_prospective.clone();
let (wasm_result, _, wasm_storage_delta, wasm_changes_delta) = self.execute_aux(compute_tx, false, native_call);
let (wasm_result, _, wasm_storage_delta, wasm_changes_delta) = self.execute_aux(
compute_tx,
false,
native_call,
);
if (result.is_ok() && wasm_result.is_ok()
&& result.as_ref().ok() == wasm_result.as_ref().ok())
@@ -613,13 +297,21 @@ impl<'a, H, N, B, T, O, Exec> StateMachine<'a, H, N, B, T, O, Exec> where
R: Decode + Encode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe,
{
let (result, was_native, storage_delta, changes_delta) = self.execute_aux(compute_tx, true, native_call.take());
let (result, was_native, storage_delta, changes_delta) = self.execute_aux(
compute_tx,
true,
native_call.take(),
);
if !was_native || result.is_ok() {
(result, storage_delta, changes_delta)
} else {
self.overlay.prospective = orig_prospective.clone();
let (wasm_result, _, wasm_storage_delta, wasm_changes_delta) = self.execute_aux(compute_tx, false, native_call);
let (wasm_result, _, wasm_storage_delta, wasm_changes_delta) = self.execute_aux(
compute_tx,
false,
native_call,
);
(wasm_result, wasm_storage_delta, wasm_changes_delta)
}
}
@@ -646,7 +338,7 @@ impl<'a, H, N, B, T, O, Exec> StateMachine<'a, H, N, B, T, O, Exec> where
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe,
Handler: FnOnce(
CallResult<R, Exec::Error>,
CallResult<R, Exec::Error>
CallResult<R, Exec::Error>,
) -> CallResult<R, Exec::Error>
{
// read changes trie configuration. The reason why we're doing it here instead of the
@@ -654,8 +346,7 @@ impl<'a, H, N, B, T, O, Exec> StateMachine<'a, H, N, B, T, O, Exec> where
// proof-of-execution on light clients. And the proof is recorded by the backend which
// is created after OverlayedChanges
let backend = self.backend.clone();
let init_overlay = |overlay: &mut OverlayedChanges, final_check: bool| {
let init_overlay = |overlay: &mut OverlayedChanges, final_check: bool, backend: &B| {
let changes_trie_config = try_read_overlay_value(
overlay,
backend,
@@ -663,32 +354,41 @@ impl<'a, H, N, B, T, O, Exec> StateMachine<'a, H, N, B, T, O, Exec> where
)?;
set_changes_trie_config(overlay, changes_trie_config, final_check)
};
init_overlay(self.overlay, false)?;
init_overlay(self.overlay, false, &self.backend)?;
let result = {
let orig_prospective = self.overlay.prospective.clone();
let (result, storage_delta, changes_delta) = match manager {
ExecutionManager::Both(on_consensus_failure) => {
self.execute_call_with_both_strategy(compute_tx, native_call.take(), orig_prospective, on_consensus_failure)
self.execute_call_with_both_strategy(
compute_tx,
native_call.take(),
orig_prospective,
on_consensus_failure,
)
},
ExecutionManager::NativeElseWasm => {
self.execute_call_with_native_else_wasm_strategy(compute_tx, native_call.take(), orig_prospective)
self.execute_call_with_native_else_wasm_strategy(
compute_tx,
native_call.take(),
orig_prospective,
)
},
ExecutionManager::AlwaysWasm => {
let (result, _, storage_delta, changes_delta) = self.execute_aux(compute_tx, false, native_call);
(result, storage_delta, changes_delta)
let res = self.execute_aux(compute_tx, false, native_call);
(res.0, res.2, res.3)
},
ExecutionManager::NativeWhenPossible => {
let (result, _was_native, storage_delta, changes_delta) = self.execute_aux(compute_tx, true, native_call);
(result, storage_delta, changes_delta)
let res = self.execute_aux(compute_tx, true, native_call);
(res.0, res.2, res.3)
},
};
result.map(move |out| (out, storage_delta, changes_delta))
};
if result.is_ok() {
init_overlay(self.overlay, true)?;
init_overlay(self.overlay, true, &self.backend)?;
}
result.map_err(|e| Box::new(e) as _)
@@ -739,23 +439,16 @@ where
H::Out: Ord + 'static,
{
let proving_backend = proving_backend::ProvingBackend::new(trie_backend);
let mut sm = StateMachine {
backend: &proving_backend,
changes_trie_storage: None as Option<&changes_trie::InMemoryStorage<H, u64>>,
offchain_ext: NeverOffchainExt::new(),
overlay,
exec,
method,
call_data,
keystore,
_hasher: PhantomData,
};
let mut sm = StateMachine::<_, H, _, InMemoryChangesTrieStorage<H, u64>, _, Exec>::new(
proving_backend, None, NeverOffchainExt::new(), overlay, exec, method, call_data, keystore,
);
let (result, _, _) = sm.execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
native_else_wasm(),
false,
None,
)?;
let proof = proving_backend.extract_proof();
let proof = sm.backend.extract_proof();
Ok((result.into_encoded(), proof))
}
@@ -792,17 +485,10 @@ where
Exec: CodeExecutor<H>,
H::Out: Ord + 'static,
{
let mut sm = StateMachine {
backend: trie_backend,
changes_trie_storage: None as Option<&changes_trie::InMemoryStorage<H, u64>>,
offchain_ext: NeverOffchainExt::new(),
overlay,
exec,
method,
call_data,
keystore,
_hasher: PhantomData,
};
let mut sm = StateMachine::<_, H, _, InMemoryChangesTrieStorage<H, u64>, _, Exec>::new(
trie_backend, None, NeverOffchainExt::new(), overlay, exec, method, call_data, keystore,
);
sm.execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
native_else_wasm(),
false,
@@ -818,11 +504,11 @@ pub fn prove_read<B, H>(
where
B: Backend<H>,
H: Hasher,
H::Out: Ord
H::Out: Ord,
{
let trie_backend = backend.as_trie_backend()
.ok_or_else(
||Box::new(ExecutionError::UnableToGenerateProof) as Box<dyn Error>
|| Box::new(ExecutionError::UnableToGenerateProof) as Box<dyn Error>
)?;
prove_read_on_trie_backend(trie_backend, key)
}
@@ -836,14 +522,13 @@ pub fn prove_child_read<B, H>(
where
B: Backend<H>,
H: Hasher,
H::Out: Ord
H::Out: Ord,
{
let trie_backend = backend.as_trie_backend()
.ok_or_else(|| Box::new(ExecutionError::UnableToGenerateProof) as Box<dyn Error>)?;
prove_child_read_on_trie_backend(trie_backend, storage_key, key)
}
/// Generate storage read proof on pre-created trie backend.
pub fn prove_read_on_trie_backend<S, H>(
trie_backend: &TrieBackend<S, H>,
@@ -852,7 +537,7 @@ pub fn prove_read_on_trie_backend<S, H>(
where
S: trie_backend_essence::TrieBackendStorage<H>,
H: Hasher,
H::Out: Ord
H::Out: Ord,
{
let proving_backend = proving_backend::ProvingBackend::<_, H>::new(trie_backend);
let result = proving_backend.storage(key).map_err(|e| Box::new(e) as Box<dyn Error>)?;
@@ -868,10 +553,11 @@ pub fn prove_child_read_on_trie_backend<S, H>(
where
S: trie_backend_essence::TrieBackendStorage<H>,
H: Hasher,
H::Out: Ord
H::Out: Ord,
{
let proving_backend = proving_backend::ProvingBackend::<_, H>::new(trie_backend);
let result = proving_backend.child_storage(storage_key, key).map_err(|e| Box::new(e) as Box<dyn Error>)?;
let result = proving_backend.child_storage(storage_key, key)
.map_err(|e| Box::new(e) as Box<dyn Error>)?;
Ok((result, proving_backend.extract_proof()))
}
@@ -883,7 +569,7 @@ pub fn read_proof_check<H>(
) -> Result<Option<Vec<u8>>, Box<dyn Error>>
where
H: Hasher,
H::Out: Ord
H::Out: Ord,
{
let proving_backend = create_proof_check_backend::<H>(root, proof)?;
read_proof_check_on_proving_backend(&proving_backend, key)
@@ -898,13 +584,12 @@ pub fn read_child_proof_check<H>(
) -> Result<Option<Vec<u8>>, Box<dyn Error>>
where
H: Hasher,
H::Out: Ord
H::Out: Ord,
{
let proving_backend = create_proof_check_backend::<H>(root, proof)?;
read_child_proof_check_on_proving_backend(&proving_backend, storage_key, key)
}
/// Check storage read proof on pre-created proving backend.
pub fn read_proof_check_on_proving_backend<H>(
proving_backend: &TrieBackend<MemoryDB<H>, H>,
@@ -912,7 +597,7 @@ pub fn read_proof_check_on_proving_backend<H>(
) -> Result<Option<Vec<u8>>, Box<dyn Error>>
where
H: Hasher,
H::Out: Ord
H::Out: Ord,
{
proving_backend.storage(key).map_err(|e| Box::new(e) as Box<dyn Error>)
}
@@ -925,14 +610,14 @@ pub fn read_child_proof_check_on_proving_backend<H>(
) -> Result<Option<Vec<u8>>, Box<dyn Error>>
where
H: Hasher,
H::Out: Ord
H::Out: Ord,
{
proving_backend.child_storage(storage_key, key).map_err(|e| Box::new(e) as Box<dyn Error>)
}
/// Sets overlayed changes' changes trie configuration. Returns error if configuration
/// differs from previous OR config decode has failed.
pub(crate) fn set_changes_trie_config(
fn set_changes_trie_config(
overlay: &mut OverlayedChanges,
config: Option<Vec<u8>>,
final_check: bool,
@@ -956,12 +641,10 @@ pub(crate) fn set_changes_trie_config(
}
/// Reads storage value from overlay or from the backend.
fn try_read_overlay_value<H, B>(overlay: &OverlayedChanges, backend: &B, key: &[u8])
-> Result<Option<Vec<u8>>, Box<dyn Error>>
where
H: Hasher,
B: Backend<H>,
{
fn try_read_overlay_value<H, B>(
overlay: &OverlayedChanges,
backend: &B, key: &[u8],
) -> Result<Option<Vec<u8>>, Box<dyn Error>> where H: Hasher, B: Backend<H> {
match overlay.storage(key).map(|x| x.map(|x| x.to_vec())) {
Some(value) => Ok(value),
None => backend
@@ -982,7 +665,7 @@ mod tests {
InMemoryStorage as InMemoryChangesTrieStorage,
Configuration as ChangesTrieConfig,
};
use primitives::{Blake2Hasher, map};
use primitives::{Blake2Hasher, map, traits::Externalities, child_storage_key::ChildStorageKey};
struct DummyCodeExecutor {
change_changes_trie_config: bool,
@@ -1034,15 +717,17 @@ mod tests {
}
}
impl Error for u8 {}
#[test]
fn execute_works() {
assert_eq!(new(
&trie_backend::tests::test_trie(),
Some(&InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new()),
let backend = trie_backend::tests::test_trie();
let mut overlayed_changes = Default::default();
let changes_trie_storage = InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new();
let mut state_machine = StateMachine::new(
backend,
Some(&changes_trie_storage),
NeverOffchainExt::new(),
&mut Default::default(),
&mut overlayed_changes,
&DummyCodeExecutor {
change_changes_trie_config: false,
native_available: true,
@@ -1052,19 +737,26 @@ mod tests {
"test",
&[],
None,
).execute(
ExecutionStrategy::NativeWhenPossible
).unwrap().0, vec![66]);
);
assert_eq!(
state_machine.execute(ExecutionStrategy::NativeWhenPossible).unwrap().0,
vec![66],
);
}
#[test]
fn execute_works_with_native_else_wasm() {
assert_eq!(new(
&trie_backend::tests::test_trie(),
Some(&InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new()),
let backend = trie_backend::tests::test_trie();
let mut overlayed_changes = Default::default();
let changes_trie_storage = InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new();
let mut state_machine = StateMachine::new(
backend,
Some(&changes_trie_storage),
NeverOffchainExt::new(),
&mut Default::default(),
&mut overlayed_changes,
&DummyCodeExecutor {
change_changes_trie_config: false,
native_available: true,
@@ -1074,19 +766,23 @@ mod tests {
"test",
&[],
None,
).execute(
ExecutionStrategy::NativeElseWasm
).unwrap().0, vec![66]);
);
assert_eq!(state_machine.execute(ExecutionStrategy::NativeElseWasm).unwrap().0, vec![66]);
}
#[test]
fn dual_execution_strategy_detects_consensus_failure() {
let mut consensus_failed = false;
assert!(new(
&trie_backend::tests::test_trie(),
Some(&InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new()),
let backend = trie_backend::tests::test_trie();
let mut overlayed_changes = Default::default();
let changes_trie_storage = InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new();
let mut state_machine = StateMachine::new(
backend,
Some(&changes_trie_storage),
NeverOffchainExt::new(),
&mut Default::default(),
&mut overlayed_changes,
&DummyCodeExecutor {
change_changes_trie_config: false,
native_available: true,
@@ -1096,14 +792,18 @@ mod tests {
"test",
&[],
None,
).execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
ExecutionManager::Both(|we, _ne| {
consensus_failed = true;
we
}),
true,
None,
).is_err());
);
assert!(
state_machine.execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
ExecutionManager::Both(|we, _ne| {
consensus_failed = true;
we
}),
true,
None,
).is_err()
);
assert!(consensus_failed);
}
@@ -1276,47 +976,51 @@ mod tests {
#[test]
fn cannot_change_changes_trie_config() {
assert!(
new(
&trie_backend::tests::test_trie(),
Some(&InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new()),
NeverOffchainExt::new(),
&mut Default::default(),
&DummyCodeExecutor {
change_changes_trie_config: true,
native_available: false,
native_succeeds: true,
fallback_succeeds: true,
},
"test",
&[],
None,
)
.execute(ExecutionStrategy::NativeWhenPossible)
.is_err()
let backend = trie_backend::tests::test_trie();
let mut overlayed_changes = Default::default();
let changes_trie_storage = InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new();
let mut state_machine = StateMachine::new(
backend,
Some(&changes_trie_storage),
NeverOffchainExt::new(),
&mut overlayed_changes,
&DummyCodeExecutor {
change_changes_trie_config: true,
native_available: false,
native_succeeds: true,
fallback_succeeds: true,
},
"test",
&[],
None,
);
assert!(state_machine.execute(ExecutionStrategy::NativeWhenPossible).is_err());
}
#[test]
fn cannot_change_changes_trie_config_with_native_else_wasm() {
assert!(
new(
&trie_backend::tests::test_trie(),
Some(&InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new()),
NeverOffchainExt::new(),
&mut Default::default(),
&DummyCodeExecutor {
change_changes_trie_config: true,
native_available: false,
native_succeeds: true,
fallback_succeeds: true,
},
"test",
&[],
None,
)
.execute(ExecutionStrategy::NativeElseWasm)
.is_err()
let backend = trie_backend::tests::test_trie();
let mut overlayed_changes = Default::default();
let changes_trie_storage = InMemoryChangesTrieStorage::<Blake2Hasher, u64>::new();
let mut state_machine = StateMachine::new(
backend,
Some(&changes_trie_storage),
NeverOffchainExt::new(),
&mut overlayed_changes,
&DummyCodeExecutor {
change_changes_trie_config: true,
native_available: false,
native_succeeds: true,
fallback_succeeds: true,
},
"test",
&[],
None,
);
assert!(state_machine.execute(ExecutionStrategy::NativeElseWasm).is_err());
}
}
@@ -16,7 +16,8 @@
//! The overlayed changes to state.
#[cfg(test)] use std::iter::FromIterator;
#[cfg(test)]
use std::iter::FromIterator;
use std::collections::{HashMap, BTreeSet};
use codec::Decode;
use crate::changes_trie::{NO_EXTRINSIC_INDEX, Configuration as ChangesTrieConfig};
@@ -350,12 +351,12 @@ impl From<Option<Vec<u8>>> for OverlayedValue {
#[cfg(test)]
mod tests {
use hex_literal::hex;
use primitives::{Blake2Hasher, H256};
use primitives::storage::well_known_keys::EXTRINSIC_INDEX;
use primitives::{
Blake2Hasher, H256, traits::Externalities, storage::well_known_keys::EXTRINSIC_INDEX,
};
use crate::backend::InMemory;
use crate::changes_trie::InMemoryStorage as InMemoryChangesTrieStorage;
use crate::ext::Ext;
use crate::Externalities;
use super::*;
fn strip_extrinsic_index(map: &HashMap<Vec<u8>, OverlayedValue>) -> HashMap<Vec<u8>, OverlayedValue> {
@@ -128,9 +128,8 @@ impl<'a, S: 'a + TrieBackendStorage<H>, H: 'a + Hasher> ProvingBackend<'a, S, H>
}
}
/// Consume the backend, extracting the gathered proof in lexicographical order
/// by value.
pub fn extract_proof(self) -> Vec<Vec<u8>> {
/// Consume the backend, extracting the gathered proof in lexicographical order by value.
pub fn extract_proof(&self) -> Vec<Vec<u8>> {
self.proof_recorder
.borrow_mut()
.drain()
@@ -207,10 +206,6 @@ impl<'a, S, H> Backend<H> for ProvingBackend<'a, S, H>
{
self.backend.child_storage_root(storage_key, delta)
}
fn as_trie_backend(&mut self) -> Option<&TrieBackend<Self::TrieBackendStorage, H>> {
None
}
}
/// Create proof check backend.
@@ -249,8 +244,7 @@ mod tests {
use crate::backend::{InMemory};
use crate::trie_backend::tests::test_trie;
use super::*;
use primitives::{Blake2Hasher};
use crate::ChildStorageKey;
use primitives::{Blake2Hasher, child_storage_key::ChildStorageKey};
fn test_proving<'a>(
trie_backend: &'a TrieBackend<PrefixedMemoryDB<Blake2Hasher>,Blake2Hasher>,
@@ -315,12 +309,8 @@ mod tests {
#[test]
fn proof_recorded_and_checked_with_child() {
let subtrie1 = ChildStorageKey::<Blake2Hasher>::from_slice(
b":child_storage:default:sub1"
).unwrap();
let subtrie2 = ChildStorageKey::<Blake2Hasher>::from_slice(
b":child_storage:default:sub2"
).unwrap();
let subtrie1 = ChildStorageKey::from_slice(b":child_storage:default:sub1").unwrap();
let subtrie2 = ChildStorageKey::from_slice(b":child_storage:default:sub2").unwrap();
let own1 = subtrie1.into_owned();
let own2 = subtrie2.into_owned();
let contents = (0..64).map(|i| (None, vec![i], Some(vec![i])))
+14 -13
View File
@@ -18,17 +18,18 @@
use std::collections::{HashMap};
use hash_db::Hasher;
use crate::backend::{InMemory, Backend};
use primitives::storage::well_known_keys::is_child_storage_key;
use crate::changes_trie::{
build_changes_trie, InMemoryStorage as ChangesTrieInMemoryStorage,
BlockNumber as ChangesTrieBlockNumber,
use crate::{
backend::{InMemory, Backend}, OverlayedChanges,
changes_trie::{
build_changes_trie, InMemoryStorage as ChangesTrieInMemoryStorage,
BlockNumber as ChangesTrieBlockNumber,
},
};
use primitives::{
storage::well_known_keys::{CHANGES_TRIE_CONFIG, CODE, HEAP_PAGES}, traits::BareCryptoStorePtr, offchain
storage::well_known_keys::{CHANGES_TRIE_CONFIG, CODE, HEAP_PAGES, is_child_storage_key},
traits::{BareCryptoStorePtr, Externalities}, offchain, child_storage_key::ChildStorageKey,
};
use codec::Encode;
use super::{ChildStorageKey, Externalities, OverlayedChanges};
const EXT_NOT_ALLOWED_TO_FAIL: &str = "Externalities not allowed to fail within runtime";
@@ -156,7 +157,7 @@ impl<H, N> Externalities<H> for TestExternalities<H, N>
self.backend.storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL)
}
fn child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
fn child_storage(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<Vec<u8>> {
self.overlay
.child_storage(storage_key.as_ref(), key)
.map(|x| x.map(|x| x.to_vec()))
@@ -166,7 +167,7 @@ impl<H, N> Externalities<H> for TestExternalities<H, N>
)
}
fn original_child_storage(&self, storage_key: ChildStorageKey<H>, key: &[u8]) -> Option<Vec<u8>> {
fn original_child_storage(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<Vec<u8>> {
self.backend
.child_storage(storage_key.as_ref(), key)
.map(|x| x.map(|x| x.to_vec()))
@@ -183,14 +184,14 @@ impl<H, N> Externalities<H> for TestExternalities<H, N>
fn place_child_storage(
&mut self,
storage_key: ChildStorageKey<H>,
storage_key: ChildStorageKey,
key: Vec<u8>,
value: Option<Vec<u8>>
) {
self.overlay.set_child_storage(storage_key.into_owned(), key, value);
}
fn kill_child_storage(&mut self, storage_key: ChildStorageKey<H>) {
fn kill_child_storage(&mut self, storage_key: ChildStorageKey) {
let backend = &self.backend;
let overlay = &mut self.overlay;
@@ -214,7 +215,7 @@ impl<H, N> Externalities<H> for TestExternalities<H, N>
});
}
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey<H>, prefix: &[u8]) {
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey, prefix: &[u8]) {
self.overlay.clear_child_prefix(storage_key.as_ref(), prefix);
@@ -249,7 +250,7 @@ impl<H, N> Externalities<H> for TestExternalities<H, N>
}
fn child_storage_root(&mut self, storage_key: ChildStorageKey<H>) -> Vec<u8> {
fn child_storage_root(&mut self, storage_key: ChildStorageKey) -> Vec<u8> {
let storage_key = storage_key.as_ref();
let (root, is_empty, _) = {
@@ -57,8 +57,6 @@ impl<S: TrieBackendStorage<H>, H: Hasher> TrieBackend<S, H> {
}
}
impl super::Error for String {}
impl<S: TrieBackendStorage<H>, H: Hasher> Backend<H> for TrieBackend<S, H> where
H::Out: Ord,
{