Make decl_module not require a return type for functions (#1230)

If no return type is specified, `Result` is added and
`Ok(())` is returned by default.

Closes: #1182
This commit is contained in:
Bastian Köcher
2018-12-10 13:36:37 +01:00
committed by GitHub
parent 52ba9a5605
commit acf1b77bcd
17 changed files with 134 additions and 147 deletions
+4 -9
View File
@@ -45,7 +45,7 @@ extern crate sr_primitives as primitives;
// depend on it being around.
extern crate srml_system as system;
use runtime_support::{StorageValue, StorageMap, dispatch::Result, Parameter};
use runtime_support::{StorageValue, StorageMap, Parameter};
use primitives::traits::{Member, SimpleArithmetic, Zero};
use system::ensure_signed;
@@ -66,7 +66,7 @@ decl_module! {
/// Issue a new class of fungible assets. There are, and will only ever be, `total`
/// such assets and they'll all belong to the `origin` initially. It will have an
/// identifier `AssetId` instance: this will be specified in the `Issued` event.
fn issue(origin, total: T::Balance) -> Result {
fn issue(origin, total: T::Balance) {
let origin = ensure_signed(origin)?;
let id = Self::next_asset_id();
@@ -75,11 +75,10 @@ decl_module! {
<Balances<T>>::insert((id, origin.clone()), total);
Self::deposit_event(RawEvent::Issued(id, origin, total));
Ok(())
}
/// Move some assets from one holder to another.
fn transfer(origin, id: AssetId, target: T::AccountId, amount: T::Balance) -> Result {
fn transfer(origin, id: AssetId, target: T::AccountId, amount: T::Balance) {
let origin = ensure_signed(origin)?;
let origin_account = (id, origin.clone());
let origin_balance = <Balances<T>>::get(&origin_account);
@@ -88,20 +87,16 @@ decl_module! {
Self::deposit_event(RawEvent::Transfered(id, origin, target.clone(), amount));
<Balances<T>>::insert(origin_account, origin_balance - amount);
<Balances<T>>::mutate((id, target), |balance| *balance += amount);
Ok(())
}
/// Destroy any assets of `id` owned by `origin`.
fn destroy(origin, id: AssetId) -> Result {
fn destroy(origin, id: AssetId) {
let origin = ensure_signed(origin)?;
let balance = <Balances<T>>::take((id, origin.clone()));
ensure!(!balance.is_zero(), "origin balance should be non-zero");
Self::deposit_event(RawEvent::Destroyed(id, origin, balance));
Ok(())
}
}
}
+4 -7
View File
@@ -14,8 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Balances: Handles setting and retrieval of free balance,
//! retrieving total balance, reserve and unreserve balance,
//! Balances: Handles setting and retrieval of free balance,
//! retrieving total balance, reserve and unreserve balance,
//! repatriating a reserved balance to a beneficiary account that exists,
//! transfering a balance between accounts (when not reserved),
//! slashing an account balance, account removal, rewards,
@@ -134,7 +134,7 @@ decl_module! {
origin,
dest: RawAddress<T::AccountId, T::AccountIndex>,
value: <T::Balance as HasCompact>::Type
) -> Result {
) {
let transactor = ensure_signed(origin)?;
let dest = Self::lookup(dest)?;
@@ -170,8 +170,6 @@ decl_module! {
Self::set_free_balance_creating(&dest, new_to_balance);
Self::deposit_event(RawEvent::Transfer(transactor, dest, value, fee));
}
Ok(())
}
/// Set the balances of a given account.
@@ -179,11 +177,10 @@ decl_module! {
who: RawAddress<T::AccountId, T::AccountIndex>,
free: <T::Balance as HasCompact>::Type,
reserved: <T::Balance as HasCompact>::Type
) -> Result {
) {
let who = Self::lookup(who)?;
Self::set_free_balance(&who, free.into());
Self::set_reserved_balance(&who, reserved.into());
Ok(())
}
}
}
+6 -14
View File
@@ -41,7 +41,6 @@ use rstd::prelude::*;
use rstd::result;
use parity_codec::Encode;
use runtime_support::{storage, Parameter};
use runtime_support::dispatch::Result;
use runtime_support::storage::StorageValue;
use runtime_support::storage::unhashed::StorageVec;
use primitives::CheckInherentError;
@@ -142,16 +141,15 @@ decl_storage! {
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Report some misbehaviour.
fn report_misbehavior(origin, _report: Vec<u8>) -> Result {
fn report_misbehavior(origin, _report: Vec<u8>) {
ensure_signed(origin)?;
// TODO.
Ok(())
}
/// Note the previous block's validator missed their opportunity to propose a block.
/// This only comes in if 2/3+1 of the validators agree that no proposal was submitted.
/// It's only relevant for the previous block.
fn note_offline(origin, offline_val_indices: Vec<u32>) -> Result {
fn note_offline(origin, offline_val_indices: Vec<u32>) {
ensure_inherent(origin)?;
assert!(
<system::Module<T>>::extrinsic_index() == Some(T::NOTE_OFFLINE_POSITION),
@@ -162,34 +160,28 @@ decl_module! {
for validator_index in offline_val_indices.into_iter() {
T::OnOfflineValidator::on_offline_validator(validator_index as usize);
}
Ok(())
}
/// Make some on-chain remark.
fn remark(origin, _remark: Vec<u8>) -> Result {
fn remark(origin, _remark: Vec<u8>) {
ensure_signed(origin)?;
Ok(())
}
/// Set the number of pages in the WebAssembly environment's heap.
fn set_heap_pages(pages: u64) -> Result {
fn set_heap_pages(pages: u64) {
storage::unhashed::put_raw(well_known_keys::HEAP_PAGES, &pages.encode());
Ok(())
}
/// Set the new code.
pub fn set_code(new: Vec<u8>) -> Result {
pub fn set_code(new: Vec<u8>) {
storage::unhashed::put_raw(well_known_keys::CODE, &new);
Ok(())
}
/// Set some items of storage.
fn set_storage(items: Vec<KeyValue>) -> Result {
fn set_storage(items: Vec<KeyValue>) {
for i in &items {
storage::unhashed::put_raw(&i.0, &i.1);
}
Ok(())
}
fn on_finalise() {
+3 -6
View File
@@ -21,7 +21,7 @@ use rstd::result;
use codec::Compact;
use substrate_primitives::u32_trait::Value as U32;
use primitives::traits::{Hash, EnsureOrigin};
use srml_support::dispatch::{Result, Dispatchable, Parameter};
use srml_support::dispatch::{Dispatchable, Parameter};
use srml_support::{StorageValue, StorageMap};
use super::{Trait as CouncilTrait, Module as Council};
use system::{self, ensure_signed};
@@ -68,7 +68,7 @@ decl_event!(
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: <T as system::Trait>::Origin {
fn deposit_event() = default;
fn propose(origin, threshold: Compact<u32>, proposal: Box<<T as Trait>::Proposal>) -> Result {
fn propose(origin, threshold: Compact<u32>, proposal: Box<<T as Trait>::Proposal>) {
let who = ensure_signed(origin)?;
let threshold = threshold.into();
@@ -90,10 +90,9 @@ decl_module! {
Self::deposit_event(RawEvent::Proposed(who, index, proposal_hash, threshold));
}
Ok(())
}
fn vote(origin, proposal: T::Hash, index: Compact<ProposalIndex>, approve: bool) -> Result {
fn vote(origin, proposal: T::Hash, index: Compact<ProposalIndex>, approve: bool) {
let who = ensure_signed(origin)?;
let index = index.into();
@@ -154,8 +153,6 @@ decl_module! {
// update voting
<Voting<T>>::insert(&proposal, voting);
}
Ok(())
}
}
}
+8 -16
View File
@@ -91,7 +91,7 @@ decl_module! {
/// Set candidate approvals. Approval slots stay valid as long as candidates in those slots
/// are registered.
fn set_approvals(origin, votes: Vec<bool>, index: Compact<VoteIndex>) -> Result {
fn set_approvals(origin, votes: Vec<bool>, index: Compact<VoteIndex>) {
let who = ensure_signed(origin)?;
let index: VoteIndex = index.into();
@@ -110,7 +110,6 @@ decl_module! {
}
<LastActiveOf<T>>::insert(&who, index);
<ApprovalsOf<T>>::insert(&who, votes);
Ok(())
}
/// Remove a voter. For it not to be a bond-consuming no-op, all approved candidate indices
@@ -124,7 +123,7 @@ decl_module! {
who: Address<T::AccountId, T::AccountIndex>,
who_index: Compact<u32>,
assumed_vote_index: Compact<VoteIndex>
) -> Result {
) {
let reporter = ensure_signed(origin)?;
let assumed_vote_index: VoteIndex = assumed_vote_index.into();
@@ -166,11 +165,10 @@ decl_module! {
<balances::Module<T>>::slash_reserved(&reporter, Self::voting_bond());
Self::deposit_event(RawEvent::BadReaperSlashed(reporter));
}
Ok(())
}
/// Remove a voter. All votes are cancelled and the voter deposit is returned.
fn retract_voter(origin, index: Compact<u32>) -> Result {
fn retract_voter(origin, index: Compact<u32>) {
let who = ensure_signed(origin)?;
ensure!(!Self::presentation_active(), "cannot retract when presenting");
@@ -183,13 +181,12 @@ decl_module! {
Self::remove_voter(&who, index, voters);
<balances::Module<T>>::unreserve(&who, Self::voting_bond());
Ok(())
}
/// Submit oneself for candidacy.
///
/// Account must have enough transferrable funds in it to pay the bond.
fn submit_candidacy(origin, slot: Compact<u32>) -> Result {
fn submit_candidacy(origin, slot: Compact<u32>) {
let who = ensure_signed(origin)?;
ensure!(!Self::is_a_candidate(&who), "duplicate candidate submission");
@@ -215,7 +212,6 @@ decl_module! {
}
<Candidates<T>>::put(candidates);
<CandidateCount<T>>::put(count as u32 + 1);
Ok(())
}
/// Claim that `signed` is one of the top Self::carry_count() + current_vote().1 candidates.
@@ -276,37 +272,33 @@ decl_module! {
/// Set the desired member count; if lower than the current count, then seats will not be up
/// election when they expire. If more, then a new vote will be started if one is not already
/// in progress.
fn set_desired_seats(count: Compact<u32>) -> Result {
fn set_desired_seats(count: Compact<u32>) {
let count: u32 = count.into();
<DesiredSeats<T>>::put(count);
Ok(())
}
/// Remove a particular member. A tally will happen instantly (if not already in a presentation
/// period) to fill the seat if removal means that the desired members are not met.
/// This is effective immediately.
fn remove_member(who: Address<T::AccountId, T::AccountIndex>) -> Result {
fn remove_member(who: Address<T::AccountId, T::AccountIndex>) {
let who = <balances::Module<T>>::lookup(who)?;
let new_council: Vec<(T::AccountId, T::BlockNumber)> = Self::active_council()
.into_iter()
.filter(|i| i.0 != who)
.collect();
<ActiveCouncil<T>>::put(new_council);
Ok(())
}
/// Set the presentation duration. If there is currently a vote being presented for, will
/// invoke `finalise_vote`.
fn set_presentation_duration(count: <T::BlockNumber as HasCompact>::Type) -> Result {
fn set_presentation_duration(count: <T::BlockNumber as HasCompact>::Type) {
<PresentationDuration<T>>::put(count.into());
Ok(())
}
/// Set the presentation duration. If there is current a vote being presented for, will
/// invoke `finalise_vote`.
fn set_term_duration(count: <T::BlockNumber as HasCompact>::Type) -> Result {
fn set_term_duration(count: <T::BlockNumber as HasCompact>::Type) {
<TermDuration<T>>::put(count.into());
Ok(())
}
fn on_finalise(n: T::BlockNumber) {
+5 -11
View File
@@ -35,7 +35,7 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event() = default;
fn propose(origin, proposal: Box<T::Proposal>) -> Result {
fn propose(origin, proposal: Box<T::Proposal>) {
let who = ensure_signed(origin)?;
let expiry = <system::Module<T>>::block_number() + Self::voting_period();
@@ -54,11 +54,9 @@ decl_module! {
<ProposalOf<T>>::insert(proposal_hash, *proposal);
<ProposalVoters<T>>::insert(proposal_hash, vec![who.clone()]);
<CouncilVoteOf<T>>::insert((proposal_hash, who.clone()), true);
Ok(())
}
fn vote(origin, proposal: T::Hash, approve: bool) -> Result {
fn vote(origin, proposal: T::Hash, approve: bool) {
let who = ensure_signed(origin)?;
ensure!(Self::is_councillor(&who), "only councillors may vote on council proposals");
@@ -67,10 +65,9 @@ decl_module! {
<ProposalVoters<T>>::mutate(proposal, |voters| voters.push(who.clone()));
}
<CouncilVoteOf<T>>::insert((proposal, who), approve);
Ok(())
}
fn veto(origin, proposal_hash: T::Hash) -> Result {
fn veto(origin, proposal_hash: T::Hash) {
let who = ensure_signed(origin)?;
ensure!(Self::is_councillor(&who), "only councillors may veto council proposals");
@@ -96,17 +93,14 @@ decl_module! {
for (c, _) in <Council<T>>::active_council() {
<CouncilVoteOf<T>>::remove((proposal_hash, c));
}
Ok(())
}
fn set_cooloff_period(blocks: <T::BlockNumber as HasCompact>::Type) -> Result {
fn set_cooloff_period(blocks: <T::BlockNumber as HasCompact>::Type) {
<CooloffPeriod<T>>::put(blocks.into());
Ok(())
}
fn set_voting_period(blocks: <T::BlockNumber as HasCompact>::Type) -> Result {
fn set_voting_period(blocks: <T::BlockNumber as HasCompact>::Type) {
<VotingPeriod<T>>::put(blocks.into());
Ok(())
}
fn on_finalise(n: T::BlockNumber) {
+4 -8
View File
@@ -65,7 +65,7 @@ decl_module! {
origin,
proposal: Box<T::Proposal>,
value: <T::Balance as HasCompact>::Type
) -> Result {
) {
let who = ensure_signed(origin)?;
let value = value.into();
@@ -80,11 +80,10 @@ decl_module! {
let mut props = Self::public_props();
props.push((index, (*proposal).clone(), who));
<PublicProps<T>>::put(props);
Ok(())
}
/// Propose a sensitive action to be taken.
fn second(origin, proposal: Compact<PropIndex>) -> Result {
fn second(origin, proposal: Compact<PropIndex>) {
let who = ensure_signed(origin)?;
let proposal: PropIndex = proposal.into();
let mut deposit = Self::deposit_of(proposal)
@@ -93,12 +92,11 @@ decl_module! {
.map_err(|_| "seconder's balance too low")?;
deposit.1.push(who);
<DepositOf<T>>::insert(proposal, deposit);
Ok(())
}
/// Vote in a referendum. If `approve_proposal` is true, the vote is to enact the proposal;
/// false would be a vote to keep the status quo.
fn vote(origin, ref_index: Compact<ReferendumIndex>, approve_proposal: bool) -> Result {
fn vote(origin, ref_index: Compact<ReferendumIndex>, approve_proposal: bool) {
let who = ensure_signed(origin)?;
let ref_index = ref_index.into();
ensure!(Self::is_active_referendum(ref_index), "vote given for invalid referendum.");
@@ -108,7 +106,6 @@ decl_module! {
<VotersFor<T>>::mutate(ref_index, |voters| voters.push(who.clone()));
}
<VoteOf<T>>::insert(&(ref_index, who), approve_proposal);
Ok(())
}
/// Start a referendum.
@@ -121,9 +118,8 @@ decl_module! {
}
/// Remove a referendum.
fn cancel_referendum(ref_index: Compact<ReferendumIndex>) -> Result {
fn cancel_referendum(ref_index: Compact<ReferendumIndex>) {
Self::clear_referendum(ref_index.into());
Ok(())
}
fn on_finalise(n: T::BlockNumber) {
+3 -4
View File
@@ -173,12 +173,11 @@ decl_module! {
// calls to be executed - we don't need to care why. Because it's privileged, we can
// assume it's a one-off operation and substantial processing/storage/memory can be used
// without worrying about gameability or attack scenarios.
fn set_dummy(new_value: T::Balance) -> Result {
// If you not specify `Result` explicitly as return value, it will be added automatically
// for you and `Ok(())` will be returned.
fn set_dummy(new_value: T::Balance) {
// Put the new value into storage.
<Dummy<T>>::put(new_value);
// All good.
Ok(())
}
// The signature could also look like: `fn on_finalise()`
+1 -2
View File
@@ -189,10 +189,9 @@ decl_module! {
fn deposit_event() = default;
/// Report some misbehaviour.
fn report_misbehavior(origin, _report: Vec<u8>) -> Result {
fn report_misbehavior(origin, _report: Vec<u8>) {
ensure_signed(origin)?;
// TODO: https://github.com/paritytech/substrate/issues/1112
Ok(())
}
fn on_finalise(block_number: T::BlockNumber) {
+2 -4
View File
@@ -93,17 +93,15 @@ decl_module! {
/// Sets the session key of `_validator` to `_key`. This doesn't take effect until the next
/// session.
fn set_key(origin, key: T::SessionKey) -> Result {
fn set_key(origin, key: T::SessionKey) {
let who = ensure_signed(origin)?;
// set new value for next session
<NextKeyFor<T>>::insert(who, key);
Ok(())
}
/// Set a new session length. Won't kick in until the next session change (at current length).
fn set_length(new: <T::BlockNumber as HasCompact>::Type) -> Result {
fn set_length(new: <T::BlockNumber as HasCompact>::Type) {
<NextSessionLength<T>>::put(new.into());
Ok(())
}
/// Forces a new session.
+8 -18
View File
@@ -105,7 +105,7 @@ decl_module! {
/// Declare the desire to stake for the transactor.
///
/// Effects will be felt at the beginning of the next era.
fn stake(origin) -> Result {
fn stake(origin) {
let who = ensure_signed(origin)?;
ensure!(Self::nominating(&who).is_none(), "Cannot stake if already nominating.");
let mut intentions = <Intentions<T>>::get();
@@ -115,7 +115,6 @@ decl_module! {
<Bondage<T>>::insert(&who, T::BlockNumber::max_value());
intentions.push(who);
<Intentions<T>>::put(intentions);
Ok(())
}
/// Retract the desire to stake for the transactor.
@@ -131,7 +130,7 @@ decl_module! {
Self::apply_unstake(&who, intentions_index as usize)
}
fn nominate(origin, target: Address<T::AccountId, T::AccountIndex>) -> Result {
fn nominate(origin, target: Address<T::AccountId, T::AccountIndex>) {
let who = ensure_signed(origin)?;
let target = <balances::Module<T>>::lookup(target)?;
@@ -148,13 +147,11 @@ decl_module! {
// Update bondage
<Bondage<T>>::insert(&who, T::BlockNumber::max_value());
Ok(())
}
/// Will panic if called when source isn't currently nominating target.
/// Updates Nominating, NominatorsFor and NominationBalance.
fn unnominate(origin, target_index: Compact<u32>) -> Result {
fn unnominate(origin, target_index: Compact<u32>) {
let source = ensure_signed(origin)?;
let target_index: u32 = target_index.into();
let target_index = target_index as usize;
@@ -180,7 +177,6 @@ decl_module! {
source,
<system::Module<T>>::block_number() + Self::bonding_duration()
);
Ok(())
}
/// Set the given account's preference for slashing behaviour should they be a validator.
@@ -190,7 +186,7 @@ decl_module! {
origin,
intentions_index: Compact<u32>,
prefs: ValidatorPrefs<T::Balance>
) -> Result {
) {
let who = ensure_signed(origin)?;
let intentions_index: u32 = intentions_index.into();
@@ -199,27 +195,22 @@ decl_module! {
}
<ValidatorPreferences<T>>::insert(who, prefs);
Ok(())
}
/// Set the number of sessions in an era.
fn set_sessions_per_era(new: <T::BlockNumber as HasCompact>::Type) -> Result {
fn set_sessions_per_era(new: <T::BlockNumber as HasCompact>::Type) {
<NextSessionsPerEra<T>>::put(new.into());
Ok(())
}
/// The length of the bonding duration in eras.
fn set_bonding_duration(new: <T::BlockNumber as HasCompact>::Type) -> Result {
fn set_bonding_duration(new: <T::BlockNumber as HasCompact>::Type) {
<BondingDuration<T>>::put(new.into());
Ok(())
}
/// The ideal number of validators.
fn set_validator_count(new: Compact<u32>) -> Result {
fn set_validator_count(new: Compact<u32>) {
let new: u32 = new.into();
<ValidatorCount<T>>::put(new);
Ok(())
}
/// Force there to be a new era. This also forces a new session immediately after.
@@ -229,10 +220,9 @@ decl_module! {
}
/// Set the offline slash grace period.
fn set_offline_slash_grace(new: Compact<u32>) -> Result {
fn set_offline_slash_grace(new: Compact<u32>) {
let new: u32 = new.into();
<OfflineSlashGrace<T>>::put(new);
Ok(())
}
}
}
+77 -22
View File
@@ -167,7 +167,9 @@ macro_rules! decl_module {
{ $( $on_finalise:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident($origin:ident $(, $param_name:ident : $param:ty)* ) -> $result:ty { $( $impl:tt )* }
$fn_vis:vis fn $fn_name:ident(
$origin:ident $(, $param_name:ident : $param:ty)*
) $( -> $result:ty )* { $( $impl:tt )* }
$($rest:tt)*
) => {
decl_module!(@normalize
@@ -179,7 +181,9 @@ macro_rules! decl_module {
[
$($t)*
$(#[doc = $doc_attr])*
$fn_vis fn $fn_name($origin $( , $param_name : $param )* ) -> $result { $( $impl )* }
$fn_vis fn $fn_name(
$origin $( , $param_name : $param )*
) $( -> $result )* { $( $impl )* }
]
$($rest)*
);
@@ -192,12 +196,16 @@ macro_rules! decl_module {
{ $( $on_finalise:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident($origin:ident : T::Origin $(, $param_name:ident : $param:ty)* ) -> $result:ty { $( $impl:tt )* }
$fn_vis:vis fn $fn_name:ident(
$origin:ident : T::Origin $(, $param_name:ident : $param:ty)*
) $( -> $result:ty )* { $( $impl:tt )* }
$($rest:tt)*
) => {
compile_error!("\
first parameter of dispatch should be marked `origin` only, with no type specified (a bit like `self`)\n\
(For root-matching dispatches, ensure the first parameter does not use the `T::Origin` type.)")
compile_error!(
"First parameter of dispatch should be marked `origin` only, with no type specified \
(a bit like `self`). (For root-matching dispatches, ensure the first parameter does \
not use the `T::Origin` type.)"
)
};
(@normalize
$(#[$attr:meta])*
@@ -207,12 +215,16 @@ first parameter of dispatch should be marked `origin` only, with no type specifi
{ $( $on_finalise:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident(origin : $origin:ty $(, $param_name:ident : $param:ty)* ) -> $result:ty { $( $impl:tt )* }
$fn_vis:vis fn $fn_name:ident(
origin : $origin:ty $(, $param_name:ident : $param:ty)*
) $( -> $result:ty )* { $( $impl:tt )* }
$($rest:tt)*
) => {
compile_error!("\
first parameter of dispatch should be marked `origin` only, with no type specified (a bit like `self`)\n\
(For root-matching dispatches, ensure the first parameter is not named`origin`.)")
compile_error!(
"First parameter of dispatch should be marked `origin` only, with no type specified \
(a bit like `self`). (For root-matching dispatches, ensure the first parameter does \
not use the `T::Origin` type.)"
)
};
(@normalize
$(#[$attr:meta])*
@@ -222,7 +234,9 @@ first parameter of dispatch should be marked `origin` only, with no type specifi
{ $( $on_finalise:tt )* }
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident($( $param_name:ident : $param:ty),* ) -> $result:ty { $( $impl:tt )* }
$fn_vis:vis fn $fn_name:ident(
$( $param_name:ident : $param:ty),*
) $( -> $result:ty )* { $( $impl:tt )* }
$($rest:tt)*
) => {
decl_module!(@normalize
@@ -234,7 +248,9 @@ first parameter of dispatch should be marked `origin` only, with no type specifi
[
$($t)*
$(#[doc = $doc_attr])*
$fn_vis fn $fn_name(root $( , $param_name : $param )* ) -> $result { $( $impl )* }
$fn_vis fn $fn_name(
root $( , $param_name : $param )*
) $( -> $result )* { $( $impl )* }
]
$($rest)*
);
@@ -340,7 +356,23 @@ first parameter of dispatch should be marked `origin` only, with no type specifi
$module:ident<$trait_instance:ident: $trait_name:ident>;
$origin_ty:ty;
root;
$vis:vis fn $name:ident ( root $(, $param:ident : $param_ty:ty )* ) -> $result:ty { $( $impl:tt )* }
$vis:vis fn $name:ident ( root $(, $param:ident : $param_ty:ty )* ) { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name> $module<$trait_instance> {
$vis fn $name($( $param: $param_ty ),* ) -> $crate::dispatch::Result {
{ $( $impl )* }
Ok(())
}
}
};
(@impl_function
$module:ident<$trait_instance:ident: $trait_name:ident>;
$origin_ty:ty;
root;
$vis:vis fn $name:ident (
root $(, $param:ident : $param_ty:ty )*
) -> $result:ty { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name> $module<$trait_instance> {
$vis fn $name($( $param: $param_ty ),* ) -> $result {
@@ -348,11 +380,32 @@ first parameter of dispatch should be marked `origin` only, with no type specifi
}
}
};
(@impl_function
$module:ident<$trait_instance:ident: $trait_name:ident>;
$origin_ty:ty;
$ignore:ident;
$vis:vis fn $name:ident ( $origin:ident $(, $param:ident : $param_ty:ty )* ) -> $result:ty { $( $impl:tt )* }
$vis:vis fn $name:ident (
$origin:ident $(, $param:ident : $param_ty:ty )*
) { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name> $module<$trait_instance> {
$vis fn $name(
$origin: $origin_ty $(, $param: $param_ty )*
) -> $crate::dispatch::Result {
{ $( $impl )* }
Ok(())
}
}
};
(@impl_function
$module:ident<$trait_instance:ident: $trait_name:ident>;
$origin_ty:ty;
$ignore:ident;
$vis:vis fn $name:ident (
$origin:ident $(, $param:ident : $param_ty:ty )*
) -> $result:ty { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name> $module<$trait_instance> {
$vis fn $name($origin: $origin_ty $(, $param: $param_ty )* ) -> $result {
@@ -369,7 +422,7 @@ first parameter of dispatch should be marked `origin` only, with no type specifi
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident(
$from:ident $( , $param_name:ident : $param:ty)*
) -> $result:ty { $( $impl:tt )* }
) $( -> $result:ty )* { $( $impl:tt )* }
)*
}
{ $( $deposit_event:tt )* }
@@ -409,7 +462,9 @@ first parameter of dispatch should be marked `origin` only, with no type specifi
$mod_type<$trait_instance: $trait_name>;
$origin_type;
$from;
$fn_vis fn $fn_name ($from $(, $param_name : $param )* ) -> $result { $( $impl )* }
$fn_vis fn $fn_name (
$from $(, $param_name : $param )*
) $( -> $result )* { $( $impl )* }
}
)*
@@ -542,7 +597,7 @@ first parameter of dispatch should be marked `origin` only, with no type specifi
}
__dispatch_impl_metadata! {
$mod_type $trait_instance $trait_name $call_type $origin_type
{$( $(#[doc = $doc_attr])* fn $fn_name($from $(, $param_name : $param )*) -> $result; )*}
{$( $(#[doc = $doc_attr])* fn $fn_name($from $(, $param_name : $param )*); )*}
}
}
}
@@ -764,13 +819,13 @@ macro_rules! __call_to_metadata {
$(
, $param_name:ident : $param:ty
)*
) -> $result:ty;
);
)*}
) => {
$crate::dispatch::CallMetadata {
name: $crate::dispatch::DecodeDifferent::Encode(stringify!($call_type)),
functions: __functions_to_metadata!(0; $origin_type;; $(
fn $fn_name( $( $param_name: $param ),* ) -> $result;
fn $fn_name( $( $param_name: $param ),* );
$( $doc_attr ),*;
)*),
}
@@ -789,14 +844,14 @@ macro_rules! __functions_to_metadata{
$(
$param_name:ident : $param:ty
),*
) -> $result:ty;
);
$( $fn_doc:expr ),*;
$( $rest:tt )*
) => {
__functions_to_metadata!(
$fn_id + 1; $origin_type;
$( $function_metadata, )* __function_to_metadata!(
fn $fn_name($( $param_name : $param ),*) -> $result; $( $fn_doc ),*; $fn_id;
fn $fn_name($( $param_name : $param ),*); $( $fn_doc ),*; $fn_id;
);
$($rest)*
)
@@ -817,7 +872,7 @@ macro_rules! __function_to_metadata {
(
fn $fn_name:ident(
$($param_name:ident : $param:ty),*
) -> $result:ty;
);
$( $fn_doc:expr ),*;
$fn_id:expr;
) => {
+1 -3
View File
@@ -49,7 +49,6 @@ extern crate parity_codec as codec;
use codec::HasCompact;
use runtime_support::{StorageValue, Parameter};
use runtime_support::dispatch::Result;
use runtime_primitives::CheckInherentError;
use runtime_primitives::traits::{
As, SimpleArithmetic, Zero, ProvideInherent, Block as BlockT, Extrinsic
@@ -75,7 +74,7 @@ decl_module! {
/// if this call hasn't been invoked by that time.
///
/// The timestamp should be greater than the previous one by the amount specified by `block_period`.
fn set(origin, now: <T::Moment as HasCompact>::Type) -> Result {
fn set(origin, now: <T::Moment as HasCompact>::Type) {
ensure_inherent(origin)?;
let now = now.into();
@@ -91,7 +90,6 @@ decl_module! {
);
<Self as Store>::Now::put(now);
<Self as Store>::DidUpdate::put(true);
Ok(())
}
fn on_finalise() {
+5 -16
View File
@@ -40,7 +40,6 @@ extern crate srml_balances as balances;
use rstd::prelude::*;
use runtime_support::{StorageValue, StorageMap};
use runtime_support::dispatch::Result;
use runtime_primitives::{Permill, traits::{Zero, EnsureOrigin}};
use codec::{HasCompact, Compact};
use balances::{OnDilution, address::Address};
@@ -77,7 +76,7 @@ decl_module! {
origin,
value: <T::Balance as HasCompact>::Type,
beneficiary: Address<T::AccountId, T::AccountIndex>
) -> Result {
) {
let proposer = ensure_signed(origin)?;
let beneficiary = <balances::Module<T>>::lookup(beneficiary)?;
let value = value.into();
@@ -91,17 +90,12 @@ decl_module! {
<Proposals<T>>::insert(c, Proposal { proposer, value, beneficiary, bond });
Self::deposit_event(RawEvent::Proposed(c));
Ok(())
}
/// Set the balance of funds available to spend.
fn set_pot(new_pot: <T::Balance as HasCompact>::Type) -> Result {
fn set_pot(new_pot: <T::Balance as HasCompact>::Type) {
// Put the new value into storage.
<Pot<T>>::put(new_pot.into());
// All good.
Ok(())
}
/// (Re-)configure this module.
@@ -110,16 +104,15 @@ decl_module! {
proposal_bond_minimum: <T::Balance as HasCompact>::Type,
spend_period: <T::BlockNumber as HasCompact>::Type,
burn: Permill
) -> Result {
) {
<ProposalBond<T>>::put(proposal_bond);
<ProposalBondMinimum<T>>::put(proposal_bond_minimum.into());
<SpendPeriod<T>>::put(spend_period.into());
<Burn<T>>::put(burn);
Ok(())
}
/// Reject a proposed spend. The original deposit will be slashed.
fn reject_proposal(origin, proposal_id: Compact<ProposalIndex>) -> Result {
fn reject_proposal(origin, proposal_id: Compact<ProposalIndex>) {
T::RejectOrigin::ensure_origin(origin)?;
let proposal_id: ProposalIndex = proposal_id.into();
@@ -127,21 +120,17 @@ decl_module! {
let value = proposal.bond;
let _ = <balances::Module<T>>::slash_reserved(&proposal.proposer, value);
Ok(())
}
/// Approve a proposal. At a later time, the proposal will be allocated to the beneficiary
/// and the original deposit will be returned.
fn approve_proposal(origin, proposal_id: Compact<ProposalIndex>) -> Result {
fn approve_proposal(origin, proposal_id: Compact<ProposalIndex>) {
T::ApproveOrigin::ensure_origin(origin)?;
let proposal_id = proposal_id.into();
ensure!(<Proposals<T>>::exists(proposal_id), "No proposal at that index");
<Approvals<T>>::mutate(|v| v.push(proposal_id));
Ok(())
}
fn on_finalise(n: T::BlockNumber) {
+3 -7
View File
@@ -35,7 +35,7 @@ extern crate srml_system as system;
extern crate srml_consensus as consensus;
use sr_std::prelude::*;
use support::{StorageValue, dispatch::Result};
use support::StorageValue;
use system::ensure_signed;
pub trait Trait: consensus::Trait + system::Trait {
@@ -47,26 +47,22 @@ decl_module! {
// Simple declaration of the `Module` type. Lets the macro know what its working on.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event() = default;
fn upgrade(origin, new: Vec<u8>) -> Result {
fn upgrade(origin, new: Vec<u8>) {
// This is a public call, so we ensure that the origin is some signed account.
let _sender = ensure_signed(origin)?;
ensure!(_sender == Self::key(), "only the current upgrade key can use the upgrade_key module");
<consensus::Module<T>>::set_code(new)?;
Self::deposit_event(RawEvent::Upgraded);
Ok(())
}
fn set_key(origin, new: T::AccountId) -> Result {
fn set_key(origin, new: T::AccountId) {
// This is a public call, so we ensure that the origin is some signed account.
let _sender = ensure_signed(origin)?;
ensure!(_sender == Self::key(), "only the current upgrade key can use the upgrade_key module");
Self::deposit_event(RawEvent::KeyChanged(Self::key()));
<Key<T>>::put(new);
Ok(())
}
}
}