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 v16
79#[cfg(feature = "current")]
80pub mod v16;
81
82/// Metadata prefix.
83pub const META_RESERVED: u32 = 0x6174656d; // 'meta' warning for endianness.
84
85/// Metadata prefixed by a u32 for reserved usage
86#[derive(Eq, Encode, PartialEq, Debug)]
87#[cfg_attr(feature = "decode", derive(Decode))]
88#[cfg_attr(feature = "serde_full", derive(Serialize))]
89pub struct RuntimeMetadataPrefixed(pub u32, pub RuntimeMetadata);
90
91impl From<RuntimeMetadataPrefixed> for Vec<u8> {
92	fn from(value: RuntimeMetadataPrefixed) -> Self {
93		value.encode()
94	}
95}
96
97/// The metadata of a runtime.
98/// The version ID encoded/decoded through
99/// the enum nature of `RuntimeMetadata`.
100#[derive(Eq, Encode, PartialEq, Debug)]
101#[cfg_attr(feature = "decode", derive(Decode))]
102#[cfg_attr(feature = "serde_full", derive(Serialize))]
103pub enum RuntimeMetadata {
104	/// Unused; enum filler.
105	V0(RuntimeMetadataDeprecated),
106	/// Version 1 for runtime metadata. No longer used.
107	V1(RuntimeMetadataDeprecated),
108	/// Version 2 for runtime metadata. No longer used.
109	V2(RuntimeMetadataDeprecated),
110	/// Version 3 for runtime metadata. No longer used.
111	V3(RuntimeMetadataDeprecated),
112	/// Version 4 for runtime metadata. No longer used.
113	V4(RuntimeMetadataDeprecated),
114	/// Version 5 for runtime metadata. No longer used.
115	V5(RuntimeMetadataDeprecated),
116	/// Version 6 for runtime metadata. No longer used.
117	V6(RuntimeMetadataDeprecated),
118	/// Version 7 for runtime metadata. No longer used.
119	V7(RuntimeMetadataDeprecated),
120	/// Version 8 for runtime metadata.
121	#[cfg(feature = "legacy")]
122	V8(v8::RuntimeMetadataV8),
123	/// Version 8 for runtime metadata, as raw encoded bytes.
124	#[cfg(not(feature = "legacy"))]
125	V8(OpaqueMetadata),
126	/// Version 9 for runtime metadata.
127	#[cfg(feature = "legacy")]
128	V9(v9::RuntimeMetadataV9),
129	/// Version 9 for runtime metadata, as raw encoded bytes.
130	#[cfg(not(feature = "legacy"))]
131	V9(OpaqueMetadata),
132	/// Version 10 for runtime metadata.
133	#[cfg(feature = "legacy")]
134	V10(v10::RuntimeMetadataV10),
135	/// Version 10 for runtime metadata, as raw encoded bytes.
136	#[cfg(not(feature = "legacy"))]
137	V10(OpaqueMetadata),
138	/// Version 11 for runtime metadata.
139	#[cfg(feature = "legacy")]
140	V11(v11::RuntimeMetadataV11),
141	/// Version 11 for runtime metadata, as raw encoded bytes.
142	#[cfg(not(feature = "legacy"))]
143	V11(OpaqueMetadata),
144	/// Version 12 for runtime metadata
145	#[cfg(feature = "legacy")]
146	V12(v12::RuntimeMetadataV12),
147	/// Version 12 for runtime metadata, as raw encoded bytes.
148	#[cfg(not(feature = "legacy"))]
149	V12(OpaqueMetadata),
150	/// Version 13 for runtime metadata.
151	#[cfg(feature = "legacy")]
152	V13(v13::RuntimeMetadataV13),
153	/// Version 13 for runtime metadata, as raw encoded bytes.
154	#[cfg(not(feature = "legacy"))]
155	V13(OpaqueMetadata),
156	/// Version 14 for runtime metadata.
157	#[cfg(feature = "current")]
158	V14(v14::RuntimeMetadataV14),
159	/// Version 14 for runtime metadata, as raw encoded bytes.
160	#[cfg(not(feature = "current"))]
161	V14(OpaqueMetadata),
162	/// Version 15 for runtime metadata.
163	#[cfg(feature = "current")]
164	V15(v15::RuntimeMetadataV15),
165	/// Version 15 for runtime metadata, as raw encoded bytes.
166	#[cfg(not(feature = "current"))]
167	V15(OpaqueMetadata),
168	/// Version 16 for runtime metadata.
169	#[cfg(feature = "current")]
170	V16(v16::RuntimeMetadataV16),
171	/// Version 16 for runtime metadata, as raw encoded bytes.
172	#[cfg(not(feature = "current"))]
173	V16(OpaqueMetadata),
174}
175
176impl RuntimeMetadata {
177	/// Get the version number of the metadata.
178	pub fn version(&self) -> u32 {
179		match self {
180			RuntimeMetadata::V0(_) => 0,
181			RuntimeMetadata::V1(_) => 1,
182			RuntimeMetadata::V2(_) => 2,
183			RuntimeMetadata::V3(_) => 3,
184			RuntimeMetadata::V4(_) => 4,
185			RuntimeMetadata::V5(_) => 5,
186			RuntimeMetadata::V6(_) => 6,
187			RuntimeMetadata::V7(_) => 7,
188			RuntimeMetadata::V8(_) => 8,
189			RuntimeMetadata::V9(_) => 9,
190			RuntimeMetadata::V10(_) => 10,
191			RuntimeMetadata::V11(_) => 11,
192			RuntimeMetadata::V12(_) => 12,
193			RuntimeMetadata::V13(_) => 13,
194			RuntimeMetadata::V14(_) => 14,
195			RuntimeMetadata::V15(_) => 15,
196			RuntimeMetadata::V16(_) => 16,
197		}
198	}
199}
200
201/// Stores the encoded `RuntimeMetadata` as raw bytes.
202#[derive(Encode, Eq, PartialEq, Debug)]
203#[cfg_attr(feature = "decode", derive(Decode))]
204#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
205pub struct OpaqueMetadata(pub Vec<u8>);
206
207/// Enum that should fail.
208#[derive(Eq, PartialEq, Debug)]
209#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
210pub enum RuntimeMetadataDeprecated {}
211
212impl Encode for RuntimeMetadataDeprecated {
213	fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
214}
215
216impl codec::EncodeLike for RuntimeMetadataDeprecated {}
217
218#[cfg(feature = "decode")]
219impl Decode for RuntimeMetadataDeprecated {
220	fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
221		Err("Decoding is not supported".into())
222	}
223}
224
225#[cfg(test)]
226mod test {
227	use super::*;
228	use std::fs;
229
230	fn load_metadata(version: u32) -> Vec<u8> {
231		fs::read(format!("./test_data/ksm_metadata_v{}.bin", version)).unwrap()
232	}
233
234	#[test]
235	fn should_decode_metadatav9() {
236		let meta: RuntimeMetadataPrefixed =
237			Decode::decode(&mut load_metadata(9).as_slice()).unwrap();
238		assert!(matches!(meta.1, RuntimeMetadata::V9(_)));
239	}
240
241	#[test]
242	fn should_decode_metadatav10() {
243		let meta: RuntimeMetadataPrefixed =
244			Decode::decode(&mut load_metadata(10).as_slice()).unwrap();
245		assert!(matches!(meta.1, RuntimeMetadata::V10(_)));
246	}
247
248	#[test]
249	fn should_decode_metadatav11() {
250		let meta: RuntimeMetadataPrefixed =
251			Decode::decode(&mut load_metadata(11).as_slice()).unwrap();
252		assert!(matches!(meta.1, RuntimeMetadata::V11(_)));
253	}
254
255	#[test]
256	fn should_decode_metadatav12() {
257		let meta: RuntimeMetadataPrefixed =
258			Decode::decode(&mut load_metadata(12).as_slice()).unwrap();
259		assert!(matches!(meta.1, RuntimeMetadata::V12(_)));
260	}
261
262	#[test]
263	fn should_decode_metadatav13() {
264		let meta: RuntimeMetadataPrefixed =
265			Decode::decode(&mut load_metadata(13).as_slice()).unwrap();
266		assert!(matches!(meta.1, RuntimeMetadata::V13(_)));
267	}
268
269	#[test]
270	fn should_decode_metadatav14() {
271		let meta: RuntimeMetadataPrefixed =
272			Decode::decode(&mut load_metadata(14).as_slice()).unwrap();
273		assert!(matches!(meta.1, RuntimeMetadata::V14(_)));
274	}
275}