sp_trie/
storage_proof.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use alloc::{collections::btree_set::BTreeSet, vec::Vec};
19use codec::{Decode, DecodeWithMemTracking, Encode};
20use core::iter::{DoubleEndedIterator, IntoIterator};
21use hash_db::{HashDB, Hasher};
22use scale_info::TypeInfo;
23
24// Note that `LayoutV1` usage here (proof compaction) is compatible
25// with `LayoutV0`.
26use crate::LayoutV1 as Layout;
27
28/// Error associated with the `storage_proof` module.
29#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
30pub enum StorageProofError {
31	/// The proof contains duplicate nodes.
32	DuplicateNodes,
33}
34
35/// A proof that some set of key-value pairs are included in the storage trie. The proof contains
36/// the storage values so that the partial storage backend can be reconstructed by a verifier that
37/// does not already have access to the key-value pairs.
38///
39/// The proof consists of the set of serialized nodes in the storage trie accessed when looking up
40/// the keys covered by the proof. Verifying the proof requires constructing the partial trie from
41/// the serialized nodes and performing the key lookups.
42#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode, DecodeWithMemTracking, TypeInfo)]
43pub struct StorageProof {
44	trie_nodes: BTreeSet<Vec<u8>>,
45}
46
47impl StorageProof {
48	/// Constructs a storage proof from a subset of encoded trie nodes in a storage backend.
49	pub fn new(trie_nodes: impl IntoIterator<Item = Vec<u8>>) -> Self {
50		StorageProof { trie_nodes: BTreeSet::from_iter(trie_nodes) }
51	}
52
53	/// Constructs a storage proof from a subset of encoded trie nodes in a storage backend.
54	///
55	/// Returns an error if the provided subset of encoded trie nodes contains duplicates.
56	pub fn new_with_duplicate_nodes_check(
57		trie_nodes: impl IntoIterator<Item = Vec<u8>>,
58	) -> Result<Self, StorageProofError> {
59		let mut trie_nodes_set = BTreeSet::new();
60		for node in trie_nodes {
61			if !trie_nodes_set.insert(node) {
62				return Err(StorageProofError::DuplicateNodes);
63			}
64		}
65
66		Ok(StorageProof { trie_nodes: trie_nodes_set })
67	}
68
69	/// Returns a new empty proof.
70	///
71	/// An empty proof is capable of only proving trivial statements (ie. that an empty set of
72	/// key-value pairs exist in storage).
73	pub fn empty() -> Self {
74		StorageProof { trie_nodes: BTreeSet::new() }
75	}
76
77	/// Returns whether this is an empty proof.
78	pub fn is_empty(&self) -> bool {
79		self.trie_nodes.is_empty()
80	}
81
82	/// Returns the number of nodes in the proof.
83	pub fn len(&self) -> usize {
84		self.trie_nodes.len()
85	}
86
87	/// Convert into an iterator over encoded trie nodes in lexicographical order constructed
88	/// from the proof.
89	pub fn into_iter_nodes(self) -> impl Sized + DoubleEndedIterator<Item = Vec<u8>> {
90		self.trie_nodes.into_iter()
91	}
92
93	/// Create an iterator over encoded trie nodes in lexicographical order constructed
94	/// from the proof.
95	pub fn iter_nodes(&self) -> impl Sized + DoubleEndedIterator<Item = &Vec<u8>> {
96		self.trie_nodes.iter()
97	}
98
99	/// Convert into plain node vector.
100	pub fn into_nodes(self) -> BTreeSet<Vec<u8>> {
101		self.trie_nodes
102	}
103
104	/// Creates a [`MemoryDB`](crate::MemoryDB) from `Self`.
105	pub fn into_memory_db<H: Hasher>(self) -> crate::MemoryDB<H> {
106		self.into()
107	}
108
109	/// Creates a [`MemoryDB`](crate::MemoryDB) from `Self` reference.
110	pub fn to_memory_db<H: Hasher>(&self) -> crate::MemoryDB<H> {
111		self.into()
112	}
113
114	/// Merges multiple storage proofs covering potentially different sets of keys into one proof
115	/// covering all keys. The merged proof output may be smaller than the aggregate size of the
116	/// input proofs due to deduplication of trie nodes.
117	pub fn merge(proofs: impl IntoIterator<Item = Self>) -> Self {
118		let trie_nodes = proofs
119			.into_iter()
120			.flat_map(|proof| proof.into_iter_nodes())
121			.collect::<BTreeSet<_>>()
122			.into_iter()
123			.collect();
124
125		Self { trie_nodes }
126	}
127
128	/// Encode as a compact proof with default trie layout.
129	pub fn into_compact_proof<H: Hasher>(
130		self,
131		root: H::Out,
132	) -> Result<CompactProof, crate::CompactProofError<H::Out, crate::Error<H::Out>>> {
133		let db = self.into_memory_db();
134		crate::encode_compact::<Layout<H>, crate::MemoryDB<H>>(&db, &root)
135	}
136
137	/// Encode as a compact proof with default trie layout.
138	pub fn to_compact_proof<H: Hasher>(
139		&self,
140		root: H::Out,
141	) -> Result<CompactProof, crate::CompactProofError<H::Out, crate::Error<H::Out>>> {
142		let db = self.to_memory_db();
143		crate::encode_compact::<Layout<H>, crate::MemoryDB<H>>(&db, &root)
144	}
145
146	/// Returns the estimated encoded size of the compact proof.
147	///
148	/// Running this operation is a slow operation (build the whole compact proof) and should only
149	/// be in non sensitive path.
150	///
151	/// Return `None` on error.
152	pub fn encoded_compact_size<H: Hasher>(self, root: H::Out) -> Option<usize> {
153		let compact_proof = self.into_compact_proof::<H>(root);
154		compact_proof.ok().map(|p| p.encoded_size())
155	}
156}
157
158impl<H: Hasher> From<StorageProof> for crate::MemoryDB<H> {
159	fn from(proof: StorageProof) -> Self {
160		From::from(&proof)
161	}
162}
163
164impl<H: Hasher> From<&StorageProof> for crate::MemoryDB<H> {
165	fn from(proof: &StorageProof) -> Self {
166		let mut db = crate::MemoryDB::default();
167		proof.iter_nodes().for_each(|n| {
168			db.insert(crate::EMPTY_PREFIX, &n);
169		});
170		db
171	}
172}
173
174/// Storage proof in compact form.
175#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
176pub struct CompactProof {
177	pub encoded_nodes: Vec<Vec<u8>>,
178}
179
180impl CompactProof {
181	/// Return an iterator on the compact encoded nodes.
182	pub fn iter_compact_encoded_nodes(&self) -> impl Iterator<Item = &[u8]> {
183		self.encoded_nodes.iter().map(Vec::as_slice)
184	}
185
186	/// Decode to a full storage_proof.
187	pub fn to_storage_proof<H: Hasher>(
188		&self,
189		expected_root: Option<&H::Out>,
190	) -> Result<(StorageProof, H::Out), crate::CompactProofError<H::Out, crate::Error<H::Out>>> {
191		let mut db = crate::MemoryDB::<H>::new(&[]);
192		let root = crate::decode_compact::<Layout<H>, _, _>(
193			&mut db,
194			self.iter_compact_encoded_nodes(),
195			expected_root,
196		)?;
197		Ok((
198			StorageProof::new(db.drain().into_iter().filter_map(|kv| {
199				if (kv.1).1 > 0 {
200					Some((kv.1).0)
201				} else {
202					None
203				}
204			})),
205			root,
206		))
207	}
208
209	/// Convert self into a [`MemoryDB`](crate::MemoryDB).
210	///
211	/// `expected_root` is the expected root of this compact proof.
212	///
213	/// Returns the memory db and the root of the trie.
214	pub fn to_memory_db<H: Hasher>(
215		&self,
216		expected_root: Option<&H::Out>,
217	) -> Result<(crate::MemoryDB<H>, H::Out), crate::CompactProofError<H::Out, crate::Error<H::Out>>>
218	{
219		let mut db = crate::MemoryDB::<H>::new(&[]);
220		let root = crate::decode_compact::<Layout<H>, _, _>(
221			&mut db,
222			self.iter_compact_encoded_nodes(),
223			expected_root,
224		)?;
225
226		Ok((db, root))
227	}
228}
229
230#[cfg(test)]
231pub mod tests {
232	use super::*;
233	use crate::{tests::create_storage_proof, StorageProof};
234
235	type Hasher = sp_core::Blake2Hasher;
236	type Layout = crate::LayoutV1<Hasher>;
237
238	const TEST_DATA: &[(&[u8], &[u8])] =
239		&[(b"key1", &[1; 64]), (b"key2", &[2; 64]), (b"key3", &[3; 64]), (b"key11", &[4; 64])];
240
241	#[test]
242	fn proof_with_duplicate_nodes_is_rejected() {
243		let (raw_proof, _root) = create_storage_proof::<Layout>(TEST_DATA);
244		assert!(matches!(
245			StorageProof::new_with_duplicate_nodes_check(raw_proof),
246			Err(StorageProofError::DuplicateNodes)
247		));
248	}
249
250	#[test]
251	fn invalid_compact_proof_does_not_panic_when_decoding() {
252		let invalid_proof = CompactProof { encoded_nodes: vec![vec![135]] };
253		let result = invalid_proof.to_memory_db::<Hasher>(None);
254		assert!(result.is_err());
255	}
256}