Files
pezkuwi-subxt/substrate/srml/support/src/origin.rs
T
thiolliere d743a8b71f Implement parameterisable modules (#1800)
* first implementation

* remove done comment

* origin done

* impl log for instance

* impl inherent for instance

* Fix wasm build + full example build

this requires parity codec implements codec for core::marker::PhantomData

* patch parity-codec link to github branch

* improve internal names and fix instance prefix

* Fix in macros

* add test modules for support

this allow to test for construct_runtime as well.

The reason to have put that in another crate is:
* if we put test in `tests/` dir of srml/support then decl_storage fails to get
  srml-support access because it believes it is inside srml-support
  crate and so derive access to `quote!{ crate }` but this is wrong
  (and I don't see any way to prevent that, and it only bother us so I
  don't think that matters that much)
* if we put test inside lib.rs then contruct_runtime cannot be used
  because it call some macros that are defined with macros
  (decl_outer_event and decl_outer_origin) and thus rustc complains.

* defaultinstance to its own struct to avoid errors

* enforce <T, I> for Event and Config, impl test

* add origin, log, inherent to test

* test more code generation

* basic storage test

* fix typo

* rename a few imports and field

* delete wip test in example and runtime

* change default prefix to make it backward compatible with test

* rename Instance to I and Instantiable to Instance

note: the name of generic parameter I is only enforce by decl_module!
and this could be rewritten

* doc

* clean old TODOs

* update parity-codec to 3.2

* update node impl version + builds

* fix warning

* fix unrelated grandpa test

* refactor code
2019-03-15 19:25:18 +01:00

225 lines
5.7 KiB
Rust

// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Macros that define an Origin type. Every function call to your runtime has an origin which
//! specifies where the extrinsic was generated from.
/// Constructs an Origin type for a runtime. This is usually called automatically by the
/// construct_runtime macro. See also __create_decl_macro.
#[macro_export]
macro_rules! impl_outer_origin {
// Macro transformations (to convert invocations with incomplete parameters to the canonical
// form)
(
$(#[$attr:meta])*
pub enum $name:ident for $runtime:ident {
$( $module:ident $( <$generic:ident $(, $instance:path )? > )? ),* $(,)?
}
) => {
$crate::impl_outer_origin! {
$(#[$attr])*
pub enum $name for $runtime where system = system {
$( $module $( <$generic $(, $instance )? > )?, )*
}
}
};
(
$(#[$attr:meta])*
pub enum $name:ident for $runtime:ident where system = $system:ident {
$( $module:ident $( <$generic:ident $(, $instance:path )?> )? ),* $(,)?
}
) => {
$crate::impl_outer_origin!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $module $( <$generic $(, $instance )? > )*, )* };
);
};
// Replace generic param with runtime
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident $( <T $(, $instance:path )? > )?,
$( $rest_module:tt )*
};
$( $parsed:tt )*
) => {
$crate::impl_outer_origin!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $rest_module )* };
$( $parsed )* $module $( <$runtime $(, $instance )? > )?,
);
};
// The main macro expansion that actually renders the Origin enum code.
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules { };
$( $module:ident $( <$generic_param:ident $(, $generic_instance:path )? > )* ,)*
) => {
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
$(#[$attr])*
#[allow(non_camel_case_types)]
pub enum $name {
system($system::Origin<$runtime>),
$(
$module($module::Origin $( <$generic_param $(, $generic_instance )? > )* ),
)*
#[allow(dead_code)]
Void($crate::Void)
}
#[allow(dead_code)]
impl $name {
pub const INHERENT: Self = $name::system($system::RawOrigin::Inherent);
pub const ROOT: Self = $name::system($system::RawOrigin::Root);
pub fn signed(by: <$runtime as $system::Trait>::AccountId) -> Self {
$name::system($system::RawOrigin::Signed(by))
}
}
impl From<$system::Origin<$runtime>> for $name {
fn from(x: $system::Origin<$runtime>) -> Self {
$name::system(x)
}
}
impl Into<Option<$system::Origin<$runtime>>> for $name {
fn into(self) -> Option<$system::Origin<$runtime>> {
if let $name::system(l) = self {
Some(l)
} else {
None
}
}
}
impl From<Option<<$runtime as $system::Trait>::AccountId>> for $name {
fn from(x: Option<<$runtime as $system::Trait>::AccountId>) -> Self {
<$system::Origin<$runtime>>::from(x).into()
}
}
$(
impl From<$module::Origin $( <$generic_param $(, $generic_instance )? > )*> for $name {
fn from(x: $module::Origin $( <$generic_param $(, $generic_instance )? > )*) -> Self {
$name::$module(x)
}
}
impl Into<Option<$module::Origin $( <$generic_param $(, $generic_instance )? > )*>> for $name {
fn into(self) -> Option<$module::Origin $( <$generic_param $(, $generic_instance )? > )*> {
if let $name::$module(l) = self {
Some(l)
} else {
None
}
}
}
)*
}
}
#[cfg(test)]
mod tests {
mod system {
pub trait Trait {
type AccountId;
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum RawOrigin<AccountId> {
Root,
Signed(AccountId),
Inherent,
}
impl<AccountId> From<Option<AccountId>> for RawOrigin<AccountId> {
fn from(s: Option<AccountId>) -> RawOrigin<AccountId> {
match s {
Some(who) => RawOrigin::Signed(who),
None => RawOrigin::Inherent,
}
}
}
pub type Origin<T> = RawOrigin<<T as Trait>::AccountId>;
}
mod origin_without_generic {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Origin;
}
mod origin_with_generic {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Origin<T> {
t: T
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TestRuntime;
impl system::Trait for TestRuntime {
type AccountId = u32;
}
impl_outer_origin!(
pub enum OriginWithoutSystem for TestRuntime {
origin_without_generic,
origin_with_generic<T>,
}
);
impl_outer_origin!(
pub enum OriginWithoutSystem2 for TestRuntime {
origin_with_generic<T>,
origin_without_generic
}
);
impl_outer_origin!(
pub enum OriginWithSystem for TestRuntime where system = system {
origin_without_generic,
origin_with_generic<T>
}
);
impl_outer_origin!(
pub enum OriginWithSystem2 for TestRuntime where system = system {
origin_with_generic<T>,
origin_without_generic,
}
);
impl_outer_origin!(
pub enum OriginEmpty for TestRuntime where system = system {}
);
}