subxt-metadata 0.50.1

Command line utilities for checking metadata compatibility between nodes.
Documentation
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use super::*;
use alloc::collections::BTreeSet;
use codec::Decode;
use core::str::FromStr;
use frame_decode::constants::ConstantTypeInfo;
use frame_decode::runtime_apis::RuntimeApiEntryInfo;
use frame_metadata::RuntimeMetadata;
use scale_info_legacy::LookupName;
use scale_type_resolver::TypeResolver;

/// Load some legacy kusama metadata from our artifacts.
fn legacy_kusama_metadata(version: u8) -> (u64, RuntimeMetadata) {
    const VERSIONS: [(u8, u64, &str); 5] = [
        (9, 1021, "metadata_v9_1021.scale"),
        (10, 1038, "metadata_v10_1038.scale"),
        (11, 1045, "metadata_v11_1045.scale"),
        (12, 2025, "metadata_v12_2025.scale"),
        (13, 9030, "metadata_v13_9030.scale"),
    ];

    let (spec_version, filename) = VERSIONS
        .iter()
        .find(|(v, _spec_version, _filename)| *v == version)
        .map(|(_, spec_version, name)| (*spec_version, *name))
        .unwrap_or_else(|| panic!("v{version} metadata artifact does not exist"));

    let mut path = std::path::PathBuf::from_str("../artifacts/kusama/").unwrap();
    path.push(filename);

    let bytes = std::fs::read(path).expect("Could not read file");
    let metadata = RuntimeMetadata::decode(&mut &*bytes).expect("Could not SCALE decode metadata");

    (spec_version, metadata)
}

/// Load our kusama types.
/// TODO: This is WRONG at the moment; change to point to kusama types when they exist:
fn kusama_types() -> scale_info_legacy::ChainTypeRegistry {
    frame_decode::legacy_types::polkadot::relay_chain()
}

/// Sanitizing paths changes things between old and new, so disable this in tests by default
/// so that we can compare paths and check that by default things translate identically.
/// Tests assume that ignore_not_found is enabled, which converts not found types to
/// special::Unknown instead of returning an error.
fn test_opts() -> super::Opts {
    super::Opts {
        sanitize_paths: false,
        ignore_not_found: true,
    }
}

/// Return a pair of original metadata + converted subxt_metadata::Metadata
fn metadata_pair(
    version: u8,
    opts: super::Opts,
) -> (TypeRegistrySet<'static>, RuntimeMetadata, crate::Metadata) {
    let (spec_version, metadata) = legacy_kusama_metadata(version);
    let types = kusama_types();

    // Extend the types with builtins.
    let types_for_spec = {
        let mut types_for_spec = types.for_spec_version(spec_version).to_owned();
        let extended_types =
            frame_decode::helpers::type_registry_from_metadata_any(&metadata).unwrap();
        types_for_spec.prepend(extended_types);
        types_for_spec
    };

    let subxt_metadata = match &metadata {
        RuntimeMetadata::V9(m) => super::from_v9(m, &types_for_spec, opts),
        RuntimeMetadata::V10(m) => super::from_v10(m, &types_for_spec, opts),
        RuntimeMetadata::V11(m) => super::from_v11(m, &types_for_spec, opts),
        RuntimeMetadata::V12(m) => super::from_v12(m, &types_for_spec, opts),
        RuntimeMetadata::V13(m) => super::from_v13(m, &types_for_spec, opts),
        _ => panic!("Metadata version {} not expected", metadata.version()),
    }
    .expect("Could not convert to subxt_metadata::Metadata");

    (types_for_spec, metadata, subxt_metadata)
}

/// A representation of the shape of some type that we can compare across metadatas.
#[derive(PartialEq, Debug, Clone)]
enum Shape {
    Array(Box<Shape>, usize),
    BitSequence(
        scale_type_resolver::BitsStoreFormat,
        scale_type_resolver::BitsOrderFormat,
    ),
    Compact(Box<Shape>),
    Composite(Vec<String>, Vec<(Option<String>, Shape)>),
    Primitive(scale_type_resolver::Primitive),
    Sequence(Vec<String>, Box<Shape>),
    Tuple(Vec<Shape>),
    Variant(Vec<String>, Vec<Variant>),
    // This is very important for performance; if we've already seen a variant at some path,
    // we'll return just the variant path next time in this, to avoid duplicating lots of variants.
    // This also eliminates recursion, since variants allow for it.
    SeenVariant(Vec<String>),
}

#[derive(PartialEq, Debug, Clone)]
struct Variant {
    index: u8,
    name: String,
    fields: Vec<(Option<String>, Shape)>,
}

impl Shape {
    /// convert some modern type definition into a [`Shape`].
    fn from_modern_type(id: u32, types: &scale_info::PortableRegistry) -> Shape {
        let mut seen_variants = BTreeSet::new();
        Shape::from_modern_type_inner(id, &mut seen_variants, types)
    }

    fn from_modern_type_inner(
        id: u32,
        seen_variants: &mut BTreeSet<Vec<String>>,
        types: &scale_info::PortableRegistry,
    ) -> Shape {
        let visitor =
            scale_type_resolver::visitor::new((seen_variants, types), |_, _| panic!("Unhandled"))
                .visit_array(|(seen_variants, types), type_id, len| {
                    let inner = Shape::from_modern_type_inner(type_id, seen_variants, types);
                    Shape::Array(Box::new(inner), len)
                })
                .visit_bit_sequence(|_, store, order| Shape::BitSequence(store, order))
                .visit_compact(|(seen_variants, types), type_id| {
                    let inner = Shape::from_modern_type_inner(type_id, seen_variants, types);
                    Shape::Compact(Box::new(inner))
                })
                .visit_composite(|(seen_variants, types), path, fields| {
                    let path = path.map(|p| p.to_owned()).collect();
                    let inners = fields
                        .map(|field| {
                            let name = field.name.map(|n| n.to_owned());
                            let inner =
                                Shape::from_modern_type_inner(field.id, seen_variants, types);
                            (name, inner)
                        })
                        .collect();
                    Shape::Composite(path, inners)
                })
                .visit_primitive(|_types, prim| Shape::Primitive(prim))
                .visit_sequence(|(seen_variants, types), path, type_id| {
                    let path = path.map(|p| p.to_owned()).collect();
                    let inner = Shape::from_modern_type_inner(type_id, seen_variants, types);
                    Shape::Sequence(path, Box::new(inner))
                })
                .visit_tuple(|(seen_variants, types), fields| {
                    let inners = fields
                        .map(|field| Shape::from_modern_type_inner(field, seen_variants, types))
                        .collect();
                    Shape::Tuple(inners)
                })
                .visit_variant(|(seen_variants, types), path, variants| {
                    let path: Vec<String> = path.map(|p| p.to_owned()).collect();
                    // very important to avoid recursion and performance costs:
                    if !seen_variants.insert(path.clone()) {
                        return Shape::SeenVariant(path);
                    }
                    let variants = variants
                        .map(|v| Variant {
                            index: v.index,
                            name: v.name.to_owned(),
                            fields: v
                                .fields
                                .map(|field| {
                                    let name = field.name.map(|n| n.to_owned());
                                    let inner = Shape::from_modern_type_inner(
                                        field.id,
                                        seen_variants,
                                        types,
                                    );
                                    (name, inner)
                                })
                                .collect(),
                        })
                        .collect();
                    Shape::Variant(path, variants)
                })
                .visit_not_found(|_types| {
                    panic!("PortableRegistry should not have a type which can't be found")
                });

        types.resolve_type(id, visitor).unwrap()
    }

    /// convert some historic type definition into a [`Shape`].
    fn from_legacy_type(name: &LookupName, types: &TypeRegistrySet<'_>) -> Shape {
        let mut seen_variants = BTreeSet::new();
        Shape::from_legacy_type_inner(name.clone(), &mut seen_variants, types)
    }

    fn from_legacy_type_inner(
        id: LookupName,
        seen_variants: &mut BTreeSet<Vec<String>>,
        types: &TypeRegistrySet<'_>,
    ) -> Shape {
        let visitor =
            scale_type_resolver::visitor::new((seen_variants, types), |_, _| panic!("Unhandled"))
                .visit_array(|(seen_variants, types), type_id, len| {
                    let inner = Shape::from_legacy_type_inner(type_id, seen_variants, types);
                    Shape::Array(Box::new(inner), len)
                })
                .visit_bit_sequence(|_types, store, order| Shape::BitSequence(store, order))
                .visit_compact(|(seen_variants, types), type_id| {
                    let inner = Shape::from_legacy_type_inner(type_id, seen_variants, types);
                    Shape::Compact(Box::new(inner))
                })
                .visit_composite(|(seen_variants, types), path, fields| {
                    let path = path.map(|p| p.to_owned()).collect();
                    let inners = fields
                        .map(|field| {
                            let name = field.name.map(|n| n.to_owned());
                            let inner =
                                Shape::from_legacy_type_inner(field.id, seen_variants, types);
                            (name, inner)
                        })
                        .collect();
                    Shape::Composite(path, inners)
                })
                .visit_primitive(|_types, prim| Shape::Primitive(prim))
                .visit_sequence(|(seen_variants, types), path, type_id| {
                    let path = path.map(|p| p.to_owned()).collect();
                    let inner = Shape::from_legacy_type_inner(type_id, seen_variants, types);
                    Shape::Sequence(path, Box::new(inner))
                })
                .visit_tuple(|(seen_variants, types), fields| {
                    let inners = fields
                        .map(|field| Shape::from_legacy_type_inner(field, seen_variants, types))
                        .collect();
                    Shape::Tuple(inners)
                })
                .visit_variant(|(seen_variants, types), path, variants| {
                    let path: Vec<String> = path.map(|p| p.to_owned()).collect();
                    // very important to avoid recursion and performance costs:
                    if !seen_variants.insert(path.clone()) {
                        return Shape::SeenVariant(path);
                    }
                    let variants = variants
                        .map(|v| Variant {
                            index: v.index,
                            name: v.name.to_owned(),
                            fields: v
                                .fields
                                .map(|field| {
                                    let name = field.name.map(|n| n.to_owned());
                                    let inner = Shape::from_legacy_type_inner(
                                        field.id,
                                        seen_variants,
                                        types,
                                    );
                                    (name, inner)
                                })
                                .collect(),
                        })
                        .collect();
                    Shape::Variant(path, variants)
                })
                .visit_not_found(|(seen_variants, _)| {
                    // When we convert legacy to modern types, any types we don't find
                    // are replaced with empty variants (since we can't have dangling types
                    // in our new PortableRegistry). Do the same here so they compare equal.
                    Shape::from_legacy_type_inner(
                        LookupName::parse("special::Unknown").unwrap(),
                        seen_variants,
                        types,
                    )
                });

        types.resolve_type(id, visitor).unwrap()
    }
}

// Go over all of the constants listed via frame-decode and check that our old
// and new metadatas both have identical output.
macro_rules! constants_eq {
    ($name:ident, $version:literal, $version_path:ident) => {
        #[test]
        fn $name() {
            let (old_types, old_md, new_md) = metadata_pair($version, test_opts());
            let RuntimeMetadata::$version_path(old_md) = old_md else {
                panic!("Wrong version")
            };

            let old: Vec<_> = old_md
                .constant_tuples()
                .map(|(p, n)| old_md.constant_info(&p, &n).unwrap())
                .map(|c| {
                    (
                        c.bytes.to_owned(),
                        Shape::from_legacy_type(&c.type_id, &old_types),
                    )
                })
                .collect();
            let new: Vec<_> = new_md
                .constant_tuples()
                .map(|(p, n)| new_md.constant_info(&p, &n).unwrap())
                .map(|c| {
                    (
                        c.bytes.to_owned(),
                        Shape::from_modern_type(c.type_id, new_md.types()),
                    )
                })
                .collect();

            assert_eq!(old, new);
        }
    };
}

constants_eq!(v9_constants_eq, 9, V9);
constants_eq!(v10_constants_eq, 10, V10);
constants_eq!(v11_constants_eq, 11, V11);
constants_eq!(v12_constants_eq, 12, V12);
constants_eq!(v13_constants_eq, 13, V13);

/// Make sure all Runtime APIs are the same once translated.
#[test]
fn runtime_apis() {
    for version in 9..=13 {
        let (old_types, _old_md, new_md) = metadata_pair(version, test_opts());

        let old: Vec<_> = old_types
            .runtime_api_tuples()
            .map(|(p, n)| {
                old_types
                    .runtime_api_info(&p, &n)
                    .unwrap()
                    .map_ids(|id| Ok::<_, ()>(Shape::from_legacy_type(&id, &old_types)))
                    .unwrap()
            })
            .collect();
        let new: Vec<_> = new_md
            .runtime_api_tuples()
            .map(|(p, n)| {
                new_md
                    .runtime_api_info(&p, &n)
                    .unwrap()
                    .map_ids(|id| Ok::<_, ()>(Shape::from_modern_type(id, new_md.types())))
                    .unwrap()
            })
            .collect();

        assert_eq!(old, new);
    }
}

macro_rules! storage_eq {
    ($name:ident, $version:literal, $version_path:ident) => {
        #[test]
        fn $name() {
            let (old_types, old_md, new_md) = metadata_pair($version, test_opts());
            let RuntimeMetadata::$version_path(old_md) = old_md else {
                panic!("Wrong version")
            };

            let old: Vec<_> = old_md
                .storage_tuples()
                .map(|(p, n)| {
                    let info = old_md
                        .storage_info(&p, &n)
                        .unwrap()
                        .map_ids(|id| Ok::<_, ()>(Shape::from_legacy_type(&id, &old_types)))
                        .unwrap();
                    (p.into_owned(), n.into_owned(), info)
                })
                .collect();

            let new: Vec<_> = new_md
                .storage_tuples()
                .map(|(p, n)| {
                    let info = new_md
                        .storage_info(&p, &n)
                        .unwrap()
                        .map_ids(|id| Ok::<_, ()>(Shape::from_modern_type(id, new_md.types())))
                        .unwrap();
                    (p.into_owned(), n.into_owned(), info)
                })
                .collect();

            if old.len() != new.len() {
                panic!("Storage entries for version 9 metadata differ in length");
            }

            for (old, new) in old.into_iter().zip(new.into_iter()) {
                assert_eq!((&old.0, &old.1), (&new.0, &new.1), "Storage entry mismatch");
                assert_eq!(
                    old.2, new.2,
                    "Storage entry {}.{} does not match!",
                    old.0, old.1
                );
            }
        }
    };
}

storage_eq!(v9_storage_eq, 9, V9);
storage_eq!(v10_storage_eq, 10, V10);
storage_eq!(v11_storage_eq, 11, V11);
storage_eq!(v12_storage_eq, 12, V12);
storage_eq!(v13_storage_eq, 13, V13);

#[test]
fn builtin_call() {
    for version in 9..=13 {
        let (old_types, _old_md, new_md) = metadata_pair(version, test_opts());

        let old = Shape::from_legacy_type(&LookupName::parse("builtin::Call").unwrap(), &old_types);
        let new = Shape::from_modern_type(new_md.outer_enums.call_enum_ty, new_md.types());
        assert_eq!(old, new, "Call types do not match in metadata V{version}!");
    }
}

#[test]
fn builtin_error() {
    for version in 9..=13 {
        let (old_types, _old_md, new_md) = metadata_pair(version, test_opts());

        let old =
            Shape::from_legacy_type(&LookupName::parse("builtin::Error").unwrap(), &old_types);
        let new = Shape::from_modern_type(new_md.outer_enums.error_enum_ty, new_md.types());
        assert_eq!(old, new, "Error types do not match in metadata V{version}!");
    }
}

#[test]
fn builtin_event() {
    for version in 9..=13 {
        let (old_types, _old_md, new_md) = metadata_pair(version, test_opts());

        let old =
            Shape::from_legacy_type(&LookupName::parse("builtin::Event").unwrap(), &old_types);
        let new = Shape::from_modern_type(new_md.outer_enums.event_enum_ty, new_md.types());
        assert_eq!(old, new, "Event types do not match in metadata V{version}!");
    }
}

#[test]
fn codegen_works() {
    for version in 9..=13 {
        // We need to do this against `subxt_codegen::Metadata` and so cannot re-use our
        // test functions for it. This is because the compiler sees some difference between
        // `subxct_codegen::Metadata` and `crate::Metadata` even though they should be identical.
        let new_md = {
            let (spec_version, metadata) = legacy_kusama_metadata(version);
            let types = kusama_types();

            let types_for_spec = {
                let mut types_for_spec = types.for_spec_version(spec_version).to_owned();
                let extended_types =
                    frame_decode::helpers::type_registry_from_metadata_any(&metadata).unwrap();
                types_for_spec.prepend(extended_types);
                types_for_spec
            };

            match &metadata {
                RuntimeMetadata::V9(m) => subxt_codegen::Metadata::from_v9(m, &types_for_spec),
                RuntimeMetadata::V10(m) => subxt_codegen::Metadata::from_v10(m, &types_for_spec),
                RuntimeMetadata::V11(m) => subxt_codegen::Metadata::from_v11(m, &types_for_spec),
                RuntimeMetadata::V12(m) => subxt_codegen::Metadata::from_v12(m, &types_for_spec),
                RuntimeMetadata::V13(m) => subxt_codegen::Metadata::from_v13(m, &types_for_spec),
                _ => panic!("Metadata version {} not expected", metadata.version()),
            }
            .expect("Could not convert to subxt_metadata::Metadata")
        };

        // We only test that generation succeeds without any errors, not necessarily that it's 100% useful:
        let codegen = subxt_codegen::CodegenBuilder::new();
        let _ = codegen
            .generate(new_md)
            .map_err(|e| e.into_compile_error())
            .unwrap_or_else(|e| panic!("Codegen failed for metadata V{version}: {e}"));
    }
}