mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Support V16 metadata and refactor metadata code (#1967)
* WIP integrate unstable v16 metadata into Subxt * first pass moving retain to the CLI tool * Remove otuer enum variant stripping and move now simpler strip_metadata to new crate. test it * tidyup to use stripmetadata package etc * Fix / comment out tests * fmt * clippy * Fix wasm example * wasm-example fix * wasm-example fix * Maske sure to move IDs around after types.retain() * fmt * Tweak comment * Find dispatch error separately to avoid issues during mapping * Expose associated type information in pallet metadata * Hopefully fix flaky archive RPC * remove unwanted temp file * Address nits * Add back commented-otu tests and address review comments * use either, and simplify for_each
This commit is contained in:
@@ -6,9 +6,9 @@ use crate::utils::{validate_url_security, FileOrUrl};
|
||||
use clap::Parser as ClapParser;
|
||||
use codec::{Decode, Encode};
|
||||
use color_eyre::eyre::{self, bail};
|
||||
use frame_metadata::{v15::RuntimeMetadataV15, RuntimeMetadata, RuntimeMetadataPrefixed};
|
||||
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
|
||||
use std::{io::Write, path::PathBuf};
|
||||
use subxt_metadata::Metadata;
|
||||
use subxt_utils_stripmetadata::StripMetadata;
|
||||
|
||||
/// Download metadata from a substrate node, for use with `subxt` codegen.
|
||||
#[derive(Debug, ClapParser)]
|
||||
@@ -43,35 +43,26 @@ pub struct Opts {
|
||||
pub async fn run(opts: Opts, output: &mut impl Write) -> color_eyre::Result<()> {
|
||||
validate_url_security(opts.file_or_url.url.as_ref(), opts.allow_insecure)?;
|
||||
let bytes = opts.file_or_url.fetch().await?;
|
||||
|
||||
let mut metadata = RuntimeMetadataPrefixed::decode(&mut &bytes[..])?;
|
||||
|
||||
let version = match &metadata.1 {
|
||||
RuntimeMetadata::V14(_) => Version::V14,
|
||||
RuntimeMetadata::V15(_) => Version::V15,
|
||||
_ => Version::Unknown,
|
||||
};
|
||||
|
||||
// Strip pallets or runtime APIs if names are provided:
|
||||
if opts.pallets.is_some() || opts.runtime_apis.is_some() {
|
||||
// convert to internal type:
|
||||
let mut md = Metadata::try_from(metadata)?;
|
||||
|
||||
// retain pallets and/or runtime APIs given:
|
||||
let retain_pallets_fn: Box<dyn Fn(&str) -> bool> = match opts.pallets.as_ref() {
|
||||
let keep_pallets_fn: Box<dyn Fn(&str) -> bool> = match opts.pallets.as_ref() {
|
||||
Some(pallets) => Box::new(|name| pallets.iter().any(|p| &**p == name)),
|
||||
None => Box::new(|_| true),
|
||||
};
|
||||
let retain_runtime_apis_fn: Box<dyn Fn(&str) -> bool> = match opts.runtime_apis.as_ref() {
|
||||
let keep_runtime_apis_fn: Box<dyn Fn(&str) -> bool> = match opts.runtime_apis.as_ref() {
|
||||
Some(apis) => Box::new(|name| apis.iter().any(|p| &**p == name)),
|
||||
None => Box::new(|_| true),
|
||||
};
|
||||
md.retain(retain_pallets_fn, retain_runtime_apis_fn);
|
||||
|
||||
// Convert back to wire format, preserving version:
|
||||
metadata = match version {
|
||||
Version::V14 => RuntimeMetadataV15::from(md).into(),
|
||||
Version::V15 => RuntimeMetadataV15::from(md).into(),
|
||||
Version::Unknown => {
|
||||
bail!("Unsupported metadata version; V14 or V15 metadata is expected.")
|
||||
match &mut metadata.1 {
|
||||
RuntimeMetadata::V14(md) => md.strip_metadata(keep_pallets_fn, keep_runtime_apis_fn),
|
||||
RuntimeMetadata::V15(md) => md.strip_metadata(keep_pallets_fn, keep_runtime_apis_fn),
|
||||
RuntimeMetadata::V16(md) => md.strip_metadata(keep_pallets_fn, keep_runtime_apis_fn),
|
||||
_ => {
|
||||
bail!("Unsupported metadata version for stripping pallets/runtime APIs: V14, V15 or V16 metadata is expected.")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,9 +94,3 @@ pub async fn run(opts: Opts, output: &mut impl Write) -> color_eyre::Result<()>
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
enum Version {
|
||||
V14,
|
||||
V15,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user