Refactor OverlayedChanges (#5989)

* Hide internal structure of OverlayChanges

* Fix tests for OverlayChanges refactor

* Do not clone pending changes

Discarding prospective changes should be equivalent as a state machine
is not to be called with peding changes.

This will be replaced by a storage transaction that is rolled back before
executing the call the second time removing this constraint.

* Doc fixes

* Remove overlong line

* Revert "Do not clone pending changes"

This reverts commit 4799491f4ac16f8517287a0fcf4a3f84ad56f46e.

* Deduplicate chield tries returned from child_infos()

* Remove redundant type annotation

* Avoid changing the storage root in tests

* Preserve extrinsic indices in trie build test

* Swap order of comitted and prospective in fn child_infos

This is only for consistency and does not impact the result.

* Rename set_pending to replace_pending for clearity
This commit is contained in:
Alexander Theißen
2020-05-20 11:39:45 +02:00
committed by GitHub
parent f275c6ab0b
commit 7a5bdb896b
5 changed files with 149 additions and 164 deletions
+13 -23
View File
@@ -28,7 +28,6 @@ use sp_core::{
storage::ChildInfo, NativeOrEncoded, NeverNativeValue, hexdisplay::HexDisplay,
traits::{CodeExecutor, CallInWasmExt, RuntimeCode},
};
use overlayed_changes::OverlayedChangeSet;
use sp_externalities::Extensions;
pub mod backend;
@@ -336,7 +335,6 @@ impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> where
fn execute_call_with_both_strategy<Handler, R, NC>(
&mut self,
mut native_call: Option<NC>,
orig_prospective: OverlayedChangeSet,
on_consensus_failure: Handler,
) -> CallResult<R, Exec::Error>
where
@@ -347,10 +345,11 @@ impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> where
CallResult<R, Exec::Error>,
) -> CallResult<R, Exec::Error>
{
let pending_changes = self.overlay.clone_pending();
let (result, was_native) = self.execute_aux(true, native_call.take());
if was_native {
self.overlay.prospective = orig_prospective.clone();
self.overlay.replace_pending(pending_changes);
let (wasm_result, _) = self.execute_aux(
false,
native_call,
@@ -372,12 +371,12 @@ impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> where
fn execute_call_with_native_else_wasm_strategy<R, NC>(
&mut self,
mut native_call: Option<NC>,
orig_prospective: OverlayedChangeSet,
) -> CallResult<R, Exec::Error>
where
R: Decode + Encode + PartialEq,
NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
{
let pending_changes = self.overlay.clone_pending();
let (result, was_native) = self.execute_aux(
true,
native_call.take(),
@@ -386,7 +385,7 @@ impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> where
if !was_native || result.is_ok() {
result
} else {
self.overlay.prospective = orig_prospective.clone();
self.overlay.replace_pending(pending_changes);
let (wasm_result, _) = self.execute_aux(
false,
native_call,
@@ -421,20 +420,16 @@ impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> where
self.overlay.set_collect_extrinsics(changes_tries_enabled);
let result = {
let orig_prospective = self.overlay.prospective.clone();
match manager {
ExecutionManager::Both(on_consensus_failure) => {
self.execute_call_with_both_strategy(
native_call.take(),
orig_prospective,
on_consensus_failure,
)
},
ExecutionManager::NativeElseWasm => {
self.execute_call_with_native_else_wasm_strategy(
native_call.take(),
orig_prospective,
)
},
ExecutionManager::AlwaysWasm(trust_level) => {
@@ -754,7 +749,6 @@ where
mod tests {
use std::collections::BTreeMap;
use codec::Encode;
use overlayed_changes::OverlayedValue;
use super::*;
use super::ext::Ext;
use super::changes_trie::Configuration as ChangesTrieConfig;
@@ -977,21 +971,16 @@ mod tests {
];
let mut state = InMemoryBackend::<BlakeTwo256>::from(initial);
let backend = state.as_trie_backend().unwrap();
let mut overlay = OverlayedChanges {
committed: map![
b"aba".to_vec() => OverlayedValue::from(Some(b"1312".to_vec())),
b"bab".to_vec() => OverlayedValue::from(Some(b"228".to_vec()))
],
prospective: map![
b"abd".to_vec() => OverlayedValue::from(Some(b"69".to_vec())),
b"bbd".to_vec() => OverlayedValue::from(Some(b"42".to_vec()))
],
..Default::default()
};
let mut offchain_overlay = Default::default();
let mut overlay = OverlayedChanges::default();
overlay.set_storage(b"aba".to_vec(), Some(b"1312".to_vec()));
overlay.set_storage(b"bab".to_vec(), Some(b"228".to_vec()));
overlay.commit_prospective();
overlay.set_storage(b"abd".to_vec(), Some(b"69".to_vec()));
overlay.set_storage(b"bbd".to_vec(), Some(b"42".to_vec()));
{
let mut offchain_overlay = Default::default();
let mut cache = StorageTransactionCache::default();
let mut ext = Ext::new(
&mut overlay,
@@ -1006,7 +995,8 @@ mod tests {
overlay.commit_prospective();
assert_eq!(
overlay.committed,
overlay.changes(None).map(|(k, v)| (k.clone(), v.value().cloned()))
.collect::<HashMap<_, _>>(),
map![
b"abc".to_vec() => None.into(),
b"abb".to_vec() => None.into(),