Rewrite impl_runtime_apis! and decl_runtime_apis! as proc-macro (#1174)

* Rewrites `impl_runtime_apis!` macro as `proc-macro`

* Adds some documentation

* Require the `impl_runtime_apis` to use a path for accessing the trait

* Make the runtime implement `GetNodeBlockType`

* Moves first chunk of runtime api code into the `impl_runtime_apis` macro

This also renames `ClientWithApi` into `RuntimeApi`.

* Make `impl_runtime_apis` use `runtime` api version automatically

* `decl_runtime_apis` automatically adds `Block: BlockT` as generic parameter

* Remove function generic arguments in block builder api

* Remove some unnused stuff from the `decl_runtime_apis` macro

* Make `InherentData` working again

* Make `impl_runtime_apis!` implement the `RuntimeApi` side as well

* Make it compile again after rebasing with master

* Split `sr-api-macros` into multiple files

* Reimplement `decl_runtime_apis!` as proc_macro

* Use `decl_runtime_apis!` for `Core` as well and improve error reporting

* Adds documentation for `decl_runtime_apis!` and `impl_runtime_apis!`

* Move some code

* Adds compile fail tests

* Adds a test and fixes some bugs

* Make `impl_runtime_apis!` support `_` as parameter name

* Fixes build errors with wasm

* Wasm rebuild after master rebase

* Apply suggestions from code review

Co-Authored-By: bkchr <bkchr@users.noreply.github.com>

* Addresses some grumbles

* Adds test to ensure that method signatures need to match

* New wasm files
This commit is contained in:
Bastian Köcher
2018-11-30 11:42:46 +01:00
committed by Gav Wood
parent 309f627d5c
commit ed421c56ee
40 changed files with 1863 additions and 1269 deletions
+19 -43
View File
@@ -17,7 +17,9 @@
#[doc(hidden)]
pub use rstd::{result::Result, vec::Vec};
#[doc(hidden)]
pub use runtime_primitives::traits::ProvideInherent;
pub use runtime_primitives::{
traits::{ProvideInherent, Block as BlockT}, CheckInherentError, InherentData
};
/// Implement the outer inherent.
@@ -39,50 +41,43 @@ pub use runtime_primitives::traits::ProvideInherent;
macro_rules! impl_outer_inherent {
(
$(#[$attr:meta])*
pub struct $name:ident where Block = $block:ident, UncheckedExtrinsic = $unchecked:ident {
$( $module:ident: $module_ty:ident $(export Error as $error_name:ident)*, )*
pub struct $name:ident where Block = $block:ident {
$( $module:ident: $module_ty:ident, )*
}
) => {
impl_outer_inherent!(
$( #[$attr] )*
pub struct $name where Block = $block, UncheckedExtrinsic = $unchecked, Error = InherentError, Call = Call {
$( $module: $module_ty $(export Error as $error_name)*, )*
pub struct $name where Block = $block, Call = Call {
$( $module: $module_ty, )*
}
);
};
(
$(#[$attr:meta])*
pub struct $name:ident where Block = $block:ident, UncheckedExtrinsic = $unchecked:ident, Error = $error:ident {
$( $module:ident: $module_ty:ident $(export Error as $error_name:ident)*, )*
pub struct $name:ident where Block = $block:ident {
$( $module:ident: $module_ty:ident, )*
}
) => {
impl_outer_inherent!(
$( #[$attr] )*
pub struct $name where Block = $block, UncheckedExtrinsic = $unchecked, Error = $error, Call = Call {
$( $module: $module_ty $(export Error as $error_name)*, )*
pub struct $name where Block = $block, Call = Call {
$( $module: $module_ty, )*
}
);
};
(
$(#[$attr:meta])*
pub struct $name:ident where Block = $block:ident, UncheckedExtrinsic = $unchecked:ident, Error = $error:ident, Call = $call:ident {
$( $module:ident: $module_ty:ident $(export Error as $error_name:ident)*, )*
pub struct $name:ident where Block = $block:ident, Call = $call:ident {
$( $module:ident: $module_ty:ident, )*
}
) => {
$( #[$attr] )*
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Encode, Decode)]
/// Inherent data to include in a block.
pub struct $name {
$( $module: <$module_ty as $crate::inherent::ProvideInherent>::Inherent, )*
}
$(
$(
pub type $error_name =<$module_ty as $crate::inherent::ProvideInherent>::Error;
)*
)*
impl $name {
/// Create a new instance.
pub fn new( $( $module: <$module_ty as $crate::inherent::ProvideInherent>::Inherent ),* ) -> Self {
@@ -91,38 +86,19 @@ macro_rules! impl_outer_inherent {
}
}
fn create_inherent_extrinsics(self) -> Vec<$unchecked> {
let mut inherent = $crate::inherent::Vec::new();
$(
inherent.extend(
<$module_ty as $crate::inherent::ProvideInherent>::create_inherent_extrinsics(self.$module)
.into_iter()
.map(|v| (v.0, $unchecked::new_unsigned($call::$module_ty(v.1))))
);
)*
inherent.as_mut_slice().sort_unstable_by_key(|v| v.0);
inherent.into_iter().map(|v| v.1).collect()
}
fn check_inherents(self, block: $block) -> $crate::inherent::Result<(), $error> {
fn check_inherents(
data: $crate::inherent::InherentData,
block: $block
) -> $crate::inherent::Result<(), $crate::inherent::CheckInherentError> {
$(
<$module_ty as $crate::inherent::ProvideInherent>::check_inherent(
&block, self.$module, &|xt| match xt.function {
&block, data.$module, &|xt| match xt.function {
Call::$module_ty(ref data) => Some(data),
_ => None,
}).map_err($error::$module_ty)?;
})?;
)*
Ok(())
}
}
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Encode)]
#[cfg_attr(feature = "std", derive(Decode))]
pub enum $error {
$( $module_ty(<$module_ty as $crate::inherent::ProvideInherent>::Error), )*
}
};
}
+1
View File
@@ -122,4 +122,5 @@ pub enum Void {}
pub use mashup::*;
#[cfg(feature = "std")]
#[doc(hidden)]
pub use serde_derive::*;
+23 -20
View File
@@ -20,7 +20,10 @@
///
/// ```nocompile
/// construct_runtime!(
/// pub enum Runtime with Log(interalIdent: DigestItem<SessionKey>) {
/// pub enum Runtime with Log(interalIdent: DigestItem<SessionKey>) where
/// Block = Block,
/// NodeBlock = runtime::Block
/// {
/// System: system,
/// Test: test::{default, Log(Test)},
/// Test2: test_with_long_module::{Module},
@@ -48,7 +51,9 @@
macro_rules! construct_runtime {
(
pub enum $runtime:ident with Log ($log_internal:ident: DigestItem<$( $log_genarg:ty ),+>)
where Block = $block:ident, UncheckedExtrinsic = $unchecked:ident
where
Block = $block:ident,
NodeBlock = $node_block:ty
{
$( $rest:tt )*
}
@@ -56,7 +61,7 @@ macro_rules! construct_runtime {
construct_runtime!(
$runtime;
$block;
$unchecked;
$node_block;
$log_internal < $( $log_genarg ),* >;
;
$( $rest )*
@@ -65,7 +70,7 @@ macro_rules! construct_runtime {
(
$runtime:ident;
$block:ident;
$unchecked:ident;
$node_block:ty;
$log_internal:ident <$( $log_genarg:ty ),+>;
$(
$expanded_name:ident: $expanded_module:ident::{
@@ -92,7 +97,7 @@ macro_rules! construct_runtime {
construct_runtime!(
$runtime;
$block;
$unchecked;
$node_block;
$log_internal < $( $log_genarg ),* >;
$(
$expanded_name: $expanded_module::{
@@ -119,7 +124,7 @@ macro_rules! construct_runtime {
(
$runtime:ident;
$block:ident;
$unchecked:ident;
$node_block:ty;
$log_internal:ident <$( $log_genarg:ty ),+>;
$(
$expanded_name:ident: $expanded_module:ident::{
@@ -153,7 +158,7 @@ macro_rules! construct_runtime {
construct_runtime!(
$runtime;
$block;
$unchecked;
$node_block;
$log_internal < $( $log_genarg ),* >;
$(
$expanded_name: $expanded_module::{
@@ -186,7 +191,7 @@ macro_rules! construct_runtime {
(
$runtime:ident;
$block:ident;
$unchecked:ident;
$node_block:ty;
$log_internal:ident <$( $log_genarg:ty ),+>;
$(
$expanded_name:ident: $expanded_module:ident::{
@@ -219,7 +224,7 @@ macro_rules! construct_runtime {
construct_runtime!(
$runtime;
$block;
$unchecked;
$node_block;
$log_internal < $( $log_genarg ),* >;
$(
$expanded_name: $expanded_module::{
@@ -251,7 +256,7 @@ macro_rules! construct_runtime {
(
$runtime:ident;
$block:ident;
$unchecked:ident;
$node_block:ty;
$log_internal:ident <$( $log_genarg:ty ),+>;
$(
$name:ident: $module:ident::{
@@ -273,6 +278,12 @@ macro_rules! construct_runtime {
#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct $runtime;
impl $crate::runtime_primitives::traits::GetNodeBlockType for $runtime {
type NodeBlock = $node_block;
}
impl $crate::runtime_primitives::traits::GetRuntimeBlockType for $runtime {
type RuntimeBlock = $block;
}
__decl_outer_event!(
$runtime;
$(
@@ -326,7 +337,6 @@ macro_rules! construct_runtime {
__decl_outer_inherent!(
$runtime;
$block;
$unchecked;
;
$(
$name: $module::{ $( $modules $( <$modules_generic> )* ),* }
@@ -1052,7 +1062,6 @@ macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$unchecked:ident;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$name:ident: $module:ident::{
Inherent $(, $modules:ident $( <$modules_generic:ident> )* )*
@@ -1064,7 +1073,6 @@ macro_rules! __decl_outer_inherent {
__decl_outer_inherent!(
$runtime;
$block;
$unchecked;
$( $parsed_modules :: $parsed_name, )* $module::$name;
$(
$rest_name: $rest_module::{
@@ -1076,7 +1084,6 @@ macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$unchecked:ident;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$name:ident: $module:ident::{
$ingore:ident $( <$ignor:ident> )* $(, $modules:ident $( <$modules_generic:ident> )* )*
@@ -1088,7 +1095,6 @@ macro_rules! __decl_outer_inherent {
__decl_outer_inherent!(
$runtime;
$block;
$unchecked;
$( $parsed_modules :: $parsed_name ),*;
$name: $module::{ $( $modules $( <$modules_generic> )* ),* }
$(
@@ -1101,7 +1107,6 @@ macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$unchecked:ident;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$name:ident: $module:ident::{}
$(, $rest_name:ident : $rest_module:ident::{
@@ -1111,7 +1116,6 @@ macro_rules! __decl_outer_inherent {
__decl_outer_inherent!(
$runtime;
$block;
$unchecked;
$( $parsed_modules :: $parsed_name ),*;
$(
$rest_name: $rest_module::{
@@ -1123,15 +1127,14 @@ macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$unchecked:ident;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
;
) => {
substrate_generate_ident_name! {
impl_outer_inherent!(
pub struct InherentData where Block = $block, UncheckedExtrinsic = $unchecked {
pub struct InherentData where Block = $block {
$(
$parsed_modules: $parsed_name export Error as "inherent-error-ident" $parsed_name,
$parsed_modules: $parsed_name,
)*
}
);