Remove Backend::destroy_state (#5068)

* Remove `Backend::destroy_state`

This removes the `destroy_state` function of `Backend` and instead moves
the functionality into the `Drop` implementation of the state. This
makes it much easier to work with the state, as the user no longer needs
to call` destroy_state` manually. However, it requires that we switch
from `RwLock` to `ReentrantMutex` as while importing a block we maybe
need to lock again in `drop`.

* Bring back the `RwLock` and some other clean ups

* Fix compilation
This commit is contained in:
Gavin Wood
2020-03-05 17:01:52 +01:00
committed by GitHub
parent 26fc97f635
commit a8441ff968
7 changed files with 318 additions and 100 deletions
+11 -23
View File
@@ -80,7 +80,6 @@ where
let changes_trie = backend::changes_tries_state_at_block(
id, self.backend.changes_trie_storage()
)?;
// make sure to destroy state before exiting this function
let state = self.backend.state_at(*id)?;
let return_data = StateMachine::new(
&state,
@@ -93,12 +92,9 @@ where
).execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
strategy.get_manager(),
None,
);
{
let _lock = self.backend.get_import_lock().read();
self.backend.destroy_state(state)?;
}
Ok(return_data?.into_encoded())
)?;
Ok(return_data.into_encoded())
}
fn contextual_call<
@@ -138,9 +134,8 @@ where
let changes_trie_state = backend::changes_tries_state_at_block(at, self.backend.changes_trie_storage())?;
let mut storage_transaction_cache = storage_transaction_cache.map(|c| c.borrow_mut());
// make sure to destroy state before exiting this function
let mut state = self.backend.state_at(*at)?;
let result = match recorder {
match recorder {
Some(recorder) => state.as_trie_backend()
.ok_or_else(||
Box::new(sp_state_machine::ExecutionError::UnableToGenerateProof)
@@ -176,18 +171,15 @@ where
)
.with_storage_transaction_cache(storage_transaction_cache.as_mut().map(|c| &mut **c))
.execute_using_consensus_failure_handler(execution_manager, native_call)
};
{
let _lock = self.backend.get_import_lock().read();
self.backend.destroy_state(state)?;
}
result.map_err(Into::into)
}.map_err(Into::into)
}
fn runtime_version(&self, id: &BlockId<Block>) -> sp_blockchain::Result<RuntimeVersion> {
let mut overlay = OverlayedChanges::default();
let changes_trie_state = backend::changes_tries_state_at_block(id, self.backend.changes_trie_storage())?;
// make sure to destroy state before exiting this function
let changes_trie_state = backend::changes_tries_state_at_block(
id,
self.backend.changes_trie_storage(),
)?;
let state = self.backend.state_at(*id)?;
let mut cache = StorageTransactionCache::<Block, B::State>::default();
let mut ext = Ext::new(
@@ -197,12 +189,8 @@ where
changes_trie_state,
None,
);
let version = self.executor.runtime_version(&mut ext);
{
let _lock = self.backend.get_import_lock().read();
self.backend.destroy_state(state)?;
}
version.map_err(|e| sp_blockchain::Error::VersionInvalid(format!("{:?}", e)).into())
self.executor.runtime_version(&mut ext)
.map_err(|e| sp_blockchain::Error::VersionInvalid(format!("{:?}", e)).into())
}
fn prove_at_trie_state<S: sp_state_machine::TrieBackendStorage<HashFor<Block>>>(