Expunge error-chain (feat. tomaka) (#2662)

* Remove error_chain

* Expunge error-chain from rpc and service.

* Expunge from transaction pool.

* Expunge from node/cli

* Expunge from keystore.

* Remove some boilerplate.

* Fix remaining stuff.

* Improve on deprecation message.

* Fix issues.

* Fix trnsaction pool tests.

* Fix the rest.

* Fix borked merge.

* Update lock
This commit is contained in:
Tomasz Drwięga
2019-05-24 11:35:31 +02:00
committed by Gavin Wood
parent 69ffec5822
commit c162fc5ff1
68 changed files with 951 additions and 1158 deletions
+45 -60
View File
@@ -16,72 +16,57 @@
//! Rust executor possible errors.
// Silence: `use of deprecated item 'std::error::Error::cause': replaced by Error::source, which can support downcasting`
// https://github.com/paritytech/substrate/issues/1547
#![allow(deprecated)]
use state_machine;
use serializer;
use wasmi;
use error_chain::{
error_chain, error_chain_processing, impl_error_chain_processed,
impl_extract_backtrace, impl_error_chain_kind
};
error_chain! {
foreign_links {
InvalidData(serializer::Error) #[doc = "Unserializable Data"];
Trap(wasmi::Trap) #[doc = "Trap occured during execution"];
Wasmi(wasmi::Error) #[doc = "Wasmi loading/instantiating error"];
}
/// Result type alias.
pub type Result<T> = std::result::Result<T, Error>;
errors {
/// Method is not found
MethodNotFound(t: String) {
description("method not found"),
display("Method not found: '{}'", t),
}
/// Error type.
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Unserializable Data
InvalidData(serializer::Error),
/// Trap occured during execution
Trap(wasmi::Trap),
/// Wasmi loading/instantiating error
Wasmi(wasmi::Error),
/// Error in the API. Parameter is an error message.
ApiError(String),
/// Method is not found
#[display(fmt="Method not found: '{}'", _0)]
MethodNotFound(String),
/// Code is invalid (expected single byte)
#[display(fmt="Invalid Code: {:?}", _0)]
InvalidCode(Vec<u8>),
/// Could not get runtime version.
#[display(fmt="On-chain runtime does not specify version")]
VersionInvalid,
/// Externalities have failed.
#[display(fmt="Externalities error")]
Externalities,
/// Invalid index.
#[display(fmt="Invalid index provided")]
InvalidIndex,
/// Invalid return type.
#[display(fmt="Invalid type returned (should be u64)")]
InvalidReturn,
/// Runtime failed.
#[display(fmt="Runtime error")]
Runtime,
/// Runtime failed.
#[display(fmt="Invalid memory reference")]
InvalidMemoryReference,
}
/// Code is invalid (expected single byte)
InvalidCode(c: Vec<u8>) {
description("invalid code"),
display("Invalid Code: {:?}", c),
}
/// Could not get runtime version.
VersionInvalid {
description("Runtime version error"),
display("On-chain runtime does not specify version"),
}
/// Externalities have failed.
Externalities {
description("externalities failure"),
display("Externalities error"),
}
/// Invalid index.
InvalidIndex {
description("index given was not in range"),
display("Invalid index provided"),
}
/// Invalid return type.
InvalidReturn {
description("u64 was not returned"),
display("Invalid type returned (should be u64)"),
}
/// Runtime failed.
Runtime {
description("runtime failure"),
display("Runtime error"),
}
/// Runtime failed.
InvalidMemoryReference {
description("invalid memory reference"),
display("Invalid memory reference"),
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::InvalidData(ref err) => Some(err),
Error::Trap(ref err) => Some(err),
Error::Wasmi(ref err) => Some(err),
_ => None,
}
}
}
@@ -15,7 +15,7 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use std::{borrow::BorrowMut, result, cell::{RefMut, RefCell}};
use crate::error::{Error, ErrorKind, Result};
use crate::error::{Error, Result};
use state_machine::{CodeExecutor, Externalities};
use crate::wasm_executor::WasmExecutor;
use wasmi::{Module as WasmModule, ModuleRef as WasmModuleInstanceRef};
@@ -55,7 +55,7 @@ fn fetch_cached_runtime_version<'a, E: Externalities<Blake2Hasher>>(
) -> Result<(&'a WasmModuleInstanceRef, &'a Option<RuntimeVersion>)> {
let code_hash = match ext.original_storage_hash(well_known_keys::CODE) {
Some(code_hash) => code_hash,
None => return Err(ErrorKind::InvalidCode(vec![]).into()),
None => return Err(Error::InvalidCode(vec![])),
};
let maybe_runtime_preproc = cache.borrow_mut().entry(code_hash.into())
@@ -69,7 +69,7 @@ fn fetch_cached_runtime_version<'a, E: Externalities<Blake2Hasher>>(
.or(default_heap_pages)
.unwrap_or(DEFAULT_HEAP_PAGES);
match WasmModule::from_buffer(code)
.map_err(|_| ErrorKind::InvalidCode(vec![]).into())
.map_err(|_| Error::InvalidCode(vec![]))
.and_then(|module| wasm_executor.prepare_module(ext, heap_pages as usize, &module))
{
Ok(module) => {
@@ -88,7 +88,7 @@ fn fetch_cached_runtime_version<'a, E: Externalities<Blake2Hasher>>(
match maybe_runtime_preproc {
RuntimePreproc::InvalidCode => {
let code = ext.original_storage(well_known_keys::CODE).unwrap_or(vec![]);
Err(ErrorKind::InvalidCode(code).into())
Err(Error::InvalidCode(code))
},
RuntimePreproc::ValidCode(m, v) => {
Ok((m, v))
@@ -101,7 +101,7 @@ fn safe_call<F, U>(f: F) -> Result<U>
{
// Substrate uses custom panic hook that terminates process on panic. Disable termination for the native call.
let _guard = panic_handler::AbortGuard::new(false);
::std::panic::catch_unwind(f).map_err(|_| ErrorKind::Runtime.into())
::std::panic::catch_unwind(f).map_err(|_| Error::Runtime)
}
/// Set up the externalities and safe calling environment to execute calls to a native runtime.
@@ -248,7 +248,7 @@ impl<D: NativeExecutionDispatch> CodeExecutor<Blake2Hasher> for NativeExecutor<D
);
(
with_native_environment(ext, move || (call)())
.and_then(|r| r.map(NativeOrEncoded::Native).map_err(Into::into)),
.and_then(|r| r.map(NativeOrEncoded::Native).map_err(|s| Error::ApiError(s.to_string()))),
true
)
}
@@ -283,7 +283,7 @@ macro_rules! native_executor_instance {
}
fn dispatch(ext: &mut $crate::Externalities<$crate::Blake2Hasher>, method: &str, data: &[u8]) -> $crate::error::Result<Vec<u8>> {
$crate::with_native_environment(ext, move || $dispatcher(method, data))?
.ok_or_else(|| $crate::error::ErrorKind::MethodNotFound(method.to_owned()).into())
.ok_or_else(|| $crate::error::Error::MethodNotFound(method.to_owned()))
}
fn native_version() -> $crate::NativeVersion {
+1 -2
View File
@@ -643,9 +643,8 @@ mod tests {
let res = WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_exhaust_heap", &code);
assert_eq!(res.is_err(), true);
if let Err(err) = res {
let inner_err = err.iter().next().unwrap();
assert_eq!(
format!("{}", inner_err),
format!("{}", err),
format!("{}", wasmi::Error::Trap(trap(allocator::OUT_OF_SPACE)))
);
}
+7 -7
View File
@@ -26,7 +26,7 @@ use wasmi::{
use wasmi::RuntimeValue::{I32, I64, self};
use wasmi::memory_units::{Pages};
use state_machine::{Externalities, ChildStorageKey};
use crate::error::{Error, ErrorKind, Result};
use crate::error::{Error, Result};
use crate::wasm_utils::UserError;
use primitives::{blake2_128, blake2_256, twox_64, twox_128, twox_256, ed25519, sr25519, Pair};
use primitives::hexdisplay::HexDisplay;
@@ -769,9 +769,9 @@ impl WasmExecutor {
fn get_mem_instance(module: &ModuleRef) -> Result<MemoryRef> {
Ok(module
.export_by_name("memory")
.ok_or_else(|| Error::from(ErrorKind::InvalidMemoryReference))?
.ok_or_else(|| Error::InvalidMemoryReference)?
.as_memory()
.ok_or_else(|| Error::from(ErrorKind::InvalidMemoryReference))?
.ok_or_else(|| Error::InvalidMemoryReference)?
.clone())
}
@@ -795,7 +795,7 @@ impl WasmExecutor {
if let Some(I64(r)) = res {
let offset = r as u32;
let length = (r as u64 >> 32) as usize;
memory.get(offset, length).map_err(|_| ErrorKind::Runtime.into()).map(Some)
memory.get(offset, length).map_err(|_| Error::Runtime).map(Some)
} else {
Ok(None)
}
@@ -828,7 +828,7 @@ impl WasmExecutor {
let used_mem = memory.used_size();
let mut fec = FunctionExecutor::new(memory.clone(), table, ext)?;
let parameters = create_parameters(&mut |data: &[u8]| {
let offset = fec.heap.allocate(data.len() as u32).map_err(|_| ErrorKind::Runtime)?;
let offset = fec.heap.allocate(data.len() as u32).map_err(|_| Error::Runtime)?;
memory.set(offset, &data)?;
Ok(offset)
})?;
@@ -841,7 +841,7 @@ impl WasmExecutor {
let result = match result {
Ok(val) => match filter_result(val, &memory)? {
Some(val) => Ok(val),
None => Err(ErrorKind::InvalidReturn.into()),
None => Err(Error::InvalidReturn),
},
Err(e) => {
trace!(target: "wasm-executor", "Failed to execute code with {} pages", memory.current_size().0);
@@ -877,7 +877,7 @@ impl WasmExecutor {
// extract a reference to a linear memory, optional reference to a table
// and then initialize FunctionExecutor.
let memory = Self::get_mem_instance(intermediate_instance.not_started_instance())?;
memory.grow(Pages(heap_pages)).map_err(|_| Error::from(ErrorKind::Runtime))?;
memory.grow(Pages(heap_pages)).map_err(|_| Error::Runtime)?;
let table: Option<TableRef> = intermediate_instance
.not_started_instance()
.export_by_name("__indirect_function_table")