Skip to content

Commit

Permalink
Remove dependency on error_chain (paritytech#277)
Browse files Browse the repository at this point in the history
* Convert validation error

* Convert wasm_executor error

* Convert block evaluation error

* Convert collation errors and the compilation

* Remove error-chain dep from service

* Remove unused Result type

* Remove unused error variants

* Remove redundant intos

* Add missing comments

* Update validation/src/collation.rs

Co-Authored-By: thiolliere <gui.thiolliere@gmail.com>

* Fix new error variant
  • Loading branch information
ascjones authored and bkchr committed Jun 5, 2019
1 parent 6c40a4a commit d6b0e1f
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 194 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description = "Types and utilities for creating and working with parachains"
parity-codec = { version = "3.5", default-features = false }
parity-codec-derive = { version = "3.3", default-features = false }
wasmi = { version = "0.4.3", optional = true }
error-chain = { version = "0.12", optional = true }
derive_more = { version = "0.14", optional = true }
serde = { version = "1.0", default-features = false }
serde_derive = { version = "1.0", optional = true }

Expand All @@ -18,4 +18,4 @@ tiny-keccak = "1.4"
[features]
default = ["std"]
wasm-api = []
std = ["parity-codec/std", "wasmi", "error-chain", "serde_derive", "serde/std"]
std = ["parity-codec/std", "wasmi", "derive_more", "serde_derive", "serde/std"]
4 changes: 0 additions & 4 deletions parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ extern crate core;
#[cfg(feature = "std")]
extern crate wasmi;

#[cfg(feature = "std")]
#[macro_use]
extern crate error_chain;

#[cfg(feature = "std")]
extern crate serde;

Expand Down
51 changes: 28 additions & 23 deletions parachain/src/wasm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,27 @@ mod ids {
pub const POST_UPWARDS_MESSAGE: usize = 2;
}

error_chain! {
types { Error, ErrorKind, ResultExt; }
foreign_links {
Wasm(WasmError);
Externalities(ExternalitiesError);
}
errors {
/// Call data too big. WASM32 only has a 32-bit address space.
ParamsTooLarge(len: usize) {
description("Validation parameters took up too much space to execute in WASM"),
display("Validation parameters took up {} bytes, max allowed by WASM is {}", len, i32::max_value()),
}
/// Bad return data or type.
BadReturn {
description("Validation function returned invalid data."),
display("Validation function returned invalid data."),
/// Error type for the wasm executor
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Wasm error
Wasm(WasmError),
/// Externalities error
Externalities(ExternalitiesError),
/// Call data too big. WASM32 only has a 32-bit address space.
#[display(fmt = "Validation parameters took up {} bytes, max allowed by WASM is {}", _0, i32::max_value())]
ParamsTooLarge(usize),
/// Bad return data or type.
#[display(fmt = "Validation function returned invalid data.")]
BadReturn,
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Wasm(ref err) => Some(err),
Error::Externalities(ref err) => Some(err),
_ => None,
}
}
}
Expand Down Expand Up @@ -284,7 +289,7 @@ pub fn validate_candidate<E: Externalities>(

// hard limit from WASM.
if encoded_call_data.len() > i32::max_value() as usize {
bail!(ErrorKind::ParamsTooLarge(encoded_call_data.len()));
return Err(Error::ParamsTooLarge(encoded_call_data.len()));
}

// allocate sufficient amount of wasm pages to fit encoded call data.
Expand All @@ -308,7 +313,7 @@ pub fn validate_candidate<E: Externalities>(
.map_err(|e| -> Error {
e.as_host_error()
.and_then(|he| he.downcast_ref::<ExternalitiesError>())
.map(|ee| ErrorKind::Externalities(ee.clone()).into())
.map(|ee| Error::Externalities(ee.clone()))
.unwrap_or_else(move || e.into())
})?;

Expand All @@ -321,24 +326,24 @@ pub fn validate_candidate<E: Externalities>(
let len_offset = len_offset as usize;

let len = u32::decode(&mut &len_bytes[..])
.ok_or_else(|| ErrorKind::BadReturn)? as usize;
.ok_or_else(|| Error::BadReturn)? as usize;

let return_offset = if len > len_offset {
bail!(ErrorKind::BadReturn);
return Err(Error::BadReturn);
} else {
len_offset - len
};

memory.with_direct_access(|mem| {
if mem.len() < return_offset + len {
return Err(ErrorKind::BadReturn.into());
return Err(Error::BadReturn);
}

ValidationResult::decode(&mut &mem[return_offset..][..len])
.ok_or_else(|| ErrorKind::BadReturn)
.ok_or_else(|| Error::BadReturn)
.map_err(Into::into)
})
}
_ => bail!(ErrorKind::BadReturn),
_ => return Err(Error::BadReturn),
}
}
1 change: 0 additions & 1 deletion service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2018"

[dependencies]
parking_lot = "0.7.1"
error-chain = "0.12"
lazy_static = "1.0"
log = "0.4.6"
slog = "^2"
Expand Down
2 changes: 1 addition & 1 deletion validation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
futures = "0.1.17"
parking_lot = "0.7.1"
tokio = "0.1.7"
error-chain = "0.12"
derive_more = "0.14.0"
log = "0.4.6"
exit-future = "0.1"
parity-codec = "3.1"
Expand Down
Loading

0 comments on commit d6b0e1f

Please sign in to comment.