Remove requirement on Hash = H256, make Proposer return StorageChanges and Proof (#3860)

* Extend `Proposer` to optionally generate a proof of the proposal

* Something

* Refactor sr-api to not depend on client anymore

* Fix benches

* Apply suggestions from code review

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Apply suggestions from code review

* Introduce new `into_storage_changes` function

* Switch to runtime api for `execute_block` and don't require `H256`
anywhere in the code

* Put the `StorageChanges` into the `Proposal`

* Move the runtime api error to its own trait

* Adds `StorageTransactionCache` to the runtime api

This requires that we add `type NodeBlock = ` to the
`impl_runtime_apis!` macro to work around some bugs in rustc :(

* Remove `type NodeBlock` and switch to a "better" hack

* Start using the transaction cache from the runtime api

* Make it compile

* Move `InMemory` to its own file

* Make all tests work again

* Return block, storage_changes and proof from Blockbuilder::bake()

* Make sure that we use/set `storage_changes` when possible

* Add test

* Fix deadlock

* Remove accidentally added folders

* Introduce `RecordProof` as argument type to be more explicit

* Update client/src/client.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update primitives/state-machine/src/ext.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Integrates review feedback

* Remove `unsafe` usage

* Update client/block-builder/src/lib.rs

Co-Authored-By: Benjamin Kampmann <ben@gnunicorn.org>

* Update client/src/call_executor.rs

* Bump versions

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
Co-authored-by: Benjamin Kampmann <ben.kampmann@googlemail.com>
This commit is contained in:
Bastian Köcher
2020-01-10 10:48:32 +01:00
committed by GitHub
parent 74d6e660c6
commit fd6b29dd2c
140 changed files with 4860 additions and 3339 deletions
@@ -16,7 +16,7 @@
use crate::utils::{
generate_crate_access, generate_hidden_includes, generate_runtime_mod_name_for_trait,
fold_fn_decl_for_client_side, unwrap_or_error, extract_parameter_names_types_and_borrows,
fold_fn_decl_for_client_side, extract_parameter_names_types_and_borrows,
generate_native_call_generator_fn_name, return_type_extract_type,
generate_method_runtime_api_impl_name, generate_call_api_at_fn_name, prefix_function_with_trait,
replace_wild_card_parameter_names,
@@ -399,7 +399,7 @@ fn generate_call_api_at_calls(decl: &ItemTrait) -> Result<TokenStream> {
R: #crate_::Encode + #crate_::Decode + PartialEq,
NC: FnOnce() -> std::result::Result<R, String> + std::panic::UnwindSafe,
Block: #crate_::BlockT,
T: #crate_::CallRuntimeAt<Block>,
T: #crate_::CallApiAt<Block>,
C: #crate_::Core<Block, Error = T::Error>,
>(
call_runtime_at: &T,
@@ -407,6 +407,9 @@ fn generate_call_api_at_calls(decl: &ItemTrait) -> Result<TokenStream> {
at: &#crate_::BlockId<Block>,
args: Vec<u8>,
changes: &std::cell::RefCell<#crate_::OverlayedChanges>,
storage_transaction_cache: &std::cell::RefCell<
#crate_::StorageTransactionCache<Block, T::StateBackend>
>,
initialized_block: &std::cell::RefCell<Option<#crate_::BlockId<Block>>>,
native_call: Option<NC>,
context: #crate_::ExecutionContext,
@@ -426,34 +429,40 @@ fn generate_call_api_at_calls(decl: &ItemTrait) -> Result<TokenStream> {
if version.apis.iter().any(|(s, v)| {
s == &ID && *v < #versions
}) {
let ret = call_runtime_at.call_api_at::<R, fn() -> _, _>(
let params = #crate_::CallApiAtParams::<_, _, fn() -> _, _> {
core_api,
at,
#old_names,
args,
changes,
function: #old_names,
native_call: None,
arguments: args,
overlayed_changes: changes,
storage_transaction_cache,
initialize_block,
None,
context,
recorder,
)?;
};
let ret = call_runtime_at.call_api_at(params)?;
update_initialized_block();
return Ok(ret);
return Ok(ret)
}
)*
let ret = call_runtime_at.call_api_at(
let params = #crate_::CallApiAtParams {
core_api,
at,
#trait_fn_name,
args,
changes,
initialize_block,
function: #trait_fn_name,
native_call,
arguments: args,
overlayed_changes: changes,
storage_transaction_cache,
initialize_block,
context,
recorder,
)?;
};
let ret = call_runtime_at.call_api_at(params)?;
update_initialized_block();
Ok(ret)
@@ -465,7 +474,7 @@ fn generate_call_api_at_calls(decl: &ItemTrait) -> Result<TokenStream> {
}
/// Generate the declaration of the trait for the runtime.
fn generate_runtime_decls(decls: &[ItemTrait]) -> TokenStream {
fn generate_runtime_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
let mut result = Vec::new();
for decl in decls {
@@ -473,12 +482,12 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> TokenStream {
extend_generics_with_block(&mut decl.generics);
let mod_name = generate_runtime_mod_name_for_trait(&decl.ident);
let found_attributes = remove_supported_attributes(&mut decl.attrs);
let api_version = unwrap_or_error(get_api_version(&found_attributes).map(|v| {
let api_version = get_api_version(&found_attributes).map(|v| {
generate_runtime_api_version(v as u32)
}));
})?;
let id = generate_runtime_api_id(&decl.ident.to_string());
let call_api_at_calls = unwrap_or_error(generate_call_api_at_calls(&decl));
let call_api_at_calls = generate_call_api_at_calls(&decl)?;
// Remove methods that have the `changed_in` attribute as they are not required for the
// runtime anymore.
@@ -495,7 +504,7 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> TokenStream {
r => Some(r.clone()),
}).collect();
let native_call_generators = unwrap_or_error(generate_native_call_generators(&decl));
let native_call_generators = generate_native_call_generators(&decl)?;
result.push(quote!(
#[doc(hidden)]
@@ -517,7 +526,7 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> TokenStream {
));
}
quote!( #( #result )* )
Ok(quote!( #( #result )* ))
}
/// Modify the given runtime api declaration to be usable on the client side.
@@ -722,7 +731,7 @@ impl<'a> Fold for ToClientSideDecl<'a> {
'static
+ Send
+ Sync
+ #crate_::ApiExt<#block_ident>
+ #crate_::ApiErrorExt
);
} else {
// Add the `Core` runtime api as super trait.
@@ -823,7 +832,7 @@ fn get_api_version(found_attributes: &HashMap<&'static str, Attribute>) -> Resul
}
/// Generate the declaration of the trait for the client side.
fn generate_client_side_decls(decls: &[ItemTrait]) -> TokenStream {
fn generate_client_side_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
let mut result = Vec::new();
for decl in decls {
@@ -848,14 +857,12 @@ fn generate_client_side_decls(decls: &[ItemTrait]) -> TokenStream {
let api_version = get_api_version(&found_attributes);
let runtime_info = unwrap_or_error(
api_version.map(|v| generate_runtime_info_impl(&decl, v))
);
let runtime_info = api_version.map(|v| generate_runtime_info_impl(&decl, v))?;
result.push(quote!( #decl #runtime_info #( #errors )* ));
}
quote!( #( #result )* )
Ok(quote!( #( #result )* ))
}
/// Checks that a trait declaration is in the format we expect.
@@ -908,15 +915,17 @@ impl<'ast> Visit<'ast> for CheckTraitDecl {
}
/// Check that the trait declarations are in the format we expect.
fn check_trait_decls(decls: &[ItemTrait]) -> Option<TokenStream> {
fn check_trait_decls(decls: &[ItemTrait]) -> Result<()> {
let mut checker = CheckTraitDecl { errors: Vec::new() };
decls.iter().for_each(|decl| visit::visit_item_trait(&mut checker, &decl));
if checker.errors.is_empty() {
None
if let Some(err) = checker.errors.pop() {
Err(checker.errors.into_iter().fold(err, |mut err, other| {
err.combine(other);
err
}))
} else {
let errors = checker.errors.into_iter().map(|e| e.to_compile_error());
Some(quote!( #( #errors )* ))
Ok(())
}
}
@@ -925,19 +934,23 @@ pub fn decl_runtime_apis_impl(input: proc_macro::TokenStream) -> proc_macro::Tok
// Parse all trait declarations
let RuntimeApiDecls { decls: api_decls } = parse_macro_input!(input as RuntimeApiDecls);
if let Some(errors) = check_trait_decls(&api_decls) {
return errors.into();
}
decl_runtime_apis_impl_inner(&api_decls).unwrap_or_else(|e| e.to_compile_error()).into()
}
fn decl_runtime_apis_impl_inner(api_decls: &[ItemTrait]) -> Result<TokenStream> {
check_trait_decls(&api_decls)?;
let hidden_includes = generate_hidden_includes(HIDDEN_INCLUDES_ID);
let runtime_decls = generate_runtime_decls(&api_decls);
let client_side_decls = generate_client_side_decls(&api_decls);
let runtime_decls = generate_runtime_decls(api_decls)?;
let client_side_decls = generate_client_side_decls(api_decls)?;
quote!(
#hidden_includes
Ok(
quote!(
#hidden_includes
#runtime_decls
#runtime_decls
#client_side_decls
).into()
#client_side_decls
)
)
}
@@ -15,10 +15,11 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use crate::utils::{
unwrap_or_error, generate_crate_access, generate_hidden_includes,
generate_crate_access, generate_hidden_includes,
generate_runtime_mod_name_for_trait, generate_method_runtime_api_impl_name,
extract_parameter_names_types_and_borrows, generate_native_call_generator_fn_name,
return_type_extract_type, generate_call_api_at_fn_name, prefix_function_with_trait,
extract_all_signature_types,
};
use proc_macro2::{Span, TokenStream};
@@ -28,7 +29,7 @@ use quote::quote;
use syn::{
spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, Path, Signature,
ImplItem, parse::{Parse, ParseStream, Result, Error}, PathArguments, GenericArgument, TypePath,
fold::{self, Fold}, parse_quote
fold::{self, Fold}, parse_quote,
};
use std::{collections::HashSet, iter};
@@ -49,7 +50,11 @@ impl Parse for RuntimeApiImpls {
impls.push(ItemImpl::parse(input)?);
}
Ok(Self { impls })
if impls.is_empty() {
Err(Error::new(Span::call_site(), "No api implementation given!"))
} else {
Ok(Self { impls })
}
}
}
@@ -222,59 +227,69 @@ fn generate_wasm_interface(impls: &[ItemImpl]) -> Result<TokenStream> {
Ok(quote!( #( #impl_calls )* ))
}
fn generate_block_and_block_id_ty(
runtime: &Type,
trait_: &'static str,
assoc_type: &'static str,
) -> (TokenStream, TokenStream) {
fn generate_runtime_api_base_structures() -> Result<TokenStream> {
let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);
let trait_ = Ident::new(trait_, Span::call_site());
let assoc_type = Ident::new(assoc_type, Span::call_site());
let block = quote!( <#runtime as #crate_::#trait_>::#assoc_type );
let block_id = quote!( #crate_::BlockId<#block> );
(block, block_id)
}
fn generate_node_block_and_block_id_ty(runtime: &Type) -> (TokenStream, TokenStream) {
generate_block_and_block_id_ty(runtime, "GetNodeBlockType", "NodeBlock")
}
fn generate_runtime_api_base_structures(impls: &[ItemImpl]) -> Result<TokenStream> {
let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);
let runtime = &impls.get(0).ok_or_else(||
Error::new(Span::call_site(), "No api implementation given!")
)?.self_ty;
let (block, block_id) = generate_node_block_and_block_id_ty(runtime);
Ok(quote!(
pub struct RuntimeApi {}
/// Implements all runtime apis for the client side.
#[cfg(any(feature = "std", test))]
pub struct RuntimeApiImpl<C: #crate_::CallRuntimeAt<#block> + 'static> {
pub struct RuntimeApiImpl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block> + 'static>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
{
call: &'static C,
commit_on_success: std::cell::RefCell<bool>,
initialized_block: std::cell::RefCell<Option<#block_id>>,
initialized_block: std::cell::RefCell<Option<#crate_::BlockId<Block>>>,
changes: std::cell::RefCell<#crate_::OverlayedChanges>,
recorder: Option<#crate_::ProofRecorder<#block>>,
storage_transaction_cache: std::cell::RefCell<
#crate_::StorageTransactionCache<Block, C::StateBackend>
>,
recorder: Option<#crate_::ProofRecorder<Block>>,
}
// `RuntimeApi` itself is not threadsafe. However, an instance is only available in a
// `ApiRef` object and `ApiRef` also has an associated lifetime. This lifetimes makes it
// impossible to move `RuntimeApi` into another thread.
#[cfg(any(feature = "std", test))]
unsafe impl<C: #crate_::CallRuntimeAt<#block>> Send for RuntimeApiImpl<C> {}
#[cfg(any(feature = "std", test))]
unsafe impl<C: #crate_::CallRuntimeAt<#block>> Sync for RuntimeApiImpl<C> {}
unsafe impl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block>> Send
for RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
{}
#[cfg(any(feature = "std", test))]
impl<C: #crate_::CallRuntimeAt<#block>> #crate_::ApiExt<#block> for RuntimeApiImpl<C> {
unsafe impl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block>> Sync
for RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
{}
#[cfg(any(feature = "std", test))]
impl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block>> #crate_::ApiErrorExt
for RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
{
type Error = C::Error;
}
#[cfg(any(feature = "std", test))]
impl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block>> #crate_::ApiExt<Block> for
RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
{
type StateBackend = C::StateBackend;
fn map_api_result<F: FnOnce(&Self) -> std::result::Result<R, E>, R, E>(
&self,
map_call: F
map_call: F,
) -> std::result::Result<R, E> where Self: Sized {
*self.commit_on_success.borrow_mut() = false;
let res = map_call(self);
@@ -287,7 +302,7 @@ fn generate_runtime_api_base_structures(impls: &[ItemImpl]) -> Result<TokenStrea
fn runtime_version_at(
&self,
at: &#block_id
at: &#crate_::BlockId<Block>,
) -> std::result::Result<#crate_::RuntimeVersion, C::Error> {
self.call.runtime_version_at(at)
}
@@ -307,13 +322,37 @@ fn generate_runtime_api_base_structures(impls: &[ItemImpl]) -> Result<TokenStrea
#crate_::StorageProof::new(trie_nodes)
})
}
fn into_storage_changes<
T: #crate_::ChangesTrieStorage<#crate_::HasherFor<Block>, #crate_::NumberFor<Block>>
>(
&self,
backend: &Self::StateBackend,
changes_trie_storage: Option<&T>,
parent_hash: Block::Hash,
) -> std::result::Result<
#crate_::StorageChanges<Self::StateBackend, Block>,
String
> where Self: Sized {
self.initialized_block.borrow_mut().take();
self.changes.replace(Default::default()).into_storage_changes(
backend,
changes_trie_storage,
parent_hash,
self.storage_transaction_cache.replace(Default::default()),
)
}
}
#[cfg(any(feature = "std", test))]
impl<C: #crate_::CallRuntimeAt<#block> + 'static> #crate_::ConstructRuntimeApi<#block, C>
impl<Block: #crate_::BlockT, C> #crate_::ConstructRuntimeApi<Block, C>
for RuntimeApi
where
C: #crate_::CallApiAt<Block> + 'static,
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
{
type RuntimeApi = RuntimeApiImpl<C>;
type RuntimeApi = RuntimeApiImpl<Block, C>;
fn construct_runtime_api<'a>(
call: &'a C,
@@ -324,20 +363,26 @@ fn generate_runtime_api_base_structures(impls: &[ItemImpl]) -> Result<TokenStrea
initialized_block: None.into(),
changes: Default::default(),
recorder: Default::default(),
storage_transaction_cache: Default::default(),
}.into()
}
}
#[cfg(any(feature = "std", test))]
impl<C: #crate_::CallRuntimeAt<#block>> RuntimeApiImpl<C> {
impl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block>> RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
{
fn call_api_at<
R: #crate_::Encode + #crate_::Decode + PartialEq,
F: FnOnce(
&C,
&Self,
&std::cell::RefCell<#crate_::OverlayedChanges>,
&std::cell::RefCell<Option<#crate_::BlockId<#block>>>,
&Option<#crate_::ProofRecorder<#block>>,
&std::cell::RefCell<#crate_::StorageTransactionCache<Block, C::StateBackend>>,
&std::cell::RefCell<Option<#crate_::BlockId<Block>>>,
&Option<#crate_::ProofRecorder<Block>>,
) -> std::result::Result<#crate_::NativeOrEncoded<R>, E>,
E,
>(
@@ -348,6 +393,7 @@ fn generate_runtime_api_base_structures(impls: &[ItemImpl]) -> Result<TokenStrea
&self.call,
self,
&self.changes,
&self.storage_transaction_cache,
&self.initialized_block,
&self.recorder,
);
@@ -413,9 +459,7 @@ fn generate_api_impl_for_runtime(impls: &[ItemImpl]) -> Result<TokenStream> {
/// `impl Api for Runtime` with `impl Api for RuntimeApi` and replace the method implementations
/// with code that calls into the runtime.
struct ApiRuntimeImplToApiRuntimeApiImpl<'a> {
node_block: &'a TokenStream,
runtime_block: &'a TypePath,
node_block_id: &'a TokenStream,
runtime_mod_path: &'a Path,
runtime_type: &'a Type,
trait_generic_arguments: &'a [GenericArgument],
@@ -425,8 +469,7 @@ struct ApiRuntimeImplToApiRuntimeApiImpl<'a> {
impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> {
fn fold_type_path(&mut self, input: TypePath) -> TypePath {
let new_ty_path = if input == *self.runtime_block {
let node_block = self.node_block;
parse_quote!( #node_block )
parse_quote!( __SR_API_BLOCK__ )
} else {
input
};
@@ -442,9 +485,7 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> {
generate_native_call_generator_fn_name(&input.sig.ident);
let call_api_at_call = generate_call_api_at_fn_name(&input.sig.ident);
let trait_generic_arguments = self.trait_generic_arguments;
let node_block = self.node_block;
let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);
let block_id = self.node_block_id;
// Generate the access to the native parameters
let param_tuple_access = if input.sig.inputs.len() == 1 {
@@ -471,7 +512,7 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> {
// Rewrite the input parameters.
input.sig.inputs = parse_quote! {
&self,
at: &#block_id,
at: &#crate_::BlockId<__SR_API_BLOCK__>,
context: #crate_::ExecutionContext,
params: Option<( #( #param_types ),* )>,
params_encoded: Vec<u8>,
@@ -495,17 +536,25 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> {
#error
self.call_api_at(
|call_runtime_at, core_api, changes, initialized_block, recorder| {
|
call_runtime_at,
core_api,
changes,
storage_transaction_cache,
initialized_block,
recorder
| {
#runtime_mod_path #call_api_at_call(
call_runtime_at,
core_api,
at,
params_encoded,
changes,
storage_transaction_cache,
initialized_block,
params.map(|p| {
#runtime_mod_path #native_call_generator_ident ::
<#runtime, #node_block #(, #trait_generic_arguments )*> (
<#runtime, __SR_API_BLOCK__ #(, #trait_generic_arguments )*> (
#( #param_tuple_access ),*
)
}),
@@ -526,13 +575,50 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> {
}
fn fold_item_impl(&mut self, mut input: ItemImpl) -> ItemImpl {
// Implement the trait for the `RuntimeApiImpl`
input.self_ty = Box::new(parse_quote!( RuntimeApiImpl<RuntimeApiImplCall> ));
// All this `UnwindSafe` magic below here is required for this rust bug:
// https://github.com/rust-lang/rust/issues/24159
// Before we directly had the final block type and rust could determine that it is unwind
// safe, but now we just have a generic parameter `Block`.
let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);
let block = self.node_block;
// Implement the trait for the `RuntimeApiImpl`
input.self_ty = Box::new(
parse_quote!( RuntimeApiImpl<__SR_API_BLOCK__, RuntimeApiImplCall> )
);
input.generics.params.push(
parse_quote!( RuntimeApiImplCall: #crate_::CallRuntimeAt<#block> + 'static )
parse_quote!(
__SR_API_BLOCK__: #crate_::BlockT + std::panic::UnwindSafe +
std::panic::RefUnwindSafe
)
);
input.generics.params.push(
parse_quote!( RuntimeApiImplCall: #crate_::CallApiAt<__SR_API_BLOCK__> + 'static )
);
let where_clause = input.generics.make_where_clause();
where_clause.predicates.push(
parse_quote! {
RuntimeApiImplCall::StateBackend:
#crate_::StateBackend<#crate_::HasherFor<__SR_API_BLOCK__>>
}
);
// Require that all types used in the function signatures are unwind safe.
extract_all_signature_types(&input.items).iter().for_each(|i| {
where_clause.predicates.push(
parse_quote! {
#i: std::panic::UnwindSafe + std::panic::RefUnwindSafe
}
);
});
where_clause.predicates.push(
parse_quote! {
__SR_API_BLOCK__::Header: std::panic::UnwindSafe + std::panic::RefUnwindSafe
}
);
// The implementation for the `RuntimeApiImpl` is only required when compiling with
@@ -555,7 +641,6 @@ fn generate_api_impl_for_runtime_api(impls: &[ItemImpl]) -> Result<TokenStream>
.ok_or_else(|| Error::new(impl_trait_path.span(), "Empty trait path not possible!"))?
.clone();
let runtime_block = extract_runtime_block_ident(impl_trait_path)?;
let (node_block, node_block_id) = generate_node_block_and_block_id_ty(&impl_.self_ty);
let runtime_type = &impl_.self_ty;
let mut runtime_mod_path = extend_with_runtime_decl_path(impl_trait_path.clone());
// remove the trait to get just the module path
@@ -568,8 +653,6 @@ fn generate_api_impl_for_runtime_api(impls: &[ItemImpl]) -> Result<TokenStream>
let mut visitor = ApiRuntimeImplToApiRuntimeApiImpl {
runtime_block,
node_block: &node_block,
node_block_id: &node_block_id,
runtime_mod_path: &runtime_mod_path,
runtime_type: &*runtime_type,
trait_generic_arguments: &trait_generic_arguments,
@@ -627,31 +710,37 @@ pub fn impl_runtime_apis_impl(input: proc_macro::TokenStream) -> proc_macro::Tok
// Parse all impl blocks
let RuntimeApiImpls { impls: api_impls } = parse_macro_input!(input as RuntimeApiImpls);
let dispatch_impl = unwrap_or_error(generate_dispatch_function(&api_impls));
let api_impls_for_runtime = unwrap_or_error(generate_api_impl_for_runtime(&api_impls));
let base_runtime_api = unwrap_or_error(generate_runtime_api_base_structures(&api_impls));
let hidden_includes = generate_hidden_includes(HIDDEN_INCLUDES_ID);
let runtime_api_versions = unwrap_or_error(generate_runtime_api_versions(&api_impls));
let wasm_interface = unwrap_or_error(generate_wasm_interface(&api_impls));
let api_impls_for_runtime_api = unwrap_or_error(generate_api_impl_for_runtime_api(&api_impls));
quote!(
#hidden_includes
#base_runtime_api
#api_impls_for_runtime
#api_impls_for_runtime_api
#runtime_api_versions
pub mod api {
use super::*;
#dispatch_impl
#wasm_interface
}
).into()
impl_runtime_apis_impl_inner(&api_impls).unwrap_or_else(|e| e.to_compile_error()).into()
}
fn impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result<TokenStream> {
let dispatch_impl = generate_dispatch_function(api_impls)?;
let api_impls_for_runtime = generate_api_impl_for_runtime(api_impls)?;
let base_runtime_api = generate_runtime_api_base_structures()?;
let hidden_includes = generate_hidden_includes(HIDDEN_INCLUDES_ID);
let runtime_api_versions = generate_runtime_api_versions(api_impls)?;
let wasm_interface = generate_wasm_interface(api_impls)?;
let api_impls_for_runtime_api = generate_api_impl_for_runtime_api(api_impls)?;
Ok(
quote!(
#hidden_includes
#base_runtime_api
#api_impls_for_runtime
#api_impls_for_runtime_api
#runtime_api_versions
pub mod api {
use super::*;
#dispatch_impl
#wasm_interface
}
)
)
}
@@ -18,6 +18,7 @@ use proc_macro2::{TokenStream, Span};
use syn::{
Result, Ident, Signature, parse_quote, Type, Pat, spanned::Spanned, FnArg, Error, token::And,
ImplItem, ReturnType,
};
use quote::quote;
@@ -26,11 +27,6 @@ use std::env;
use proc_macro_crate::crate_name;
/// Unwrap the given result, if it is an error, `compile_error!` will be generated.
pub fn unwrap_or_error(res: Result<TokenStream>) -> TokenStream {
res.unwrap_or_else(|e| e.to_compile_error())
}
fn generate_hidden_includes_mod_name(unique_id: &'static str) -> Ident {
Ident::new(&format!("sp_api_hidden_includes_{}", unique_id), Span::call_site())
}
@@ -81,10 +77,10 @@ pub fn generate_method_runtime_api_impl_name(trait_: &Ident, method: &Ident) ->
}
/// Get the type of a `syn::ReturnType`.
pub fn return_type_extract_type(rt: &syn::ReturnType) -> Type {
pub fn return_type_extract_type(rt: &ReturnType) -> Type {
match rt {
syn::ReturnType::Default => parse_quote!( () ),
syn::ReturnType::Type(_, ref ty) => *ty.clone(),
ReturnType::Default => parse_quote!( () ),
ReturnType::Type(_, ref ty) => *ty.clone(),
}
}
@@ -176,3 +172,30 @@ pub fn generate_call_api_at_fn_name(fn_name: &Ident) -> Ident {
pub fn prefix_function_with_trait<F: ToString>(trait_: &Ident, function: &F) -> String {
format!("{}_{}", trait_.to_string(), function.to_string())
}
/// Extract all types that appear in signatures in the given `ImplItem`'s.
///
/// If a type is a reference, the inner type is extracted (without the reference).
pub fn extract_all_signature_types(items: &[ImplItem]) -> Vec<Type> {
items.iter()
.filter_map(|i| match i {
ImplItem::Method(method) => Some(&method.sig),
_ => None,
})
.map(|sig| {
let ret_ty = match &sig.output {
ReturnType::Default => None,
ReturnType::Type(_, ty) => Some((**ty).clone()),
};
sig.inputs.iter().filter_map(|i| match i {
FnArg::Typed(arg) => Some(&arg.ty),
_ => None,
}).map(|ty| match &**ty {
Type::Reference(t) => (*t.elem).clone(),
_ => (**ty).clone(),
}).chain(ret_ty)
})
.flatten()
.collect()
}