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
//! Interpret storage data.
//!
//! Chain storage could be queried by rpc `state_getStorage` using a whole key
//! or a key prefix.
//!
//! Storage key has prefix built from `prefix` of
//! [`PalletStorageMetadata`](frame_metadata::v14::PalletStorageMetadata) and
//! `name` of [`StorageEntryMetadata`], both processed as bytes in
//! [`twox_128`](sp_crypto_hashing::twox_128) and concatenated together.
//!
//! There are `Plain` and `Map` variants of [`StorageEntryType`].
//!
//! The storage key for `Plain` variant contains only the prefix.
//!
//! The storage key for `Map` has additional data after the prefix that in some
//! cases could be parsed too. For `Map` there are multiple keys with the same
//! prefix.
//!
//! Storage value contains corresponding encoded data with type described in
//! [`StorageEntryType`].
//!
//! [`Storage`] contains parsed storage entry data (key, value, and general
//! documentation associated with every key with the same prefix).
use external_memory_tools::{AddressableBuffer, ExternalMemory};
use frame_metadata::v14::{StorageEntryMetadata, StorageEntryType, StorageHasher};
use scale_info::{form::PortableForm, interner::UntrackedSymbol, TypeDef};
use sp_crypto_hashing::{blake2_128, twox_64};

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

#[cfg(feature = "std")]
use std::any::TypeId;

#[cfg(not(feature = "std"))]
use core::any::TypeId;

use crate::cards::{Documented, ExtendedData, Info};
use crate::decode_all_as_type;
use crate::decoding_sci::{decode_with_type, Ty};
use crate::error::{ParserError, StorageError};
use crate::propagated::Propagated;
use crate::traits::{AsMetadata, ResolveType};

/// Parsed storage entry data: key, value, general docs.
#[derive(Debug, Eq, PartialEq)]
pub struct Storage {
    /// Storage key data.
    pub key: KeyData,

    /// Storage value decoded.
    pub value: ExtendedData,

    /// [`StorageEntryMetadata`] documentation, common for all storage entries
    /// with the same prefix.
    pub docs: String,
}

/// Processed key.
#[derive(Debug, Eq, PartialEq)]
pub enum KeyData {
    /// Plain storage. Key contains only prefix, no additional data.
    Plain,

    /// Map storage with a single [`StorageHasher`].
    ///
    /// Key contains prefix and a single hash. No restrictions on key type.
    SingleHash {
        /// Processed key data.
        content: KeyPart,
    },

    /// Map storage with a set of [`StorageHasher`]s.
    ///
    /// Key contains prefix and a set of hashes. Associated key is expected to
    /// be a tuple type, the number of fields in tuple is exactly the same as
    /// the number of elements in `Vec<StorageHasher>`.
    TupleHash {
        /// Set of processed key elements. Has the same number of elements as
        /// the set of [`StorageHasher`]s.
        content: Vec<KeyPart>,

        /// [`Info`] associated with key type as a whole.
        info: Info,
    },
}

/// Processed key part.
#[derive(Debug, Eq, PartialEq)]
pub enum KeyPart {
    /// Hash has no concatenated raw part, no data decoded.
    Hash(HashData),

    /// Data following hash decoded.
    Parsed(ExtendedData),
}

/// Data for hash that was not decoded.
#[derive(Debug, Eq, PartialEq)]
pub struct HashData {
    /// Hash itself.
    pub hash: Hash,

    /// Associated type id in `PortableRegistry`.
    pub type_id: u32,
}

/// Raw hashes.
#[derive(Debug, Eq, PartialEq)]
pub enum Hash {
    Blake2_128([u8; BLAKE2_128_LEN]),
    Blake2_256([u8; BLAKE2_256_LEN]),
    Twox128([u8; TWOX128_LEN]),
    Twox256([u8; TWOX256_LEN]),
}

/// Lenght in bytes for Blake2_128 hash.
pub const BLAKE2_128_LEN: usize = 16;

/// Lenght in bytes for Blake2_256 hash.
pub const BLAKE2_256_LEN: usize = 32;

/// Lenght in bytes for Twox_128 hash.
pub const TWOX128_LEN: usize = 16;

/// Lenght in bytes for Twox_256 hash.
pub const TWOX256_LEN: usize = 32;

/// Lenght in bytes for Twox_64 hash.
pub const TWOX64_LEN: usize = 8;

/// Parse a storage entry (both the key and the corresponding value).
///
/// The key here is used "as is", i.e. starts with the prefix.
/// Prefix content **is not** checked here.
///
/// Both the key and the value are expected to be processed completely, i.e.
/// with no data remaining.
pub fn decode_as_storage_entry<B, E, M>(
    key_input: &B,
    value_input: &B,
    ext_memory: &mut E,
    entry_metadata: &StorageEntryMetadata<PortableForm>,
    registry: &M::TypeRegistry,
) -> Result<Storage, StorageError<E>>
where
    B: AddressableBuffer<E>,
    E: ExternalMemory,
    M: AsMetadata<E>,
{
    let docs = entry_metadata.collect_docs();

    let position_key_after_prefix = 2 * TWOX128_LEN;
    if position_key_after_prefix > key_input.total_len() {
        return Err(StorageError::KeyShorterThanPrefix);
    }

    let (key, value) = match &entry_metadata.ty {
        StorageEntryType::Plain(value_ty) => {
            if position_key_after_prefix != key_input.total_len() {
                return Err(StorageError::PlainKeyExceedsPrefix);
            }
            let key = KeyData::Plain;
            let value = decode_all_as_type::<B, E, M>(value_ty, value_input, ext_memory, registry)
                .map_err(StorageError::ParsingValue)?;
            (key, value)
        }
        StorageEntryType::Map {
            hashers,
            key: key_ty,
            value: value_ty,
        } => {
            let key = process_key_mapped::<B, E, M>(
                hashers,
                key_ty,
                key_input,
                ext_memory,
                position_key_after_prefix,
                registry,
            )?;
            let value = decode_all_as_type::<B, E, M>(value_ty, value_input, ext_memory, registry)
                .map_err(StorageError::ParsingValue)?;
            (key, value)
        }
    };
    Ok(Storage { key, value, docs })
}

/// Hash processing for hashes with known length and **no** concatenated
/// decodeable part.
///
/// Position moves according to the hash length. No parsing occurs here.
macro_rules! cut_hash {
    ($func:ident, $hash_len:ident, $enum_variant:ident) => {
        fn $func<B, E>(
            key_ty: &UntrackedSymbol<TypeId>,
            key_input: &B,
            ext_memory: &mut E,
            position: &mut usize,
        ) -> Result<KeyPart, StorageError<E>>
        where
            B: AddressableBuffer<E>,
            E: ExternalMemory,
        {
            let slice = key_input
                .read_slice(ext_memory, *position, $hash_len)
                .map_err(|e| StorageError::ParsingKey(ParserError::Buffer(e)))?;
            let hash_part: [u8; $hash_len] = slice
                .as_ref()
                .try_into()
                .expect("constant length, always fits");
            *position += $hash_len;
            Ok(KeyPart::Hash(HashData {
                hash: Hash::$enum_variant(hash_part),
                type_id: key_ty.id,
            }))
        }
    };
}

cut_hash!(cut_blake2_128, BLAKE2_128_LEN, Blake2_128);
cut_hash!(cut_blake2_256, BLAKE2_256_LEN, Blake2_256);
cut_hash!(cut_twox_128, TWOX128_LEN, Twox128);
cut_hash!(cut_twox_256, TWOX256_LEN, Twox256);

/// Hash processing for hashes with known length and **added** concatenated
/// decodeable part.
///
/// Position moves according to the hash length. Data parsing starts at first
/// byte after the hash. Bytes slice corresponding to the parsed data is
/// hashed and matched with the hash found in the key.
macro_rules! check_hash {
    ($func:ident, $hash_len:ident, $fn_into:ident) => {
        fn $func<B, E, M>(
            ty: &UntrackedSymbol<TypeId>,
            key_input: &B,
            ext_memory: &mut E,
            position: &mut usize,
            registry: &M::TypeRegistry,
        ) -> Result<KeyPart, StorageError<E>>
        where
            B: AddressableBuffer<E>,
            E: ExternalMemory,
            M: AsMetadata<E>,
        {
            let slice = key_input
                .read_slice(ext_memory, *position, $hash_len)
                .map_err(|e| StorageError::ParsingKey(ParserError::Buffer(e)))?;
            let hash_part: [u8; $hash_len] = slice
                .as_ref()
                .try_into()
                .expect("constant length, always fits");
            *position += $hash_len;
            let position_decoder_starts = *position;
            let parsed_key = decode_with_type::<B, E, M>(
                &Ty::Symbol(&ty),
                key_input,
                ext_memory,
                position,
                registry,
                Propagated::new(),
            )
            .map_err(StorageError::ParsingKey)?;
            if hash_part
                != $fn_into(
                    &key_input
                        .read_slice(
                            ext_memory,
                            position_decoder_starts,
                            *position - position_decoder_starts,
                        )
                        .expect("positions checked, valid slice")
                        .as_ref(),
                )
            {
                Err(StorageError::KeyPartHashMismatch)
            } else {
                Ok(KeyPart::Parsed(parsed_key))
            }
        }
    };
}

check_hash!(check_blake2_128, BLAKE2_128_LEN, blake2_128);
check_hash!(check_twox_64, TWOX64_LEN, twox_64);

/// Process the storage key data for `StorageEntryType::Map{..}` keys.
///
/// Starting position is explicitly set as a variable. For "as is" storage key
/// the starting position must be 2*[`TWOX128_LEN`], for trimmed storage key the
/// starting position must be `0`.
///
/// Key is expected to get processed completely, i.e. with no data remaining.
pub fn process_key_mapped<B, E, M>(
    hashers: &[StorageHasher],
    key_ty: &UntrackedSymbol<TypeId>,
    key_input: &B,
    ext_memory: &mut E,
    mut position: usize,
    registry: &M::TypeRegistry,
) -> Result<KeyData, StorageError<E>>
where
    B: AddressableBuffer<E>,
    E: ExternalMemory,
    M: AsMetadata<E>,
{
    let key_data = {
        if hashers.len() == 1 {
            match hashers[0] {
                StorageHasher::Blake2_128 => KeyData::SingleHash {
                    content: cut_blake2_128::<B, E>(key_ty, key_input, ext_memory, &mut position)?,
                },
                StorageHasher::Blake2_256 => KeyData::SingleHash {
                    content: cut_blake2_256::<B, E>(key_ty, key_input, ext_memory, &mut position)?,
                },
                StorageHasher::Blake2_128Concat => KeyData::SingleHash {
                    content: check_blake2_128::<B, E, M>(
                        key_ty,
                        key_input,
                        ext_memory,
                        &mut position,
                        registry,
                    )?,
                },
                StorageHasher::Twox128 => KeyData::SingleHash {
                    content: cut_twox_128::<B, E>(key_ty, key_input, ext_memory, &mut position)?,
                },
                StorageHasher::Twox256 => KeyData::SingleHash {
                    content: cut_twox_256::<B, E>(key_ty, key_input, ext_memory, &mut position)?,
                },
                StorageHasher::Twox64Concat => KeyData::SingleHash {
                    content: check_twox_64::<B, E, M>(
                        key_ty,
                        key_input,
                        ext_memory,
                        &mut position,
                        registry,
                    )?,
                },
                StorageHasher::Identity => {
                    let parsed_key = decode_with_type::<B, E, M>(
                        &Ty::Symbol(key_ty),
                        key_input,
                        ext_memory,
                        &mut position,
                        registry,
                        Propagated::new(),
                    )
                    .map_err(StorageError::ParsingKey)?;
                    KeyData::SingleHash {
                        content: KeyPart::Parsed(parsed_key),
                    }
                }
            }
        } else {
            let key_ty_resolved = registry
                .resolve_ty(key_ty.id, ext_memory)
                .map_err(|e| StorageError::ParsingKey(ParserError::Registry(e)))?;
            let info = Info::from_ty(&key_ty_resolved);
            match key_ty_resolved.type_def {
                TypeDef::Tuple(t) => {
                    let tuple_elements = &t.fields;
                    if tuple_elements.len() != hashers.len() {
                        return Err(StorageError::MultipleHashesNumberMismatch);
                    }
                    let mut content: Vec<KeyPart> = Vec::new();
                    for index in 0..tuple_elements.len() {
                        match hashers[index] {
                            StorageHasher::Blake2_128 => content.push(cut_blake2_128::<B, E>(
                                &tuple_elements[index],
                                key_input,
                                ext_memory,
                                &mut position,
                            )?),
                            StorageHasher::Blake2_256 => content.push(cut_blake2_256::<B, E>(
                                &tuple_elements[index],
                                key_input,
                                ext_memory,
                                &mut position,
                            )?),
                            StorageHasher::Blake2_128Concat => {
                                content.push(check_blake2_128::<B, E, M>(
                                    &tuple_elements[index],
                                    key_input,
                                    ext_memory,
                                    &mut position,
                                    registry,
                                )?)
                            }
                            StorageHasher::Twox128 => content.push(cut_twox_128::<B, E>(
                                &tuple_elements[index],
                                key_input,
                                ext_memory,
                                &mut position,
                            )?),
                            StorageHasher::Twox256 => content.push(cut_twox_256::<B, E>(
                                &tuple_elements[index],
                                key_input,
                                ext_memory,
                                &mut position,
                            )?),
                            StorageHasher::Twox64Concat => content.push(check_twox_64::<B, E, M>(
                                &tuple_elements[index],
                                key_input,
                                ext_memory,
                                &mut position,
                                registry,
                            )?),
                            StorageHasher::Identity => {
                                let parsed_key = decode_with_type::<B, E, M>(
                                    &Ty::Symbol(&tuple_elements[index]),
                                    key_input,
                                    ext_memory,
                                    &mut position,
                                    registry,
                                    Propagated::new(),
                                )
                                .map_err(StorageError::ParsingKey)?;
                                content.push(KeyPart::Parsed(parsed_key))
                            }
                        }
                    }
                    KeyData::TupleHash { content, info }
                }
                _ => return Err(StorageError::MultipleHashesNotATuple),
            }
        }
    };
    if position == key_input.total_len() {
        Ok(key_data)
    } else {
        Err(StorageError::KeyPartsUnused)
    }
}