Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Dispatch returns result.
  • Loading branch information
gavofyork committed May 31, 2018
commit ff5da1a75fba44f63314bcf0defb2d957cc20134
1 change: 1 addition & 0 deletions substrate/runtime-std/with_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use std::ptr;
pub use std::rc;
pub use std::slice;
pub use std::vec;
pub use std::result;

pub mod collections {
pub use std::collections::btree_map;
Expand Down
1 change: 1 addition & 0 deletions substrate/runtime-std/without_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub use core::num;
pub use core::ops;
pub use core::ptr;
pub use core::slice;
pub use core::result;

pub mod collections {
pub use alloc::btree_map;
Expand Down
40 changes: 23 additions & 17 deletions substrate/runtime-support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -104,7 +107,7 @@ macro_rules! decl_dispatch {
$(
$param_name:ident : $param:ty
),*
)
) -> $result:ty
= $id:expr ;
)*
}
Expand All @@ -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! {
Expand All @@ -131,7 +134,7 @@ macro_rules! decl_dispatch {
$(
, $param_name:ident : $param:ty
)*
)
) -> $result:ty
= $id:expr ;
)*
}
Expand All @@ -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! {
Expand All @@ -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()
}
}
}
Expand All @@ -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 ),* ) =>
Expand Down Expand Up @@ -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 ),* ) =>
Expand Down Expand Up @@ -261,6 +266,7 @@ macro_rules! __decl_dispatch_module_common {
$param_name:ident : $param:ty
),*
)
-> $result:ty
= $id:expr ;
)*
) => {
Expand Down Expand Up @@ -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 ),* ) =>
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
Expand Down
33 changes: 2 additions & 31 deletions substrate/runtime-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}}
}

Expand All @@ -60,38 +60,9 @@ 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 {
Expand Down
16 changes: 10 additions & 6 deletions substrate/runtime/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern crate substrate_primitives;

use rstd::prelude::*;
use runtime_support::{storage, Parameter};
use runtime_support::dispatch::Result;
use runtime_support::storage::unhashed::StorageVec;
use primitives::traits::RefInto;
use substrate_primitives::bft::MisbehaviorReport;
Expand All @@ -59,11 +60,11 @@ pub trait Trait: system::Trait {
decl_module! {
pub struct Module<T: Trait>;
pub enum Call where aux: T::PublicAux {
fn report_misbehavior(aux, report: MisbehaviorReport) = 0;
fn report_misbehavior(aux, report: MisbehaviorReport) -> Result = 0;
}
pub enum PrivCall {
fn set_code(new: Vec<u8>) = 0;
fn set_storage(items: Vec<KeyValue>) = 1;
fn set_code(new: Vec<u8>) -> Result = 0;
fn set_storage(items: Vec<KeyValue>) -> Result = 1;
}
}

Expand All @@ -74,20 +75,23 @@ impl<T: Trait> Module<T> {
}

/// Set the new code.
fn set_code(new: Vec<u8>) {
fn set_code(new: Vec<u8>) -> Result {
storage::unhashed::put_raw(CODE, &new);
Ok(())
}

/// Set some items of storage.
fn set_storage(items: Vec<KeyValue>) {
fn set_storage(items: Vec<KeyValue>) -> Result {
for i in &items {
storage::unhashed::put_raw(&i.0, &i.1);
}
Ok(())
}

/// Report some misbehaviour.
fn report_misbehavior(_aux: &T::PublicAux, _report: MisbehaviorReport) {
fn report_misbehavior(_aux: &T::PublicAux, _report: MisbehaviorReport) -> Result {
// TODO.
Ok(())
}

/// Set the current set of authorities' session keys.
Expand Down
Loading