From bac71feb0141514ec28bae28e6a6fa9f9884f516 Mon Sep 17 00:00:00 2001
From: Amar Singh <asinghchrony@protonmail.com>
Date: Mon, 11 Apr 2022 04:45:59 -0400
Subject: [PATCH] Make functionality to read relay state proof entries public
 (#1135)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* 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>
---
 .../src/relay_state_snapshot.rs               | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/cumulus/pallets/parachain-system/src/relay_state_snapshot.rs b/cumulus/pallets/parachain-system/src/relay_state_snapshot.rs
index ba3149119fd..9f69dd94762 100644
--- a/cumulus/pallets/parachain-system/src/relay_state_snapshot.rs
+++ b/cumulus/pallets/parachain-system/src/relay_state_snapshot.rs
@@ -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)
+	}
 }
-- 
GitLab