Remove runtime registered extensions after execution (#7236)

* Remove runtime registered extensions after execution

This prevents a bug when an extension was registered in native, but the
native execution aborted without removing the extension again. Now, when
executing the wasm code the extension is still registered and it fails
of being registered. So, the wasm execution fails as well. This can
happen for example for the `VerificationExt`.

* Make it better
This commit is contained in:
Bastian Köcher
2020-10-01 11:12:05 +02:00
committed by GitHub
parent 8656a34289
commit dbf4558633
7 changed files with 180 additions and 39 deletions
+11 -9
View File
@@ -19,7 +19,7 @@
use crate::{
StorageKey, StorageValue, OverlayedChanges,
backend::Backend,
backend::Backend, overlayed_changes::OverlayedExtensions,
};
use hash_db::Hasher;
use sp_core::{
@@ -27,8 +27,9 @@ use sp_core::{
hexdisplay::HexDisplay,
};
use sp_trie::{trie_types::Layout, empty_child_trie_root};
use sp_externalities::{Externalities, Extensions, Extension,
ExtensionStore};
use sp_externalities::{
Externalities, Extensions, Extension, ExtensionStore,
};
use codec::{Decode, Encode, EncodeAppend};
use sp_std::{fmt, any::{Any, TypeId}, vec::Vec, vec, boxed::Box};
@@ -115,7 +116,7 @@ pub struct Ext<'a, H, N, B>
_phantom: sp_std::marker::PhantomData<N>,
/// Extensions registered with this instance.
#[cfg(feature = "std")]
extensions: Option<&'a mut Extensions>,
extensions: Option<OverlayedExtensions<'a>>,
}
@@ -159,7 +160,7 @@ impl<'a, H, N, B> Ext<'a, H, N, B>
storage_transaction_cache,
id: rand::random(),
_phantom: Default::default(),
extensions,
extensions: extensions.map(OverlayedExtensions::new),
}
}
@@ -753,7 +754,7 @@ where
extension: Box<dyn Extension>,
) -> Result<(), sp_externalities::Error> {
if let Some(ref mut extensions) = self.extensions {
extensions.register_with_type_id(type_id, extension)
extensions.register(type_id, extension)
} else {
Err(sp_externalities::Error::ExtensionsAreNotSupported)
}
@@ -761,9 +762,10 @@ where
fn deregister_extension_by_type_id(&mut self, type_id: TypeId) -> Result<(), sp_externalities::Error> {
if let Some(ref mut extensions) = self.extensions {
match extensions.deregister(type_id) {
Some(_) => Ok(()),
None => Err(sp_externalities::Error::ExtensionIsNotRegistered(type_id))
if extensions.deregister(type_id) {
Ok(())
} else {
Err(sp_externalities::Error::ExtensionIsNotRegistered(type_id))
}
} else {
Err(sp_externalities::Error::ExtensionsAreNotSupported)