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