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
+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);