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 all commits
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
23 changes: 13 additions & 10 deletions polkadot/runtime/src/parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use polkadot_primitives::parachain::{Id, Chain, DutyRoster, CandidateReceipt};
use {system, session};

use runtime_support::{StorageValue, StorageMap};
use runtime_support::dispatch::Result;

#[cfg(any(feature = "std", test))]
use rstd::marker::PhantomData;
Expand All @@ -44,7 +45,7 @@ decl_module! {
pub struct Module<T: Trait>;
pub enum Call where aux: <T as Trait>::PublicAux {
// provide candidate receipts for parachains, in ascending order by id.
fn set_heads(aux, heads: Vec<CandidateReceipt>) = 0;
fn set_heads(aux, heads: Vec<CandidateReceipt>) -> Result = 0;
}
}

Expand Down Expand Up @@ -137,24 +138,24 @@ impl<T: Trait> Module<T> {
<Parachains<T>>::put(parachains);
}

fn set_heads(aux: &<T as Trait>::PublicAux, heads: Vec<CandidateReceipt>) {
assert!(aux.is_empty());
assert!(!<DidUpdate<T>>::exists(), "Parachain heads must be updated only once in the block");
assert!(
fn set_heads(aux: &<T as Trait>::PublicAux, heads: Vec<CandidateReceipt>) -> Result {
ensure!(aux.is_empty(), "set_heads must not be signed");
ensure!(!<DidUpdate<T>>::exists(), "Parachain heads must be updated only once in the block");
ensure!(
<system::Module<T>>::extrinsic_index() == T::SET_POSITION,
"Parachain heads update extrinsic must be at position {} in the block",
T::SET_POSITION
"Parachain heads update extrinsic must be at position {} in the block"
// , T::SET_POSITION
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left in for completeness, but given we're returning &'static str (for efficiency) and have no formatting tools (since it would severely bloat the runtime), I doubt there's much that can be done right now. A later PR could perhaps switch &'static str to String in the case of compiling to native and then it can be formatted properly.

);

let active_parachains = Self::active_parachains();
let mut iter = active_parachains.iter();

// perform this check before writing to storage.
for head in &heads {
assert!(
ensure!(
iter.find(|&p| p == &head.parachain_index).is_some(),
"Submitted candidate for unregistered or out-of-order parachain {}",
head.parachain_index.into_inner()
"Submitted candidate for unregistered or out-of-order parachain {}"
// , head.parachain_index.into_inner()
);
}

Expand All @@ -164,6 +165,8 @@ impl<T: Trait> Module<T> {
}

<DidUpdate<T>>::put(true);

Ok(())
}
}

Expand Down
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
45 changes: 15 additions & 30 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,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}
}
#[cfg(feature = "std")]
macro_rules! assert_noop {
( $x:expr , $y:expr ) => {
let h = runtime_io::storage_root();
assert_err!($x, $y);
assert_eq!(h, runtime_io::storage_root());
}
}

#[macro_export]
macro_rules! ensure_unwrap_err {
($x:expr, $y: expr) => {
if let Err(v) = $x {
v
} else {
fail!{$y}
}
#[cfg(feature = "std")]
macro_rules! assert_err {
( $x:expr , $y:expr ) => {
assert_eq!($x, Err($y));
}
}

#[macro_export]
#[cfg(feature = "std")]
macro_rules! assert_noop {
( $( $x:tt )* ) => {
let h = runtime_io::storage_root();
{
$( $x )*
}
assert_eq!(h, runtime_io::storage_root());
macro_rules! assert_ok {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be just ?;, I know, but the code looks more uniform with this macro.

( $x:expr ) => {
assert!($x.is_ok());
}
}
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