Runtime dispatch calls return Result (#192)

* Merge remote-tracking branch 'origin/master' into gav-xts-dont-panic

* Update wasm.

* consensus, session and staking all panic-safe.

* Democracy doesn't panic in apply.

* Fix tests.

* Extra helper macro, council depanicked.

* Fix one test.

* Fix up all council tests. No panics!

* Council voting depanicked.

* Dispatch returns result.

* session & staking tests updated

* Fix democracy tests.

* Fix council tests.

* Fix up polkadot parachains in runtime

* Fix borked merge
This commit is contained in:
Gav Wood
2018-06-01 18:32:40 +02:00
committed by GitHub
parent 1dada4f7a0
commit a306074eb7
13 changed files with 657 additions and 594 deletions
@@ -19,20 +19,23 @@
pub use rstd::prelude::{Vec, Clone, Eq, PartialEq};
#[cfg(feature = "std")]
pub use std::fmt;
pub use rstd::result;
pub use rstd::marker::PhantomData;
#[cfg(feature = "std")]
use serde;
pub use codec::{Slicable, Input};
pub type Result = result::Result<(), &'static str>;
pub trait Dispatchable {
type Trait;
fn dispatch(self);
fn dispatch(self) -> Result;
}
pub trait AuxDispatchable {
type Aux;
type Trait;
fn dispatch(self, aux: &Self::Aux);
fn dispatch(self, aux: &Self::Aux) -> Result;
}
#[cfg(feature = "std")]
@@ -104,7 +107,7 @@ macro_rules! decl_dispatch {
$(
$param_name:ident : $param:ty
),*
)
) -> $result:ty
= $id:expr ;
)*
}
@@ -114,7 +117,7 @@ macro_rules! decl_dispatch {
impl for $mod_type<$trait_instance: $trait_name>;
pub enum $call_type;
$(
fn $fn_name( $( $param_name: $param ),* ) = $id;
fn $fn_name( $( $param_name: $param ),* ) -> $result = $id;
)*
}
decl_dispatch! {
@@ -131,7 +134,7 @@ macro_rules! decl_dispatch {
$(
, $param_name:ident : $param:ty
)*
)
) -> $result:ty
= $id:expr ;
)*
}
@@ -141,7 +144,7 @@ macro_rules! decl_dispatch {
impl for $mod_type<$trait_instance: $trait_name>;
pub enum $call_type where aux: $aux_type;
$(
fn $fn_name(aux $(, $param_name: $param )*)= $id;
fn $fn_name(aux $(, $param_name: $param )*) -> $result = $id;
)*
}
decl_dispatch! {
@@ -154,11 +157,11 @@ macro_rules! decl_dispatch {
impl for $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
) => {
impl<$trait_instance: $trait_name> $mod_type<$trait_instance> {
pub fn aux_dispatch<D: $crate::dispatch::AuxDispatchable<Trait = $trait_instance>>(d: D, aux: &D::Aux) {
d.dispatch(aux);
pub fn aux_dispatch<D: $crate::dispatch::AuxDispatchable<Trait = $trait_instance>>(d: D, aux: &D::Aux) -> $crate::dispatch::Result {
d.dispatch(aux)
}
pub fn dispatch<D: $crate::dispatch::Dispatchable<Trait = $trait_instance>>(d: D) {
d.dispatch();
pub fn dispatch<D: $crate::dispatch::Dispatchable<Trait = $trait_instance>>(d: D) -> $crate::dispatch::Result {
d.dispatch()
}
}
}
@@ -176,19 +179,20 @@ macro_rules! __decl_dispatch_module_without_aux {
$param_name:ident : $param:ty
),*
)
-> $result:ty
= $id:expr ;
)*
) => {
__decl_dispatch_module_common! {
impl for $mod_type<$trait_instance: $trait_name>;
pub enum $call_type;
$( fn $fn_name( $( $param_name : $param ),* ) = $id ; )*
$( fn $fn_name( $( $param_name : $param ),* ) -> $result = $id ; )*
}
impl<$trait_instance: $trait_name> $crate::dispatch::Dispatchable
for $call_type<$trait_instance>
{
type Trait = $trait_instance;
fn dispatch(self) {
fn dispatch(self) -> $crate::dispatch::Result {
match self {
$(
$call_type::$fn_name( $( $param_name ),* ) =>
@@ -218,20 +222,21 @@ macro_rules! __decl_dispatch_module_with_aux {
, $param_name:ident : $param:ty
)*
)
-> $result:ty
= $id:expr ;
)*
) => {
__decl_dispatch_module_common! {
impl for $mod_type<$trait_instance: $trait_name>;
pub enum $call_type;
$( fn $fn_name( $( $param_name : $param ),* ) = $id ; )*
$( fn $fn_name( $( $param_name : $param ),* ) -> $result = $id ; )*
}
impl<$trait_instance: $trait_name> $crate::dispatch::AuxDispatchable
for $call_type<$trait_instance>
{
type Trait = $trait_instance;
type Aux = $aux_type;
fn dispatch(self, aux: &Self::Aux) {
fn dispatch(self, aux: &Self::Aux) -> $crate::dispatch::Result {
match self {
$(
$call_type::$fn_name( $( $param_name ),* ) =>
@@ -261,6 +266,7 @@ macro_rules! __decl_dispatch_module_common {
$param_name:ident : $param:ty
),*
)
-> $result:ty
= $id:expr ;
)*
) => {
@@ -320,7 +326,7 @@ macro_rules! __decl_dispatch_module_common {
impl<$trait_instance: $trait_name> $crate::dispatch::fmt::Debug
for $call_type<$trait_instance>
{
fn fmt(&self, f: &mut $crate::dispatch::fmt::Formatter) -> Result<(), $crate::dispatch::fmt::Error> {
fn fmt(&self, f: &mut $crate::dispatch::fmt::Formatter) -> $crate::dispatch::result::Result<(), $crate::dispatch::fmt::Error> {
match *self {
$(
$call_type::$fn_name( $( ref $param_name ),* ) =>
@@ -408,7 +414,7 @@ macro_rules! impl_outer_dispatch {
impl $crate::dispatch::AuxDispatchable for $call_type {
type Aux = $aux;
type Trait = $call_type;
fn dispatch(self, aux: &$aux) {
fn dispatch(self, aux: &$aux) -> $crate::dispatch::Result {
match self {
$(
$call_type::$camelcase(call) => call.dispatch(&aux),
@@ -448,7 +454,7 @@ macro_rules! impl_outer_dispatch {
impl_outer_dispatch_common! { $call_type, $($camelcase = $id,)* }
impl $crate::dispatch::Dispatchable for $call_type {
type Trait = $call_type;
fn dispatch(self) {
fn dispatch(self) -> $crate::dispatch::Result {
match self {
$(
$call_type::$camelcase(call) => call.dispatch(),
+20 -35
View File
@@ -46,11 +46,11 @@ pub use self::hashable::Hashable;
pub use self::dispatch::{Parameter, Dispatchable, Callable, AuxDispatchable, AuxCallable, IsSubType, IsAuxSubType};
pub use runtime_io::print;
#[macro_export]
macro_rules! fail {
( $y:expr ) => {{
$crate::print($y);
return;
return Err($y);
}}
}
@@ -60,46 +60,31 @@ macro_rules! ensure {
if !$x {
fail!($y);
}
}};
($x:expr) => {{
if !$x {
$crate::print("Bailing! Cannot ensure: ");
$crate::print(stringify!($x));
return;
}
}}
}
#[macro_export]
macro_rules! ensure_unwrap {
($x:expr, $y: expr) => {
if let Some(v) = $x {
v
} else {
fail!{$y}
}
}
}
#[macro_export]
macro_rules! ensure_unwrap_err {
($x:expr, $y: expr) => {
if let Err(v) = $x {
v
} else {
fail!{$y}
}
}
}
#[macro_export]
#[cfg(feature = "std")]
macro_rules! assert_noop {
( $( $x:tt )* ) => {
( $x:expr , $y:expr ) => {
let h = runtime_io::storage_root();
{
$( $x )*
}
assert_err!($x, $y);
assert_eq!(h, runtime_io::storage_root());
}
}
#[macro_export]
#[cfg(feature = "std")]
macro_rules! assert_err {
( $x:expr , $y:expr ) => {
assert_eq!($x, Err($y));
}
}
#[macro_export]
#[cfg(feature = "std")]
macro_rules! assert_ok {
( $x:expr ) => {
assert!($x.is_ok());
}
}