Skip to content

Commit

Permalink
Update node-api metadata (#734)
Browse files Browse the repository at this point in the history
* update metadata_types

* fix compilation

* add tests

* fix test

* fix type
  • Loading branch information
haerdib authored Feb 27, 2024
1 parent bcc748e commit a1775ad
Showing 1 changed file with 64 additions and 12 deletions.
76 changes: 64 additions & 12 deletions node-api/src/metadata/metadata_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -39,8 +42,6 @@ pub struct Metadata {
pallets: BTreeMap<String, PalletMetadataInner>,
/// Find the location in the pallet Vec by pallet index.
pallets_by_index: BTreeMap<u8, String>,
/// Metadata of the extrinsic.
extrinsic: ExtrinsicMetadata<PortableForm>,
// Type of the DispatchError type, which is what comes back if
// an extrinsic fails.
dispatch_error_ty: Option<u32>,
Expand All @@ -67,6 +68,11 @@ impl Metadata {
Some(PalletMetadata { inner, types: self.types() })
}

/// Return the type of the `Runtime`.
pub fn ty(&self) -> &<PortableForm as Form>::Type {
&self.runtime_metadata.ty
}

/// Return the DispatchError type ID if it exists.
pub fn dispatch_error_ty(&self) -> Option<u32> {
self.dispatch_error_ty
Expand All @@ -89,7 +95,7 @@ impl Metadata {

/// Return details about the extrinsic format.
pub fn extrinsic(&self) -> &ExtrinsicMetadata<PortableForm> {
&self.extrinsic
&self.runtime_metadata.extrinsic
}

/// An iterator over all of the runtime APIs.
Expand All @@ -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<PortableForm> {
&self.runtime_metadata.outer_enums
}

/// Returns the custom types of the metadata.
pub fn custom(&self) -> &CustomMetadata<PortableForm> {
&self.runtime_metadata.custom
}

#[cfg(feature = "std")]
pub fn pretty_format(&self) -> Result<String, std::string::FromUtf8Error> {
let buf = Vec::new();
Expand Down Expand Up @@ -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<String, StorageEntryMetadata<PortableForm>>,
/// Type ID for the pallet Call enum.
Expand All @@ -266,12 +280,15 @@ struct PalletMetadataInner {
event_ty: Option<u32>,
/// Event variants by name/u8.
event_variant_index: VariantIndex,
/// Map from constant name to constant details.
constants: BTreeMap<String, PalletConstantMetadata<PortableForm>>,
/// Type ID for the pallet Error enum.
error_ty: Option<u32>,
/// Error variants by name/u8.
error_variant_index: VariantIndex,
/// Map from constant name to constant details.
constants: BTreeMap<String, PalletConstantMetadata<PortableForm>>,
/// 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<String>,
}
Expand Down Expand Up @@ -408,7 +425,6 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
runtime_metadata: m.clone(),
pallets,
pallets_by_index,
extrinsic: m.extrinsic,
dispatch_error_ty,
apis,
})
Expand Down Expand Up @@ -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());
}
}

0 comments on commit a1775ad

Please sign in to comment.