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
474
475
476
477
478
479
480
481
//! Decoders for special types: primitives, `PerThing` items, well-known arrays.
use external_memory_tools::{AddressableBuffer, ExternalMemory};
use num_bigint::{BigInt, BigUint};
use parity_scale_codec::{DecodeAll, HasCompact};
use primitive_types::{H160, H256, H512};
use sp_arithmetic::{PerU16, Perbill, Percent, Permill, Perquintill};
use substrate_crypto_light::{
    common::AccountId32,
    ecdsa::{Public as PublicEcdsa, Signature as SignatureEcdsa},
    ed25519::{Public as PublicEd25519, Signature as SignatureEd25519},
    sr25519::{Public as PublicSr25519, Signature as SignatureSr25519},
};

#[cfg(all(not(feature = "std"), not(test)))]
use core::mem::size_of;
#[cfg(any(feature = "std", test))]
use std::mem::size_of;

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

use crate::additional_types::Era;
use crate::cards::{ParsedData, Sequence, SequenceData};
use crate::compacts::get_compact;
use crate::error::{ParserError, RegistryError, RegistryInternalError};
use crate::printing_balance::AsBalance;
use crate::propagated::SpecialtySet;
use crate::special_indicators::{SpecialtyH256, SpecialtyUnsignedInteger};

/// Stable length trait.
///
/// Encoded data length in bytes is always identical for the type.
pub(crate) trait StableLength: Sized {
    /// Encoded length for the type.
    fn len_encoded() -> usize;

    /// Shift position marker after decoding is done.
    fn shift_position(position: &mut usize) {
        *position += Self::len_encoded();
    }

    /// Get type value from the data.
    ///
    /// Slice of appropriate length is selected from input bytes starting at
    /// `position`, and decoded as the type. `position` marker gets moved after
    /// decoding.
    fn cut_and_decode<B, E>(
        data: &B,
        ext_memory: &mut E,
        position: &mut usize,
    ) -> Result<Self, ParserError<E>>
    where
        B: AddressableBuffer<E>,
        E: ExternalMemory;
}

/// Implement [`StableLength`] for types with stable [`size_of`].
macro_rules! impl_stable_length_mem_size_decode {
    ($($ty: ty), *) => {
        $(
            impl StableLength for $ty {
                fn len_encoded() -> usize {
                    size_of::<Self>()
                }
                fn cut_and_decode<B, E>(data: &B, ext_memory: &mut E, position: &mut usize) -> Result<Self, ParserError<E>>
                where
                    B: AddressableBuffer<E>,
                    E: ExternalMemory
                {
                    let slice_to_decode = data.read_slice(ext_memory, *position, Self::len_encoded())?;
                    let out = <Self>::decode_all(&mut slice_to_decode.as_ref())
                        .map_err(|_| ParserError::TypeFailure{position: *position, ty: stringify!($ty)})?;
                    Self::shift_position(position);
                    Ok(out)
                }
            }
        )*
    }
}

impl_stable_length_mem_size_decode!(
    bool,
    i8,
    i16,
    i32,
    i64,
    i128,
    u8,
    u16,
    u32,
    u64,
    u128,
    PerU16,
    Percent,
    Permill,
    Perbill,
    Perquintill
);

/// Known size for [`BigInt`] and [`BigUint`].
const BIG_LEN: usize = 32;

/// Known size for [`char`].
const CHAR_LEN: usize = 4;

/// Implement [`StableLength`] for [`BigInt`] and [`BigUint`].
macro_rules! impl_stable_length_big_construct {
    ($($big: ty, $get: ident), *) => {
        $(
            impl StableLength for $big {
                fn len_encoded() -> usize {
                    BIG_LEN
                }
                fn cut_and_decode<B, E>(data: &B, ext_memory: &mut E, position: &mut usize) -> Result<Self, ParserError<E>>
                where
                    B: AddressableBuffer<E>,
                    E: ExternalMemory
                {
                    let slice_to_big256 = data.read_slice(ext_memory, *position, Self::len_encoded())?;
                    let out = Self::$get(slice_to_big256.as_ref());
                    Self::shift_position(position);
                    Ok(out)
                }
            }
        )*
    }
}

impl_stable_length_big_construct!(BigUint, from_bytes_le);
impl_stable_length_big_construct!(BigInt, from_signed_bytes_le);

impl StableLength for char {
    fn len_encoded() -> usize {
        CHAR_LEN
    }
    fn cut_and_decode<B, E>(
        data: &B,
        ext_memory: &mut E,
        position: &mut usize,
    ) -> Result<Self, ParserError<E>>
    where
        B: AddressableBuffer<E>,
        E: ExternalMemory,
    {
        let slice_to_char = data.read_slice(ext_memory, *position, Self::len_encoded())?;
        match char::from_u32(<u32>::from_le_bytes(
            slice_to_char
                .as_ref()
                .try_into()
                .expect("constant length, always fit"),
        )) {
            Some(ch) => {
                Self::shift_position(position);
                Ok(ch)
            }
            None => Err(ParserError::TypeFailure {
                position: *position,
                ty: "char",
            }),
        }
    }
}

/// Implement [`StableLength`] for well-known hashes and arrays.
macro_rules! impl_stable_length_array {
    ($($array: ty, $len: expr), *) => {
        $(
            impl StableLength for $array {
                fn len_encoded() -> usize {
                    $len
                }
                fn cut_and_decode<B, E> (data: &B, ext_memory: &mut E, position: &mut usize) -> Result<Self, ParserError<E>>
                where
                    B: AddressableBuffer<E>,
                    E: ExternalMemory
                {
                    let slice_to_array = data.read_slice(ext_memory, *position, Self::len_encoded())?;
                    let out = Self(slice_to_array.as_ref().try_into().expect("stable known length"));
                    Self::shift_position(position);
                    Ok(out)
                }
            }
        )*
    }
}

impl_stable_length_array!(H160, Self::len_bytes());
impl_stable_length_array!(H256, Self::len_bytes());
impl_stable_length_array!(H512, Self::len_bytes());
impl_stable_length_array!(AccountId32, substrate_crypto_light::common::HASH_256_LEN);
impl_stable_length_array!(PublicEcdsa, substrate_crypto_light::ecdsa::PUBLIC_LEN);
impl_stable_length_array!(PublicEd25519, substrate_crypto_light::ed25519::PUBLIC_LEN);
impl_stable_length_array!(PublicSr25519, substrate_crypto_light::sr25519::PUBLIC_LEN);
impl_stable_length_array!(SignatureEcdsa, substrate_crypto_light::ecdsa::SIGNATURE_LEN);
impl_stable_length_array!(
    SignatureEd25519,
    substrate_crypto_light::ed25519::SIGNATURE_LEN
);
impl_stable_length_array!(
    SignatureSr25519,
    substrate_crypto_light::sr25519::SIGNATURE_LEN
);

/// Unsigned integer trait. Compatible with compacts, uses the propagated
/// [`SpecialtyUnsignedInteger`].
pub(crate) trait UnsignedInteger:
    StableLength + AsBalance + HasCompact + std::fmt::Display
{
    fn parse_unsigned_integer<B, E>(
        data: &B,
        ext_memory: &mut E,
        position: &mut usize,
        specialty_set: SpecialtySet,
    ) -> Result<ParsedData, ParserError<E>>
    where
        B: AddressableBuffer<E>,
        E: ExternalMemory;
}

/// Implement [`UnsignedInteger`] trait for all unsigned integers.
macro_rules! impl_unsigned_integer {
    ($($ty: ty, $enum_variant: ident), *) => {
        $(
            impl UnsignedInteger for $ty {
                fn parse_unsigned_integer<B, E> (data: &B, ext_memory: &mut E, position: &mut usize, specialty_set: SpecialtySet) -> Result<ParsedData, ParserError<E>>
                where
                    B: AddressableBuffer<E>,
                    E: ExternalMemory
                {
                    let value = {
                        if specialty_set.compact_at.is_some() {get_compact::<Self, B, E>(data, ext_memory, position)?}
                        else {<Self>::cut_and_decode::<B, E>(data, ext_memory, position)?}
                    };
                    Ok(ParsedData::$enum_variant{value, specialty: specialty_set.unsigned_integer()})
                }
            }
        )*
    }
}

impl_unsigned_integer!(u8, PrimitiveU8);
impl_unsigned_integer!(u16, PrimitiveU16);
impl_unsigned_integer!(u32, PrimitiveU32);
impl_unsigned_integer!(u64, PrimitiveU64);
impl_unsigned_integer!(u128, PrimitiveU128);

/// Trait for stable length types that must be checked for propagated compact
/// flag.
pub(crate) trait CheckCompact: StableLength {
    fn parse_check_compact<B, E>(
        data: &B,
        ext_memory: &mut E,
        position: &mut usize,
        compact_at: Option<u32>,
    ) -> Result<ParsedData, ParserError<E>>
    where
        B: AddressableBuffer<E>,
        E: ExternalMemory;
}

/// Implement [`CheckCompact`] for `PerThing` that can be compact.
macro_rules! impl_allow_compact {
    ($($perthing: ident), *) => {
        $(
            impl CheckCompact for $perthing where $perthing: HasCompact {
                fn parse_check_compact<B, E> (data: &B, ext_memory: &mut E, position: &mut usize, compact_at: Option<u32>) -> Result<ParsedData, ParserError<E>>
                where
                    B: AddressableBuffer<E>,
                    E: ExternalMemory
                {
                    let value = {
                        if compact_at.is_some() {get_compact::<Self, B, E>(data, ext_memory, position)?}
                        else {<Self>::cut_and_decode::<B, E>(data, ext_memory, position)?}
                    };
                    Ok(ParsedData::$perthing(value))
                }
            }
        )*
    }
}

impl_allow_compact!(PerU16, Percent, Permill, Perbill, Perquintill);

/// Implement [`CheckCompact`] for types that can not be compact.
macro_rules! impl_block_compact {
    ($($ty: ty, $enum_variant: ident), *) => {
        $(
            impl CheckCompact for $ty {
                fn parse_check_compact<B, E> (data: &B, ext_memory: &mut E, position: &mut usize, compact_at: Option<u32>) -> Result<ParsedData, ParserError<E>>
                where
                    B: AddressableBuffer<E>,
                    E: ExternalMemory
                {
                    let value = {
                        if let Some(id) = compact_at {return Err(ParserError::Registry(RegistryError::Internal(RegistryInternalError::UnexpectedCompactInsides{id})))}
                        else {<Self>::cut_and_decode::<B, E>(data, ext_memory, position)?}
                    };
                    Ok(ParsedData::$enum_variant(value))
                }
            }
        )*
    }
}

impl_block_compact!(bool, PrimitiveBool);
impl_block_compact!(char, PrimitiveChar);
impl_block_compact!(i8, PrimitiveI8);
impl_block_compact!(i16, PrimitiveI16);
impl_block_compact!(i32, PrimitiveI32);
impl_block_compact!(i64, PrimitiveI64);
impl_block_compact!(i128, PrimitiveI128);
impl_block_compact!(BigInt, PrimitiveI256);
impl_block_compact!(BigUint, PrimitiveU256);
impl_block_compact!(AccountId32, Id);
impl_block_compact!(PublicEd25519, PublicEd25519);
impl_block_compact!(PublicSr25519, PublicSr25519);
impl_block_compact!(PublicEcdsa, PublicEcdsa);
impl_block_compact!(SignatureEd25519, SignatureEd25519);
impl_block_compact!(SignatureSr25519, SignatureSr25519);
impl_block_compact!(SignatureEcdsa, SignatureEcdsa);
impl_block_compact!(H160, H160);
impl_block_compact!(H512, H512);

/// Trait to collect some variants of [`ParsedData`] into [`Sequence`].
///
/// Some simple types are easier displayed if `Vec<ParsedData>` is re-arranged
/// into single `ParsedData::Sequence(_)`. This is expecially true for `u8` and
/// `Vec<u8>`.
trait Collectable: Sized {
    fn husk_set(parsed_data_set: &[ParsedData]) -> Option<Sequence>;
}

/// Implement [`Collectable`] for unsigned integers.
macro_rules! impl_collect_vec {
    ($($ty: ty, $enum_variant_input: ident, $enum_variant_output: ident), *) => {
        $(
            impl Collectable for $ty {
                /// Collecting data into `Sequence`.
                ///
                /// This function is unfallible. If somehow not all data is
                /// of the same `ParsedData` variant, the `Sequence` just does
                /// not get assembled and parsed data would be displayed as
                /// `SequenceRaw`.
                fn husk_set(parsed_data_set: &[ParsedData]) -> Option<Sequence> {
                    let mut out: Vec<Self> = Vec::new();
                    for x in parsed_data_set.iter() {
                        if let ParsedData::$enum_variant_input{value: a, specialty: SpecialtyUnsignedInteger::None} = x {out.push(*a)}
                        else {return None}
                    }
                    Some(Sequence::$enum_variant_output(out))
                }
            }
        )*
    }
}

impl_collect_vec!(u8, PrimitiveU8, U8);
impl_collect_vec!(u16, PrimitiveU16, U16);
impl_collect_vec!(u32, PrimitiveU32, U32);
impl_collect_vec!(u64, PrimitiveU64, U64);
impl_collect_vec!(u128, PrimitiveU128, U128);

impl Collectable for Vec<u8> {
    fn husk_set(parsed_data_set: &[ParsedData]) -> Option<Sequence> {
        let mut out: Vec<Self> = Vec::new();
        let mut inner_element_info = None;

        for x in parsed_data_set.iter() {
            match x {
                ParsedData::Sequence(sequence_data) => {
                    if let Sequence::U8(a) = &sequence_data.data {
                        match inner_element_info {
                            Some(ref b) => {
                                if b != &sequence_data.element_info {
                                    return None;
                                }
                            }
                            None => {
                                inner_element_info = Some(sequence_data.element_info.to_owned());
                            }
                        }
                        out.push(a.clone())
                    } else {
                        return None;
                    }
                }
                ParsedData::SequenceRaw(a) => {
                    if a.data.is_empty() {
                        out.push(Vec::new())
                    } else {
                        return None;
                    }
                }
                _ => return None,
            }
        }
        let inner_element_info = inner_element_info.unwrap_or_default();
        Some(Sequence::VecU8 {
            sequence: out,
            inner_element_info,
        })
    }
}

/// Try collecting [`Sequence`]. Expected variant of [`ParsedData`] is
/// determined by the first set element.
pub(crate) fn wrap_sequence(set: &[ParsedData]) -> Option<Sequence> {
    match set.first() {
        Some(ParsedData::PrimitiveU8 { .. }) => u8::husk_set(set),
        Some(ParsedData::PrimitiveU16 { .. }) => u16::husk_set(set),
        Some(ParsedData::PrimitiveU32 { .. }) => u32::husk_set(set),
        Some(ParsedData::PrimitiveU64 { .. }) => u64::husk_set(set),
        Some(ParsedData::PrimitiveU128 { .. }) => u128::husk_set(set),
        Some(ParsedData::Sequence(SequenceData {
            element_info: _,
            data: Sequence::U8(_),
        })) => <Vec<u8>>::husk_set(set),
        _ => None,
    }
}

/// Parse part of the data as [`H256`], apply available [`SpecialtyH256`].
///
/// Position marker gets changed accordingly.
pub(crate) fn special_case_h256<B, E>(
    data: &B,
    ext_memory: &mut E,
    position: &mut usize,
    specialty_hash: SpecialtyH256,
) -> Result<ParsedData, ParserError<E>>
where
    B: AddressableBuffer<E>,
    E: ExternalMemory,
{
    let hash = H256::cut_and_decode::<B, E>(data, ext_memory, position)?;
    match specialty_hash {
        SpecialtyH256::GenesisHash => Ok(ParsedData::GenesisHash(hash)),
        SpecialtyH256::BlockHash => Ok(ParsedData::BlockHash(hash)),
        SpecialtyH256::None => Ok(ParsedData::H256(hash)),
    }
}

/// Encoded length of the immortal [`Era`].
const IMMORTAL_ERA_ENCODED_LEN: usize = 1;

/// Encoded length of the mortal [`Era`].
const MORTAL_ERA_ENCODED_LEN: usize = 2;

/// Parse part of the data as [`Era`].
///
/// Position marker gets changed accordingly.
pub(crate) fn special_case_era<B, E>(
    data: &B,
    ext_memory: &mut E,
    position: &mut usize,
) -> Result<ParsedData, ParserError<E>>
where
    B: AddressableBuffer<E>,
    E: ExternalMemory,
{
    let immortal_era_slice = data.read_slice(ext_memory, *position, IMMORTAL_ERA_ENCODED_LEN)?;
    match Era::decode_all(&mut immortal_era_slice.as_ref()) {
        Ok(era) => {
            *position += IMMORTAL_ERA_ENCODED_LEN;
            Ok(ParsedData::Era(era))
        }
        Err(_) => {
            let mortal_era_slice =
                data.read_slice(ext_memory, *position, MORTAL_ERA_ENCODED_LEN)?;
            match Era::decode_all(&mut mortal_era_slice.as_ref()) {
                Ok(era) => {
                    *position += MORTAL_ERA_ENCODED_LEN;
                    Ok(ParsedData::Era(era))
                }
                Err(_) => Err(ParserError::TypeFailure {
                    position: *position,
                    ty: "Era",
                }),
            }
        }
    }
}