Skip to content

Commit 59d195d

Browse files
authored
metadata: Use v15 internally (paritytech#912)
* Update frame-metadata to v15.1.0 Signed-off-by: Alexandru Vasile <[email protected]> * Enable V15 unstable metadata in frame-metadata Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Move validation hashing to dedicated file Signed-off-by: Alexandru Vasile <[email protected]> * Use sp-metadata-ir from substrate to work with metadata Signed-off-by: Alexandru Vasile <[email protected]> * Revert using sp-metadata-ir in favor of conversion to v15 Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Convert v14 to v15 Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Use v15 for validation Signed-off-by: Alexandru Vasile <[email protected]> * codegen: Use v15 for codegen Signed-off-by: Alexandru Vasile <[email protected]> * metadata/bench: Use v15 Signed-off-by: Alexandru Vasile <[email protected]> * Adjust to v15 metadata Signed-off-by: Alexandru Vasile <[email protected]> * Adjust testing Signed-off-by: Alexandru Vasile <[email protected]> * Improve documentation Signed-off-by: Alexandru Vasile <[email protected]> * force CI Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Address feedback Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Use HASH_LEN Signed-off-by: Alexandru Vasile <[email protected]> * metadta: Remove `LatestRuntimeMetadata` type alias Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Remove `metadata_to_latest` to avoid pancis Signed-off-by: Alexandru Vasile <[email protected]> --------- Signed-off-by: Alexandru Vasile <[email protected]>
1 parent 2f1b67b commit 59d195d

30 files changed

+1089
-922
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ serde_json = "1.0.96"
3232
# hex encoded metadata to bytes
3333
hex = "0.4.3"
3434
# actual metadata types
35-
frame-metadata = { version = "15.0.0", features = ["v14", "std"] }
35+
frame-metadata = { version = "15.1.0", features = ["v14", "v15-unstable", "std"] }
3636
# decode bytes into the metadata types
3737
scale = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
3838
# generate the item mod for codegen

cli/src/commands/compatibility.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use clap::Parser as ClapParser;
66
use color_eyre::eyre::{self, WrapErr};
7-
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed, RuntimeMetadataV14, META_RESERVED};
7+
use frame_metadata::{
8+
v15::RuntimeMetadataV15, RuntimeMetadata, RuntimeMetadataPrefixed, META_RESERVED,
9+
};
810
use jsonrpsee::client_transport::ws::Uri;
911
use scale::Decode;
1012
use serde::{Deserialize, Serialize};
1113
use std::collections::HashMap;
12-
use subxt_metadata::{get_metadata_hash, get_pallet_hash};
14+
use subxt_metadata::{get_metadata_hash, get_pallet_hash, metadata_v14_to_latest};
1315

1416
/// Verify metadata compatibility between substrate nodes.
1517
#[derive(Debug, ClapParser)]
@@ -94,7 +96,7 @@ async fn handle_full_metadata(nodes: &[Uri]) -> color_eyre::Result<()> {
9496
Ok(())
9597
}
9698

97-
async fn fetch_runtime_metadata(url: &Uri) -> color_eyre::Result<RuntimeMetadataV14> {
99+
async fn fetch_runtime_metadata(url: &Uri) -> color_eyre::Result<RuntimeMetadataV15> {
98100
let bytes = subxt_codegen::utils::fetch_metadata_bytes(url).await?;
99101

100102
let metadata = <RuntimeMetadataPrefixed as Decode>::decode(&mut &bytes[..])?;
@@ -108,7 +110,8 @@ async fn fetch_runtime_metadata(url: &Uri) -> color_eyre::Result<RuntimeMetadata
108110
}
109111

110112
match metadata.1 {
111-
RuntimeMetadata::V14(v14) => Ok(v14),
113+
RuntimeMetadata::V14(v14) => Ok(metadata_v14_to_latest(v14)),
114+
RuntimeMetadata::V15(v15) => Ok(v15),
112115
_ => Err(eyre::eyre!(
113116
"Node {:?} with unsupported metadata version: {:?}",
114117
url,

cli/src/commands/metadata.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use color_eyre::eyre;
88
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
99
use scale::{Decode, Encode};
1010
use std::io::{self, Write};
11-
use subxt_metadata::retain_metadata_pallets;
11+
use subxt_metadata::{metadata_v14_to_latest, retain_metadata_pallets};
1212

1313
/// Download metadata from a substrate node, for use with `subxt` codegen.
1414
#[derive(Debug, ClapParser)]
@@ -20,6 +20,9 @@ pub struct Opts {
2020
format: String,
2121
/// Generate a subset of the metadata that contains only the
2222
/// types needed to represent the provided pallets.
23+
///
24+
/// The returned metadata is updated to the latest available version
25+
/// when using the option.
2326
#[clap(long, use_value_delimiter = true, value_parser)]
2427
pallets: Option<Vec<String>>,
2528
}
@@ -29,8 +32,9 @@ pub async fn run(opts: Opts) -> color_eyre::Result<()> {
2932
let mut metadata = <RuntimeMetadataPrefixed as Decode>::decode(&mut &bytes[..])?;
3033

3134
if let Some(pallets) = opts.pallets {
32-
let metadata_v14 = match &mut metadata.1 {
33-
RuntimeMetadata::V14(metadata_v14) => metadata_v14,
35+
let mut metadata_v15 = match metadata.1 {
36+
RuntimeMetadata::V14(metadata_v14) => metadata_v14_to_latest(metadata_v14),
37+
RuntimeMetadata::V15(metadata_v15) => metadata_v15,
3438
_ => {
3539
return Err(eyre::eyre!(
3640
"Unsupported metadata version {:?}, expected V14.",
@@ -39,9 +43,10 @@ pub async fn run(opts: Opts) -> color_eyre::Result<()> {
3943
}
4044
};
4145

42-
retain_metadata_pallets(metadata_v14, |pallet_name| {
46+
retain_metadata_pallets(&mut metadata_v15, |pallet_name| {
4347
pallets.iter().any(|p| &**p == pallet_name)
4448
});
49+
metadata = metadata_v15.into();
4550
}
4651

4752
match opts.format.as_str() {

codegen/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ description = "Generate an API for interacting with a substrate node from FRAME
1515
[dependencies]
1616
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full"] }
1717
darling = "0.14.4"
18-
frame-metadata = "15.0.0"
18+
frame-metadata = { version = "15.1.0", features = ["v14", "v15-unstable", "std"] }
1919
heck = "0.4.1"
2020
proc-macro2 = "1.0.55"
2121
quote = "1.0.8"

codegen/src/api/calls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
types::{CompositeDefFields, TypeGenerator},
88
CratePath,
99
};
10-
use frame_metadata::{v14::RuntimeMetadataV14, PalletMetadata};
10+
use frame_metadata::v15::{PalletMetadata, RuntimeMetadataV15};
1111
use heck::{ToSnakeCase as _, ToUpperCamelCase as _};
1212
use proc_macro2::TokenStream as TokenStream2;
1313
use quote::{format_ident, quote};
@@ -23,7 +23,7 @@ use scale_info::form::PortableForm;
2323
/// - `pallet` - Pallet metadata from which the calls are generated.
2424
/// - `types_mod_ident` - The ident of the base module that we can use to access the generated types from.
2525
pub fn generate_calls(
26-
metadata: &RuntimeMetadataV14,
26+
metadata: &RuntimeMetadataV15,
2727
type_gen: &TypeGenerator,
2828
pallet: &PalletMetadata<PortableForm>,
2929
types_mod_ident: &syn::Ident,

codegen/src/api/constants.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// see LICENSE for license details.
44

55
use crate::{types::TypeGenerator, CratePath};
6-
use frame_metadata::{v14::RuntimeMetadataV14, PalletMetadata};
6+
use frame_metadata::v15::{PalletMetadata, RuntimeMetadataV15};
77
use heck::ToSnakeCase as _;
88
use proc_macro2::TokenStream as TokenStream2;
99
use quote::{format_ident, quote};
@@ -35,7 +35,7 @@ use super::CodegenError;
3535
/// - `pallet` - Pallet metadata from which the calls are generated.
3636
/// - `types_mod_ident` - The ident of the base module that we can use to access the generated types from.
3737
pub fn generate_constants(
38-
metadata: &RuntimeMetadataV14,
38+
metadata: &RuntimeMetadataV15,
3939
type_gen: &TypeGenerator,
4040
pallet: &PalletMetadata<PortableForm>,
4141
types_mod_ident: &syn::Ident,

codegen/src/api/events.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// see LICENSE for license details.
44

55
use crate::{types::TypeGenerator, CratePath};
6-
use frame_metadata::PalletMetadata;
6+
use frame_metadata::v15::PalletMetadata;
77
use proc_macro2::TokenStream as TokenStream2;
88
use quote::quote;
99
use scale_info::form::PortableForm;

codegen/src/api/mod.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ mod constants;
99
mod events;
1010
mod storage;
1111

12-
use subxt_metadata::get_metadata_per_pallet_hash;
12+
use frame_metadata::v15::RuntimeMetadataV15;
13+
use subxt_metadata::{get_metadata_per_pallet_hash, metadata_v14_to_latest};
1314

1415
use super::DerivesRegistry;
1516
use crate::error::CodegenError;
@@ -20,7 +21,7 @@ use crate::{
2021
CratePath,
2122
};
2223
use codec::Decode;
23-
use frame_metadata::{v14::RuntimeMetadataV14, RuntimeMetadata, RuntimeMetadataPrefixed};
24+
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
2425
use heck::ToSnakeCase as _;
2526
use proc_macro2::TokenStream as TokenStream2;
2627
use quote::{format_ident, quote};
@@ -152,7 +153,7 @@ pub fn generate_runtime_api_from_bytes(
152153

153154
/// Create the API for interacting with a Substrate runtime.
154155
pub struct RuntimeGenerator {
155-
metadata: RuntimeMetadataV14,
156+
metadata: RuntimeMetadataV15,
156157
}
157158

158159
impl RuntimeGenerator {
@@ -161,11 +162,20 @@ impl RuntimeGenerator {
161162
/// **Note:** If you have the metadata path, URL or bytes to hand, prefer to use
162163
/// one of the `generate_runtime_api_from_*` functions for generating the runtime API
163164
/// from that.
165+
///
166+
/// # Panics
167+
///
168+
/// Panics if the runtime metadata version is not supported.
169+
///
170+
/// Supported versions: v14 and v15.
164171
pub fn new(metadata: RuntimeMetadataPrefixed) -> Self {
165-
match metadata.1 {
166-
RuntimeMetadata::V14(v14) => Self { metadata: v14 },
172+
let metadata = match metadata.1 {
173+
RuntimeMetadata::V14(v14) => metadata_v14_to_latest(v14),
174+
RuntimeMetadata::V15(v15) => v15,
167175
_ => panic!("Unsupported metadata version {:?}", metadata.1),
168-
}
176+
};
177+
178+
RuntimeGenerator { metadata }
169179
}
170180

171181
/// Generate the API for interacting with a Substrate runtime.

codegen/src/api/storage.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// see LICENSE for license details.
44

55
use crate::{types::TypeGenerator, CratePath};
6-
use frame_metadata::{
7-
v14::RuntimeMetadataV14, PalletMetadata, StorageEntryMetadata, StorageEntryModifier,
6+
use frame_metadata::v15::{
7+
PalletMetadata, RuntimeMetadataV15, StorageEntryMetadata, StorageEntryModifier,
88
StorageEntryType,
99
};
1010
use heck::ToSnakeCase as _;
@@ -24,7 +24,7 @@ use super::CodegenError;
2424
/// - `pallet` - Pallet metadata from which the storages are generated.
2525
/// - `types_mod_ident` - The ident of the base module that we can use to access the generated types from.
2626
pub fn generate_storage(
27-
metadata: &RuntimeMetadataV14,
27+
metadata: &RuntimeMetadataV15,
2828
type_gen: &TypeGenerator,
2929
pallet: &PalletMetadata<PortableForm>,
3030
types_mod_ident: &syn::Ident,
@@ -64,7 +64,7 @@ pub fn generate_storage(
6464
}
6565

6666
fn generate_storage_entry_fns(
67-
metadata: &RuntimeMetadataV14,
67+
metadata: &RuntimeMetadataV15,
6868
type_gen: &TypeGenerator,
6969
pallet: &PalletMetadata<PortableForm>,
7070
storage_entry: &StorageEntryMetadata<PortableForm>,

codegen/src/error.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ pub enum CodegenError {
1313
#[error("Could not find type with ID {0} in the type registry; please raise a support issue.")]
1414
TypeNotFound(u32),
1515
/// Cannot fetch the metadata bytes.
16-
#[error("Failed to fetch metadata, make sure that you're pointing at a node which is providing V14 metadata: {0}")]
16+
#[error("Failed to fetch metadata, make sure that you're pointing at a node which is providing substrate-based metadata: {0}")]
1717
Fetch(#[from] FetchMetadataError),
1818
/// Failed IO for the metadata file.
19-
#[error("Failed IO for {0}, make sure that you are providing the correct file path for metadata V14: {1}")]
19+
#[error("Failed IO for {0}, make sure that you are providing the correct file path for metadata: {1}")]
2020
Io(String, std::io::Error),
2121
/// Cannot decode the metadata bytes.
2222
#[error("Could not decode metadata, only V14 metadata is supported: {0}")]
@@ -25,7 +25,7 @@ pub enum CodegenError {
2525
#[error("Out-of-line subxt modules are not supported, make sure you are providing a body to your module: pub mod polkadot {{ ... }}")]
2626
InvalidModule(Span),
2727
/// Expected named or unnamed fields.
28-
#[error("Fields should either be all named or all unnamed, make sure you are providing a valid metadata V14: {0}")]
28+
#[error("Fields should either be all named or all unnamed, make sure you are providing a valid metadata: {0}")]
2929
InvalidFields(String),
3030
/// Substitute types must have a valid path.
3131
#[error("Type substitution error: {0}")]
@@ -34,20 +34,20 @@ pub enum CodegenError {
3434
#[error("Invalid type path {0}: {1}")]
3535
InvalidTypePath(String, syn::Error),
3636
/// Metadata for constant could not be found.
37-
#[error("Metadata for constant entry {0}_{1} could not be found. Make sure you are providing a valid metadata V14")]
37+
#[error("Metadata for constant entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
3838
MissingConstantMetadata(String, String),
3939
/// Metadata for storage could not be found.
40-
#[error("Metadata for storage entry {0}_{1} could not be found. Make sure you are providing a valid metadata V14")]
40+
#[error("Metadata for storage entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
4141
MissingStorageMetadata(String, String),
4242
/// Metadata for call could not be found.
43-
#[error("Metadata for call entry {0}_{1} could not be found. Make sure you are providing a valid metadata V14")]
43+
#[error("Metadata for call entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
4444
MissingCallMetadata(String, String),
4545
/// Call variant must have all named fields.
46-
#[error("Call variant for type {0} must have all named fields. Make sure you are providing a valid metadata V14")]
46+
#[error("Call variant for type {0} must have all named fields. Make sure you are providing a valid substrate-based metadata")]
4747
InvalidCallVariant(u32),
4848
/// Type should be an variant/enum.
4949
#[error(
50-
"{0} type should be an variant/enum type. Make sure you are providing a valid metadata V14"
50+
"{0} type should be an variant/enum type. Make sure you are providing a valid substrate-based metadata"
5151
)]
5252
InvalidType(String),
5353
}

metadata/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ description = "Command line utilities for checking metadata compatibility betwee
1515

1616
[dependencies]
1717
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full"] }
18-
frame-metadata = "15.0.0"
18+
frame-metadata = { version = "15.1.0", features = ["v14", "v15-unstable", "std"] }
1919
scale-info = "2.5.0"
2020
sp-core-hashing = "8.0.0"
2121

metadata/benches/bench.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44

55
use codec::Decode;
66
use criterion::*;
7-
use frame_metadata::{RuntimeMetadata::V14, RuntimeMetadataPrefixed, RuntimeMetadataV14};
7+
use frame_metadata::{v15::RuntimeMetadataV15, RuntimeMetadata, RuntimeMetadataPrefixed};
88
use scale_info::{form::PortableForm, TypeDef, TypeDefVariant};
99
use std::{fs, path::Path};
1010
use subxt_metadata::{
1111
get_call_hash, get_constant_hash, get_metadata_hash, get_pallet_hash, get_storage_hash,
12+
metadata_v14_to_latest,
1213
};
1314

14-
fn load_metadata() -> RuntimeMetadataV14 {
15+
fn load_metadata() -> RuntimeMetadataV15 {
1516
let bytes = fs::read(Path::new("../artifacts/polkadot_metadata.scale"))
1617
.expect("Cannot read metadata blob");
1718
let meta: RuntimeMetadataPrefixed =
1819
Decode::decode(&mut &*bytes).expect("Cannot decode scale metadata");
1920

2021
match meta.1 {
21-
V14(v14) => v14,
22+
RuntimeMetadata::V14(v14) => metadata_v14_to_latest(v14),
23+
RuntimeMetadata::V15(v15) => v15,
2224
_ => panic!("Unsupported metadata version {:?}", meta.1),
2325
}
2426
}

0 commit comments

Comments
 (0)