referrerpolicy=no-referrer-when-downgrade

sp_metadata_ir/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Intermediate representation of the runtime metadata.
19
20#![cfg_attr(not(feature = "std"), no_std)]
21#![warn(missing_docs)]
22
23extern crate alloc;
24
25// Re-export.
26#[doc(hidden)]
27pub use frame_metadata;
28
29mod types;
30use frame_metadata::RuntimeMetadataPrefixed;
31pub use types::*;
32
33mod v14;
34mod v15;
35mod v16;
36
37/// Metadata V14.
38const V14: u32 = 14;
39
40/// Metadata V15.
41const V15: u32 = 15;
42
43/// Unstable metadata V16.
44const V16: u32 = 16;
45
46/// Transform the IR to the specified version.
47///
48/// Use [`supported_versions`] to find supported versions.
49pub fn into_version(metadata: MetadataIR, version: u32) -> Option<RuntimeMetadataPrefixed> {
50	// Note: Unstable metadata version is `u32::MAX` until stabilized.
51	match version {
52		// Version V14. This needs to be around until the
53		// deprecation of the `Metadata_metadata` runtime call in favor of
54		// `Metadata_metadata_at_version.
55		V14 => Some(into_v14(metadata)),
56
57		// Version V15
58		V15 => Some(into_v15(metadata)),
59
60		// Version V16 - latest-stable.
61		V16 => Some(into_v16(metadata)),
62
63		_ => None,
64	}
65}
66
67/// Returns the supported metadata versions.
68pub fn supported_versions() -> alloc::vec::Vec<u32> {
69	alloc::vec![V14, V15, V16]
70}
71
72/// Transform the IR to the latest stable metadata version.
73pub fn into_latest(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
74	let latest: frame_metadata::v15::RuntimeMetadataV15 = metadata.into();
75	latest.into()
76}
77
78/// Transform the IR to metadata version 14.
79pub fn into_v14(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
80	let latest: frame_metadata::v14::RuntimeMetadataV14 = metadata.into();
81	latest.into()
82}
83
84/// Transform the IR to metadata version 15.
85pub fn into_v15(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
86	let latest: frame_metadata::v15::RuntimeMetadataV15 = metadata.into();
87	latest.into()
88}
89
90/// Transform the IR to metadata version 16.
91pub fn into_v16(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
92	let latest: frame_metadata::v16::RuntimeMetadataV16 = metadata.into();
93	latest.into()
94}
95
96/// INTERNAL USE ONLY
97///
98/// Special trait that is used together with `InternalConstructRuntime` by `construct_runtime!` to
99/// fetch the runtime api metadata without exploding when there is no runtime api implementation
100/// available.
101#[doc(hidden)]
102pub trait InternalImplRuntimeApis {
103	fn runtime_metadata(&self) -> alloc::vec::Vec<RuntimeApiMetadataIR>;
104}
105
106#[cfg(test)]
107mod test {
108	use super::*;
109	use frame_metadata::{v14::META_RESERVED, RuntimeMetadata};
110	use scale_info::meta_type;
111
112	fn ir_metadata() -> MetadataIR {
113		MetadataIR {
114			pallets: vec![],
115			extrinsic: ExtrinsicMetadataIR {
116				ty: meta_type::<()>(),
117				versions: vec![0],
118				address_ty: meta_type::<()>(),
119				call_ty: meta_type::<()>(),
120				signature_ty: meta_type::<()>(),
121				extra_ty: meta_type::<()>(),
122				extensions: vec![],
123			},
124			ty: meta_type::<()>(),
125			apis: vec![],
126			outer_enums: OuterEnumsIR {
127				call_enum_ty: meta_type::<()>(),
128				event_enum_ty: meta_type::<()>(),
129				error_enum_ty: meta_type::<()>(),
130			},
131		}
132	}
133
134	#[test]
135	fn into_version_14() {
136		let ir = ir_metadata();
137		let metadata = into_version(ir, V14).expect("Should return prefixed metadata");
138
139		assert_eq!(metadata.0, META_RESERVED);
140
141		assert!(matches!(metadata.1, RuntimeMetadata::V14(_)));
142	}
143
144	#[test]
145	fn into_version_15() {
146		let ir = ir_metadata();
147		let metadata = into_version(ir, V15).expect("Should return prefixed metadata");
148
149		assert_eq!(metadata.0, META_RESERVED);
150
151		assert!(matches!(metadata.1, RuntimeMetadata::V15(_)));
152	}
153
154	#[test]
155	fn into_version_16() {
156		let ir = ir_metadata();
157		let metadata = into_version(ir, V16).expect("Should return prefixed metadata");
158
159		assert_eq!(metadata.0, META_RESERVED);
160
161		assert!(matches!(metadata.1, RuntimeMetadata::V16(_)));
162	}
163}