Make functionality to read relay state proof entries public (#1135)

* add read entry and read optional entry to RelayChainStateProof pub methods

* rm toolchain

* docs

* Update pallets/parachain-system/src/relay_state_snapshot.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Amar Singh
2022-04-11 04:45:59 -04:00
committed by GitHub
parent 81c7c25b30
commit bac71feb01
@@ -63,6 +63,10 @@ pub struct MessagingStateSnapshot {
pub enum Error {
/// The provided proof was created against unexpected storage root.
RootMismatch,
/// The entry cannot be read.
ReadEntry(ReadEntryErr),
/// The optional entry cannot be read.
ReadOptionalEntry(ReadEntryErr),
/// The slot cannot be extracted.
Slot(ReadEntryErr),
/// The upgrade go-ahead signal cannot be read.
@@ -273,4 +277,28 @@ impl RelayChainStateProof {
)
.map_err(Error::UpgradeRestriction)
}
/// Read an entry given by the key and try to decode it. If the value specified by the key according
/// to the proof is empty, the `fallback` value will be returned.
///
/// Returns `Err` in case the backend can't return the value under the specific key (likely due to
/// a malformed proof), in case the decoding fails, or in case where the value is empty in the relay
/// chain state and no fallback was provided.
pub fn read_entry<T>(&self, key: &[u8], fallback: Option<T>) -> Result<T, Error>
where
T: Decode,
{
read_entry(&self.trie_backend, key, fallback).map_err(Error::ReadEntry)
}
/// Read an optional entry given by the key and try to decode it.
///
/// Returns `Err` in case the backend can't return the value under the specific key (likely due to
/// a malformed proof) or if the value couldn't be decoded.
pub fn read_optional_entry<T>(&self, key: &[u8]) -> Result<Option<T>, Error>
where
T: Decode,
{
read_optional_entry(&self.trie_backend, key).map_err(Error::ReadOptionalEntry)
}
}