frame_decode/methods/
storage_decoder.rs

1// Copyright (C) 2022-2025 Parity Technologies (UK) Ltd. (admin@parity.io)
2// This file is a part of the frame-decode crate.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//         http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::storage_encoder::encode_storage_key_prefix;
17use super::storage_type_info::{StorageHasher, StorageInfo, StorageTypeInfo};
18use crate::methods::storage_type_info::StorageInfoError;
19use crate::utils::{
20    DecodableValues, DecodeErrorTrace, IntoDecodableValues, decode_with_error_tracing,
21};
22use alloc::vec;
23use alloc::vec::Vec;
24use core::ops::Range;
25use scale_type_resolver::TypeResolver;
26
27/// An error returned trying to decode storage bytes.
28#[non_exhaustive]
29#[allow(missing_docs)]
30#[derive(Clone, Debug, thiserror::Error)]
31pub enum StorageKeyDecodeError<TypeId> {
32    #[error("Cannot get storage info: {0}")]
33    CannotGetInfo(StorageInfoError<'static>),
34    #[error(
35        "The hashed storage prefix given does not match the pallet and storage name asked to decode."
36    )]
37    PrefixMismatch,
38    #[error("Not enough bytes left: we need at least {needed} bytes but have {have} bytes")]
39    NotEnoughBytes { needed: usize, have: usize },
40    #[error(
41        "Cannot decode storage key '{ty:?}':\n\n{reason}\n\nDecoded so far:\n\n{decoded_so_far}"
42    )]
43    CannotDecodeKey {
44        ty: TypeId,
45        reason: DecodeErrorTrace,
46        decoded_so_far: StorageKey<TypeId>,
47    },
48}
49
50impl<TypeId> StorageKeyDecodeError<TypeId> {
51    /// Map the storage key error type IDs to something else.
52    pub fn map_type_id<NewTypeId, F>(self, mut f: F) -> StorageKeyDecodeError<NewTypeId>
53    where
54        F: FnMut(TypeId) -> NewTypeId,
55    {
56        match self {
57            StorageKeyDecodeError::CannotGetInfo(e) => StorageKeyDecodeError::CannotGetInfo(e),
58            StorageKeyDecodeError::PrefixMismatch => StorageKeyDecodeError::PrefixMismatch,
59            StorageKeyDecodeError::NotEnoughBytes { needed, have } => {
60                StorageKeyDecodeError::NotEnoughBytes { needed, have }
61            }
62            StorageKeyDecodeError::CannotDecodeKey {
63                ty,
64                reason,
65                decoded_so_far,
66            } => StorageKeyDecodeError::CannotDecodeKey {
67                ty: f(ty),
68                reason,
69                decoded_so_far: decoded_so_far.map_type_id(f),
70            },
71        }
72    }
73}
74
75/// An error returned trying to decode the values in storage keys
76#[non_exhaustive]
77#[allow(missing_docs)]
78#[derive(Debug, thiserror::Error)]
79pub enum StorageKeyValueDecodeError {
80    #[error("Cannot decode storage value at index {index}: {error}")]
81    DecodeError {
82        index: usize,
83        error: scale_decode::Error,
84    },
85    #[error("Cannot decode storage key values; need {need} values but have {have}")]
86    WrongNumberOfValues { have: usize, need: usize },
87    #[error(
88        "There were leftover bytes after decoding a value, indicating that decoding was not successful"
89    )]
90    LeftoverBytes { bytes: Vec<u8> },
91    #[error(
92        "An invalid byte range was asked for from the key bytes, implying that the key bytes are not those that the information is about"
93    )]
94    InvalidRange,
95}
96
97/// An error returned trying to decode storage bytes.
98#[non_exhaustive]
99#[allow(missing_docs)]
100#[derive(Clone, Debug)]
101pub enum StorageValueDecodeError<TypeId> {
102    CannotGetInfo(StorageInfoError<'static>),
103    CannotDecodeValue {
104        ty: TypeId,
105        reason: DecodeErrorTrace,
106    },
107}
108
109impl<TypeId: core::fmt::Debug> core::error::Error for StorageValueDecodeError<TypeId> {}
110
111impl<TypeId: core::fmt::Debug> core::fmt::Display for StorageValueDecodeError<TypeId> {
112    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
113        match self {
114            StorageValueDecodeError::CannotGetInfo(storage_info_error) => {
115                write!(f, "Cannot get storage info:\n\n{storage_info_error}")
116            }
117            StorageValueDecodeError::CannotDecodeValue { ty, reason } => {
118                write!(f, "Cannot decode value with type ID {ty:?}:\n\n{reason}")
119            }
120        }
121    }
122}
123
124impl<TypeId> StorageValueDecodeError<TypeId> {
125    /// Map the storage value error type IDs to something else.
126    pub fn map_type_id<NewTypeId, F>(self, mut f: F) -> StorageValueDecodeError<NewTypeId>
127    where
128        F: FnMut(TypeId) -> NewTypeId,
129    {
130        match self {
131            StorageValueDecodeError::CannotGetInfo(e) => StorageValueDecodeError::CannotGetInfo(e),
132            StorageValueDecodeError::CannotDecodeValue { ty, reason } => {
133                StorageValueDecodeError::CannotDecodeValue { ty: f(ty), reason }
134            }
135        }
136    }
137}
138
139/// Details about a storage key.
140#[derive(Clone, Debug)]
141pub struct StorageKey<TypeId> {
142    parts: Vec<StorageKeyPart<TypeId>>,
143}
144
145impl<TypeId: core::fmt::Debug> core::fmt::Display for StorageKey<TypeId> {
146    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
147        // Plain entries have no keys:
148        if self.parts.is_empty() {
149            write!(f, "No storage parts")?;
150            return Ok(());
151        }
152
153        // hash type: blake2,
154        // hash range: 0..13,
155        // value range: 13..23,
156        // value type: AccountId
157        //
158        // ...
159        for key in self.parts.iter() {
160            writeln!(f, "Hash type: {:?}", key.hasher)?;
161            writeln!(
162                f,
163                "Hash range: {}..{}",
164                key.hash_range.start, key.hash_range.end
165            )?;
166            if let Some(v) = &key.value {
167                writeln!(f, "Value type: {:?}", v.ty)?;
168                writeln!(f, "Value range: {}..{}", v.range.start, v.range.end)?;
169            }
170
171            writeln!(f)?;
172        }
173
174        Ok(())
175    }
176}
177
178impl<TypeId> core::ops::Index<usize> for StorageKey<TypeId> {
179    type Output = StorageKeyPart<TypeId>;
180    fn index(&self, index: usize) -> &Self::Output {
181        &self.parts[index]
182    }
183}
184
185impl<TypeId> StorageKey<TypeId> {
186    /// Iterate over the parts of this storage key.
187    pub fn parts(&self) -> impl ExactSizeIterator<Item = &StorageKeyPart<TypeId>> {
188        self.parts.iter()
189    }
190
191    /// Map the storage key type IDs to something else.
192    pub fn map_type_id<NewTypeId, F>(self, mut f: F) -> StorageKey<NewTypeId>
193    where
194        F: FnMut(TypeId) -> NewTypeId,
195    {
196        StorageKey {
197            parts: self
198                .parts
199                .into_iter()
200                .map(|p| p.map_type_id(&mut f))
201                .collect(),
202        }
203    }
204}
205
206/// The decoded representation of a storage key.
207#[derive(Clone, Debug)]
208pub struct StorageKeyPart<TypeId> {
209    hash_range: Range<u32>,
210    value: Option<StorageKeyPartValue<TypeId>>,
211    hasher: StorageHasher,
212}
213
214impl<TypeId> StorageKeyPart<TypeId> {
215    /// The byte range of the hash for this storage key part.
216    pub fn hash_range(&self) -> Range<usize> {
217        Range {
218            start: self.hash_range.start as usize,
219            end: self.hash_range.end as usize,
220        }
221    }
222
223    /// The hasher used for this storage key part.
224    pub fn hasher(&self) -> StorageHasher {
225        self.hasher
226    }
227
228    /// If applicable (ie this part uses a concat or ident hasher), return information
229    /// about the value encoded into this hash.
230    pub fn value(&self) -> Option<&StorageKeyPartValue<TypeId>> {
231        self.value.as_ref()
232    }
233
234    /// Map the storage part type ID to something else.
235    pub fn map_type_id<NewTypeId, F>(self, f: F) -> StorageKeyPart<NewTypeId>
236    where
237        F: FnMut(TypeId) -> NewTypeId,
238    {
239        StorageKeyPart {
240            hash_range: self.hash_range,
241            value: self.value.map(|v| v.map_type_id(f)),
242            hasher: self.hasher,
243        }
244    }
245}
246
247/// Information about the value contained within a storage key part hash.
248#[derive(Clone, Debug)]
249pub struct StorageKeyPartValue<TypeId> {
250    range: Range<u32>,
251    ty: TypeId,
252}
253
254impl<TypeId> StorageKeyPartValue<TypeId> {
255    /// The byte range for this value in the storage key.
256    pub fn range(&self) -> Range<usize> {
257        Range {
258            start: self.range.start as usize,
259            end: self.range.end as usize,
260        }
261    }
262
263    /// The type ID for this value.
264    pub fn ty(&self) -> &TypeId {
265        &self.ty
266    }
267
268    /// Map the storage part type ID to something else.
269    pub fn map_type_id<NewTypeId, F>(self, mut f: F) -> StorageKeyPartValue<NewTypeId>
270    where
271        F: FnMut(TypeId) -> NewTypeId,
272    {
273        StorageKeyPartValue {
274            range: self.range,
275            ty: f(self.ty),
276        }
277    }
278}
279
280/// Decode a storage key, returning information about it.
281///
282/// This information can be used to identify and, where possible, decode the parts of the storage key.
283///
284/// # Example
285///
286/// Here, we decode some storage keys from a block.
287///
288/// ```rust
289/// use frame_decode::storage::decode_storage_key;
290/// use frame_decode::helpers::decode_with_visitor;
291/// use frame_metadata::RuntimeMetadata;
292/// use parity_scale_codec::Decode;
293/// use scale_value::scale::ValueVisitor;
294///
295/// let metadata_bytes = std::fs::read("artifacts/metadata_10000000_9180.scale").unwrap();
296/// let RuntimeMetadata::V14(metadata) = RuntimeMetadata::decode(&mut &*metadata_bytes).unwrap() else { return };
297///
298/// let storage_keyval_bytes = std::fs::read("artifacts/storage_10000000_9180_system_account.json").unwrap();
299/// let storage_keyval_hex: Vec<(String, String)> = serde_json::from_slice(&storage_keyval_bytes).unwrap();
300///
301/// for (key, _val) in storage_keyval_hex {
302///     let key_bytes = hex::decode(key.trim_start_matches("0x")).unwrap();
303///
304///     // Decode the storage key, returning information about it:
305///     let storage_info = decode_storage_key(
306///         "System",
307///         "Account",
308///         &mut &*key_bytes,
309///         &metadata,
310///         &metadata.types
311///     ).unwrap();
312///
313///     for part in storage_info.parts() {
314///         // Access information about the hasher for this part of the key:
315///         let hash_bytes = &key_bytes[part.hash_range()];
316///         let hasher = part.hasher();
317///
318///         // If the value is encoded as part of the hasher, we can find and
319///         // decode the value too:
320///         if let Some(value_info) = part.value() {
321///             let value_bytes = &key_bytes[value_info.range()];
322///             let value = decode_with_visitor(
323///                 &mut &*value_bytes,
324///                 *value_info.ty(),
325///                 &metadata.types,
326///                 ValueVisitor::new()
327///             ).unwrap();
328///         }
329///     }
330/// }
331/// ```
332pub fn decode_storage_key<Info, Resolver>(
333    pallet_name: &str,
334    storage_entry: &str,
335    cursor: &mut &[u8],
336    info: &Info,
337    type_resolver: &Resolver,
338) -> Result<StorageKey<Info::TypeId>, StorageKeyDecodeError<Info::TypeId>>
339where
340    Info: StorageTypeInfo,
341    Info::TypeId: Clone + core::fmt::Debug,
342    Resolver: TypeResolver<TypeId = Info::TypeId>,
343{
344    let storage_info = info
345        .storage_info(pallet_name, storage_entry)
346        .map_err(|e| StorageKeyDecodeError::CannotGetInfo(e.into_owned()))?;
347
348    // Sanity check that the storage key prefix is what we expect:
349    let expected_prefix = encode_storage_key_prefix(pallet_name, storage_entry);
350    if cursor[..32] != expected_prefix {
351        return Err(StorageKeyDecodeError::PrefixMismatch);
352    }
353
354    decode_storage_key_with_info(cursor, &storage_info, type_resolver)
355}
356
357/// Decode a storage key, returning information about it.
358///
359/// Unlike [`decode_storage_key`], which obtains the storage info internally given the pallet and storage entry names,
360/// this function takes the storage info as an argument. This is useful if you already have the storage info available,
361/// for example if you are decoding multiple keys for the same storage entry.
362///
363/// # Warning
364///
365/// Unlike [`decode_storage_key`], this does not check that the bytes start with the expected prefix; ensuring that the
366/// storage information lines up with the bytes is the caller's responsibility.
367pub fn decode_storage_key_with_info<Resolver>(
368    cursor: &mut &[u8],
369    storage_info: &StorageInfo<<Resolver as TypeResolver>::TypeId>,
370    type_resolver: &Resolver,
371) -> Result<
372    StorageKey<<Resolver as TypeResolver>::TypeId>,
373    StorageKeyDecodeError<<Resolver as TypeResolver>::TypeId>,
374>
375where
376    Resolver: TypeResolver,
377    <Resolver as TypeResolver>::TypeId: Clone + core::fmt::Debug,
378{
379    let bytes = *cursor;
380    let curr_idx = |cursor: &mut &[u8]| (bytes.len() - cursor.len()) as u32;
381
382    let _prefix = strip_bytes(cursor, 32)?;
383
384    let mut parts = vec![];
385    for key in &*storage_info.keys {
386        let hasher = if storage_info.use_old_v9_storage_hashers {
387            map_to_old_v9_storage_hashers(key.hasher)
388        } else {
389            key.hasher
390        };
391
392        let start_idx = curr_idx(cursor);
393        let part = match &hasher {
394            StorageHasher::Blake2_128 | StorageHasher::Twox128 => {
395                strip_bytes(cursor, 16)?;
396                StorageKeyPart {
397                    hash_range: Range {
398                        start: start_idx,
399                        end: curr_idx(cursor),
400                    },
401                    value: None,
402                    hasher,
403                }
404            }
405            StorageHasher::Blake2_256 | StorageHasher::Twox256 => {
406                strip_bytes(cursor, 32)?;
407                StorageKeyPart {
408                    hash_range: Range {
409                        start: start_idx,
410                        end: curr_idx(cursor),
411                    },
412                    value: None,
413                    hasher,
414                }
415            }
416            StorageHasher::Blake2_128Concat => {
417                strip_bytes(cursor, 16)?;
418                let hash_end_idx = curr_idx(cursor);
419                decode_with_error_tracing(
420                    cursor,
421                    key.key_id.clone(),
422                    type_resolver,
423                    scale_decode::visitor::IgnoreVisitor::new(),
424                )
425                .map_err(|e| StorageKeyDecodeError::CannotDecodeKey {
426                    ty: key.key_id.clone(),
427                    reason: e,
428                    decoded_so_far: StorageKey {
429                        parts: parts.clone(),
430                    },
431                })?;
432                StorageKeyPart {
433                    hash_range: Range {
434                        start: start_idx,
435                        end: hash_end_idx,
436                    },
437                    value: Some(StorageKeyPartValue {
438                        range: Range {
439                            start: hash_end_idx,
440                            end: curr_idx(cursor),
441                        },
442                        ty: key.key_id.clone(),
443                    }),
444                    hasher,
445                }
446            }
447            StorageHasher::Twox64Concat => {
448                strip_bytes(cursor, 8)?;
449                let hash_end_idx = curr_idx(cursor);
450                decode_with_error_tracing(
451                    cursor,
452                    key.key_id.clone(),
453                    type_resolver,
454                    scale_decode::visitor::IgnoreVisitor::new(),
455                )
456                .map_err(|e| StorageKeyDecodeError::CannotDecodeKey {
457                    ty: key.key_id.clone(),
458                    reason: e,
459                    decoded_so_far: StorageKey {
460                        parts: parts.clone(),
461                    },
462                })?;
463                StorageKeyPart {
464                    hash_range: Range {
465                        start: start_idx,
466                        end: hash_end_idx,
467                    },
468                    value: Some(StorageKeyPartValue {
469                        range: Range {
470                            start: hash_end_idx,
471                            end: curr_idx(cursor),
472                        },
473                        ty: key.key_id.clone(),
474                    }),
475                    hasher,
476                }
477            }
478            StorageHasher::Identity => {
479                decode_with_error_tracing(
480                    cursor,
481                    key.key_id.clone(),
482                    type_resolver,
483                    scale_decode::visitor::IgnoreVisitor::new(),
484                )
485                .map_err(|e| StorageKeyDecodeError::CannotDecodeKey {
486                    ty: key.key_id.clone(),
487                    reason: e,
488                    decoded_so_far: StorageKey {
489                        parts: parts.clone(),
490                    },
491                })?;
492                StorageKeyPart {
493                    hash_range: Range {
494                        start: start_idx,
495                        end: start_idx,
496                    },
497                    value: Some(StorageKeyPartValue {
498                        range: Range {
499                            start: start_idx,
500                            end: curr_idx(cursor),
501                        },
502                        ty: key.key_id.clone(),
503                    }),
504                    hasher,
505                }
506            }
507        };
508        parts.push(part)
509    }
510
511    Ok(StorageKey { parts })
512}
513
514/// Attempt to decode the values attached to parts of a storage key into the provided output type.
515/// [`decode_storage_key`] and related functions take a storage key and return information (in the
516/// form of [`StorageKey`]) which describes each of the parts of the key.
517///
518/// This function takes that information and returns a user-defined generic type which implements
519/// [`IntoDecodableValues`], and attempts to decode some or all of the values from it (in order).
520///
521/// # Example
522///
523/// Here, we decode some storage keys from a block.
524///
525/// ```rust
526/// use frame_decode::storage::{ decode_storage_key, decode_storage_key_values };
527/// use frame_decode::helpers::decode_with_visitor;
528/// use frame_metadata::RuntimeMetadata;
529/// use parity_scale_codec::Decode;
530/// use scale_value::scale::ValueVisitor;
531///
532/// let metadata_bytes = std::fs::read("artifacts/metadata_10000000_9180.scale").unwrap();
533/// let RuntimeMetadata::V14(metadata) = RuntimeMetadata::decode(&mut &*metadata_bytes).unwrap() else { return };
534///
535/// let storage_keyval_bytes = std::fs::read("artifacts/storage_10000000_9180_system_account.json").unwrap();
536/// let storage_keyval_hex: Vec<(String, String)> = serde_json::from_slice(&storage_keyval_bytes).unwrap();
537///
538/// for (key, _val) in storage_keyval_hex {
539///     let key_bytes = hex::decode(key.trim_start_matches("0x")).unwrap();
540///
541///     // First decode the storage key, returning information about it:
542///     let storage_info = decode_storage_key(
543///         "System",
544///         "Account",
545///         &mut &*key_bytes,
546///         &metadata,
547///         &metadata.types
548///     ).unwrap();
549///
550///     // Use this information to decode any values within the key. Here
551///     // we ask for the first (and here only) value present, which we know to
552///     // be decodable into a [u8; 32] because it's an AccountId32.
553///     let values: ([u8;32],) = decode_storage_key_values(
554///         &key_bytes,
555///         &storage_info,
556///         &metadata.types
557///     ).unwrap();
558///
559///     println!("Account ID Hex: {}", hex::encode(&values.0));
560///
561///     // If we don't know what we are decoding, we can target a Vec<scale_value::Value>
562///     // which allows arbitrary items to be decoded into it.
563///     let values: Vec<scale_value::Value> = decode_storage_key_values(
564///         &key_bytes,
565///         &storage_info,
566///         &metadata.types
567///     ).unwrap();
568///
569///     println!("All values extracted from key:");
570///     for value in values {
571///         println!("  {value}");
572///     }
573/// }
574/// ```
575pub fn decode_storage_key_values<Values, Resolver>(
576    key_bytes: &[u8],
577    decoded_key: &StorageKey<Resolver::TypeId>,
578    types: &Resolver,
579) -> Result<Values, StorageKeyValueDecodeError>
580where
581    Values: IntoDecodableValues,
582    Resolver: TypeResolver,
583{
584    let num_values = decoded_key
585        .parts()
586        .filter(|part| part.value.is_some())
587        .count();
588
589    let needed_values = Values::num_decodable_values();
590
591    // If a specific number of values are needed, ensure that we have enough available.
592    if let Some(needed_values) = needed_values
593        && num_values < needed_values
594    {
595        return Err(StorageKeyValueDecodeError::WrongNumberOfValues {
596            have: num_values,
597            need: needed_values,
598        });
599    }
600
601    let mut decode_target = Values::into_decodable_values();
602
603    // Iterate over **at most** needed_values, or all of the available values if no limit.
604    let value_info_iter =
605        (0..needed_values.unwrap_or(usize::MAX)).zip(decoded_key.parts().filter_map(|p| p.value()));
606
607    for (idx, value_info) in value_info_iter {
608        // This will panic if the key bytes provided don't line up with the information.
609        let value_bytes = &mut key_bytes
610            .get(value_info.range())
611            .ok_or(StorageKeyValueDecodeError::InvalidRange)?;
612        let value_ty = value_info.ty().clone();
613
614        decode_target
615            .decode_next_value(value_bytes, value_ty, types)
616            .map_err(|e| StorageKeyValueDecodeError::DecodeError {
617                index: idx,
618                error: e,
619            })?;
620
621        if !value_bytes.is_empty() {
622            return Err(StorageKeyValueDecodeError::LeftoverBytes {
623                bytes: value_bytes.to_vec(),
624            });
625        }
626    }
627
628    Ok(decode_target.decoded_target())
629}
630
631/// Decode a storage value.
632///
633/// # Example
634///
635/// Here, we decode some storage values from a block.
636///
637/// ```rust
638/// use frame_decode::storage::decode_storage_value;
639/// use frame_decode::helpers::decode_with_visitor;
640/// use frame_metadata::RuntimeMetadata;
641/// use parity_scale_codec::Decode;
642/// use scale_value::scale::ValueVisitor;
643///
644/// let metadata_bytes = std::fs::read("artifacts/metadata_10000000_9180.scale").unwrap();
645/// let RuntimeMetadata::V14(metadata) = RuntimeMetadata::decode(&mut &*metadata_bytes).unwrap() else { return };
646///
647/// let storage_keyval_bytes = std::fs::read("artifacts/storage_10000000_9180_system_account.json").unwrap();
648/// let storage_keyval_hex: Vec<(String, String)> = serde_json::from_slice(&storage_keyval_bytes).unwrap();
649///
650/// for (_key, val) in storage_keyval_hex {
651///     let value_bytes = hex::decode(val.trim_start_matches("0x")).unwrap();
652///
653///     // Decode the storage value, here into a scale_value::Value:
654///     let account_value = decode_storage_value(
655///         "System",
656///         "Account",
657///         &mut &*value_bytes,
658///         &metadata,
659///         &metadata.types,
660///         ValueVisitor::new()
661///     ).unwrap();
662/// }
663/// ```
664pub fn decode_storage_value<'scale, 'resolver, Info, Resolver, V>(
665    pallet_name: &str,
666    storage_entry: &str,
667    cursor: &mut &'scale [u8],
668    info: &Info,
669    type_resolver: &'resolver Resolver,
670    visitor: V,
671) -> Result<V::Value<'scale, 'resolver>, StorageValueDecodeError<Info::TypeId>>
672where
673    Info: StorageTypeInfo,
674    Info::TypeId: Clone + core::fmt::Debug,
675    Resolver: TypeResolver<TypeId = Info::TypeId>,
676    V: scale_decode::Visitor<TypeResolver = Resolver>,
677    V::Error: core::fmt::Debug,
678{
679    let storage_info = info
680        .storage_info(pallet_name, storage_entry)
681        .map_err(|e| StorageValueDecodeError::CannotGetInfo(e.into_owned()))?;
682
683    decode_storage_value_with_info(cursor, &storage_info, type_resolver, visitor)
684}
685
686/// Decode a storage value.
687///
688/// Unlike [`decode_storage_value`], which obtains the storage info internally given the pallet and storage entry names,
689/// this function takes the storage info as an argument. This is useful if you already have the storage info available,
690/// for example if you are decoding multiple keys for the same storage entry.
691pub fn decode_storage_value_with_info<'scale, 'resolver, V>(
692    cursor: &mut &'scale [u8],
693    storage_info: &StorageInfo<<V::TypeResolver as TypeResolver>::TypeId>,
694    type_resolver: &'resolver V::TypeResolver,
695    visitor: V,
696) -> Result<
697    V::Value<'scale, 'resolver>,
698    StorageValueDecodeError<<V::TypeResolver as TypeResolver>::TypeId>,
699>
700where
701    V: scale_decode::Visitor,
702    V::Error: core::fmt::Debug,
703{
704    let value_id = storage_info.value_id.clone();
705
706    decode_with_error_tracing(cursor, value_id.clone(), type_resolver, visitor).map_err(|e| {
707        StorageValueDecodeError::CannotDecodeValue {
708            ty: value_id,
709            reason: e,
710        }
711    })
712}
713
714/// Decode the default storage value given some [`StorageInfo`].
715///
716/// The resulting value may be tied to the lifetime of the [`StorageInfo`] being provided if the implementation decides to borrow
717/// from it. This is also why no `decode_default_storage_value` function exists; the [`StorageInfo`] must outlive this call.
718pub fn decode_default_storage_value_with_info<'info, 'resolver, V>(
719    storage_info: &'info StorageInfo<<V::TypeResolver as TypeResolver>::TypeId>,
720    type_resolver: &'resolver V::TypeResolver,
721    visitor: V,
722) -> Result<
723    Option<V::Value<'info, 'resolver>>,
724    StorageValueDecodeError<<V::TypeResolver as TypeResolver>::TypeId>,
725>
726where
727    V: scale_decode::Visitor,
728    V::Error: core::fmt::Debug,
729{
730    let value_id = storage_info.value_id.clone();
731
732    let Some(default_bytes) = &storage_info.default_value else {
733        return Ok(None);
734    };
735    let value = decode_with_error_tracing(
736        &mut &**default_bytes,
737        value_id.clone(),
738        type_resolver,
739        visitor,
740    )
741    .map_err(|e| StorageValueDecodeError::CannotDecodeValue {
742        ty: value_id,
743        reason: e,
744    })?;
745
746    Ok(Some(value))
747}
748
749fn strip_bytes<'a, T>(
750    cursor: &mut &'a [u8],
751    num: usize,
752) -> Result<&'a [u8], StorageKeyDecodeError<T>> {
753    let bytes = cursor
754        .get(..num)
755        .ok_or_else(|| StorageKeyDecodeError::NotEnoughBytes {
756            needed: num,
757            have: cursor.len(),
758        })?;
759
760    *cursor = &cursor[num..];
761    Ok(bytes)
762}
763
764fn map_to_old_v9_storage_hashers(hasher: StorageHasher) -> StorageHasher {
765    match hasher {
766        // See https://github.com/paritytech/substrate/commit/bbb363f4320b4a72e059c0fca96af42296d5a6bf#diff-aa7bc120d701816def0f2a5eb469212d2b7021d2fc9d3b284f843f3f8089e91a.
767        // We SCALE decode into the "new" variants seen there, but need to translate back to the old ones, which
768        // is what was actually encoded from the runtime.
769        StorageHasher::Blake2_128 => StorageHasher::Blake2_128,
770        StorageHasher::Blake2_256 => StorageHasher::Blake2_256,
771        StorageHasher::Blake2_128Concat => StorageHasher::Twox128,
772        StorageHasher::Twox128 => StorageHasher::Twox256,
773        StorageHasher::Twox256 => StorageHasher::Twox64Concat,
774
775        // These types didn't exist prior to the change, so just leave them alone:
776        StorageHasher::Twox64Concat => StorageHasher::Twox64Concat,
777        StorageHasher::Identity => StorageHasher::Identity,
778    }
779}
780
781#[cfg(test)]
782mod test {
783    use super::*;
784
785    #[test]
786    fn test_strip_bytes() {
787        let v = vec![0, 1, 2, 3, 4, 5, 6, 7, 8];
788        let cursor = &mut &*v;
789        let stripped = strip_bytes::<()>(cursor, 4).unwrap();
790        assert_eq!(stripped, &[0, 1, 2, 3]);
791        assert_eq!(cursor, &[4, 5, 6, 7, 8]);
792    }
793}