substrate_parser 0.7.1

parser for Substrate chain data
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
//! Traits for generalized metadata.
#[cfg(not(feature = "std"))]
use core::{
    any::TypeId,
    fmt::{Debug, Display},
};
#[cfg(feature = "std")]
use std::{
    any::TypeId,
    fmt::{Debug, Display},
};

use crate::std::{
    borrow::ToOwned,
    string::{String, ToString},
    vec::Vec,
};

use external_memory_tools::ExternalMemory;
use frame_metadata::{
    v14::{
        PalletMetadata as PalletMetadataV14, RuntimeMetadataV14,
        SignedExtensionMetadata as SignedExtensionMetadataV14,
    },
    v15::{
        PalletMetadata as PalletMetadataV15, RuntimeMetadataV15,
        SignedExtensionMetadata as SignedExtensionMetadataV15,
    },
};
use parity_scale_codec::{Decode, Encode};
use scale_info::{
    form::PortableForm, interner::UntrackedSymbol, PortableRegistry, Type, TypeDef,
    TypeDefPrimitive, TypeParameter,
};

use crate::cards::ParsedData;
use crate::decode_all_as_type;
use crate::decoding_sci::husk_type;
use crate::error::{
    MetaStructureErrorV14, MetaVersionErrorPallets, RegistryError, RegistryInternalError,
};
use crate::propagated::Checker;
use crate::special_indicators::{SpecialtyStr, SpecialtyUnsignedInteger};

/// Metadata sufficient for parsing of signable transactions, storage data, and
/// bytes with a known type.
pub trait AsMetadata<E: ExternalMemory>: Debug + Sized {
    type TypeRegistry: ResolveType<E>;
    type MetaStructureError: Debug + Display + Eq;
    fn types(&self) -> Self::TypeRegistry;
    fn spec_name_version(&self) -> Result<SpecNameVersion, Self::MetaStructureError>;
    fn call_ty(&self) -> Result<UntrackedSymbol<TypeId>, Self::MetaStructureError>;
    fn signed_extensions(&self) -> Result<Vec<SignedExtensionMetadata>, Self::MetaStructureError>;
}

/// Metadata sufficient for parsing of unchecked extrinsics.
pub trait AsCompleteMetadata<E: ExternalMemory>: AsMetadata<E> {
    fn extrinsic_type_params(&self) -> Result<ExtrinsicTypeParams, Self::MetaStructureError>;
    fn extrinsic_version(&self) -> Result<u8, Self::MetaStructureError>;
}

/// Set of types defining unchecked extrinsic contents.
#[repr(C)]
#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq)]
pub struct ExtrinsicTypeParams {
    pub address_ty: UntrackedSymbol<TypeId>,
    pub call_ty: UntrackedSymbol<TypeId>,
    pub signature_ty: UntrackedSymbol<TypeId>,
    pub extra_ty: UntrackedSymbol<TypeId>,
}

/// Metadata of the signed extensions of an extrinsic.
#[repr(C)]
#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq)]
pub struct SignedExtensionMetadata {
    pub identifier: String,
    pub ty: UntrackedSymbol<TypeId>,
    pub additional_signed: UntrackedSymbol<TypeId>,
}

/// Transform type into [`SignedExtensionMetadata`].
macro_rules! impl_signed_extension_metadata_from {
    ($($ty: ty), *) => {
        $(
            impl From<$ty> for SignedExtensionMetadata {
                fn from(signed_extension_metadata: $ty) -> Self {
                    Self {
                        identifier: signed_extension_metadata.identifier,
                        ty: signed_extension_metadata.ty,
                        additional_signed: signed_extension_metadata.additional_signed,
                    }
                }
            }
        )*
    }
}

impl_signed_extension_metadata_from!(
    SignedExtensionMetadataV14<PortableForm>,
    SignedExtensionMetadataV15<PortableForm>
);

/// Metadata `spec_name` and `spec_version`.
///
/// There is a well-known Substrate type
/// [`RuntimeVersion`](https://docs.rs/sp-version/latest/sp_version/struct.RuntimeVersion.html)
/// that describes the contents of `Version` constant in `System` pallet of the
/// metadata in most chains. This `RuntimeVersion` has `spec_version` type set
/// to `u32`. However, it is not necessarily the case that all chains in all
/// versions will stick to `u32`, as the type of `Version` constant itself is
/// described in the metadata.
///
/// Thus, metadata is printed into `String`, to accomodate reasonable types
/// variation of the `spec_version`.
#[repr(C)]
#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq)]
pub struct SpecNameVersion {
    pub printed_spec_version: String,
    pub spec_name: String,
}

/// Generalized types registry. Could be addressed in external memory.
pub trait ResolveType<E: ExternalMemory> {
    fn resolve_ty(
        &self,
        id: u32,
        ext_memory: &mut E,
    ) -> Result<Type<PortableForm>, RegistryError<E>>;
}

impl<E: ExternalMemory> ResolveType<E> for PortableRegistry {
    fn resolve_ty(
        &self,
        id: u32,
        _ext_memory: &mut E,
    ) -> Result<Type<PortableForm>, RegistryError<E>> {
        match self.resolve(id) {
            Some(a) => Ok(a.to_owned()),
            None => Err(RegistryError::Internal(
                RegistryInternalError::TypeNotResolved { id },
            )),
        }
    }
}

impl<E: ExternalMemory> AsMetadata<E> for RuntimeMetadataV14 {
    type TypeRegistry = PortableRegistry;

    type MetaStructureError = MetaStructureErrorV14;

    fn types(&self) -> Self::TypeRegistry {
        self.types.to_owned()
    }

    fn spec_name_version(&self) -> Result<SpecNameVersion, Self::MetaStructureError> {
        let (value, ty) = version_constant_data_and_ty_v14(&self.pallets)?;
        match decode_all_as_type::<&[u8], (), RuntimeMetadataV14>(
            &ty,
            &value.as_ref(),
            &mut (),
            &self.types,
        ) {
            Ok(extended_data) => Ok(spec_name_version_from_runtime_version_data(
                extended_data.data,
            )?),
            Err(_) => Err(MetaStructureErrorV14::Version(
                MetaVersionErrorPallets::RuntimeVersionNotDecodeable,
            )),
        }
    }

    fn call_ty(&self) -> Result<UntrackedSymbol<TypeId>, Self::MetaStructureError> {
        let extrinsic_type_params =
            <RuntimeMetadataV14 as AsCompleteMetadata<E>>::extrinsic_type_params(self)?;
        Ok(extrinsic_type_params.call_ty)
    }

    fn signed_extensions(&self) -> Result<Vec<SignedExtensionMetadata>, Self::MetaStructureError> {
        Ok(self
            .extrinsic
            .signed_extensions
            .iter()
            .cloned()
            .map(SignedExtensionMetadata::from)
            .collect())
    }
}

impl<E: ExternalMemory> AsCompleteMetadata<E> for RuntimeMetadataV14 {
    fn extrinsic_type_params(&self) -> Result<ExtrinsicTypeParams, Self::MetaStructureError> {
        let husked_extrinsic_ty = husk_type::<(), RuntimeMetadataV14>(
            &self.extrinsic.ty,
            &self.types,
            &mut (),
            Checker::new(),
        )?;

        // check here that the underlying type is really `Vec<u8>`
        match husked_extrinsic_ty.ty.type_def {
            TypeDef::Sequence(s) => {
                let element_ty_id = s.type_param.id;
                let element_ty = self.types.resolve_ty(element_ty_id, &mut ())?;
                if let TypeDef::Primitive(TypeDefPrimitive::U8) = element_ty.type_def {
                    process_extrinsic_type_params(husked_extrinsic_ty.ty.type_params)
                } else {
                    Err(MetaStructureErrorV14::UnexpectedExtrinsicType {
                        extrinsic_ty_id: husked_extrinsic_ty.id,
                    })
                }
            }
            TypeDef::Composite(c) => {
                if c.fields.len() != 1 {
                    Err(MetaStructureErrorV14::UnexpectedExtrinsicType {
                        extrinsic_ty_id: husked_extrinsic_ty.id,
                    })
                } else {
                    let field_ty_id = c.fields[0].ty.id;
                    let field_ty = self.types.resolve_ty(field_ty_id, &mut ())?;
                    match field_ty.type_def {
                        TypeDef::Sequence(s) => {
                            let element_ty_id = s.type_param.id;
                            let element_ty = self.types.resolve_ty(element_ty_id, &mut ())?;
                            if let TypeDef::Primitive(TypeDefPrimitive::U8) = element_ty.type_def {
                                process_extrinsic_type_params(husked_extrinsic_ty.ty.type_params)
                            } else {
                                Err(MetaStructureErrorV14::UnexpectedExtrinsicType {
                                    extrinsic_ty_id: husked_extrinsic_ty.id,
                                })
                            }
                        }
                        _ => Err(MetaStructureErrorV14::UnexpectedExtrinsicType {
                            extrinsic_ty_id: husked_extrinsic_ty.id,
                        }),
                    }
                }
            }
            _ => Err(MetaStructureErrorV14::UnexpectedExtrinsicType {
                extrinsic_ty_id: husked_extrinsic_ty.id,
            }),
        }
    }

    fn extrinsic_version(&self) -> Result<u8, Self::MetaStructureError> {
        Ok(self.extrinsic.version)
    }
}

impl<E: ExternalMemory> AsMetadata<E> for RuntimeMetadataV15 {
    type TypeRegistry = PortableRegistry;

    type MetaStructureError = MetaVersionErrorPallets;

    fn types(&self) -> Self::TypeRegistry {
        self.types.to_owned()
    }

    fn spec_name_version(&self) -> Result<SpecNameVersion, Self::MetaStructureError> {
        let (value, ty) = version_constant_data_and_ty_v15(&self.pallets)?;
        match decode_all_as_type::<&[u8], (), RuntimeMetadataV15>(
            &ty,
            &value.as_ref(),
            &mut (),
            &self.types,
        ) {
            Ok(extended_data) => Ok(spec_name_version_from_runtime_version_data(
                extended_data.data,
            )?),
            Err(_) => Err(MetaVersionErrorPallets::RuntimeVersionNotDecodeable),
        }
    }

    fn call_ty(&self) -> Result<UntrackedSymbol<TypeId>, Self::MetaStructureError> {
        Ok(self.extrinsic.call_ty)
    }

    fn signed_extensions(&self) -> Result<Vec<SignedExtensionMetadata>, Self::MetaStructureError> {
        Ok(self
            .extrinsic
            .signed_extensions
            .iter()
            .cloned()
            .map(SignedExtensionMetadata::from)
            .collect())
    }
}

impl<E: ExternalMemory> AsCompleteMetadata<E> for RuntimeMetadataV15 {
    fn extrinsic_type_params(&self) -> Result<ExtrinsicTypeParams, Self::MetaStructureError> {
        Ok(ExtrinsicTypeParams {
            address_ty: self.extrinsic.address_ty,
            call_ty: self.extrinsic.call_ty,
            signature_ty: self.extrinsic.signature_ty,
            extra_ty: self.extrinsic.extra_ty,
        })
    }

    fn extrinsic_version(&self) -> Result<u8, Self::MetaStructureError> {
        Ok(self.extrinsic.version)
    }
}

/// Transform extrinsic type parameters set into [`ExtrinsicTypeParams`].
fn process_extrinsic_type_params(
    extrinsic_type_params: Vec<TypeParameter<PortableForm>>,
) -> Result<ExtrinsicTypeParams, MetaStructureErrorV14> {
    let mut found_address = None;
    let mut found_signature = None;
    let mut found_extra = None;
    let mut found_call = None;

    for param in extrinsic_type_params.iter() {
        match param.name.as_str() {
            ADDRESS_INDICATOR => found_address = param.ty,
            SIGNATURE_INDICATOR => found_signature = param.ty,
            EXTRA_INDICATOR => found_extra = param.ty,
            CALL_INDICATOR => found_call = param.ty,
            _ => (),
        }
    }

    let address_ty = found_address.ok_or(MetaStructureErrorV14::NoAddressParam)?;
    let call_ty = found_call.ok_or(MetaStructureErrorV14::NoCallParam)?;
    let extra_ty = found_extra.ok_or(MetaStructureErrorV14::NoExtraParam)?;
    let signature_ty = found_signature.ok_or(MetaStructureErrorV14::NoSignatureParam)?;

    Ok(ExtrinsicTypeParams {
        address_ty,
        call_ty,
        signature_ty,
        extra_ty,
    })
}

/// [`TypeParameter`] name for `address`.
pub const ADDRESS_INDICATOR: &str = "Address";

/// [`TypeParameter`] name for `call`.
pub const CALL_INDICATOR: &str = "Call";

/// [`TypeParameter`] name for `extra`.
pub const EXTRA_INDICATOR: &str = "Extra";

/// [`TypeParameter`] name for `signature`.
pub const SIGNATURE_INDICATOR: &str = "Signature";

/// Find `Version` constant and its type in `System` pallet.
macro_rules! version_constant_data_and_ty {
    ($(#[$attr:meta] $ty: ty, $func: ident), *) => {
        $(
            #[$attr]
            pub fn $func(pallets: &[$ty]) -> Result<(Vec<u8>, UntrackedSymbol<TypeId>), MetaVersionErrorPallets> {
                let mut runtime_version_data_and_ty = None;
                let mut system_block = false;
                for pallet in pallets.iter() {
                    if pallet.name == "System" {
                        system_block = true;
                        for constant in pallet.constants.iter() {
                            if constant.name == "Version" {
                                runtime_version_data_and_ty = Some((constant.value.to_vec(), constant.ty))
                            }
                        }
                        break;
                    }
                }
                if !system_block {
                    return Err(MetaVersionErrorPallets::NoSystemPallet);
                }
                runtime_version_data_and_ty.ok_or(MetaVersionErrorPallets::NoVersionInConstants)
            }
        )*
    }
}

version_constant_data_and_ty!(
    /// Find `Version` constant and its type in `System` pallet for `V14` metadata.
    PalletMetadataV14<PortableForm>,
    version_constant_data_and_ty_v14
);
version_constant_data_and_ty!(
    /// Find `Version` constant and its type in `System` pallet for `V15` metadata.
    PalletMetadataV15<PortableForm>,
    version_constant_data_and_ty_v15
);

/// Extract [`SpecNameVersion`] from parsed data.
fn spec_name_version_from_runtime_version_data(
    parsed_data: ParsedData,
) -> Result<SpecNameVersion, MetaVersionErrorPallets> {
    let mut printed_spec_version = None;
    let mut spec_name = None;

    if let ParsedData::Composite(fields) = parsed_data {
        for field in fields.iter() {
            match &field.data.data {
                ParsedData::PrimitiveU8 {
                    value,
                    specialty: SpecialtyUnsignedInteger::SpecVersion,
                } => {
                    if printed_spec_version.is_none() {
                        printed_spec_version = Some(value.to_string())
                    } else {
                        return Err(MetaVersionErrorPallets::SpecVersionIdentifierTwice);
                    }
                }
                ParsedData::PrimitiveU16 {
                    value,
                    specialty: SpecialtyUnsignedInteger::SpecVersion,
                } => {
                    if printed_spec_version.is_none() {
                        printed_spec_version = Some(value.to_string())
                    } else {
                        return Err(MetaVersionErrorPallets::SpecVersionIdentifierTwice);
                    }
                }
                ParsedData::PrimitiveU32 {
                    value,
                    specialty: SpecialtyUnsignedInteger::SpecVersion,
                } => {
                    if printed_spec_version.is_none() {
                        printed_spec_version = Some(value.to_string())
                    } else {
                        return Err(MetaVersionErrorPallets::SpecVersionIdentifierTwice);
                    }
                }
                ParsedData::PrimitiveU64 {
                    value,
                    specialty: SpecialtyUnsignedInteger::SpecVersion,
                } => {
                    if printed_spec_version.is_none() {
                        printed_spec_version = Some(value.to_string())
                    } else {
                        return Err(MetaVersionErrorPallets::SpecVersionIdentifierTwice);
                    }
                }
                ParsedData::PrimitiveU128 {
                    value,
                    specialty: SpecialtyUnsignedInteger::SpecVersion,
                } => {
                    if printed_spec_version.is_none() {
                        printed_spec_version = Some(value.to_string())
                    } else {
                        return Err(MetaVersionErrorPallets::SpecVersionIdentifierTwice);
                    }
                }
                ParsedData::Text {
                    text,
                    specialty: SpecialtyStr::SpecName,
                } => {
                    if spec_name.is_none() {
                        spec_name = Some(text.to_owned())
                    } else {
                        return Err(MetaVersionErrorPallets::SpecNameIdentifierTwice);
                    }
                }
                _ => (),
            }
        }
    } else {
        return Err(MetaVersionErrorPallets::UnexpectedRuntimeVersionFormat);
    }
    let printed_spec_version = match printed_spec_version {
        Some(a) => a,
        None => return Err(MetaVersionErrorPallets::NoSpecVersionIdentifier),
    };
    let spec_name = match spec_name {
        Some(a) => a,
        None => return Err(MetaVersionErrorPallets::NoSpecNameIdentifier),
    };
    Ok(SpecNameVersion {
        printed_spec_version,
        spec_name,
    })
}