frame_metadata/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Decodable variant of the RuntimeMetadata.
17
18#![cfg_attr(not(feature = "std"), no_std)]
19#![warn(missing_docs)]
20#[cfg(all(
21	any(feature = "decode", feature = "serde_full"),
22	feature = "legacy",
23	not(feature = "std")
24))]
25compile_error!("decode and serde_full features prior to v14 require std");
26
27#[cfg(feature = "serde_full")]
28use serde::{Deserialize, Serialize};
29
30#[cfg(feature = "decode")]
31use codec::{Decode, Error, Input};
32
33cfg_if::cfg_if! {
34	if #[cfg(not(feature = "std"))] {
35		extern crate alloc;
36		use alloc::vec::Vec;
37	}
38}
39
40use codec::{Encode, Output};
41
42/// A type that decodes to a different type than it encodes.
43#[cfg(feature = "legacy")]
44pub mod decode_different;
45
46/// Metadata v8
47#[cfg(feature = "legacy")]
48pub mod v8;
49
50/// Metadata v9
51#[cfg(feature = "legacy")]
52pub mod v9;
53
54/// Metadata v10
55#[cfg(feature = "legacy")]
56pub mod v10;
57
58/// Metadata v11
59#[cfg(feature = "legacy")]
60pub mod v11;
61
62/// Metadata v12
63#[cfg(feature = "legacy")]
64pub mod v12;
65
66/// Metadata v13
67#[cfg(feature = "legacy")]
68pub mod v13;
69
70/// Metadata v14
71#[cfg(feature = "current")]
72pub mod v14;
73
74/// Metadata v15
75#[cfg(feature = "current")]
76pub mod v15;
77
78/// Metadata prefix.
79pub const META_RESERVED: u32 = 0x6174656d; // 'meta' warning for endianness.
80
81/// Metadata prefixed by a u32 for reserved usage
82#[derive(Eq, Encode, PartialEq, Debug)]
83#[cfg_attr(feature = "decode", derive(Decode))]
84#[cfg_attr(feature = "serde_full", derive(Serialize))]
85pub struct RuntimeMetadataPrefixed(pub u32, pub RuntimeMetadata);
86
87impl From<RuntimeMetadataPrefixed> for Vec<u8> {
88	fn from(value: RuntimeMetadataPrefixed) -> Self {
89		value.encode()
90	}
91}
92
93/// The metadata of a runtime.
94/// The version ID encoded/decoded through
95/// the enum nature of `RuntimeMetadata`.
96#[derive(Eq, Encode, PartialEq, Debug)]
97#[cfg_attr(feature = "decode", derive(Decode))]
98#[cfg_attr(feature = "serde_full", derive(Serialize))]
99pub enum RuntimeMetadata {
100	/// Unused; enum filler.
101	V0(RuntimeMetadataDeprecated),
102	/// Version 1 for runtime metadata. No longer used.
103	V1(RuntimeMetadataDeprecated),
104	/// Version 2 for runtime metadata. No longer used.
105	V2(RuntimeMetadataDeprecated),
106	/// Version 3 for runtime metadata. No longer used.
107	V3(RuntimeMetadataDeprecated),
108	/// Version 4 for runtime metadata. No longer used.
109	V4(RuntimeMetadataDeprecated),
110	/// Version 5 for runtime metadata. No longer used.
111	V5(RuntimeMetadataDeprecated),
112	/// Version 6 for runtime metadata. No longer used.
113	V6(RuntimeMetadataDeprecated),
114	/// Version 7 for runtime metadata. No longer used.
115	V7(RuntimeMetadataDeprecated),
116	/// Version 8 for runtime metadata.
117	#[cfg(feature = "legacy")]
118	V8(v8::RuntimeMetadataV8),
119	/// Version 8 for runtime metadata, as raw encoded bytes.
120	#[cfg(not(feature = "legacy"))]
121	V8(OpaqueMetadata),
122	/// Version 9 for runtime metadata.
123	#[cfg(feature = "legacy")]
124	V9(v9::RuntimeMetadataV9),
125	/// Version 9 for runtime metadata, as raw encoded bytes.
126	#[cfg(not(feature = "legacy"))]
127	V9(OpaqueMetadata),
128	/// Version 10 for runtime metadata.
129	#[cfg(feature = "legacy")]
130	V10(v10::RuntimeMetadataV10),
131	/// Version 10 for runtime metadata, as raw encoded bytes.
132	#[cfg(not(feature = "legacy"))]
133	V10(OpaqueMetadata),
134	/// Version 11 for runtime metadata.
135	#[cfg(feature = "legacy")]
136	V11(v11::RuntimeMetadataV11),
137	/// Version 11 for runtime metadata, as raw encoded bytes.
138	#[cfg(not(feature = "legacy"))]
139	V11(OpaqueMetadata),
140	/// Version 12 for runtime metadata
141	#[cfg(feature = "legacy")]
142	V12(v12::RuntimeMetadataV12),
143	/// Version 12 for runtime metadata, as raw encoded bytes.
144	#[cfg(not(feature = "legacy"))]
145	V12(OpaqueMetadata),
146	/// Version 13 for runtime metadata.
147	#[cfg(feature = "legacy")]
148	V13(v13::RuntimeMetadataV13),
149	/// Version 13 for runtime metadata, as raw encoded bytes.
150	#[cfg(not(feature = "legacy"))]
151	V13(OpaqueMetadata),
152	/// Version 14 for runtime metadata.
153	#[cfg(feature = "current")]
154	V14(v14::RuntimeMetadataV14),
155	/// Version 14 for runtime metadata, as raw encoded bytes.
156	#[cfg(not(feature = "current"))]
157	V14(OpaqueMetadata),
158	/// Version 15 for runtime metadata.
159	#[cfg(feature = "current")]
160	V15(v15::RuntimeMetadataV15),
161	/// Version 15 for runtime metadata, as raw encoded bytes.
162	#[cfg(not(feature = "current"))]
163	V15(OpaqueMetadata),
164}
165
166impl RuntimeMetadata {
167	/// Get the version number of the metadata.
168	pub fn version(&self) -> u32 {
169		match self {
170			RuntimeMetadata::V0(_) => 0,
171			RuntimeMetadata::V1(_) => 1,
172			RuntimeMetadata::V2(_) => 2,
173			RuntimeMetadata::V3(_) => 3,
174			RuntimeMetadata::V4(_) => 4,
175			RuntimeMetadata::V5(_) => 5,
176			RuntimeMetadata::V6(_) => 6,
177			RuntimeMetadata::V7(_) => 7,
178			RuntimeMetadata::V8(_) => 8,
179			RuntimeMetadata::V9(_) => 9,
180			RuntimeMetadata::V10(_) => 10,
181			RuntimeMetadata::V11(_) => 11,
182			RuntimeMetadata::V12(_) => 12,
183			RuntimeMetadata::V13(_) => 13,
184			RuntimeMetadata::V14(_) => 14,
185			RuntimeMetadata::V15(_) => 15,
186		}
187	}
188}
189
190/// Stores the encoded `RuntimeMetadata` as raw bytes.
191#[derive(Encode, Eq, PartialEq, Debug)]
192#[cfg_attr(feature = "decode", derive(Decode))]
193#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
194pub struct OpaqueMetadata(pub Vec<u8>);
195
196/// Enum that should fail.
197#[derive(Eq, PartialEq, Debug)]
198#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
199pub enum RuntimeMetadataDeprecated {}
200
201impl Encode for RuntimeMetadataDeprecated {
202	fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
203}
204
205impl codec::EncodeLike for RuntimeMetadataDeprecated {}
206
207#[cfg(feature = "decode")]
208impl Decode for RuntimeMetadataDeprecated {
209	fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
210		Err("Decoding is not supported".into())
211	}
212}
213
214#[cfg(test)]
215mod test {
216	use super::*;
217	use std::fs;
218
219	fn load_metadata(version: u32) -> Vec<u8> {
220		fs::read(format!("./test_data/ksm_metadata_v{}.bin", version)).unwrap()
221	}
222
223	#[test]
224	fn should_decode_metadatav9() {
225		let meta: RuntimeMetadataPrefixed =
226			Decode::decode(&mut load_metadata(9).as_slice()).unwrap();
227		assert!(matches!(meta.1, RuntimeMetadata::V9(_)));
228	}
229
230	#[test]
231	fn should_decode_metadatav10() {
232		let meta: RuntimeMetadataPrefixed =
233			Decode::decode(&mut load_metadata(10).as_slice()).unwrap();
234		assert!(matches!(meta.1, RuntimeMetadata::V10(_)));
235	}
236
237	#[test]
238	fn should_decode_metadatav11() {
239		let meta: RuntimeMetadataPrefixed =
240			Decode::decode(&mut load_metadata(11).as_slice()).unwrap();
241		assert!(matches!(meta.1, RuntimeMetadata::V11(_)));
242	}
243
244	#[test]
245	fn should_decode_metadatav12() {
246		let meta: RuntimeMetadataPrefixed =
247			Decode::decode(&mut load_metadata(12).as_slice()).unwrap();
248		assert!(matches!(meta.1, RuntimeMetadata::V12(_)));
249	}
250
251	#[test]
252	fn should_decode_metadatav13() {
253		let meta: RuntimeMetadataPrefixed =
254			Decode::decode(&mut load_metadata(13).as_slice()).unwrap();
255		assert!(matches!(meta.1, RuntimeMetadata::V13(_)));
256	}
257
258	#[test]
259	fn should_decode_metadatav14() {
260		let meta: RuntimeMetadataPrefixed =
261			Decode::decode(&mut load_metadata(14).as_slice()).unwrap();
262		assert!(matches!(meta.1, RuntimeMetadata::V14(_)));
263	}
264}