Make constants exposable in metadata (#2975)

* Some cleanup

* Add module constant metadata declaration

* Begin to integrate the constants in `decl_module`

* Fixes tests

* Fix compilation and add tests

* Remove duplicate code

* Expose constants in democracy and staking + further fixes

* Update srml/metadata/src/lib.rs

Co-Authored-By: YJ <yjkimjunior@gmail.com>

* Hide `RawEvent` metadata function

* Prevent whitespaces in types

* Fix `offchain_worker` and `constants` with instances

* Up the `impl_version`

* Fix macro

* Incrase impl_version
This commit is contained in:
Bastian Köcher
2019-07-01 10:05:28 +02:00
committed by GitHub
parent 5f1538b834
commit 7202403bfc
12 changed files with 858 additions and 580 deletions
+223 -34
View File
@@ -17,12 +17,14 @@
//! Dispatch system. Contains a macro for defining runtime modules and
//! generating values representing lazy module function calls.
pub use crate::rstd::prelude::{Vec, Clone, Eq, PartialEq};
pub use crate::rstd::{result, prelude::{Vec, Clone, Eq, PartialEq}, marker};
#[cfg(feature = "std")]
pub use std::fmt;
pub use crate::rstd::result;
pub use crate::codec::{Codec, Decode, Encode, Input, Output, HasCompact, EncodeAsRef};
pub use srml_metadata::{FunctionMetadata, DecodeDifferent, DecodeDifferentArray, FunctionArgumentMetadata};
pub use srml_metadata::{
FunctionMetadata, DecodeDifferent, DecodeDifferentArray, FunctionArgumentMetadata,
ModuleConstantMetadata, DefaultByte, DefaultByteGetter,
};
pub use sr_primitives::weights::{TransactionWeight, Weighable, Weight};
/// A type that cannot be instantiated.
@@ -217,6 +219,7 @@ macro_rules! decl_module {
{}
{}
{}
{}
[]
$($t)*
);
@@ -237,6 +240,7 @@ macro_rules! decl_module {
{}
{}
{}
{}
[]
$($t)*
);
@@ -251,7 +255,8 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
$vis:vis fn deposit_event $(<$dpeg:ident $(, $dpeg_instance:ident)?>)* () = default;
$($rest:tt)*
@@ -264,7 +269,8 @@ macro_rules! decl_module {
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
[ $($t)* ]
{ $( $constants )* }
[ $( $dispatchables )* ]
$($rest)*
);
};
@@ -276,7 +282,8 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
$vis:vis fn deposit_event $(<$dpeg:ident $(, $dpeg_instance:ident)?>)* (
$($param_name:ident : $param:ty),*
@@ -291,7 +298,8 @@ macro_rules! decl_module {
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
[ $($t)* ]
{ $( $constants )* }
[ $( $dispatchables )* ]
$($rest)*
);
};
@@ -303,7 +311,8 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{}
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
fn on_finalize($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
$($rest:tt)*
@@ -316,7 +325,8 @@ macro_rules! decl_module {
{ $( $on_initialize )* }
{ fn on_finalize( $( $param_name : $param ),* ) { $( $impl )* } }
{ $( $offchain )* }
[ $($t)* ]
{ $( $constants )* }
[ $( $dispatchables )* ]
$($rest)*
);
};
@@ -328,7 +338,8 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{}
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
fn on_finalise($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
$($rest:tt)*
@@ -345,7 +356,8 @@ macro_rules! decl_module {
{}
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
fn on_initialize($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
$($rest:tt)*
@@ -358,7 +370,8 @@ macro_rules! decl_module {
{ fn on_initialize( $( $param_name : $param ),* ) { $( $impl )* } }
{ $( $on_finalize )* }
{ $( $offchain )* }
[ $($t)* ]
{ $( $constants )* }
[ $( $dispatchables )* ]
$($rest)*
);
};
@@ -370,7 +383,8 @@ macro_rules! decl_module {
{}
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
fn on_initialise($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
$($rest:tt)*
@@ -381,42 +395,90 @@ macro_rules! decl_module {
};
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>
pub struct $mod_type:ident<
$trait_instance:ident: $trait_name:ident
$(<I>, I: $instantiable:path $(= $module_default_instance:path)?)?
>
for enum $call_type:ident where origin: $origin_type:ty, system = $system:ident
{ $( $deposit_event:tt )* }
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
fn offchain_worker($($param_name:ident : $param:ty),* ) { $( $impl:tt )* }
$($rest:tt)*
) => {
$crate::decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>
pub struct $mod_type<
$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?
>
for enum $call_type where origin: $origin_type, system = $system
{ $( $deposit_event )* }
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ fn offchain_worker( $( $param_name : $param ),* ) { $( $impl )* } }
[ $($t)* ]
{ $( $constants )* }
[ $( $dispatchables )* ]
$($rest)*
);
};
// This puts a constant in the parsed constants list.
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<
$trait_instance:ident: $trait_name:ident
$(<I>, $instance:ident: $instantiable:path $(= $module_default_instance:path)?)?
>
for enum $call_type:ident where origin: $origin_type:ty, system = $system:ident
{ $( $deposit_event:tt )* }
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$( #[doc = $doc_attr:tt] )*
const $name:ident: $ty:ty = $value:expr;
$( $rest:tt )*
) => {
$crate::decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<
$trait_instance: $trait_name
$( <I>, $instance: $instantiable $(= $module_default_instance)? )?
>
for enum $call_type where origin: $origin_type, system = $system
{ $( $deposit_event )* }
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
{
$( $constants )*
$( #[doc = $doc_attr ] )*
$name: $ty = $value;
}
[ $( $dispatchables )* ]
$($rest)*
);
};
// This puts the function statement into the [], decreasing `$rest` and moving toward finishing the parse.
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<
$trait_instance:ident:
$trait_name:ident$(<I>, $instance:ident: $instantiable:path $(= $module_default_instance:path)?)?
$trait_instance:ident: $trait_name:ident
$(<I>, $instance:ident: $instantiable:path $(= $module_default_instance:path)?)?
>
for enum $call_type:ident where origin: $origin_type:ty, system = $system:ident
{ $( $deposit_event:tt )* }
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
#[weight = $weight:expr]
$fn_vis:vis fn $fn_name:ident(
@@ -434,8 +496,9 @@ macro_rules! decl_module {
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
{ $( $constants )* }
[
$($t)*
$( $dispatchables )*
$(#[doc = $doc_attr])*
#[weight = $weight]
$fn_vis fn $fn_name(
@@ -458,7 +521,8 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
$fn_vis:vis fn $fn_name:ident(
$from:ident $(, $(#[$codec_attr:ident])* $param_name:ident : $param:ty)*
@@ -475,7 +539,8 @@ macro_rules! decl_module {
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
[ $($t)* ]
{ $( $constants )* }
[ $( $dispatchables )* ]
$(#[doc = $doc_attr])*
#[weight = $crate::dispatch::TransactionWeight::default()]
$fn_vis fn $fn_name(
@@ -493,7 +558,8 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
$(#[weight = $weight:expr])?
$fn_vis:vis fn $fn_name:ident(
@@ -516,7 +582,8 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
$(#[weight = $weight:expr])?
$fn_vis:vis fn $fn_name:ident(
@@ -539,7 +606,8 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
$(#[weight = $weight:expr])?
$fn_vis:vis fn $fn_name:ident(
@@ -555,7 +623,8 @@ macro_rules! decl_module {
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
[ $($t)* ]
{ $( $constants )* }
[ $( $dispatchables )* ]
$(#[doc = $doc_attr])*
$(#[weight = $weight])?
@@ -575,18 +644,20 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
[ $($t:tt)* ]
{ $( $constants:tt )* }
[ $( $dispatchables:tt )* ]
) => {
$crate::decl_module!(@imp
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?>
for enum $call_type where origin: $origin_type, system = $system {
$($t)*
$( $dispatchables )*
}
{ $( $deposit_event )* }
{ $( $on_initialize )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
{ $( $constants )* }
);
};
@@ -939,10 +1010,9 @@ macro_rules! decl_module {
{ $( $on_initialize:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
{ $( $constants:tt )* }
) => {
$crate::__check_reserved_fn_name! {
$($fn_name)*
}
$crate::__check_reserved_fn_name! { $( $fn_name )* }
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -1068,7 +1138,10 @@ macro_rules! decl_module {
impl<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?> $crate::dispatch::fmt::Debug
for $call_type<$trait_instance $(, $instance)?>
{
fn fmt(&self, _f: &mut $crate::dispatch::fmt::Formatter) -> $crate::dispatch::result::Result<(), $crate::dispatch::fmt::Error> {
fn fmt(
&self,
_f: &mut $crate::dispatch::fmt::Formatter,
) -> $crate::dispatch::result::Result<(), $crate::dispatch::fmt::Error> {
match *self {
$(
$call_type::$fn_name( $( ref $param_name ),* ) =>
@@ -1110,7 +1183,10 @@ macro_rules! decl_module {
impl<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?> $mod_type<$trait_instance $(, $instance)?> {
#[doc(hidden)]
pub fn dispatch<D: $crate::dispatch::Dispatchable<Trait = $trait_instance>>(d: D, origin: D::Origin) -> $crate::dispatch::Result {
pub fn dispatch<D: $crate::dispatch::Dispatchable<Trait = $trait_instance>>(
d: D,
origin: D::Origin,
) -> $crate::dispatch::Result {
d.dispatch(origin)
}
}
@@ -1118,6 +1194,10 @@ macro_rules! decl_module {
$mod_type<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?> $call_type $origin_type
{$( $(#[doc = $doc_attr])* fn $fn_name($from $(, $(#[$codec_attr])* $param_name : $param )*); )*}
}
$crate::__impl_module_constants_metadata ! {
$mod_type<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?>
$( $constants )*
}
}
}
@@ -1191,6 +1271,115 @@ macro_rules! __dispatch_impl_metadata {
}
}
/// Implement metadata for module constants.
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_module_constants_metadata {
// Without instance
(
$mod_type:ident<$trait_instance:ident: $trait_name:ident>
$(
$( #[doc = $doc_attr:tt] )*
$name:ident: $type:ty = $value:expr;
)*
) => {
$crate::paste::item! {
$crate::__impl_module_constants_metadata! {
GENERATE_CODE
$mod_type<$trait_instance: $trait_name>
$(
$( #[doc = $doc_attr] )*
[< $name DefaultByteGetter >]
$name<$trait_instance: $trait_name>: $type = $value;
)*
}
}
};
// With instance
(
$mod_type:ident<$trait_instance:ident: $trait_name:ident<I>, $instance:ident: $instantiable:path>
$(
$( #[doc = $doc_attr:tt] )*
$name:ident: $type:ty = $value:expr;
)*
) => {
$crate::paste::item! {
$crate::__impl_module_constants_metadata! {
GENERATE_CODE
$mod_type<$trait_instance: $trait_name<I>, $instance: $instantiable>
$(
$( #[doc = $doc_attr] )*
[< $name DefaultByteGetter >]
$name<$trait_instance: $trait_name<I>, $instance: $instantiable>: $type = $value;
)*
}
}
};
// Do the code generation
(GENERATE_CODE
$mod_type:ident<$trait_instance:ident: $trait_name:ident $(<I>, $instance:ident: $instantiable:path)?>
$(
$( #[doc = $doc_attr:tt] )*
$default_byte_name:ident
$name:ident<
$const_trait_instance:ident: $const_trait_name:ident $(
<I>, $const_instance:ident: $const_instantiable:path
)*
>: $type:ty = $value:expr;
)*
) => {
impl<$trait_instance: 'static + $trait_name $(<I>, $instance: $instantiable)?>
$mod_type<$trait_instance $(, $instance)?>
{
#[doc(hidden)]
pub fn module_constants_metadata() -> &'static [$crate::dispatch::ModuleConstantMetadata] {
// Create the `ByteGetter`s
$(
#[allow(non_upper_case_types)]
#[allow(non_camel_case_types)]
struct $default_byte_name<
$const_trait_instance: $const_trait_name $(
<I>, $const_instance: $const_instantiable
)?
>($crate::dispatch::marker::PhantomData<
($const_trait_instance $(, $const_instance)?)
>);
impl<$const_trait_instance: 'static + $const_trait_name $(
<I>, $const_instance: $const_instantiable)?
> $crate::dispatch::DefaultByte
for $default_byte_name <$const_trait_instance $(, $const_instance)?>
{
fn default_byte(&self) -> $crate::dispatch::Vec<u8> {
let value: $type = $value;
$crate::dispatch::Encode::encode(&value)
}
}
)*
&[
$(
$crate::dispatch::ModuleConstantMetadata {
name: $crate::dispatch::DecodeDifferent::Encode(stringify!($name)),
ty: $crate::dispatch::DecodeDifferent::Encode(stringify!($type)),
value: $crate::dispatch::DecodeDifferent::Encode(
$crate::dispatch::DefaultByteGetter(
&$default_byte_name::<
$const_trait_instance $(, $const_instance)?
>(
$crate::dispatch::marker::PhantomData
)
)
),
documentation: $crate::dispatch::DecodeDifferent::Encode(
&[ $( $doc_attr ),* ]
),
}
),*
]
}
}
}
}
/// Convert the list of calls into their JSON representation, joined by ",".
#[macro_export]
#[doc(hidden)]