mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 03:27:58 +00:00
Some metadata cleanup and improvements (#857)
* Do not encode the `origin` parameter in the metadata * Remove obsolete macro * Encode outer dispatch in metadata
This commit is contained in:
@@ -25,7 +25,7 @@ use serde;
|
||||
pub use codec::{Codec, Decode, Encode, Input, Output};
|
||||
pub use substrate_metadata::{
|
||||
ModuleMetadata, FunctionMetadata, DecodeDifferent,
|
||||
CallMetadata, FunctionArgumentMetadata
|
||||
CallMetadata, FunctionArgumentMetadata, OuterDispatchMetadata, OuterDispatchCall
|
||||
};
|
||||
|
||||
pub type Result = result::Result<(), &'static str>;
|
||||
@@ -394,15 +394,13 @@ pub trait IsSubType<T: Callable> {
|
||||
/// Implement a meta-dispatch module to dispatch to other dispatchers.
|
||||
#[macro_export]
|
||||
macro_rules! impl_outer_dispatch {
|
||||
() => ();
|
||||
(
|
||||
$(#[$attr:meta])*
|
||||
pub enum $call_type:ident where origin: $origin:ty {
|
||||
pub enum $call_type:ident for $runtime:ident where origin: $origin:ty {
|
||||
$(
|
||||
$camelcase:ident,
|
||||
$module:ident::$camelcase:ident,
|
||||
)*
|
||||
}
|
||||
$( $rest:tt )*
|
||||
) => {
|
||||
$(#[$attr])*
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
@@ -435,7 +433,7 @@ macro_rules! impl_outer_dispatch {
|
||||
}
|
||||
}
|
||||
)*
|
||||
impl_outer_dispatch!{ $($rest)* }
|
||||
__impl_outer_dispatch_metadata!($runtime; $call_type; $( $module::$camelcase, )*);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,11 +456,54 @@ macro_rules! __impl_outer_dispatch_common {
|
||||
__impl_encode!(dest; *self; 0; $call_type; $( fn $camelcase( outer_dispatch_param ); )*)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement the `json_metadata` function.
|
||||
/// Implement metadata for outer dispatch.
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! __impl_outer_dispatch_metadata {
|
||||
(
|
||||
$runtime:ident;
|
||||
$outer_name:ident;
|
||||
$( $module:ident::$call:ident, )*
|
||||
) => {
|
||||
impl $runtime {
|
||||
pub fn outer_dispatch_metadata() -> $crate::dispatch::OuterDispatchMetadata {
|
||||
$crate::dispatch::OuterDispatchMetadata {
|
||||
name: $crate::dispatch::DecodeDifferent::Encode(stringify!($outer_name)),
|
||||
calls: __impl_outer_dispatch_metadata!(@encode_calls 0; ; $( $module::$call, )*),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
(@encode_calls
|
||||
$index:expr;
|
||||
$( $encoded_call:expr ),*;
|
||||
$module:ident::$call:ident,
|
||||
$( $rest_module:ident::$rest:ident, )*
|
||||
) => {
|
||||
__impl_outer_dispatch_metadata!(
|
||||
@encode_calls
|
||||
$index + 1;
|
||||
$( $encoded_call, )*
|
||||
$crate::dispatch::OuterDispatchCall {
|
||||
name: $crate::dispatch::DecodeDifferent::Encode(stringify!($call)),
|
||||
prefix: $crate::dispatch::DecodeDifferent::Encode(stringify!($module)),
|
||||
index: $index,
|
||||
};
|
||||
$( $rest_module::$rest, )*
|
||||
)
|
||||
};
|
||||
(@encode_calls
|
||||
$index:expr;
|
||||
$( $encoded_call:expr ),*;
|
||||
) => {
|
||||
$crate::dispatch::DecodeDifferent::Encode(&[ $( $encoded_call ),* ])
|
||||
};
|
||||
}
|
||||
|
||||
/// Implement metadata for dispatch.
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! __dispatch_impl_metadata {
|
||||
@@ -499,7 +540,7 @@ macro_rules! __call_to_metadata {
|
||||
$crate::dispatch::CallMetadata {
|
||||
name: $crate::dispatch::DecodeDifferent::Encode(stringify!($call_type)),
|
||||
functions: __functions_to_metadata!(0; $origin_type;; $(
|
||||
fn $fn_name($from $(, $param_name: $param )* ) -> $result;
|
||||
fn $fn_name( $( $param_name: $param ),* ) -> $result;
|
||||
$( $doc_attr ),*;
|
||||
)*),
|
||||
}
|
||||
@@ -510,15 +551,14 @@ macro_rules! __call_to_metadata {
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! __functions_to_metadata{
|
||||
// ROOT
|
||||
(
|
||||
$fn_id:expr;
|
||||
$origin_type:ty;
|
||||
$( $function_metadata:expr ),*;
|
||||
fn $fn_name:ident(root
|
||||
fn $fn_name:ident(
|
||||
$(
|
||||
, $param_name:ident : $param:ty
|
||||
)*
|
||||
$param_name:ident : $param:ty
|
||||
),*
|
||||
) -> $result:ty;
|
||||
$( $fn_doc:expr ),*;
|
||||
$( $rest:tt )*
|
||||
@@ -531,31 +571,6 @@ macro_rules! __functions_to_metadata{
|
||||
$($rest)*
|
||||
)
|
||||
};
|
||||
// NON ROOT
|
||||
(
|
||||
$fn_id:expr;
|
||||
$origin_type:ty;
|
||||
$( $function_metadata:expr ),*;
|
||||
fn $fn_name:ident(origin
|
||||
$(
|
||||
, $param_name:ident : $param:ty
|
||||
)*
|
||||
) -> $result:ty;
|
||||
$( $fn_doc:expr ),*;
|
||||
$($rest:tt)*
|
||||
) => {
|
||||
__functions_to_metadata!(
|
||||
$fn_id + 1; $origin_type;
|
||||
$( $function_metadata, )* __function_to_metadata!(
|
||||
fn $fn_name(
|
||||
origin: $origin_type
|
||||
$( ,$param_name : $param )*
|
||||
) -> $result; $( $fn_doc ),*; $fn_id;
|
||||
);
|
||||
$($rest)*
|
||||
)
|
||||
};
|
||||
// BASE CASE
|
||||
(
|
||||
$fn_id:expr;
|
||||
$origin_type:ty;
|
||||
@@ -590,41 +605,6 @@ macro_rules! __function_to_metadata {
|
||||
documentation: $crate::dispatch::DecodeDifferent::Encode(&[ $( $fn_doc ),* ]),
|
||||
}
|
||||
};
|
||||
(
|
||||
fn $fn_name:ident() -> $result:ty;
|
||||
$( $fn_doc:expr ),*;
|
||||
$fn_id:expr;
|
||||
) => {
|
||||
$crate::dispatch::FunctionMetadata {
|
||||
id: $fn_id,
|
||||
name: $crate::dispatch::DecodeDifferent::Encode(stringify!($fn_name)),
|
||||
arguments: $crate::dispatch::DecodeDifferent::Encode(&[]),
|
||||
documentation: $crate::dispatch::DecodeDifferent::Encode(&[ $( $fn_doc ),* ]),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Convert a function documentation attribute into its JSON representation.
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! __function_doc_to_json {
|
||||
(
|
||||
$prefix_str:tt;
|
||||
$doc_attr:tt
|
||||
$($rest:tt)*
|
||||
) => {
|
||||
concat!(
|
||||
$prefix_str, r#" ""#,
|
||||
$doc_attr,
|
||||
r#"""#,
|
||||
__function_doc_to_json!(","; $($rest)*)
|
||||
)
|
||||
};
|
||||
(
|
||||
$prefix_str:tt;
|
||||
) => {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -664,12 +644,7 @@ mod tests {
|
||||
FunctionMetadata {
|
||||
id: 0,
|
||||
name: DecodeDifferent::Encode("aux_0"),
|
||||
arguments: DecodeDifferent::Encode(&[
|
||||
FunctionArgumentMetadata {
|
||||
name: DecodeDifferent::Encode("origin"),
|
||||
ty: DecodeDifferent::Encode("T::Origin"),
|
||||
}
|
||||
]),
|
||||
arguments: DecodeDifferent::Encode(&[]),
|
||||
documentation: DecodeDifferent::Encode(&[
|
||||
" Hi, this is a comment."
|
||||
])
|
||||
@@ -678,10 +653,6 @@ mod tests {
|
||||
id: 1,
|
||||
name: DecodeDifferent::Encode("aux_1"),
|
||||
arguments: DecodeDifferent::Encode(&[
|
||||
FunctionArgumentMetadata {
|
||||
name: DecodeDifferent::Encode("origin"),
|
||||
ty: DecodeDifferent::Encode("T::Origin"),
|
||||
},
|
||||
FunctionArgumentMetadata {
|
||||
name: DecodeDifferent::Encode("data"),
|
||||
ty: DecodeDifferent::Encode("i32"),
|
||||
@@ -693,10 +664,6 @@ mod tests {
|
||||
id: 2,
|
||||
name: DecodeDifferent::Encode("aux_2"),
|
||||
arguments: DecodeDifferent::Encode(&[
|
||||
FunctionArgumentMetadata {
|
||||
name: DecodeDifferent::Encode("origin"),
|
||||
ty: DecodeDifferent::Encode("T::Origin"),
|
||||
},
|
||||
FunctionArgumentMetadata {
|
||||
name: DecodeDifferent::Encode("data"),
|
||||
ty: DecodeDifferent::Encode("i32"),
|
||||
|
||||
@@ -61,10 +61,10 @@ mod hashable;
|
||||
#[macro_use]
|
||||
pub mod event;
|
||||
#[macro_use]
|
||||
pub mod metadata;
|
||||
#[macro_use]
|
||||
mod origin;
|
||||
#[macro_use]
|
||||
pub mod metadata;
|
||||
#[macro_use]
|
||||
mod runtime;
|
||||
|
||||
pub use self::storage::{StorageVec, StorageList, StorageValue, StorageMap};
|
||||
@@ -72,7 +72,6 @@ pub use self::hashable::Hashable;
|
||||
pub use self::dispatch::{Parameter, Dispatchable, Callable, IsSubType};
|
||||
pub use runtime_io::print;
|
||||
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! fail {
|
||||
( $y:expr ) => {{
|
||||
|
||||
@@ -37,6 +37,7 @@ macro_rules! impl_runtime_metadata {
|
||||
$crate::metadata::RuntimeMetadata {
|
||||
outer_event: Self::outer_event_metadata(),
|
||||
modules: __runtime_modules_to_metadata!($runtime;; $( $rest )*),
|
||||
outer_dispatch: Self::outer_dispatch_metadata(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,14 +100,15 @@ mod tests {
|
||||
use super::*;
|
||||
use substrate_metadata::{
|
||||
EventMetadata, OuterEventMetadata, RuntimeModuleMetadata, CallMetadata, ModuleMetadata,
|
||||
StorageFunctionModifier, StorageFunctionType, FunctionMetadata, FunctionArgumentMetadata,
|
||||
StorageMetadata, StorageFunctionMetadata,
|
||||
StorageFunctionModifier, StorageFunctionType, FunctionMetadata,
|
||||
StorageMetadata, StorageFunctionMetadata, OuterDispatchMetadata, OuterDispatchCall
|
||||
};
|
||||
use codec::{Decode, Encode};
|
||||
|
||||
mod system {
|
||||
pub trait Trait {
|
||||
type Origin;
|
||||
type Origin: Into<Option<RawOrigin<Self::AccountId>>> + From<RawOrigin<Self::AccountId>>;
|
||||
type AccountId;
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
@@ -118,6 +120,24 @@ mod tests {
|
||||
SystemEvent,
|
||||
}
|
||||
);
|
||||
|
||||
#[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 event_module {
|
||||
@@ -163,16 +183,19 @@ mod tests {
|
||||
);
|
||||
|
||||
decl_module! {
|
||||
pub struct ModuleWithStorage<T: Trait> for enum Call where origin: T::Origin {}
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
trait Store for ModuleWithStorage<T: Trait> as TestStorage {
|
||||
trait Store for Module<T: Trait> as TestStorage {
|
||||
StorageMethod : u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type EventModule = event_module::Module<TestRuntime>;
|
||||
type EventModule2 = event_module2::Module<TestRuntime>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Deserialize, Serialize)]
|
||||
pub struct TestRuntime;
|
||||
|
||||
@@ -183,24 +206,36 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
impl_outer_origin! {
|
||||
pub enum Origin for TestRuntime {}
|
||||
}
|
||||
|
||||
impl_outer_dispatch! {
|
||||
pub enum Call for TestRuntime where origin: Origin {
|
||||
event_module::EventModule,
|
||||
event_module2::EventModule2,
|
||||
}
|
||||
}
|
||||
|
||||
impl event_module::Trait for TestRuntime {
|
||||
type Origin = u32;
|
||||
type Origin = Origin;
|
||||
type Balance = u32;
|
||||
}
|
||||
|
||||
impl event_module2::Trait for TestRuntime {
|
||||
type Origin = u32;
|
||||
type Origin = Origin;
|
||||
type Balance = u32;
|
||||
}
|
||||
|
||||
impl system::Trait for TestRuntime {
|
||||
type Origin = u32;
|
||||
type Origin = Origin;
|
||||
type AccountId = u32;
|
||||
}
|
||||
|
||||
impl_runtime_metadata!(
|
||||
for TestRuntime with modules
|
||||
event_module::Module,
|
||||
event_module2::ModuleWithStorage with Storage,
|
||||
event_module2::Module with Storage,
|
||||
);
|
||||
|
||||
const EXPECTED_METADATA: RuntimeMetadata = RuntimeMetadata {
|
||||
@@ -251,12 +286,7 @@ mod tests {
|
||||
FunctionMetadata {
|
||||
id: 0,
|
||||
name: DecodeDifferent::Encode("aux_0"),
|
||||
arguments: DecodeDifferent::Encode(&[
|
||||
FunctionArgumentMetadata {
|
||||
name: DecodeDifferent::Encode("origin"),
|
||||
ty: DecodeDifferent::Encode("T::Origin"),
|
||||
}
|
||||
]),
|
||||
arguments: DecodeDifferent::Encode(&[]),
|
||||
documentation: DecodeDifferent::Encode(&[]),
|
||||
}
|
||||
])
|
||||
@@ -269,7 +299,7 @@ mod tests {
|
||||
prefix: DecodeDifferent::Encode("event_module2"),
|
||||
module: DecodeDifferent::Encode(FnEncode(||
|
||||
ModuleMetadata {
|
||||
name: DecodeDifferent::Encode("ModuleWithStorage"),
|
||||
name: DecodeDifferent::Encode("Module"),
|
||||
call: CallMetadata {
|
||||
name: DecodeDifferent::Encode("Call"),
|
||||
functions: DecodeDifferent::Encode(&[])
|
||||
@@ -290,7 +320,22 @@ mod tests {
|
||||
}
|
||||
))),
|
||||
}
|
||||
])
|
||||
]),
|
||||
outer_dispatch: OuterDispatchMetadata {
|
||||
name: DecodeDifferent::Encode("Call"),
|
||||
calls: DecodeDifferent::Encode(&[
|
||||
OuterDispatchCall {
|
||||
name: DecodeDifferent::Encode("EventModule"),
|
||||
prefix: DecodeDifferent::Encode("event_module"),
|
||||
index: 0,
|
||||
},
|
||||
OuterDispatchCall {
|
||||
name: DecodeDifferent::Encode("EventModule2"),
|
||||
prefix: DecodeDifferent::Encode("event_module2"),
|
||||
index: 1,
|
||||
}
|
||||
])
|
||||
}
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -655,8 +655,8 @@ macro_rules! __decl_outer_dispatch {
|
||||
;
|
||||
) => {
|
||||
impl_outer_dispatch!(
|
||||
pub enum Call where origin: Origin {
|
||||
$( $parsed_name, )*
|
||||
pub enum Call for $runtime where origin: Origin {
|
||||
$( $parsed_modules::$parsed_name, )*
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user