Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/node-template/pallets/template/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl system::Trait for Test {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}
impl Trait for Test {
type Event = ();
Expand Down
3 changes: 3 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ impl system::Trait for Runtime {
type OnKilledAccount = ();
/// The data to be stored in an account.
type AccountData = balances::AccountData<Balance>;
// This is the central place where all calls are dispatched from.
type RootDispatcher = system::Module<Self>;
}

impl aura::Trait for Runtime {
Expand Down Expand Up @@ -219,6 +221,7 @@ impl transaction_payment::Trait for Runtime {
impl sudo::Trait for Runtime {
type Event = Event;
type Call = Call;
type Dispatcher = <Self as system::Trait>::RootDispatcher;
}

/// Used for the module template in `./template.rs`
Expand Down
8 changes: 8 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl frame_system::Trait for Runtime {
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = frame_system::Module<Self>;
}

parameter_types! {
Expand All @@ -160,6 +161,7 @@ impl pallet_utility::Trait for Runtime {
type MultisigDepositBase = MultisigDepositBase;
type MultisigDepositFactor = MultisigDepositFactor;
type MaxSignatories = MaxSignatories;
type Dispatcher = <Self as frame_system::Trait>::RootDispatcher;
}

parameter_types! {
Expand Down Expand Up @@ -341,6 +343,7 @@ impl pallet_democracy::Trait for Runtime {
type CooloffPeriod = CooloffPeriod;
type PreimageByteDeposit = PreimageByteDeposit;
type Slash = Treasury;
type Dispatcher = <Self as frame_system::Trait>::RootDispatcher;
}

parameter_types! {
Expand All @@ -353,6 +356,7 @@ impl pallet_collective::Trait<CouncilCollective> for Runtime {
type Proposal = Call;
type Event = Event;
type MotionDuration = CouncilMotionDuration;
type Dispatcher = <Self as frame_system::Trait>::RootDispatcher;
}

parameter_types! {
Expand Down Expand Up @@ -388,6 +392,7 @@ impl pallet_collective::Trait<TechnicalCollective> for Runtime {
type Proposal = Call;
type Event = Event;
type MotionDuration = TechnicalMotionDuration;
type Dispatcher = <Self as frame_system::Trait>::RootDispatcher;
}

impl pallet_membership::Trait<pallet_membership::Instance1> for Runtime {
Expand Down Expand Up @@ -447,6 +452,7 @@ impl pallet_contracts::Trait for Runtime {
type Event = Event;
type DetermineContractAddress = pallet_contracts::SimpleAddressDeterminer<Runtime>;
type ComputeDispatchFee = pallet_contracts::DefaultDispatchFeeComputor<Runtime>;
type Dispatcher = <Self as frame_system::Trait>::RootDispatcher;
type TrieIdGenerator = pallet_contracts::TrieIdFromParentCounter<Runtime>;
type GasPayment = ();
type RentPayment = ();
Expand All @@ -469,6 +475,7 @@ impl pallet_contracts::Trait for Runtime {
impl pallet_sudo::Trait for Runtime {
type Event = Event;
type Call = Call;
type Dispatcher = <Self as frame_system::Trait>::RootDispatcher;
}

/// A runtime transaction submitter.
Expand Down Expand Up @@ -586,6 +593,7 @@ impl pallet_recovery::Trait for Runtime {
type FriendDepositFactor = FriendDepositFactor;
type MaxFriends = MaxFriends;
type RecoveryDeposit = RecoveryDeposit;
type Dispatcher = <Self as frame_system::Trait>::RootDispatcher;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ mod tests {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}
impl Trait for Test {
type Event = ();
Expand Down
1 change: 1 addition & 0 deletions frame/aura/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl frame_system::Trait for Test {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}

impl pallet_timestamp::Trait for Test {
Expand Down
1 change: 1 addition & 0 deletions frame/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ mod tests {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}

impl_outer_origin! {
Expand Down
1 change: 1 addition & 0 deletions frame/authorship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ mod tests {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl frame_system::Trait for Test {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}

impl_opaque_keys! {
Expand Down
1 change: 1 addition & 0 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ impl<T: Subtrait<I>, I: Instance> frame_system::Trait for ElevatedTrait<T, I> {
type OnNewAccount = T::OnNewAccount;
type OnKilledAccount = T::OnKilledAccount;
type AccountData = T::AccountData;
type RootDispatcher = ();
}
impl<T: Subtrait<I>, I: Instance> Trait<I> for ElevatedTrait<T, I> {
type Balance = T::Balance;
Expand Down
1 change: 1 addition & 0 deletions frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl frame_system::Trait for Test {
type AccountData = super::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}
parameter_types! {
pub const TransactionBaseFee: u64 = 0;
Expand Down
1 change: 1 addition & 0 deletions frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl frame_system::Trait for Test {
type AccountData = super::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = Module<Test>;
type RootDispatcher = ();
}
parameter_types! {
pub const TransactionBaseFee: u64 = 0;
Expand Down
4 changes: 2 additions & 2 deletions frame/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod utils;
pub use utils::*;
#[doc(hidden)]
pub use sp_io::storage::root as storage_root;
pub use sp_runtime::traits::Dispatchable;
pub use sp_runtime::{SimpleDispatcher, traits::{Dispatchable, Dispatcher}};

/// Construct pallet benchmarks for weighing dispatchables.
///
Expand Down Expand Up @@ -158,7 +158,7 @@ macro_rules! benchmarks_iter {
) => {
$crate::benchmarks_iter! {
{ $( $common )* } ( $( $names )* ) $name { $( $code )* }: {
<Call<T> as $crate::Dispatchable>::dispatch(Call::<T>::$dispatch($($arg),*), $origin.into())?;
<$crate::SimpleDispatcher as $crate::Dispatcher<_, _>>::dispatch(Call::<T>::$dispatch($($arg),*), $origin.into()).result?;
} $( $rest )*
}
};
Expand Down
1 change: 1 addition & 0 deletions frame/benchmarking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl frame_system::Trait for Test {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}

impl Trait for Test {
Expand Down
14 changes: 10 additions & 4 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
use sp_std::{prelude::*, result};
use sp_core::u32_trait::Value as U32;
use sp_runtime::RuntimeDebug;
use sp_runtime::traits::{Hash, EnsureOrigin};
use sp_runtime::traits::{Hash, EnsureOrigin, Dispatcher};
use frame_support::weights::SimpleDispatchInfo;
use frame_support::{
dispatch::{Dispatchable, Parameter}, codec::{Encode, Decode},
Expand Down Expand Up @@ -69,6 +69,9 @@ pub trait Trait<I=DefaultInstance>: frame_system::Trait {

/// The time-out for council motions.
type MotionDuration: Get<Self::BlockNumber>;

/// The means of dispatching the calls to the runtime.
type Dispatcher: Dispatcher<<Self as Trait<I>>::Proposal, <Self as Trait<I>>::Origin>;
}

/// Origin for the collective module.
Expand Down Expand Up @@ -203,7 +206,7 @@ decl_module! {
ensure!(Self::is_member(&who), Error::<T, I>::NotMember);

let proposal_hash = T::Hashing::hash_of(&proposal);
let ok = proposal.dispatch(RawOrigin::Member(who).into()).is_ok();
let ok = T::Dispatcher::dispatch(*proposal, RawOrigin::Member(who).into()).is_ok();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can ever come up with an abstraction here to actually prevent one from calling proposal.dispatch() outside of the context of a Dispatcher. I think it should be possible by checking some global environmental value. But maybe it is not even needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could require the trait to accept a parameter of a type for which an instance can only be created from the Dispatcher's module.

Copy link
Member

@athei athei Mar 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rewrite applies gav's suggestion in order to accomplish this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a [should_panic] test case around it? I'd love to see that 😄

Self::deposit_event(RawEvent::MemberExecuted(proposal_hash, ok));
}

Expand All @@ -222,7 +225,7 @@ decl_module! {

if threshold < 2 {
let seats = Self::members().len() as MemberCount;
let ok = proposal.dispatch(RawOrigin::Members(1, seats).into()).is_ok();
let ok = T::Dispatcher::dispatch(*proposal, RawOrigin::Members(1, seats).into()).is_ok();
Self::deposit_event(RawEvent::Executed(proposal_hash, ok));
} else {
let index = Self::proposal_count();
Expand Down Expand Up @@ -360,7 +363,7 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
// execute motion, assuming it exists.
if let Some(p) = ProposalOf::<T, I>::take(&proposal) {
let origin = RawOrigin::Members(voting.threshold, seats).into();
let ok = p.dispatch(origin).is_ok();
let ok = T::Dispatcher::dispatch(p, origin).is_ok();
Self::deposit_event(RawEvent::Executed(proposal, ok));
}
} else {
Expand Down Expand Up @@ -537,18 +540,21 @@ mod tests {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}
impl Trait<Instance1> for Test {
type Origin = Origin;
type Proposal = Call;
type Event = Event;
type MotionDuration = MotionDuration;
type Dispatcher = sp_runtime::SimpleDispatcher;
}
impl Trait for Test {
type Origin = Origin;
type Proposal = Call;
type Event = Event;
type MotionDuration = MotionDuration;
type Dispatcher = sp_runtime::SimpleDispatcher;
}

pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
Expand Down
9 changes: 7 additions & 2 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ use sp_std::{prelude::*, marker::PhantomData, fmt::Debug};
use codec::{Codec, Encode, Decode};
use sp_io::hashing::blake2_256;
use sp_runtime::{
traits::{Hash, StaticLookup, Zero, MaybeSerializeDeserialize, Member, SignedExtension},
traits::{
Hash, StaticLookup, Zero, MaybeSerializeDeserialize, Member, SignedExtension, Dispatcher,
},
transaction_validity::{
ValidTransaction, InvalidTransaction, TransactionValidity, TransactionValidityError,
},
Expand Down Expand Up @@ -366,6 +368,9 @@ pub trait Trait: frame_system::Trait {
/// trie id generator
type TrieIdGenerator: TrieIdGenerator<Self::AccountId>;

/// The means of dispatching the calls to the runtime.
type Dispatcher: Dispatcher<<Self as Trait>::Call, Self::Origin>;

/// Handler for the unbalanced reduction when making a gas payment.
type GasPayment: OnUnbalanced<NegativeImbalanceOf<Self>>;

Expand Down Expand Up @@ -763,7 +768,7 @@ impl<T: Trait> Module<T> {
origin: who,
call,
} => {
let result = call.dispatch(RawOrigin::Signed(who.clone()).into());
let result = T::Dispatcher::dispatch(call, RawOrigin::Signed(who.clone()).into()).result;
Self::deposit_event(RawEvent::Dispatched(who, result.is_ok()));
}
RestoreTo {
Expand Down
2 changes: 2 additions & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl frame_system::Trait for Test {
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = Contracts;
type RootDispatcher = ();
}
impl pallet_balances::Trait for Test {
type Balance = u64;
Expand Down Expand Up @@ -174,6 +175,7 @@ impl Trait for Test {
type MaxDepth = MaxDepth;
type MaxValueSize = MaxValueSize;
type BlockGasLimit = BlockGasLimit;
type Dispatcher = sp_runtime::SimpleDispatcher;
}

type Balances = pallet_balances::Module<Test>;
Expand Down
16 changes: 13 additions & 3 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@

use sp_std::prelude::*;
use sp_runtime::{
DispatchResult, DispatchError, traits::{Zero, EnsureOrigin, Hash, Dispatchable, Saturating},
DispatchResult, DispatchError, traits::{
Zero, EnsureOrigin, Hash, Dispatchable, Dispatcher, Saturating
},

};
use codec::{Ref, Decode};
use frame_support::{
Expand Down Expand Up @@ -270,6 +273,9 @@ pub trait Trait: frame_system::Trait + Sized {

/// Handler for the unbalanced reduction when slashing a preimage deposit.
type Slash: OnUnbalanced<NegativeImbalanceOf<Self>>;

/// The means of dispatching the proposals.
type Dispatcher: Dispatcher<Self::Proposal, Self::Origin>;
}

decl_storage! {
Expand Down Expand Up @@ -1539,7 +1545,11 @@ impl<T: Trait> Module<T> {
let _ = T::Currency::unreserve(&who, amount);
Self::deposit_event(RawEvent::PreimageUsed(proposal_hash, who, amount));

let ok = proposal.dispatch(frame_system::RawOrigin::Root.into()).is_ok();
let ok = T::Dispatcher::dispatch(
proposal,
frame_system::RawOrigin::Root.into()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
frame_system::RawOrigin::Root.into()
frame_system::RawOrigin::Root.into(),

)
.is_ok();
Self::deposit_event(RawEvent::Executed(index, ok));

Ok(())
Expand Down Expand Up @@ -1663,4 +1673,4 @@ impl<T: Trait> Module<T> {
}
Ok(())
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

2 changes: 2 additions & 0 deletions frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl frame_system::Trait for Test {
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
Expand Down Expand Up @@ -152,6 +153,7 @@ impl super::Trait for Test {
type Slash = ();
type InstantOrigin = EnsureSignedBy<Six, u64>;
type InstantAllowed = InstantAllowed;
type Dispatcher = sp_runtime::SimpleDispatcher;
}

fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
1 change: 1 addition & 0 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ mod tests {
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions frame/elections/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl frame_system::Trait for Test {
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions frame/example-offchain-worker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl frame_system::Trait for Test {
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}

type Extrinsic = TestXt<Call<Test>, ()>;
Expand Down
1 change: 1 addition & 0 deletions frame/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ mod tests {
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type RootDispatcher = ();
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
Expand Down
Loading