mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-20 04:41:04 +00:00
1a88833d73
* impl_runtime_apis: Generate getters for `metadata_at` functions Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * runtime: Implement new `Metadata` runtime trait Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * runtime: Move `metadata_at` functions to construct_runtime macro Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * contruct_runtime: Use `OpaqueMetadata` from hidden imports Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Adjust testing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/tests: Add tests for the new API Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/tests: Adjust metdata naming Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Expose `metadata-v14` feature flag Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Expose metadata only under feature flags Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Expose v14 metadata by default Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Expose metadata feature for testing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Test metadata under different feature flags Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update primitives/api/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * Update primitives/api/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * client/tests: Adjust testing to reflect trait Metadata change Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/metadata-ir: Add intermediate representation types for metadata Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/metadata-ir: Convert metadata to V14 Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/metadata-ir: Add API to convert metadata to multiple versions Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/metadata-ir: Expose V14 under feature flag Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Adjust to metadata IR Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: More adjustments Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Guard v14 details under feature flag Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Adjust testing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * CI: Ensure `quick-benchmarks` uses `metadata-v14` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Use `metadata-v14` for benchmarks Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Adjust cargo fmt Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * kitchensink-runtime: Add feature flag for `metadata-v14` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support/test: Adjust testing Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support/test: Check crates locally Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Activate metadata-v14 for pallets Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Remove metadata-v14 feature flag Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/metadata_ir: Move `api.rs` to `mod.rs` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Handle latest metadata conversion via IR Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/tests: Add constant for metadata version 14 Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support/test: Fix merge conflict Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update frame/support/Cargo.toml Co-authored-by: Bastian Köcher <git@kchr.de> * Update frame/support/src/metadata_ir/mod.rs Co-authored-by: Bastian Köcher <git@kchr.de> * Update frame/support/test/Cargo.toml Co-authored-by: Bastian Köcher <git@kchr.de> * Update primitives/api/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * frame/metadata: Collect pallet documentation for MetadataIR Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/tests: Check pallet documentation is propagated to MetadataIR Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * frame/support: Improve documentation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> --------- Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> Co-authored-by: parity-processbot <> Co-authored-by: Bastian Köcher <git@kchr.de>
218 lines
6.6 KiB
Rust
218 lines
6.6 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! Implementation of `storage_metadata` on module structure, used by construct_runtime.
|
|
|
|
use super::{DeclStorageDefExt, StorageLineDefExt, StorageLineTypeDef};
|
|
use frame_support_procedural_tools::get_doc_literals;
|
|
use proc_macro2::TokenStream;
|
|
use quote::quote;
|
|
|
|
fn storage_line_metadata_type(scrate: &TokenStream, line: &StorageLineDefExt) -> TokenStream {
|
|
let value_type = &line.value_type;
|
|
match &line.storage_type {
|
|
StorageLineTypeDef::Simple(_) => {
|
|
quote! {
|
|
#scrate::metadata_ir::StorageEntryTypeIR::Plain(
|
|
#scrate::scale_info::meta_type::<#value_type>()
|
|
)
|
|
}
|
|
},
|
|
StorageLineTypeDef::Map(map) => {
|
|
let hasher = map.hasher.into_metadata();
|
|
let key = &map.key;
|
|
quote! {
|
|
#scrate::metadata_ir::StorageEntryTypeIR::Map {
|
|
hashers: #scrate::sp_std::vec! [ #scrate::metadata_ir::#hasher ],
|
|
key: #scrate::scale_info::meta_type::<#key>(),
|
|
value: #scrate::scale_info::meta_type::<#value_type>(),
|
|
}
|
|
}
|
|
},
|
|
StorageLineTypeDef::DoubleMap(map) => {
|
|
let hasher1 = map.hasher1.into_metadata();
|
|
let hasher2 = map.hasher2.into_metadata();
|
|
let key1 = &map.key1;
|
|
let key2 = &map.key2;
|
|
quote! {
|
|
#scrate::metadata_ir::StorageEntryTypeIR::Map {
|
|
hashers: #scrate::sp_std::vec! [
|
|
#scrate::metadata_ir::#hasher1,
|
|
#scrate::metadata_ir::#hasher2,
|
|
],
|
|
key: #scrate::scale_info::meta_type::<(#key1, #key2)>(),
|
|
value: #scrate::scale_info::meta_type::<#value_type>(),
|
|
}
|
|
}
|
|
},
|
|
StorageLineTypeDef::NMap(map) => {
|
|
let key_tuple = &map.to_key_tuple();
|
|
let hashers = map
|
|
.hashers
|
|
.iter()
|
|
.map(|hasher| hasher.to_storage_hasher_struct())
|
|
.collect::<Vec<_>>();
|
|
quote! {
|
|
#scrate::metadata_ir::StorageEntryTypeIR::Map {
|
|
hashers: #scrate::sp_std::vec! [
|
|
#( #scrate::metadata_ir::StorageHasherIR::#hashers, )*
|
|
],
|
|
key: #scrate::scale_info::meta_type::<#key_tuple>(),
|
|
value: #scrate::scale_info::meta_type::<#value_type>(),
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
fn default_byte_getter(
|
|
scrate: &TokenStream,
|
|
line: &StorageLineDefExt,
|
|
def: &DeclStorageDefExt,
|
|
) -> (TokenStream, TokenStream) {
|
|
let default = line
|
|
.default_value
|
|
.as_ref()
|
|
.map(|d| quote!( #d ))
|
|
.unwrap_or_else(|| quote!(Default::default()));
|
|
|
|
let str_name = line.name.to_string();
|
|
let struct_name =
|
|
syn::Ident::new(&("__GetByteStruct".to_string() + &str_name), line.name.span());
|
|
let cache_name =
|
|
syn::Ident::new(&("__CACHE_GET_BYTE_STRUCT_".to_string() + &str_name), line.name.span());
|
|
|
|
let runtime_generic = &def.module_runtime_generic;
|
|
let runtime_trait = &def.module_runtime_trait;
|
|
let optional_instance_bound_optional_default = &def.optional_instance_bound_optional_default;
|
|
let optional_instance_bound = &def.optional_instance_bound;
|
|
let optional_instance = &def.optional_instance;
|
|
let optional_comma_instance = optional_instance.as_ref().map(|i| quote!(, #i));
|
|
let where_clause = &def.where_clause;
|
|
|
|
let query_type = &line.query_type;
|
|
|
|
let struct_def = quote! {
|
|
#[doc(hidden)]
|
|
pub struct #struct_name<
|
|
#runtime_generic, #optional_instance_bound_optional_default
|
|
>(pub #scrate::sp_std::marker::PhantomData<(#runtime_generic #optional_comma_instance)>);
|
|
|
|
#[cfg(feature = "std")]
|
|
#[allow(non_upper_case_globals)]
|
|
static #cache_name: #scrate::once_cell::sync::OnceCell<#scrate::sp_std::vec::Vec<u8>> =
|
|
#scrate::once_cell::sync::OnceCell::new();
|
|
|
|
#[cfg(feature = "std")]
|
|
impl<#runtime_generic: #runtime_trait, #optional_instance_bound>
|
|
#struct_name<#runtime_generic, #optional_instance>
|
|
#where_clause
|
|
{
|
|
fn default_byte(&self) -> #scrate::sp_std::vec::Vec<u8> {
|
|
use #scrate::codec::Encode;
|
|
#cache_name.get_or_init(|| {
|
|
let def_val: #query_type = #default;
|
|
<#query_type as Encode>::encode(&def_val)
|
|
}).clone()
|
|
}
|
|
}
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
impl<#runtime_generic: #runtime_trait, #optional_instance_bound>
|
|
#struct_name<#runtime_generic, #optional_instance>
|
|
#where_clause
|
|
{
|
|
fn default_byte(&self) -> #scrate::sp_std::vec::Vec<u8> {
|
|
use #scrate::codec::Encode;
|
|
let def_val: #query_type = #default;
|
|
<#query_type as Encode>::encode(&def_val)
|
|
}
|
|
}
|
|
};
|
|
let struct_instance = quote!(
|
|
#struct_name::<#runtime_generic, #optional_instance>(#scrate::sp_std::marker::PhantomData)
|
|
);
|
|
|
|
(struct_def, struct_instance)
|
|
}
|
|
|
|
pub fn impl_metadata(def: &DeclStorageDefExt) -> TokenStream {
|
|
let scrate = &def.hidden_crate;
|
|
let mut entries = TokenStream::new();
|
|
let mut default_byte_getter_struct_defs = TokenStream::new();
|
|
|
|
for line in def.storage_lines.iter() {
|
|
let str_name = line.name.to_string();
|
|
|
|
let modifier = if line.is_option {
|
|
quote!(#scrate::metadata_ir::StorageEntryModifierIR::Optional)
|
|
} else {
|
|
quote!(#scrate::metadata_ir::StorageEntryModifierIR::Default)
|
|
};
|
|
|
|
let ty = storage_line_metadata_type(scrate, line);
|
|
|
|
let (default_byte_getter_struct_def, default_byte_getter_struct_instance) =
|
|
default_byte_getter(scrate, line, def);
|
|
|
|
let docs = get_doc_literals(&line.attrs);
|
|
|
|
let entry = quote! {
|
|
#scrate::metadata_ir::StorageEntryMetadataIR {
|
|
name: #str_name,
|
|
modifier: #modifier,
|
|
ty: #ty,
|
|
default: #default_byte_getter_struct_instance.default_byte(),
|
|
docs: #scrate::sp_std::vec![ #( #docs ),* ],
|
|
},
|
|
};
|
|
|
|
default_byte_getter_struct_defs.extend(default_byte_getter_struct_def);
|
|
entries.extend(entry);
|
|
}
|
|
|
|
let prefix = if let Some(instance) = &def.module_instance {
|
|
let instance_generic = &instance.instance_generic;
|
|
quote!(#instance_generic::PREFIX)
|
|
} else {
|
|
let prefix = def.crate_name.to_string();
|
|
quote!(#prefix)
|
|
};
|
|
|
|
let store_metadata = quote!(
|
|
#scrate::metadata_ir::PalletStorageMetadataIR {
|
|
prefix: #prefix,
|
|
entries: #scrate::sp_std::vec![ #entries ],
|
|
}
|
|
);
|
|
|
|
let module_struct = &def.module_struct;
|
|
let module_impl = &def.module_impl;
|
|
let where_clause = &def.where_clause;
|
|
|
|
quote!(
|
|
#default_byte_getter_struct_defs
|
|
|
|
impl #module_impl #module_struct #where_clause {
|
|
#[doc(hidden)]
|
|
pub fn storage_metadata() -> #scrate::metadata_ir::PalletStorageMetadataIR {
|
|
#store_metadata
|
|
}
|
|
}
|
|
)
|
|
}
|