1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#![cfg_attr(not(feature = "std"), no_std)]
cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
use codec::{Decode, Error, Input};
use serde::{
Deserialize,
Serialize,
};
} else {
extern crate alloc;
use alloc::vec::Vec;
}
}
use codec::{Encode, Output};
#[cfg(any(
feature = "v13",
feature = "v12",
feature = "v11",
feature = "v10",
feature = "v9",
feature = "v8",
feature = "legacy"
))]
pub mod decode_different;
#[cfg(feature = "v8")]
pub mod v8;
#[cfg(feature = "v9")]
pub mod v9;
#[cfg(feature = "v10")]
pub mod v10;
#[cfg(feature = "v11")]
pub mod v11;
#[cfg(feature = "v12")]
pub mod v12;
#[cfg(feature = "v13")]
pub mod v13;
#[cfg(feature = "v14")]
pub mod v14;
#[cfg(feature = "v14")]
pub use self::v14::*;
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
pub struct RuntimeMetadataPrefixed(pub u32, pub RuntimeMetadata);
impl Into<Vec<u8>> for RuntimeMetadataPrefixed {
fn into(self) -> Vec<u8> {
self.encode()
}
}
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
pub enum RuntimeMetadata {
V0(RuntimeMetadataDeprecated),
V1(RuntimeMetadataDeprecated),
V2(RuntimeMetadataDeprecated),
V3(RuntimeMetadataDeprecated),
V4(RuntimeMetadataDeprecated),
V5(RuntimeMetadataDeprecated),
V6(RuntimeMetadataDeprecated),
V7(RuntimeMetadataDeprecated),
#[cfg(any(feature = "v8", feature = "legacy"))]
V8(v8::RuntimeMetadataV8),
#[cfg(not(feature = "v8"))]
V8(OpaqueMetadata),
#[cfg(any(feature = "v9", feature = "legacy"))]
V9(v9::RuntimeMetadataV9),
#[cfg(not(feature = "v9"))]
V9(OpaqueMetadata),
#[cfg(any(feature = "v10", feature = "legacy"))]
V10(v10::RuntimeMetadataV10),
#[cfg(not(feature = "v10"))]
V10(OpaqueMetadata),
#[cfg(any(feature = "v11", feature = "legacy"))]
V11(v11::RuntimeMetadataV11),
#[cfg(not(feature = "v11"))]
V11(OpaqueMetadata),
#[cfg(any(feature = "v12", feature = "legacy"))]
V12(v12::RuntimeMetadataV12),
#[cfg(not(feature = "v12"))]
V12(OpaqueMetadata),
#[cfg(any(feature = "v13", feature = "legacy"))]
V13(v13::RuntimeMetadataV13),
#[cfg(not(feature = "v13"))]
V13(OpaqueMetadata),
#[cfg(feature = "v14")]
V14(v14::RuntimeMetadataV14),
#[cfg(not(feature = "v14"))]
V14(OpaqueMetadata),
}
impl RuntimeMetadata {
pub fn version(&self) -> u32 {
match self {
RuntimeMetadata::V0(_) => 0,
RuntimeMetadata::V1(_) => 1,
RuntimeMetadata::V2(_) => 2,
RuntimeMetadata::V3(_) => 3,
RuntimeMetadata::V4(_) => 4,
RuntimeMetadata::V5(_) => 5,
RuntimeMetadata::V6(_) => 6,
RuntimeMetadata::V7(_) => 7,
RuntimeMetadata::V8(_) => 8,
RuntimeMetadata::V9(_) => 9,
RuntimeMetadata::V10(_) => 10,
RuntimeMetadata::V11(_) => 11,
RuntimeMetadata::V12(_) => 12,
RuntimeMetadata::V13(_) => 13,
RuntimeMetadata::V14(_) => 14,
}
}
}
#[derive(Encode, Eq, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Deserialize, Debug))]
pub struct OpaqueMetadata(pub Vec<u8>);
#[derive(Eq, PartialEq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub enum RuntimeMetadataDeprecated {}
impl Encode for RuntimeMetadataDeprecated {
fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
}
impl codec::EncodeLike for RuntimeMetadataDeprecated {}
#[cfg(feature = "std")]
impl Decode for RuntimeMetadataDeprecated {
fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
Err("Decoding is not supported".into())
}
}
#[cfg(test)]
mod test {
use super::*;
use std::fs;
fn load_metadata(version: u32) -> Vec<u8> {
fs::read(format!("./test_data/ksm_metadata_v{}.bin", version)).unwrap()
}
#[test]
fn should_decode_metadatav9() {
let meta: RuntimeMetadataPrefixed =
Decode::decode(&mut load_metadata(9).as_slice()).unwrap();
assert!(matches!(meta.1, RuntimeMetadata::V9(_)));
}
#[test]
fn should_decode_metadatav10() {
let meta: RuntimeMetadataPrefixed =
Decode::decode(&mut load_metadata(10).as_slice()).unwrap();
assert!(matches!(meta.1, RuntimeMetadata::V10(_)));
}
#[test]
fn should_decode_metadatav11() {
let meta: RuntimeMetadataPrefixed =
Decode::decode(&mut load_metadata(11).as_slice()).unwrap();
assert!(matches!(meta.1, RuntimeMetadata::V11(_)));
}
#[test]
fn should_decode_metadatav12() {
let meta: RuntimeMetadataPrefixed =
Decode::decode(&mut load_metadata(12).as_slice()).unwrap();
assert!(matches!(meta.1, RuntimeMetadata::V12(_)));
}
#[test]
fn should_decode_metadatav13() {
let meta: RuntimeMetadataPrefixed =
Decode::decode(&mut load_metadata(13).as_slice()).unwrap();
assert!(matches!(meta.1, RuntimeMetadata::V13(_)));
}
#[test]
fn should_decode_metadatav14() {
let meta: RuntimeMetadataPrefixed =
Decode::decode(&mut load_metadata(14).as_slice()).unwrap();
assert!(matches!(meta.1, RuntimeMetadata::V14(_)));
}
}