From 37e14070acbf432e6c82857d9fc7cd4dbd7fa303 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Fri, 30 Jun 2023 12:33:35 +0300 Subject: [PATCH] Stabilize V15 Metadata (#14481) * Update frame-metadata to latest branch Signed-off-by: Alexandru Vasile * Stabilize V15 Signed-off-by: Alexandru Vasile * Update frame-metadata Signed-off-by: Alexandru Vasile * Use frame-metadata from crates.io Signed-off-by: Alexandru Vasile * Adjust testing Signed-off-by: Alexandru Vasile * test: Adjust frame-support metadata docs Signed-off-by: Alexandru Vasile * Update primitives/metadata-ir/src/lib.rs Co-authored-by: James Wilson --------- Signed-off-by: Alexandru Vasile Co-authored-by: James Wilson Co-authored-by: parity-processbot <> --- Cargo.lock | 4 +- frame/support/Cargo.toml | 2 +- .../src/construct_runtime/expand/metadata.rs | 4 +- frame/support/test/tests/construct_runtime.rs | 2 +- frame/support/test/tests/pallet.rs | 53 +++++++++++++++---- frame/support/test/tests/pallet_instance.rs | 2 +- primitives/metadata-ir/Cargo.toml | 2 +- primitives/metadata-ir/src/lib.rs | 25 +++++---- 8 files changed, 64 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4eeeab6fc36a..bf6ea25cb24a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2699,9 +2699,9 @@ dependencies = [ [[package]] name = "frame-metadata" -version = "15.2.0" +version = "16.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2a893ede8dde2293e94dacf9c8f5db5d0506cd909257a8f0ac2b7d610baf50c" +checksum = "87cf1549fba25a6fcac22785b61698317d958e96cac72a59102ea45b9ae64692" dependencies = [ "cfg-if", "parity-scale-codec", diff --git a/frame/support/Cargo.toml b/frame/support/Cargo.toml index 045f1169045c8..b9fd5e7e65549 100644 --- a/frame/support/Cargo.toml +++ b/frame/support/Cargo.toml @@ -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" } diff --git a/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/frame/support/procedural/src/construct_runtime/expand/metadata.rs index 7b99ecf6ad273..3f88d8c6d1353 100644 --- a/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -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> { diff --git a/frame/support/test/tests/construct_runtime.rs b/frame/support/test/tests/construct_runtime.rs index 85e790095eb31..f91ea6cdf4127 100644 --- a/frame/support/test/tests/construct_runtime.rs +++ b/frame/support/test/tests/construct_runtime.rs @@ -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![ diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index b7a7e2a767a6e..5129346acea55 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -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 for u64 { @@ -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") { @@ -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, @@ -1570,6 +1574,7 @@ fn metadata() { }, ], error: Some(PalletErrorMetadata { ty: meta_type::>() }), + docs: expected_pallet_doc, }, PalletMetadata { index: 2, @@ -1608,6 +1613,7 @@ fn metadata() { event: Some(PalletEventMetadata { ty: meta_type::() }), constants: vec![], error: None, + docs: vec![], }, #[cfg(feature = "frame-feature-testing")] PalletMetadata { @@ -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 { @@ -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`."], }, ]; @@ -1642,24 +1650,48 @@ fn metadata() { } let extrinsic = ExtrinsicMetadata { - ty: meta_type::(), version: 4, signed_extensions: vec![SignedExtensionMetadata { identifier: "UnitSignedExtension", ty: meta_type::<()>(), additional_signed: meta_type::<()>(), }], + address_ty: meta_type::<<::SignaturePayload as SignaturePayloadT>::SignatureAddress>(), + call_ty: meta_type::<::Call>(), + signature_ty: meta_type::< + <::SignaturePayload as SignaturePayloadT>::Signature + >(), + extra_ty: meta_type::<<::SignaturePayload as SignaturePayloadT>::SignatureExtra>(), }; - let expected_metadata: RuntimeMetadataPrefixed = - RuntimeMetadataLastVersion::new(pallets, extrinsic, meta_type::()).into(); + let outer_enums = OuterEnums { + call_enum_ty: meta_type::(), + event_enum_ty: meta_type::(), + error_enum_ty: meta_type::(), + }; + + let expected_metadata: RuntimeMetadataPrefixed = RuntimeMetadataLastVersion::new( + pallets, + extrinsic, + meta_type::(), + 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"), }; @@ -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(); @@ -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] diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index 7918f93605e5c..03dcc455096e9 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -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, diff --git a/primitives/metadata-ir/Cargo.toml b/primitives/metadata-ir/Cargo.toml index 924fc08027563..49fd23e208e41 100644 --- a/primitives/metadata-ir/Cargo.toml +++ b/primitives/metadata-ir/Cargo.toml @@ -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" } diff --git a/primitives/metadata-ir/src/lib.rs b/primitives/metadata-ir/src/lib.rs index 9dc03f964a09e..edfa58f861894 100644 --- a/primitives/metadata-ir/src/lib.rs +++ b/primitives/metadata-ir/src/lib.rs @@ -25,7 +25,7 @@ pub use frame_metadata; mod types; -use frame_metadata::{RuntimeMetadataPrefixed, RuntimeMetadataV14}; +use frame_metadata::RuntimeMetadataPrefixed; pub use types::*; mod v14; @@ -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 { + // 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, } } @@ -65,7 +58,13 @@ pub fn supported_versions() -> sp_std::vec::Vec { /// 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() }