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

Dispatch Macro Enhancements #681

@gavofyork

Description

@gavofyork
  • 1. Introduce ability to not have origin parameter at all (dispatch pre-executes system::ensure_root(origin)?)
  • 2. Introduce logic for arbitrary origin matching in the macro

Notes

  1. fn definitions in decl_module that do not include an origin term should be assumed to be privileged and automatically ensure! that origin == Root.

Before:

pub struct Module<T: Trait>;
pub enum Call where aux: T::PublicAux {
	fn propose_spend(aux, value: T::Balance, beneficiary: T::AccountId) -> Result = 0;
}
pub enum PrivCall {
	fn set_pot(new_pot: T::Balance) -> Result = 0;
}

After:

pub struct Module<T: Trait> where origin: T::Origin {
	fn propose_spend(origin, value: T::Balance, beneficiary: T::AccountId) -> Result = 0;
	fn set_pot(new_pot: T::Balance) -> Result = 1;
}
  1. This can be done by ensuring that the impl_dispatch places the ensure! in the match dispatcher before dispatching in the cases where no origin is provided. A more sophisticated macro would allow origin to be required to match a particular value:

Before:

use {T::Origin::council as CouncilOrigin, council::Origin::Members};
use {T::Origin::system as SystemOrigin, system::Origin::Signed};
pub struct Module<T: Trait> where origin: T::Origin {
	fn propose(origin, value: T::Balance, beneficiary: T::AccountId) -> Result = 0;
	fn accept(origin, index: ProposalIndex) -> Result = 1;
}
impl<T: Trait> Module<T> {
	fn propose(origin: T::Origin, index: ProposalIndex) -> Result {
		match origin {
			SystemOrigin(Signed(&who)) => {
				ensure!(Self::check_account(who), "not a good account");
				Self::do_some_stuff(who)
			}
			_ => return Err("invalid origin"),
		}
	}
	fn accept(origin: T::Origin, index: ProposalIndex) -> Result {
		match origin {
			CouncilOrigin(Members(n)) if n >= 2 => {
				// ...
			}
			_ => return Err("invalid origin"),
		}
	}
}

After:

use {T::Origin::council as CouncilOrigin, council::Origin::Members};
pub struct Module<T: Trait> where origin: T::Origin {
	fn propose(SystemOrigin(Signed(who)), value: T::Balance, beneficiary: T::AccountId) -> Result = 0;
	fn accept(CouncilOrigin(Members(n)) if n >= 2, index: ProposalIndex) -> Result = 1;
}
impl<T: Trait> Module<T> {
	fn propose(who: &T::AccountId, index: ProposalIndex) -> Result {
		ensure!(Self::check_account(who), "not a good account");
		Self::do_some_stuff(who)
	}
	fn accept(index: ProposalIndex) -> Result {
		// ...
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    J0-enhancementAn additional feature request.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions