Extend PalletInfoAccess with module_name and crate_version method (#9690)

* Record pallet indices in CallMetadata

* Resurrect PalletVersion infrastructure and rename as CrateVersion

* cargo fmt

* Add missing runtime generics to pallet struct

* Fix path to instance

* Fix test

* Fix UI test expectations

* Fix UI test expectations

* Move crate_version function to PalletInfoAccess

* Update UI test expectations

* Add crate_name method to PalletInfo

* Convert path to module name instead of exposing crate name

* cargo fmt

* Keep the double colons when constructing the module name

* Remove unused import

* Update UI test expectations

* Update frame/support/src/traits/metadata.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update UI test expectations

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Keith Yeung
2021-09-28 02:35:33 -07:00
committed by GitHub
parent 951a033e8d
commit 7b65b049cb
13 changed files with 299 additions and 16 deletions
@@ -132,7 +132,7 @@ fn construct_runtime_parsed(definition: RuntimeDefinition) -> Result<TokenStream
let outer_origin = expand::expand_outer_origin(&name, &pallets, pallets_token, &scrate)?;
let all_pallets = decl_all_pallets(&name, pallets.iter());
let pallet_to_index = decl_pallet_runtime_setup(&pallets, &scrate);
let pallet_to_index = decl_pallet_runtime_setup(&name, &pallets, &scrate);
let dispatch = expand::expand_outer_dispatch(&name, &pallets, &scrate);
let metadata = expand::expand_runtime_metadata(&name, &pallets, &scrate, &unchecked_extrinsic);
@@ -236,13 +236,24 @@ fn decl_all_pallets<'a>(
}
fn decl_pallet_runtime_setup(
runtime: &Ident,
pallet_declarations: &[Pallet],
scrate: &TokenStream2,
) -> TokenStream2 {
let names = pallet_declarations.iter().map(|d| &d.name);
let names2 = pallet_declarations.iter().map(|d| &d.name);
let names = pallet_declarations.iter().map(|d| &d.name).collect::<Vec<_>>();
let name_strings = pallet_declarations.iter().map(|d| d.name.to_string());
let module_names = pallet_declarations.iter().map(|d| d.path.module_name());
let indices = pallet_declarations.iter().map(|pallet| pallet.index as usize);
let pallet_structs = pallet_declarations
.iter()
.map(|pallet| {
let path = &pallet.path;
match pallet.instance.as_ref() {
Some(inst) => quote!(#path::Pallet<#runtime, #path::#inst>),
None => quote!(#path::Pallet<#runtime>),
}
})
.collect::<Vec<_>>();
quote!(
/// Provides an implementation of `PalletInfo` to provide information
@@ -264,13 +275,37 @@ fn decl_pallet_runtime_setup(
fn name<P: 'static>() -> Option<&'static str> {
let type_id = #scrate::sp_std::any::TypeId::of::<P>();
#(
if type_id == #scrate::sp_std::any::TypeId::of::<#names2>() {
if type_id == #scrate::sp_std::any::TypeId::of::<#names>() {
return Some(#name_strings)
}
)*
None
}
fn module_name<P: 'static>() -> Option<&'static str> {
let type_id = #scrate::sp_std::any::TypeId::of::<P>();
#(
if type_id == #scrate::sp_std::any::TypeId::of::<#names>() {
return Some(#module_names)
}
)*
None
}
fn crate_version<P: 'static>() -> Option<#scrate::traits::CrateVersion> {
let type_id = #scrate::sp_std::any::TypeId::of::<P>();
#(
if type_id == #scrate::sp_std::any::TypeId::of::<#names>() {
return Some(
<#pallet_structs as #scrate::traits::PalletInfoAccess>::crate_version()
)
}
)*
None
}
}
)
}
@@ -188,6 +188,18 @@ pub struct PalletPath {
pub inner: Path,
}
impl PalletPath {
pub fn module_name(&self) -> String {
self.inner.segments.iter().fold(String::new(), |mut acc, segment| {
if !acc.is_empty() {
acc.push_str("::");
}
acc.push_str(&segment.ident.to_string());
acc
})
}
}
impl Parse for PalletPath {
fn parse(input: ParseStream) -> Result<Self> {
let mut lookahead = input.lookahead1();