Make decl_error! errors usable (#4449)

* Make `decl_error!` errors usable

This pr implements support for returning errors of different pallets in
a pallet. These errors need to be declared with `decl_error!`.

The pr changes the following:

- Each dispatchable function now returns a `DispatchResult` which is an
alias for `Result<(), DispatchError>`.
- `DispatchError` is an enum that has 4 variants:
  - `Other`: For storing string error messages
  - `CannotLookup`: Variant that is returned when something returns a
  `sp_runtime::LookupError`
  - `BadOrigin`: Variant that is returned for any kind of bad origin
  - `Module`: The error of a specific module. Contains the `index`,
  `error` and the `message`. The index is the index of the module in
  `construct_runtime!`. `error` is the index of the error in the error
  enum declared by `decl_error!`. `message` is the message to the error
  variant (this will not be encoded).
- `construct_runtime!` now creates a new struct `ModuleToIndex`. This
struct implements the trait `ModuleToIndex`.
- `frame_system::Trait` has a new associated type: `ModuleToIndex` that
expects the `ModuleToIndex` generated by `construct_runtime!`.
- All error strings returned in any module are being converted now to `DispatchError`.
- `BadOrigin` is the default error returned by any type that implements `EnsureOrigin`.

* Fix frame system benchmarks
This commit is contained in:
Bastian Köcher
2019-12-19 14:01:52 +01:00
committed by Gavin Wood
parent 0aab5c659e
commit 8e393aa5a8
69 changed files with 868 additions and 611 deletions
@@ -0,0 +1,135 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
#![recursion_limit="128"]
use sp_runtime::{generic, traits::{BlakeTwo256, Block as _, Verify}, DispatchError};
use sp_core::{H256, sr25519};
mod system;
pub trait Currency {}
mod module1 {
use super::*;
pub trait Trait<I>: system::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait<I>, I: Instance = DefaultInstance> for enum Call
where origin: <T as system::Trait>::Origin
{
pub fn fail(_origin) -> frame_support::dispatch::DispatchResult {
Err(Error::<T, I>::Something.into())
}
}
}
frame_support::decl_error! {
pub enum Error for Module<T: Trait<I>, I: Instance> {
Something
}
}
frame_support::decl_storage! {
trait Store for Module<T: Trait<I>, I: Instance=DefaultInstance> as Module {}
}
}
mod module2 {
use super::*;
pub trait Trait: system::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call
where origin: <T as system::Trait>::Origin
{
pub fn fail(_origin) -> frame_support::dispatch::DispatchResult {
Err(Error::<T>::Something.into())
}
}
}
frame_support::decl_error! {
pub enum Error for Module<T: Trait> {
Something
}
}
frame_support::decl_storage! {
trait Store for Module<T: Trait> as Module {}
}
}
impl module1::Trait<module1::Instance1> for Runtime {}
impl module1::Trait<module1::Instance2> for Runtime {}
impl module2::Trait for Runtime {}
pub type Signature = sr25519::Signature;
pub type AccountId = <Signature as Verify>::Signer;
pub type BlockNumber = u64;
pub type Index = u64;
impl system::Trait for Runtime {
type Hash = H256;
type Origin = Origin;
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type ModuleToIndex = ModuleToIndex;
}
frame_support::construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{Module, Call, Event},
Module1_1: module1::<Instance1>::{Module, Call, Storage},
Module2: module2::{Module, Call, Storage},
Module1_2: module1::<Instance2>::{Module, Call, Storage},
}
);
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
#[test]
fn check_module1_1_error_type() {
assert_eq!(
Module1_1::fail(system::Origin::<Runtime>::Root.into()),
Err(DispatchError::Module { index: 1, error: 0, message: Some("Something") }),
);
}
#[test]
fn check_module1_2_error_type() {
assert_eq!(
Module1_2::fail(system::Origin::<Runtime>::Root.into()),
Err(DispatchError::Module { index: 3, error: 0, message: Some("Something") }),
);
}
#[test]
fn check_module2_error_type() {
assert_eq!(
Module2::fail(system::Origin::<Runtime>::Root.into()),
Err(DispatchError::Module { index: 2, error: 0, message: Some("Something") }),
);
}
@@ -238,6 +238,7 @@ impl system::Trait for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type ModuleToIndex = ();
}
frame_support::construct_runtime!(
@@ -160,6 +160,7 @@ impl system::Trait for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type ModuleToIndex = ();
}
impl module::Trait for Runtime {}
@@ -12,14 +12,14 @@ macro_rules! reserved {
pub mod system {
use frame_support::dispatch;
pub fn ensure_root<R>(_: R) -> dispatch::Result {
pub fn ensure_root<R>(_: R) -> dispatch::DispatchResult {
Ok(())
}
}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn $reserved(_origin) -> dispatch::Result { unreachable!() }
fn $reserved(_origin) -> dispatch::DispatchResult { unreachable!() }
}
}
}
+2 -1
View File
@@ -8,6 +8,7 @@ pub trait Trait: 'static + Eq + Clone {
type Hash;
type AccountId: Encode + EncodeLike + Decode;
type Event: From<Event>;
type ModuleToIndex: frame_support::traits::ModuleToIndex;
}
frame_support::decl_module! {
@@ -28,7 +29,7 @@ frame_support::decl_event!(
);
frame_support::decl_error! {
pub enum Error {
pub enum Error for Module<T: Trait> {
/// Test error documentation
TestError,
/// Error documentation