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 unstable;
34mod v14;
35mod v15;
36
37/// Metadata V14.
38const V14: u32 = 14;
39
40/// Metadata V15.
41const V15: u32 = 15;
42
43/// Unstable metadata V16.
44const UNSTABLE_V16: u32 = u32::MAX;
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 - latest stable.
58		V15 => Some(into_latest(metadata)),
59
60		// Unstable metadata under `u32::MAX`.
61		UNSTABLE_V16 => Some(into_unstable(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, UNSTABLE_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 unstable metadata version 16.
85pub fn into_unstable(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
86	let latest: frame_metadata::v16::RuntimeMetadataV16 = metadata.into();
87	latest.into()
88}
89
90/// INTERNAL USE ONLY
91///
92/// Special trait that is used together with `InternalConstructRuntime` by `construct_runtime!` to
93/// fetch the runtime api metadata without exploding when there is no runtime api implementation
94/// available.
95#[doc(hidden)]
96pub trait InternalImplRuntimeApis {
97	fn runtime_metadata(&self) -> alloc::vec::Vec<RuntimeApiMetadataIR>;
98}
99
100#[cfg(test)]
101mod test {
102	use super::*;
103	use frame_metadata::{v14::META_RESERVED, RuntimeMetadata};
104	use scale_info::meta_type;
105
106	fn ir_metadata() -> MetadataIR {
107		MetadataIR {
108			pallets: vec![],
109			extrinsic: ExtrinsicMetadataIR {
110				ty: meta_type::<()>(),
111				versions: vec![0],
112				address_ty: meta_type::<()>(),
113				call_ty: meta_type::<()>(),
114				signature_ty: meta_type::<()>(),
115				extra_ty: meta_type::<()>(),
116				extensions: vec![],
117			},
118			ty: meta_type::<()>(),
119			apis: vec![],
120			outer_enums: OuterEnumsIR {
121				call_enum_ty: meta_type::<()>(),
122				event_enum_ty: meta_type::<()>(),
123				error_enum_ty: meta_type::<()>(),
124			},
125		}
126	}
127
128	#[test]
129	fn into_version_14() {
130		let ir = ir_metadata();
131		let metadata = into_version(ir, V14).expect("Should return prefixed metadata");
132
133		assert_eq!(metadata.0, META_RESERVED);
134
135		assert!(matches!(metadata.1, RuntimeMetadata::V14(_)));
136	}
137
138	#[test]
139	fn into_version_15() {
140		let ir = ir_metadata();
141		let metadata = into_version(ir, V15).expect("Should return prefixed metadata");
142
143		assert_eq!(metadata.0, META_RESERVED);
144
145		assert!(matches!(metadata.1, RuntimeMetadata::V15(_)));
146	}
147}