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
+9 -10
View File
@@ -25,12 +25,11 @@
use sp_std::prelude::*;
use sp_runtime::{
RuntimeDebug,
print,
RuntimeDebug, DispatchResult, print,
traits::{Zero, One, StaticLookup, Bounded, Saturating},
};
use frame_support::{
dispatch::Result, decl_storage, decl_event, ensure, decl_module,
decl_storage, decl_event, ensure, decl_module,
weights::SimpleDispatchInfo,
traits::{
Currency, ExistenceRequirement, Get, LockableCurrency, LockIdentifier,
@@ -346,7 +345,7 @@ decl_module! {
#[compact] index: VoteIndex,
hint: SetIndex,
#[compact] value: BalanceOf<T>
) -> Result {
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_set_approvals(who, votes, index, hint, value)
}
@@ -363,7 +362,7 @@ decl_module! {
#[compact] index: VoteIndex,
hint: SetIndex,
#[compact] value: BalanceOf<T>
) -> Result {
) -> DispatchResult {
let who = Self::proxy(ensure_signed(origin)?).ok_or("not a proxy")?;
Self::do_set_approvals(who, votes, index, hint, value)
}
@@ -526,7 +525,7 @@ decl_module! {
candidate: <T::Lookup as StaticLookup>::Source,
#[compact] total: BalanceOf<T>,
#[compact] index: VoteIndex
) -> Result {
) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(
!total.is_zero(),
@@ -587,7 +586,7 @@ decl_module! {
// better safe than sorry.
let imbalance = T::Currency::slash(&who, bad_presentation_punishment).0;
T::BadPresentation::on_unbalanced(imbalance);
Err(if dupe { "duplicate presentation" } else { "incorrect total" })
Err(if dupe { "duplicate presentation" } else { "incorrect total" })?
}
}
@@ -714,7 +713,7 @@ impl<T: Trait> Module<T> {
// Private
/// Check there's nothing to do this block
fn end_block(block_number: T::BlockNumber) -> Result {
fn end_block(block_number: T::BlockNumber) -> DispatchResult {
if (block_number % T::VotingPeriod::get()).is_zero() {
if let Some(number) = Self::next_tally() {
if block_number == number {
@@ -750,7 +749,7 @@ impl<T: Trait> Module<T> {
index: VoteIndex,
hint: SetIndex,
value: BalanceOf<T>,
) -> Result {
) -> DispatchResult {
let candidates_len = <Self as Store>::Candidates::decode_len().unwrap_or(0_usize);
ensure!(!Self::presentation_active(), "no approval changes during presentation period");
@@ -873,7 +872,7 @@ impl<T: Trait> Module<T> {
/// approved candidates in their place. If the total number of members is less than the desired
/// membership a new vote is started. Clears all presented candidates, returning the bond of the
/// elected ones.
fn finalize_tally() -> Result {
fn finalize_tally() -> DispatchResult {
let (_, coming, expiring): (T::BlockNumber, u32, Vec<T::AccountId>) =
<NextFinalize<T>>::take()
.ok_or("finalize can only be called after a tally is started.")?;
+1
View File
@@ -53,6 +53,7 @@ impl frame_system::Trait for Test {
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
}
parameter_types! {
+4 -1
View File
@@ -1034,7 +1034,10 @@ fn election_double_presentations_should_be_punished() {
System::set_block_number(6);
assert_ok!(Elections::present_winner(Origin::signed(4), 2, 20, 0));
assert_ok!(Elections::present_winner(Origin::signed(4), 5, 50, 0));
assert_eq!(Elections::present_winner(Origin::signed(4), 5, 50, 0), Err("duplicate presentation"));
assert_eq!(
Elections::present_winner(Origin::signed(4), 5, 50, 0),
Err("duplicate presentation".into()),
);
assert_ok!(Elections::end_block(System::block_number()));
assert_eq!(Elections::members(), vec![(5, 11), (2, 11)]);