Metadata: Retain a subset of metadata pallets (#879)

* Update cargo.lock to use scale-info v2.4.0

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* metadata: Retain only a subset of the metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Generate top level Event

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* metadata: Only retain DispatchError

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* metadata: Export just the retain method

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Retain pallets

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* metadata: Do not include extrinsic metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* retain: Fix clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* test-runtime: Generate per pallet metadata and rs file

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ui-tests: Check per metadata generated files

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "test-runtime: Generate per pallet metadata and rs file"

This reverts commit 725a2e5f8339a795892dbbd2df19e49330ae3a9b.

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ui-tests: Adjust path to metadata file

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ui-tests: Change drop order to keep `PalletMetadata` around

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update metadata/src/retain.rs

Co-authored-by: James Wilson <james@jsdw.me>

* Address feedback

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* retain: Keep extrinsic type

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Introduce `MetadataSource`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Use `MetadataSource` helper

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Use `FileOrUrl` flatten command argument

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* retain: Do not include generic type params in retained metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Adjust subxt to scale-info v2.5.0

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update scaleinfo to v2.5.0

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Remove deprecated fn

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Fix clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* benches: Use inner fields of scale info

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* address nits, and strip RuntimeCall type instead of trying to filter out use of it for better overall wins/clarity

* fix UI test

* move utils out of commands folder and fix clippy etc

* address nits

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
Alexandru Vasile
2023-04-04 18:16:51 +03:00
committed by GitHub
parent c08eb6c8f1
commit 8a7c17289a
14 changed files with 464 additions and 72 deletions
+4 -25
View File
@@ -2,10 +2,9 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use crate::utils::FileOrUrl;
use clap::Parser as ClapParser;
use color_eyre::eyre;
use jsonrpsee::client_transport::ws::Uri;
use std::{fs, io::Read, path::PathBuf};
use subxt_codegen::{DerivesRegistry, TypeSubstitutes, TypeSubstitutionError};
/// Generate runtime API client code from metadata.
@@ -15,12 +14,8 @@ use subxt_codegen::{DerivesRegistry, TypeSubstitutes, TypeSubstitutionError};
/// `subxt codegen | rustfmt --edition=2018 --emit=stdout`
#[derive(Debug, ClapParser)]
pub struct Opts {
/// The url of the substrate node to query for metadata for codegen.
#[clap(name = "url", long, value_parser)]
url: Option<Uri>,
/// The path to the encoded metadata file.
#[clap(short, long, value_parser)]
file: Option<PathBuf>,
#[command(flatten)]
file_or_url: FileOrUrl,
/// Additional derives
#[clap(long = "derive")]
derives: Vec<String>,
@@ -65,23 +60,7 @@ fn substitute_type_parser(src: &str) -> Result<(String, String), String> {
}
pub async fn run(opts: Opts) -> color_eyre::Result<()> {
let bytes = if let Some(file) = opts.file.as_ref() {
if opts.url.is_some() {
eyre::bail!("specify one of `--url` or `--file` but not both")
};
let mut file = fs::File::open(file)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
bytes
} else {
let url = opts.url.unwrap_or_else(|| {
"http://localhost:9933"
.parse::<Uri>()
.expect("default url is valid")
});
subxt_codegen::utils::fetch_metadata_bytes(&url).await?
};
let bytes = opts.file_or_url.fetch().await?;
codegen(
&bytes,
+30 -16
View File
@@ -2,47 +2,61 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use crate::utils::FileOrUrl;
use clap::Parser as ClapParser;
use color_eyre::eyre;
use frame_metadata::RuntimeMetadataPrefixed;
use jsonrpsee::client_transport::ws::Uri;
use scale::Decode;
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
use scale::{Decode, Encode};
use std::io::{self, Write};
use subxt_codegen::utils::fetch_metadata_hex;
use subxt_metadata::retain_metadata_pallets;
/// Download metadata from a substrate node, for use with `subxt` codegen.
#[derive(Debug, ClapParser)]
pub struct Opts {
/// The url of the substrate node to query for metadata.
#[clap(
name = "url",
long,
value_parser,
default_value = "http://localhost:9933"
)]
url: Uri,
#[command(flatten)]
file_or_url: FileOrUrl,
/// The format of the metadata to display: `json`, `hex` or `bytes`.
#[clap(long, short, default_value = "bytes")]
format: String,
/// Generate a subset of the metadata that contains only the
/// types needed to represent the provided pallets.
#[clap(long, use_value_delimiter = true, value_parser)]
pallets: Option<Vec<String>>,
}
pub async fn run(opts: Opts) -> color_eyre::Result<()> {
let hex_data = fetch_metadata_hex(&opts.url).await?;
let bytes = opts.file_or_url.fetch().await?;
let mut metadata = <RuntimeMetadataPrefixed as Decode>::decode(&mut &bytes[..])?;
if let Some(pallets) = opts.pallets {
let metadata_v14 = match &mut metadata.1 {
RuntimeMetadata::V14(metadata_v14) => metadata_v14,
_ => {
return Err(eyre::eyre!(
"Unsupported metadata version {:?}, expected V14.",
metadata.1
))
}
};
retain_metadata_pallets(metadata_v14, |pallet_name| {
pallets.iter().any(|p| &**p == pallet_name)
});
}
match opts.format.as_str() {
"json" => {
let bytes = hex::decode(hex_data.trim_start_matches("0x"))?;
let metadata = <RuntimeMetadataPrefixed as Decode>::decode(&mut &bytes[..])?;
let json = serde_json::to_string_pretty(&metadata)?;
println!("{json}");
Ok(())
}
"hex" => {
let hex_data = format!("0x{:?}", hex::encode(metadata.encode()));
println!("{hex_data}");
Ok(())
}
"bytes" => {
let bytes = hex::decode(hex_data.trim_start_matches("0x"))?;
let bytes = metadata.encode();
Ok(io::stdout().write_all(&bytes)?)
}
_ => Err(eyre::eyre!(
+1
View File
@@ -5,6 +5,7 @@
#![deny(unused_crate_dependencies)]
mod commands;
mod utils;
use clap::Parser as ClapParser;
/// Subxt utilities for interacting with Substrate based nodes.
+45
View File
@@ -0,0 +1,45 @@
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use clap::Args;
use color_eyre::eyre;
use std::{fs, io::Read, path::PathBuf};
use subxt_codegen::utils::Uri;
/// The source of the metadata.
#[derive(Debug, Args)]
pub struct FileOrUrl {
/// The url of the substrate node to query for metadata for codegen.
#[clap(long, value_parser)]
url: Option<Uri>,
/// The path to the encoded metadata file.
#[clap(long, value_parser)]
file: Option<PathBuf>,
}
impl FileOrUrl {
/// Fetch the metadata bytes.
pub async fn fetch(&self) -> color_eyre::Result<Vec<u8>> {
match (&self.file, &self.url) {
// Can't provide both --file and --url
(Some(_), Some(_)) => {
eyre::bail!("specify one of `--url` or `--file` but not both")
}
// Load from --file path
(Some(path), None) => {
let mut file = fs::File::open(path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
Ok(bytes)
}
// Fetch from --url
(None, Some(uri)) => Ok(subxt_codegen::utils::fetch_metadata_bytes(uri).await?),
// Default if neither is provided; fetch from local url
(None, None) => {
let uri = Uri::from_static("http://localhost:9933");
Ok(subxt_codegen::utils::fetch_metadata_bytes(&uri).await?)
}
}
}
}