From a1775ad38cd4d4b7614185e8c98269162ca12d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bigna=20H=C3=A4rdi?= Date: Tue, 27 Feb 2024 09:03:24 +0100 Subject: [PATCH] Update node-api metadata (#734) * update metadata_types * fix compilation * add tests * fix test * fix type --- node-api/src/metadata/metadata_types.rs | 76 +++++++++++++++++++++---- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/node-api/src/metadata/metadata_types.rs b/node-api/src/metadata/metadata_types.rs index 79bc9bd59..1416f1c07 100644 --- a/node-api/src/metadata/metadata_types.rs +++ b/node-api/src/metadata/metadata_types.rs @@ -20,12 +20,15 @@ use alloc::{ use codec::{Decode, Encode}; use frame_metadata::{ v15::{ - ExtrinsicMetadata, PalletConstantMetadata, RuntimeApiMethodMetadata, - RuntimeMetadataLastVersion, StorageEntryMetadata, + CustomMetadata, ExtrinsicMetadata, OuterEnums, PalletConstantMetadata, + RuntimeApiMethodMetadata, RuntimeMetadataLastVersion, StorageEntryMetadata, }, RuntimeMetadata, RuntimeMetadataPrefixed, META_RESERVED, }; -use scale_info::{form::PortableForm, PortableRegistry, Type, Variant}; +use scale_info::{ + form::{Form, PortableForm}, + PortableRegistry, Type, Variant, +}; use sp_storage::StorageKey; #[cfg(feature = "std")] @@ -39,8 +42,6 @@ pub struct Metadata { pallets: BTreeMap, /// Find the location in the pallet Vec by pallet index. pallets_by_index: BTreeMap, - /// Metadata of the extrinsic. - extrinsic: ExtrinsicMetadata, // Type of the DispatchError type, which is what comes back if // an extrinsic fails. dispatch_error_ty: Option, @@ -67,6 +68,11 @@ impl Metadata { Some(PalletMetadata { inner, types: self.types() }) } + /// Return the type of the `Runtime`. + pub fn ty(&self) -> &::Type { + &self.runtime_metadata.ty + } + /// Return the DispatchError type ID if it exists. pub fn dispatch_error_ty(&self) -> Option { self.dispatch_error_ty @@ -89,7 +95,7 @@ impl Metadata { /// Return details about the extrinsic format. pub fn extrinsic(&self) -> &ExtrinsicMetadata { - &self.extrinsic + &self.runtime_metadata.extrinsic } /// An iterator over all of the runtime APIs. @@ -105,6 +111,16 @@ impl Metadata { Some(RuntimeApiMetadata { inner, types: self.types() }) } + /// Return the outer enums types as found in the runtime. + pub fn outer_enums(&self) -> &OuterEnums { + &self.runtime_metadata.outer_enums + } + + /// Returns the custom types of the metadata. + pub fn custom(&self) -> &CustomMetadata { + &self.runtime_metadata.custom + } + #[cfg(feature = "std")] pub fn pretty_format(&self) -> Result { let buf = Vec::new(); @@ -249,13 +265,11 @@ impl<'a> PalletMetadata<'a> { } } -// Based on https://github.com/paritytech/subxt/blob/8413c4d2dd625335b9200dc2289670accdf3391a/metadata/src/lib.rs#L274-L298 +// Based on https://github.com/paritytech/frame-metadata/blob/94e7743fa454963609763cf9cccbb7f85bc96d2f/frame-metadata/src/v15.rs#L249-L276 #[derive(Debug, Clone)] struct PalletMetadataInner { /// Pallet name. name: String, - /// Pallet index. - index: u8, /// Pallet storage metadata. storage: BTreeMap>, /// Type ID for the pallet Call enum. @@ -266,12 +280,15 @@ struct PalletMetadataInner { event_ty: Option, /// Event variants by name/u8. event_variant_index: VariantIndex, + /// Map from constant name to constant details. + constants: BTreeMap>, /// Type ID for the pallet Error enum. error_ty: Option, /// Error variants by name/u8. error_variant_index: VariantIndex, - /// Map from constant name to constant details. - constants: BTreeMap>, + /// Define the index of the pallet, this index will be used for the encoding of pallet event, + /// call and origin variants. + index: u8, /// Pallet documentation. docs: Vec, } @@ -408,7 +425,6 @@ impl TryFrom for Metadata { runtime_metadata: m.clone(), pallets, pallets_by_index, - extrinsic: m.extrinsic, dispatch_error_ty, apis, }) @@ -499,3 +515,39 @@ impl Metadata { .key(first_double_map_key, second_double_map_key)) } } + +#[cfg(test)] +mod tests { + use super::*; + use codec::Decode; + use scale_info::TypeDef; + use sp_core::Bytes; + use std::fs; + + fn metadata() -> Metadata { + let encoded_metadata: Bytes = fs::read("./../ksm_metadata_v14.bin").unwrap().into(); + Decode::decode(&mut encoded_metadata.0.as_slice()).unwrap() + } + + #[test] + fn outer_enum_access() { + let metadata = metadata(); + + let call_enum_ty = metadata.outer_enums().call_enum_ty; + let ty = metadata.types().types.get(call_enum_ty.id as usize).unwrap(); + if let TypeDef::Variant(variant) = &ty.ty.type_def { + // The first pallet call is from System pallet. + assert_eq!(variant.variants[0].name, "System"); + } else { + panic!("Expected Variant outer enum call type."); + } + } + + #[test] + fn custom_ksm_metadata_v14_is_empty() { + let metadata = metadata(); + let custom_metadata = metadata.custom(); + + assert!(custom_metadata.map.is_empty()); + } +}