Retain specific runtime APIs (#961)

* retain runtime apis

* fix ui test

* use boxed closures
This commit is contained in:
Tadeo Hepperle
2023-05-22 11:02:36 +02:00
committed by GitHub
parent 9980e9db72
commit e63d618b28
4 changed files with 76 additions and 33 deletions
+19 -7
View File
@@ -8,7 +8,7 @@ use codec::{Decode, Encode};
use color_eyre::eyre;
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
use std::io::{self, Write};
use subxt_metadata::{metadata_v14_to_latest, retain_metadata_pallets};
use subxt_metadata::{metadata_v14_to_latest, retain_metadata};
/// Download metadata from a substrate node, for use with `subxt` codegen.
#[derive(Debug, ClapParser)]
@@ -25,13 +25,20 @@ pub struct Opts {
/// when using the option.
#[clap(long, use_value_delimiter = true, value_parser)]
pallets: Option<Vec<String>>,
/// Generate a subset of the metadata that contains only the
/// runtime APIs needed.
///
/// The returned metadata is updated to the latest available version
/// when using the option.
#[clap(long, use_value_delimiter = true, value_parser)]
runtime_apis: Option<Vec<String>>,
}
pub async fn run(opts: Opts) -> color_eyre::Result<()> {
let bytes = opts.file_or_url.fetch().await?;
let mut metadata = <RuntimeMetadataPrefixed as Decode>::decode(&mut &bytes[..])?;
if let Some(pallets) = opts.pallets {
if opts.pallets.is_some() || opts.runtime_apis.is_some() {
let mut metadata_v15 = match metadata.1 {
RuntimeMetadata::V14(metadata_v14) => metadata_v14_to_latest(metadata_v14),
RuntimeMetadata::V15(metadata_v15) => metadata_v15,
@@ -39,13 +46,18 @@ pub async fn run(opts: Opts) -> color_eyre::Result<()> {
return Err(eyre::eyre!(
"Unsupported metadata version {:?}, expected V14.",
metadata.1
))
));
}
};
retain_metadata_pallets(&mut metadata_v15, |pallet_name| {
pallets.iter().any(|p| &**p == pallet_name)
});
let retain_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() {
Some(apis) => Box::new(|name| apis.iter().any(|p| &**p == name)),
None => Box::new(|_| true),
};
retain_metadata(&mut metadata_v15, retain_pallets_fn, retain_runtime_apis_fn);
metadata = metadata_v15.into();
}