enhance dispatch (#720)

* normalize decl_storage

* dispatch the function call

* add test case

* fix the root case

* add system

* fix the typo in unit test

* fix the doc generation for decl_module

* fix the unit test due to the interface change
This commit is contained in:
Guanqun Lu
2018-09-12 21:56:37 +08:00
committed by Gav Wood
parent 1e01162505
commit 4685018991
11 changed files with 214 additions and 104 deletions
+3 -4
View File
@@ -48,7 +48,7 @@ use runtime_support::dispatch::Result;
use primitives::traits::{Zero, One, SimpleArithmetic, OnFinalise, MakePayment,
As, Lookup, Member, CheckedAdd, CheckedSub};
use address::Address as RawAddress;
use system::{ensure_signed, ensure_root};
use system::ensure_signed;
mod mock;
@@ -131,7 +131,7 @@ pub trait Trait: system::Trait {
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn transfer(origin, dest: RawAddress<T::AccountId, T::AccountIndex>, value: T::Balance) -> Result;
fn set_balance(origin, who: RawAddress<T::AccountId, T::AccountIndex>, free: T::Balance, reserved: T::Balance) -> Result;
fn set_balance(who: RawAddress<T::AccountId, T::AccountIndex>, free: T::Balance, reserved: T::Balance) -> Result;
}
}
@@ -313,8 +313,7 @@ impl<T: Trait> Module<T> {
}
/// Set the balances of a given account.
fn set_balance(origin: T::Origin, who: Address<T>, free: T::Balance, reserved: T::Balance) -> Result {
ensure_root(origin)?;
fn set_balance(who: Address<T>, free: T::Balance, reserved: T::Balance) -> Result {
let who = Self::lookup(who)?;
Self::set_free_balance(&who, free);
Self::set_reserved_balance(&who, reserved);
+5 -7
View File
@@ -48,7 +48,7 @@ use runtime_support::storage::StorageValue;
use runtime_support::storage::unhashed::StorageVec;
use primitives::traits::{MaybeSerializeDebug, OnFinalise, Member, DigestItem};
use primitives::bft::MisbehaviorReport;
use system::{ensure_signed, ensure_inherent, ensure_root};
use system::{ensure_signed, ensure_inherent};
#[cfg(any(feature = "std", test))]
use substrate_primitives::Blake2Hasher;
@@ -132,8 +132,8 @@ decl_module! {
fn report_misbehavior(origin, report: MisbehaviorReport<T::Hash, T::BlockNumber>) -> Result;
fn note_offline(origin, offline_val_indices: Vec<u32>) -> Result;
fn remark(origin, remark: Vec<u8>) -> Result;
fn set_code(origin, new: Vec<u8>) -> Result;
fn set_storage(origin, items: Vec<KeyValue>) -> Result;
fn set_code(new: Vec<u8>) -> Result;
fn set_storage(items: Vec<KeyValue>) -> Result;
}
}
@@ -144,15 +144,13 @@ impl<T: Trait> Module<T> {
}
/// Set the new code.
fn set_code(origin: T::Origin, new: Vec<u8>) -> Result {
ensure_root(origin)?;
fn set_code(new: Vec<u8>) -> Result {
storage::unhashed::put_raw(CODE, &new);
Ok(())
}
/// Set some items of storage.
fn set_storage(origin: T::Origin, items: Vec<KeyValue>) -> Result {
ensure_root(origin)?;
fn set_storage(items: Vec<KeyValue>) -> Result {
for i in &items {
storage::unhashed::put_raw(&i.0, &i.1);
}
+11 -15
View File
@@ -22,7 +22,7 @@ use runtime_io::print;
use srml_support::{StorageValue, StorageMap, dispatch::Result};
use democracy;
use balances::{self, address::Address};
use system::{self, ensure_signed, ensure_root};
use system::{self, ensure_signed};
// no polynomial attacks:
//
@@ -92,10 +92,10 @@ decl_module! {
fn submit_candidacy(origin, slot: u32) -> Result;
fn present_winner(origin, candidate: Address<T::AccountId, T::AccountIndex>, total: T::Balance, index: VoteIndex) -> Result;
fn set_desired_seats(origin, count: u32) -> Result;
fn remove_member(origin, who: Address<T::AccountId, T::AccountIndex>) -> Result;
fn set_presentation_duration(origin, count: T::BlockNumber) -> Result;
fn set_term_duration(origin, count: T::BlockNumber) -> Result;
fn set_desired_seats(count: u32) -> Result;
fn remove_member(who: Address<T::AccountId, T::AccountIndex>) -> Result;
fn set_presentation_duration(count: T::BlockNumber) -> Result;
fn set_term_duration(count: T::BlockNumber) -> Result;
}
}
@@ -406,8 +406,7 @@ impl<T: Trait> Module<T> {
/// 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(origin: T::Origin, count: u32) -> Result {
ensure_root(origin)?;
fn set_desired_seats(count: u32) -> Result {
<DesiredSeats<T>>::put(count);
Ok(())
}
@@ -415,8 +414,7 @@ impl<T: Trait> Module<T> {
/// 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(origin: T::Origin, who: Address<T::AccountId, T::AccountIndex>) -> Result {
ensure_root(origin)?;
fn remove_member(who: Address<T::AccountId, T::AccountIndex>) -> Result {
let who = <balances::Module<T>>::lookup(who)?;
let new_council: Vec<(T::AccountId, T::BlockNumber)> = Self::active_council()
.into_iter()
@@ -428,16 +426,14 @@ impl<T: Trait> Module<T> {
/// Set the presentation duration. If there is currently a vote being presented for, will
/// invoke `finalise_vote`.
fn set_presentation_duration(origin: T::Origin, count: T::BlockNumber) -> Result {
ensure_root(origin)?;
fn set_presentation_duration(count: T::BlockNumber) -> Result {
<PresentationDuration<T>>::put(count);
Ok(())
}
/// Set the presentation duration. If there is current a vote being presented for, will
/// invoke `finalise_vote`.
fn set_term_duration(origin: T::Origin, count: T::BlockNumber) -> Result {
ensure_root(origin)?;
fn set_term_duration(count: T::BlockNumber) -> Result {
<TermDuration<T>>::put(count);
Ok(())
}
@@ -1073,7 +1069,7 @@ mod tests {
assert_ok!(Council::end_block(System::block_number()));
System::set_block_number(8);
assert_ok!(Council::set_desired_seats(Origin::ROOT, 3));
assert_ok!(Council::set_desired_seats(3));
assert_ok!(Council::end_block(System::block_number()));
System::set_block_number(10);
@@ -1326,7 +1322,7 @@ mod tests {
System::set_block_number(8);
assert_ok!(Council::set_approvals(Origin::signed(6), vec![false, false, true, false], 1));
assert_ok!(Council::set_desired_seats(Origin::ROOT, 3));
assert_ok!(Council::set_desired_seats(3));
assert_ok!(Council::end_block(System::block_number()));
System::set_block_number(10);
+5 -7
View File
@@ -24,7 +24,7 @@ use srml_support::dispatch::Result;
use srml_support::{StorageValue, StorageMap, IsSubType};
use {system, democracy};
use super::{Trait as CouncilTrait, Module as Council};
use system::{ensure_signed, ensure_root};
use system::ensure_signed;
pub trait Trait: CouncilTrait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
@@ -36,8 +36,8 @@ decl_module! {
fn vote(origin, proposal: T::Hash, approve: bool) -> Result;
fn veto(origin, proposal_hash: T::Hash) -> Result;
fn set_cooloff_period(origin, blocks: T::BlockNumber) -> Result;
fn set_voting_period(origin, blocks: T::BlockNumber) -> Result;
fn set_cooloff_period(blocks: T::BlockNumber) -> Result;
fn set_voting_period(blocks: T::BlockNumber) -> Result;
}
}
@@ -155,14 +155,12 @@ impl<T: Trait> Module<T> {
Ok(())
}
fn set_cooloff_period(origin: T::Origin, blocks: T::BlockNumber) -> Result {
ensure_root(origin)?;
fn set_cooloff_period(blocks: T::BlockNumber) -> Result {
<CooloffPeriod<T>>::put(blocks);
Ok(())
}
fn set_voting_period(origin: T::Origin, blocks: T::BlockNumber) -> Result {
ensure_root(origin)?;
fn set_voting_period(blocks: T::BlockNumber) -> Result {
<VotingPeriod<T>>::put(blocks);
Ok(())
}
+6 -8
View File
@@ -46,7 +46,7 @@ use rstd::result;
use primitives::traits::{Zero, OnFinalise, As, MaybeSerializeDebug};
use srml_support::{StorageValue, StorageMap, Parameter, Dispatchable, IsSubType};
use srml_support::dispatch::Result;
use system::{ensure_signed, ensure_root};
use system::ensure_signed;
#[cfg(any(feature = "std", test))]
use std::collections::HashMap;
@@ -71,8 +71,8 @@ decl_module! {
fn second(origin, proposal: PropIndex) -> Result;
fn vote(origin, ref_index: ReferendumIndex, approve_proposal: bool) -> Result;
fn start_referendum(origin, proposal: Box<T::Proposal>, vote_threshold: VoteThreshold) -> Result;
fn cancel_referendum(origin, ref_index: ReferendumIndex) -> Result;
fn start_referendum(proposal: Box<T::Proposal>, vote_threshold: VoteThreshold) -> Result;
fn cancel_referendum(ref_index: ReferendumIndex) -> Result;
}
}
@@ -215,8 +215,7 @@ impl<T: Trait> Module<T> {
}
/// Start a referendum.
fn start_referendum(origin: T::Origin, proposal: Box<T::Proposal>, vote_threshold: VoteThreshold) -> Result {
ensure_root(origin)?;
fn start_referendum(proposal: Box<T::Proposal>, vote_threshold: VoteThreshold) -> Result {
Self::inject_referendum(
<system::Module<T>>::block_number() + Self::voting_period(),
*proposal,
@@ -225,8 +224,7 @@ impl<T: Trait> Module<T> {
}
/// Remove a referendum.
fn cancel_referendum(origin: T::Origin, ref_index: ReferendumIndex) -> Result {
ensure_root(origin)?;
fn cancel_referendum(ref_index: ReferendumIndex) -> Result {
Self::clear_referendum(ref_index);
Ok(())
}
@@ -598,7 +596,7 @@ mod tests {
System::set_block_number(1);
let r = Democracy::inject_referendum(1, set_balance_proposal(2), VoteThreshold::SuperMajorityApprove).unwrap();
assert_ok!(Democracy::vote(Origin::signed(1), r, true));
assert_ok!(Democracy::cancel_referendum(Origin::ROOT, r));
assert_ok!(Democracy::cancel_referendum(r));
assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
+3 -6
View File
@@ -60,7 +60,7 @@ extern crate srml_balances as balances;
use runtime_primitives::traits::OnFinalise;
use runtime_support::{StorageValue, dispatch::Result};
use system::{ensure_signed, ensure_root};
use system::ensure_signed;
/// Our module's configuration trait. All our types and consts go in here. If the
/// module is dependent on specific other modules, then their configuration traits
@@ -113,7 +113,7 @@ decl_module! {
fn accumulate_foo(origin, increase_by: T::Balance) -> Result;
/// A privileged call; in this case it resets our dummy value to something new.
fn set_dummy(origin, new_dummy: T::Balance) -> Result;
fn set_dummy(new_dummy: T::Balance) -> Result;
}
}
@@ -264,10 +264,7 @@ impl<T: Trait> Module<T> {
// 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(origin: T::Origin, new_value: T::Balance) -> Result {
// This is a privileged call, so we ensure that the origin is "Root".
ensure_root(origin)?;
fn set_dummy(new_value: T::Balance) -> Result {
// Put the new value into storage.
<Dummy<T>>::put(new_value);
+10 -12
View File
@@ -52,7 +52,7 @@ use rstd::prelude::*;
use primitives::traits::{Zero, One, OnFinalise, Convert, As};
use runtime_support::{StorageValue, StorageMap};
use runtime_support::dispatch::Result;
use system::{ensure_signed, ensure_root};
use system::ensure_signed;
#[cfg(any(feature = "std", test))]
use std::collections::HashMap;
@@ -77,8 +77,8 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn set_key(origin, key: T::SessionKey) -> Result;
fn set_length(origin, new: T::BlockNumber) -> Result;
fn force_new_session(origin, apply_rewards: bool) -> Result;
fn set_length(new: T::BlockNumber) -> Result;
fn force_new_session(apply_rewards: bool) -> Result;
}
}
@@ -144,15 +144,13 @@ impl<T: Trait> Module<T> {
}
/// Set a new era length. Won't kick in until the next era change (at current length).
fn set_length(origin: T::Origin, new: T::BlockNumber) -> Result {
ensure_root(origin)?;
fn set_length(new: T::BlockNumber) -> Result {
<NextSessionLength<T>>::put(new);
Ok(())
}
/// Forces a new session.
pub fn force_new_session(origin: T::Origin, apply_rewards: bool) -> Result {
ensure_root(origin)?;
pub fn force_new_session(apply_rewards: bool) -> Result {
Self::apply_force_new_session(apply_rewards)
}
@@ -357,7 +355,7 @@ mod tests {
fn should_work_with_early_exit() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
assert_ok!(Session::set_length(Origin::ROOT, 10));
assert_ok!(Session::set_length(10));
assert_eq!(Session::blocks_remaining(), 1);
Session::check_rotate_session(1);
@@ -369,7 +367,7 @@ mod tests {
System::set_block_number(7);
assert_eq!(Session::current_index(), 1);
assert_eq!(Session::blocks_remaining(), 5);
assert_ok!(Session::force_new_session(Origin::ROOT, false));
assert_ok!(Session::force_new_session(false));
Session::check_rotate_session(7);
System::set_block_number(8);
@@ -392,14 +390,14 @@ mod tests {
with_externalities(&mut new_test_ext(), || {
// Block 1: Change to length 3; no visible change.
System::set_block_number(1);
assert_ok!(Session::set_length(Origin::ROOT, 3));
assert_ok!(Session::set_length(3));
Session::check_rotate_session(1);
assert_eq!(Session::length(), 2);
assert_eq!(Session::current_index(), 0);
// Block 2: Length now changed to 3. Index incremented.
System::set_block_number(2);
assert_ok!(Session::set_length(Origin::ROOT, 3));
assert_ok!(Session::set_length(3));
Session::check_rotate_session(2);
assert_eq!(Session::length(), 3);
assert_eq!(Session::current_index(), 1);
@@ -412,7 +410,7 @@ mod tests {
// Block 4: Change to length 2; no visible change.
System::set_block_number(4);
assert_ok!(Session::set_length(Origin::ROOT, 2));
assert_ok!(Session::set_length(2));
Session::check_rotate_session(4);
assert_eq!(Session::length(), 3);
assert_eq!(Session::current_index(), 1);
+11 -16
View File
@@ -54,7 +54,7 @@ use session::OnSessionChange;
use primitives::traits::{Zero, One, Bounded, OnFinalise,
As, Lookup};
use balances::{address::Address, OnDilution};
use system::{ensure_root, ensure_signed};
use system::ensure_signed;
mod mock;
@@ -110,11 +110,11 @@ decl_module! {
fn unnominate(origin, target_index: u32) -> Result;
fn register_preferences(origin, intentions_index: u32, prefs: ValidatorPrefs<T::Balance>) -> Result;
fn set_sessions_per_era(origin, new: T::BlockNumber) -> Result;
fn set_bonding_duration(origin, new: T::BlockNumber) -> Result;
fn set_validator_count(origin, new: u32) -> Result;
fn force_new_era(origin, apply_rewards: bool) -> Result;
fn set_offline_slash_grace(origin, new: u32) -> Result;
fn set_sessions_per_era(new: T::BlockNumber) -> Result;
fn set_bonding_duration(new: T::BlockNumber) -> Result;
fn set_validator_count(new: u32) -> Result;
fn force_new_era(apply_rewards: bool) -> Result;
fn set_offline_slash_grace(new: u32) -> Result;
}
}
@@ -325,30 +325,26 @@ impl<T: Trait> Module<T> {
// PRIV DISPATCH
/// Set the number of sessions in an era.
fn set_sessions_per_era(origin: T::Origin, new: T::BlockNumber) -> Result {
ensure_root(origin)?;
fn set_sessions_per_era(new: T::BlockNumber) -> Result {
<NextSessionsPerEra<T>>::put(&new);
Ok(())
}
/// The length of the bonding duration in eras.
fn set_bonding_duration(origin: T::Origin, new: T::BlockNumber) -> Result {
ensure_root(origin)?;
fn set_bonding_duration(new: T::BlockNumber) -> Result {
<BondingDuration<T>>::put(&new);
Ok(())
}
/// The length of a staking era in sessions.
fn set_validator_count(origin: T::Origin, new: u32) -> Result {
ensure_root(origin)?;
fn set_validator_count(new: u32) -> Result {
<ValidatorCount<T>>::put(&new);
Ok(())
}
/// Force there to be a new era. This also forces a new session immediately after.
/// `apply_rewards` should be true for validators to get the session reward.
fn force_new_era(origin: T::Origin, apply_rewards: bool) -> Result {
ensure_root(origin)?;
fn force_new_era(apply_rewards: bool) -> Result {
Self::apply_force_new_era(apply_rewards)
}
@@ -360,8 +356,7 @@ impl<T: Trait> Module<T> {
/// Set the offline slash grace period.
fn set_offline_slash_grace(origin: T::Origin, new: u32) -> Result {
ensure_root(origin)?;
fn set_offline_slash_grace(new: u32) -> Result {
<OfflineSlashGrace<T>>::put(&new);
Ok(())
}
+3 -3
View File
@@ -75,7 +75,7 @@ fn note_offline_grace_should_work() {
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
Balances::set_free_balance(&10, 70);
Balances::set_free_balance(&20, 70);
assert_ok!(Staking::set_offline_slash_grace(Origin::ROOT, 1));
assert_ok!(Staking::set_offline_slash_grace(1));
assert_eq!(Staking::offline_slash_grace(), 1);
assert_eq!(Staking::slash_count(&10), 0);
@@ -236,7 +236,7 @@ fn staking_should_work() {
assert_eq!(Staking::validator_count(), 2);
assert_eq!(Session::validators(), vec![10, 20]);
assert_ok!(Staking::set_bonding_duration(Origin::ROOT, 2));
assert_ok!(Staking::set_bonding_duration(2));
assert_eq!(Staking::bonding_duration(), 2);
// Block 1: Add three validators. No obvious change.
@@ -440,7 +440,7 @@ fn staking_eras_work() {
// Block 3: Schedule an era length change; no visible changes.
System::set_block_number(3);
assert_ok!(Staking::set_sessions_per_era(Origin::ROOT, 3));
assert_ok!(Staking::set_sessions_per_era(3));
Session::check_rotate_session(System::block_number());
assert_eq!(Session::current_index(), 3);
assert_eq!(Staking::sessions_per_era(), 2);
+153 -19
View File
@@ -64,13 +64,106 @@ macro_rules! decl_module {
(
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty {$(
for enum $call_type:ident where origin: $origin_type:ty {
$($t:tt)*
}
) => {
decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = system
[]
$($t)*
);
};
(
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident {
$($t:tt)*
}
) => {
decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = $system
[]
$($t)*
);
};
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
fn $fn_name:ident(origin $(, $param_name:ident : $param:ty)* ) -> $result:ty ;
$($rest:tt)*
) => {
decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = $system
[ $($t)* $(#[doc = $doc_attr])* fn $fn_name(origin $( , $param_name : $param )* ) -> $result; ]
$($rest)*
);
};
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident
[ $($t:tt)* ]
$(#[doc = $doc_attr:tt])*
fn $fn_name:ident($( $param_name:ident : $param:ty),* ) -> $result:ty ;
$($rest:tt)*
) => {
decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = $system
[ $($t)* $(#[doc = $doc_attr])* fn $fn_name(root $( , $param_name : $param )* ) -> $result; ]
$($rest)*
);
};
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident
[ $($t:tt)* ]
) => {
decl_module!(@imp
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
for enum $call_type where origin: $origin_type where system = $system {
$($t)*
}
);
};
(@call
origin
$mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ]
) => {
<$mod_type<$trait_instance>>::$fn_name( $origin $(, $param_name )* )
};
(@call
root
$mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ]
) => {
{
$system::ensure_root($origin)?;
<$mod_type<$trait_instance>>::$fn_name( $( $param_name ),* )
}
};
(@imp
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident {
$(
$(#[doc = $doc_attr:tt])*
fn $fn_name:ident(origin
$(
, $param_name:ident : $param:ty
)*
) -> $result:ty;
fn $fn_name:ident($from:ident $( , $param_name:ident : $param:ty)*) -> $result:ty;
)*}
) => {
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
@@ -194,8 +287,9 @@ macro_rules! decl_module {
fn dispatch(self, _origin: Self::Origin) -> $crate::dispatch::Result {
match self {
$(
$call_type::$fn_name( $( $param_name ),* ) =>
<$mod_type<$trait_instance>>::$fn_name( _origin $(, $param_name )* ),
$call_type::$fn_name( $( $param_name ),* ) => {
decl_module!(@call $from $mod_type $trait_instance $fn_name _origin $system [ $( $param_name ),* ])
},
)*
_ => { panic!("__PhantomItem should never be used.") },
}
@@ -212,10 +306,9 @@ macro_rules! decl_module {
d.dispatch(origin)
}
}
__dispatch_impl_json_metadata! {
$mod_type $trait_instance $trait_name $call_type $origin_type
{$( $(#[doc = $doc_attr])* fn $fn_name(origin $(, $param_name : $param )*) -> $result; )*}
{$( $(#[doc = $doc_attr])* fn $fn_name($from $(, $param_name : $param )*) -> $result; )*}
}
}
}
@@ -386,12 +479,11 @@ macro_rules! __dispatch_impl_json_metadata {
#[macro_export]
#[doc(hidden)]
macro_rules! __call_to_json {
// WITH AUX
(
$call_type:ident $origin_type:ty
{$(
$(#[doc = $doc_attr:tt])*
fn $fn_name:ident(origin
fn $fn_name:ident($from:ident
$(
, $param_name:ident : $param:ty
)*
@@ -402,7 +494,7 @@ macro_rules! __call_to_json {
r#"{ "name": ""#, stringify!($call_type),
r#"", "functions": {"#,
__functions_to_json!(""; 0; $origin_type; $(
fn $fn_name(origin $(, $param_name: $param )* ) -> $result;
fn $fn_name($from $(, $param_name: $param )* ) -> $result;
__function_doc_to_json!(""; $($doc_attr)*);
)*), " } }"
)
@@ -413,12 +505,15 @@ macro_rules! __call_to_json {
#[macro_export]
#[doc(hidden)]
macro_rules! __functions_to_json {
// WITHOUT AUX
// ROOT
(
$prefix_str:tt;
$fn_id:expr;
fn $fn_name:ident(
$($param_name:ident : $param:ty),*
$origin_type:ty;
fn $fn_name:ident(root
$(
, $param_name:ident : $param:ty
)*
) -> $result:ty;
$fn_doc:expr;
$($rest:tt)*
@@ -426,14 +521,14 @@ macro_rules! __functions_to_json {
concat!($prefix_str, " ",
__function_to_json!(
fn $fn_name(
$($param_name : $param),*
$( $param_name : $param ),*
) -> $result;
$fn_doc;
$fn_id;
), __functions_to_json!(","; $fn_id + 1; $($rest)*)
), __functions_to_json!(","; $fn_id + 1; $origin_type; $($rest)*)
)
};
// WITH AUX
// NON ROOT
(
$prefix_str:tt;
$fn_id:expr;
@@ -489,6 +584,18 @@ macro_rules! __function_to_json {
r#" ], "description": ["#, $fn_doc, " ] }"
)
};
(
fn $fn_name:ident() -> $result:ty;
$fn_doc:tt;
$fn_id:expr;
) => {
concat!(
r#"""#, stringify!($fn_id), r#"""#,
r#": { "name": ""#, stringify!($fn_name),
r#"", "params": [ "#,
r#" ], "description": ["#, $fn_doc, " ] }"
)
};
}
/// Convert a function documentation attribute into its JSON representation.
@@ -526,12 +633,22 @@ mod tests {
type Origin;
}
pub mod system {
use super::Result;
pub fn ensure_root<R>(_: R) -> Result {
Ok(())
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Hi, this is a comment.
fn aux_0(origin) -> Result;
fn aux_1(origin, data: i32) -> Result;
fn aux_2(origin, data: i32, data2: String) -> Result;
fn aux_3() -> Result;
fn aux_4(data: i32) -> Result;
}
}
@@ -541,14 +658,23 @@ mod tests {
r#""0": { "name": "aux_0", "params": [ "#,
r#"{ "name": "origin", "type": "T::Origin" }"#,
r#" ], "description": [ " Hi, this is a comment." ] }, "#,
r#""0 + 1": { "name": "aux_1", "params": [ "#,
r#"{ "name": "origin", "type": "T::Origin" }, "#,
r#"{ "name": "data", "type": "i32" }"#,
r#" ], "description": [ ] }, "#,
r#""0 + 1 + 1": { "name": "aux_2", "params": [ "#,
r#"{ "name": "origin", "type": "T::Origin" }, "#,
r#"{ "name": "data", "type": "i32" }, "#,
r#"{ "name": "data2", "type": "String" }"#,
r#" ], "description": [ ] }, "#,
r#""0 + 1 + 1 + 1": { "name": "aux_3", "params": [ "#,
r#" ], "description": [ ] }, "#,
r#""0 + 1 + 1 + 1 + 1": { "name": "aux_4", "params": [ "#,
r#"{ "name": "data", "type": "i32" }"#,
r#" ], "description": [ ] }"#,
r#" } }"#,
r#" }"#,
@@ -566,6 +692,14 @@ mod tests {
fn aux_2(_: T::Origin, _: i32, _: String) -> Result {
unreachable!()
}
fn aux_3() -> Result {
unreachable!()
}
fn aux_4(_: i32) -> Result {
unreachable!()
}
}
struct TraitImpl {}
+4 -7
View File
@@ -46,7 +46,7 @@ use runtime_support::{StorageValue, StorageMap};
use runtime_support::dispatch::Result;
use runtime_primitives::{Permill, traits::{OnFinalise, Zero, EnsureOrigin}};
use balances::OnDilution;
use system::{ensure_signed, ensure_root};
use system::ensure_signed;
/// Our module's configuration trait. All our types and consts go in here. If the
/// module is dependent on specific other modules, then their configuration traits
@@ -77,10 +77,10 @@ decl_module! {
fn propose_spend(origin, value: T::Balance, beneficiary: T::AccountId) -> Result;
// Set the balance of funds available to spend.
fn set_pot(origin, new_pot: T::Balance) -> Result;
fn set_pot(new_pot: T::Balance) -> Result;
// (Re-)configure this module.
fn configure(origin, proposal_bond: Permill, proposal_bond_minimum: T::Balance, spend_period: T::BlockNumber, burn: Permill) -> Result;
fn configure(proposal_bond: Permill, proposal_bond_minimum: T::Balance, spend_period: T::BlockNumber, burn: Permill) -> Result;
// Reject a proposed spend. The original deposit will be slashed.
fn reject_proposal(origin, roposal_id: ProposalIndex) -> Result;
@@ -197,8 +197,7 @@ impl<T: Trait> Module<T> {
Ok(())
}
fn set_pot(origin: T::Origin, new_pot: T::Balance) -> Result {
ensure_root(origin)?;
fn set_pot(new_pot: T::Balance) -> Result {
// Put the new value into storage.
<Pot<T>>::put(new_pot);
@@ -207,13 +206,11 @@ impl<T: Trait> Module<T> {
}
fn configure(
origin: T::Origin,
proposal_bond: Permill,
proposal_bond_minimum: T::Balance,
spend_period: T::BlockNumber,
burn: Permill
) -> Result {
ensure_root(origin)?;
<ProposalBond<T>>::put(proposal_bond);
<ProposalBondMinimum<T>>::put(proposal_bond_minimum);
<SpendPeriod<T>>::put(spend_period);