Skip to content

Commit a1775ad

Browse files
authored
Update node-api metadata (#734)
* update metadata_types * fix compilation * add tests * fix test * fix type
1 parent bcc748e commit a1775ad

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

node-api/src/metadata/metadata_types.rs

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ use alloc::{
2020
use codec::{Decode, Encode};
2121
use frame_metadata::{
2222
v15::{
23-
ExtrinsicMetadata, PalletConstantMetadata, RuntimeApiMethodMetadata,
24-
RuntimeMetadataLastVersion, StorageEntryMetadata,
23+
CustomMetadata, ExtrinsicMetadata, OuterEnums, PalletConstantMetadata,
24+
RuntimeApiMethodMetadata, RuntimeMetadataLastVersion, StorageEntryMetadata,
2525
},
2626
RuntimeMetadata, RuntimeMetadataPrefixed, META_RESERVED,
2727
};
28-
use scale_info::{form::PortableForm, PortableRegistry, Type, Variant};
28+
use scale_info::{
29+
form::{Form, PortableForm},
30+
PortableRegistry, Type, Variant,
31+
};
2932
use sp_storage::StorageKey;
3033

3134
#[cfg(feature = "std")]
@@ -39,8 +42,6 @@ pub struct Metadata {
3942
pallets: BTreeMap<String, PalletMetadataInner>,
4043
/// Find the location in the pallet Vec by pallet index.
4144
pallets_by_index: BTreeMap<u8, String>,
42-
/// Metadata of the extrinsic.
43-
extrinsic: ExtrinsicMetadata<PortableForm>,
4445
// Type of the DispatchError type, which is what comes back if
4546
// an extrinsic fails.
4647
dispatch_error_ty: Option<u32>,
@@ -67,6 +68,11 @@ impl Metadata {
6768
Some(PalletMetadata { inner, types: self.types() })
6869
}
6970

71+
/// Return the type of the `Runtime`.
72+
pub fn ty(&self) -> &<PortableForm as Form>::Type {
73+
&self.runtime_metadata.ty
74+
}
75+
7076
/// Return the DispatchError type ID if it exists.
7177
pub fn dispatch_error_ty(&self) -> Option<u32> {
7278
self.dispatch_error_ty
@@ -89,7 +95,7 @@ impl Metadata {
8995

9096
/// Return details about the extrinsic format.
9197
pub fn extrinsic(&self) -> &ExtrinsicMetadata<PortableForm> {
92-
&self.extrinsic
98+
&self.runtime_metadata.extrinsic
9399
}
94100

95101
/// An iterator over all of the runtime APIs.
@@ -105,6 +111,16 @@ impl Metadata {
105111
Some(RuntimeApiMetadata { inner, types: self.types() })
106112
}
107113

114+
/// Return the outer enums types as found in the runtime.
115+
pub fn outer_enums(&self) -> &OuterEnums<PortableForm> {
116+
&self.runtime_metadata.outer_enums
117+
}
118+
119+
/// Returns the custom types of the metadata.
120+
pub fn custom(&self) -> &CustomMetadata<PortableForm> {
121+
&self.runtime_metadata.custom
122+
}
123+
108124
#[cfg(feature = "std")]
109125
pub fn pretty_format(&self) -> Result<String, std::string::FromUtf8Error> {
110126
let buf = Vec::new();
@@ -249,13 +265,11 @@ impl<'a> PalletMetadata<'a> {
249265
}
250266
}
251267

252-
// Based on https://github.com/paritytech/subxt/blob/8413c4d2dd625335b9200dc2289670accdf3391a/metadata/src/lib.rs#L274-L298
268+
// Based on https://github.com/paritytech/frame-metadata/blob/94e7743fa454963609763cf9cccbb7f85bc96d2f/frame-metadata/src/v15.rs#L249-L276
253269
#[derive(Debug, Clone)]
254270
struct PalletMetadataInner {
255271
/// Pallet name.
256272
name: String,
257-
/// Pallet index.
258-
index: u8,
259273
/// Pallet storage metadata.
260274
storage: BTreeMap<String, StorageEntryMetadata<PortableForm>>,
261275
/// Type ID for the pallet Call enum.
@@ -266,12 +280,15 @@ struct PalletMetadataInner {
266280
event_ty: Option<u32>,
267281
/// Event variants by name/u8.
268282
event_variant_index: VariantIndex,
283+
/// Map from constant name to constant details.
284+
constants: BTreeMap<String, PalletConstantMetadata<PortableForm>>,
269285
/// Type ID for the pallet Error enum.
270286
error_ty: Option<u32>,
271287
/// Error variants by name/u8.
272288
error_variant_index: VariantIndex,
273-
/// Map from constant name to constant details.
274-
constants: BTreeMap<String, PalletConstantMetadata<PortableForm>>,
289+
/// Define the index of the pallet, this index will be used for the encoding of pallet event,
290+
/// call and origin variants.
291+
index: u8,
275292
/// Pallet documentation.
276293
docs: Vec<String>,
277294
}
@@ -408,7 +425,6 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
408425
runtime_metadata: m.clone(),
409426
pallets,
410427
pallets_by_index,
411-
extrinsic: m.extrinsic,
412428
dispatch_error_ty,
413429
apis,
414430
})
@@ -499,3 +515,39 @@ impl Metadata {
499515
.key(first_double_map_key, second_double_map_key))
500516
}
501517
}
518+
519+
#[cfg(test)]
520+
mod tests {
521+
use super::*;
522+
use codec::Decode;
523+
use scale_info::TypeDef;
524+
use sp_core::Bytes;
525+
use std::fs;
526+
527+
fn metadata() -> Metadata {
528+
let encoded_metadata: Bytes = fs::read("./../ksm_metadata_v14.bin").unwrap().into();
529+
Decode::decode(&mut encoded_metadata.0.as_slice()).unwrap()
530+
}
531+
532+
#[test]
533+
fn outer_enum_access() {
534+
let metadata = metadata();
535+
536+
let call_enum_ty = metadata.outer_enums().call_enum_ty;
537+
let ty = metadata.types().types.get(call_enum_ty.id as usize).unwrap();
538+
if let TypeDef::Variant(variant) = &ty.ty.type_def {
539+
// The first pallet call is from System pallet.
540+
assert_eq!(variant.variants[0].name, "System");
541+
} else {
542+
panic!("Expected Variant outer enum call type.");
543+
}
544+
}
545+
546+
#[test]
547+
fn custom_ksm_metadata_v14_is_empty() {
548+
let metadata = metadata();
549+
let custom_metadata = metadata.custom();
550+
551+
assert!(custom_metadata.map.is_empty());
552+
}
553+
}

0 commit comments

Comments
 (0)