Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
fix the unit test due to the interface change
Browse files Browse the repository at this point in the history
  • Loading branch information
Guanqun Lu committed Sep 12, 2018
1 parent 1e40b66 commit f7a8bd5
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 85 deletions.
7 changes: 3 additions & 4 deletions substrate/runtime/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 5 additions & 7 deletions substrate/runtime/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
}
Expand Down
26 changes: 11 additions & 15 deletions substrate/runtime/council/src/seats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use runtime_io::print;
use substrate_runtime_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:
//
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -406,17 +406,15 @@ 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(())
}

/// 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()
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 5 additions & 7 deletions substrate/runtime/council/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use substrate_runtime_support::dispatch::Result;
use substrate_runtime_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>;
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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(())
}
Expand Down
14 changes: 6 additions & 8 deletions substrate/runtime/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use rstd::result;
use primitives::traits::{Zero, OnFinalise, As, MaybeSerializeDebug};
use substrate_runtime_support::{StorageValue, StorageMap, Parameter, Dispatchable, IsSubType};
use substrate_runtime_support::dispatch::Result;
use system::{ensure_signed, ensure_root};
use system::ensure_signed;

#[cfg(any(feature = "std", test))]
use std::collections::HashMap;
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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(()));

Expand Down
9 changes: 3 additions & 6 deletions substrate/runtime/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extern crate substrate_runtime_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
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);

Expand Down
22 changes: 10 additions & 12 deletions substrate/runtime/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit f7a8bd5

Please sign in to comment.