Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Stabilize V15 Metadata (#14481)
Browse files Browse the repository at this point in the history
* Update frame-metadata to latest branch

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

* Stabilize V15

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

* Update frame-metadata

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

* Use frame-metadata from crates.io

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

* Adjust testing

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

* test: Adjust frame-support metadata docs

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

* Update primitives/metadata-ir/src/lib.rs

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

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
Co-authored-by: parity-processbot <>
  • Loading branch information
lexnv and jsdw authored Jun 30, 2023
1 parent c14ff62 commit 37e1407
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
serde = { version = "1.0.163", default-features = false, features = ["alloc", "derive"] }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
frame-metadata = { version = "15.2.0", default-features = false, features = ["unstable"] }
frame-metadata = { version = "16.0.0", default-features = false, features = ["current"] }
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ pub fn expand_runtime_metadata(
}

pub fn metadata() -> #scrate::metadata::RuntimeMetadataPrefixed {
#scrate::metadata_ir::into_latest(#runtime::metadata_ir())
// Note: this always returns the V14 version. The runtime API function
// must be deprecated.
#scrate::metadata_ir::into_v14(#runtime::metadata_ir())
}

pub fn metadata_at_version(version: u32) -> Option<#scrate::OpaqueMetadata> {
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/construct_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ fn call_subtype_conversion() {

#[test]
fn test_metadata() {
use frame_support::metadata::*;
use frame_support::metadata::{v14::*, *};
use scale_info::meta_type;

let pallets = vec![
Expand Down
53 changes: 43 additions & 10 deletions frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ parameter_types! {
}

/// Latest stable metadata version used for testing.
const LATEST_METADATA_VERSION: u32 = 14;
const LATEST_METADATA_VERSION: u32 = 15;

pub struct SomeType1;
impl From<SomeType1> for u64 {
Expand Down Expand Up @@ -1300,7 +1300,8 @@ fn migrate_from_pallet_version_to_storage_version() {

#[test]
fn metadata() {
use frame_support::metadata::*;
use codec::Decode;
use frame_support::metadata::{v15::*, *};

fn maybe_docs(doc: Vec<&'static str>) -> Vec<&'static str> {
if cfg!(feature = "no-metadata-docs") {
Expand All @@ -1310,6 +1311,9 @@ fn metadata() {
}
}

let readme = "Support code for the runtime.\n\nLicense: Apache-2.0";
let expected_pallet_doc = vec![" Pallet documentation", readme, readme];

let pallets = vec![
PalletMetadata {
index: 1,
Expand Down Expand Up @@ -1570,6 +1574,7 @@ fn metadata() {
},
],
error: Some(PalletErrorMetadata { ty: meta_type::<pallet::Error<Runtime>>() }),
docs: expected_pallet_doc,
},
PalletMetadata {
index: 2,
Expand Down Expand Up @@ -1608,6 +1613,7 @@ fn metadata() {
event: Some(PalletEventMetadata { ty: meta_type::<pallet2::Event>() }),
constants: vec![],
error: None,
docs: vec![],
},
#[cfg(feature = "frame-feature-testing")]
PalletMetadata {
Expand All @@ -1618,6 +1624,7 @@ fn metadata() {
event: None,
constants: vec![],
error: None,
docs: vec![" Test that the supertrait check works when we pass some parameter to the `frame_system::Config`."],
},
#[cfg(feature = "frame-feature-testing-2")]
PalletMetadata {
Expand All @@ -1628,6 +1635,7 @@ fn metadata() {
event: None,
constants: vec![],
error: None,
docs: vec![" Test that the supertrait check works when we pass some parameter to the `frame_system::Config`."],
},
];

Expand All @@ -1642,24 +1650,48 @@ fn metadata() {
}

let extrinsic = ExtrinsicMetadata {
ty: meta_type::<UncheckedExtrinsic>(),
version: 4,
signed_extensions: vec![SignedExtensionMetadata {
identifier: "UnitSignedExtension",
ty: meta_type::<()>(),
additional_signed: meta_type::<()>(),
}],
address_ty: meta_type::<<<UncheckedExtrinsic as ExtrinsicT>::SignaturePayload as SignaturePayloadT>::SignatureAddress>(),
call_ty: meta_type::<<UncheckedExtrinsic as ExtrinsicT>::Call>(),
signature_ty: meta_type::<
<<UncheckedExtrinsic as ExtrinsicT>::SignaturePayload as SignaturePayloadT>::Signature
>(),
extra_ty: meta_type::<<<UncheckedExtrinsic as ExtrinsicT>::SignaturePayload as SignaturePayloadT>::SignatureExtra>(),
};

let expected_metadata: RuntimeMetadataPrefixed =
RuntimeMetadataLastVersion::new(pallets, extrinsic, meta_type::<Runtime>()).into();
let outer_enums = OuterEnums {
call_enum_ty: meta_type::<RuntimeCall>(),
event_enum_ty: meta_type::<RuntimeEvent>(),
error_enum_ty: meta_type::<RuntimeError>(),
};

let expected_metadata: RuntimeMetadataPrefixed = RuntimeMetadataLastVersion::new(
pallets,
extrinsic,
meta_type::<Runtime>(),
vec![],
outer_enums,
CustomMetadata { map: Default::default() },
)
.into();
let expected_metadata = match expected_metadata.1 {
RuntimeMetadata::V14(metadata) => metadata,
RuntimeMetadata::V15(metadata) => metadata,
_ => panic!("metadata has been bumped, test needs to be updated"),
};

let actual_metadata = match Runtime::metadata().1 {
RuntimeMetadata::V14(metadata) => metadata,
let bytes = &Runtime::metadata_at_version(LATEST_METADATA_VERSION)
.expect("Metadata must be present; qed");

let actual_metadata: RuntimeMetadataPrefixed =
Decode::decode(&mut &bytes[..]).expect("Metadata encoded properly; qed");

let actual_metadata = match actual_metadata.1 {
RuntimeMetadata::V15(metadata) => metadata,
_ => panic!("metadata has been bumped, test needs to be updated"),
};

Expand All @@ -1671,8 +1703,9 @@ fn metadata_at_version() {
use frame_support::metadata::*;
use sp_core::Decode;

// Metadata always returns the V14.3
let metadata = Runtime::metadata();
let at_metadata = match Runtime::metadata_at_version(LATEST_METADATA_VERSION) {
let at_metadata = match Runtime::metadata_at_version(14) {
Some(opaque) => {
let bytes = &*opaque;
let metadata: RuntimeMetadataPrefixed = Decode::decode(&mut &bytes[..]).unwrap();
Expand All @@ -1686,7 +1719,7 @@ fn metadata_at_version() {

#[test]
fn metadata_versions() {
assert_eq!(vec![LATEST_METADATA_VERSION, u32::MAX], Runtime::metadata_versions());
assert_eq!(vec![14, LATEST_METADATA_VERSION], Runtime::metadata_versions());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ fn pallet_on_genesis() {

#[test]
fn metadata() {
use frame_support::metadata::*;
use frame_support::metadata::{v14::*, *};

let system_pallet_metadata = PalletMetadata {
index: 0,
Expand Down
2 changes: 1 addition & 1 deletion primitives/metadata-ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
frame-metadata = { version = "15.2.0", default-features = false, features = ["unstable"] }
frame-metadata = { version = "16.0.0", default-features = false, features = ["current"] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
sp-std = { version = "8.0.0", default-features = false, path = "../std" }

Expand Down
25 changes: 12 additions & 13 deletions primitives/metadata-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
pub use frame_metadata;

mod types;
use frame_metadata::{RuntimeMetadataPrefixed, RuntimeMetadataV14};
use frame_metadata::RuntimeMetadataPrefixed;
pub use types::*;

mod v14;
Expand All @@ -35,25 +35,18 @@ mod v15;
const V14: u32 = 14;

/// Metadata V15.
///
/// Not yet stable, thus we set it to `u32::MAX`.
const V15: u32 = u32::MAX;
const V15: u32 = 15;

/// Transform the IR to the specified version.
///
/// Use [`supported_versions`] to find supported versions.
pub fn into_version(metadata: MetadataIR, version: u32) -> Option<RuntimeMetadataPrefixed> {
// Note: Unstable metadata version is `u32::MAX` until stabilized.
match version {
// Latest stable version.
V14 => {
let v14: frame_metadata::v14::RuntimeMetadataV14 = metadata.into();
Some(v14.into())
},
V14 => Some(into_v14(metadata)),
// Unstable metadata.
V15 => {
let v15: frame_metadata::v15::RuntimeMetadataV15 = metadata.into();
Some(v15.into())
},
V15 => Some(into_latest(metadata)),
_ => None,
}
}
Expand All @@ -65,7 +58,13 @@ pub fn supported_versions() -> sp_std::vec::Vec<u32> {

/// Transform the IR to the latest stable metadata version.
pub fn into_latest(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
let latest: RuntimeMetadataV14 = metadata.into();
let latest: frame_metadata::v15::RuntimeMetadataV15 = metadata.into();
latest.into()
}

/// Transform the IR to metadata version 14.
pub fn into_v14(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
let latest: frame_metadata::v14::RuntimeMetadataV14 = metadata.into();
latest.into()
}

Expand Down

0 comments on commit 37e1407

Please sign in to comment.