1use 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
24use crate::LayoutV1 as Layout;
27
28#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
30pub enum StorageProofError {
31 DuplicateNodes,
33}
34
35#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode, DecodeWithMemTracking, TypeInfo)]
43pub struct StorageProof {
44 trie_nodes: BTreeSet<Vec<u8>>,
45}
46
47impl StorageProof {
48 pub fn new(trie_nodes: impl IntoIterator<Item = Vec<u8>>) -> Self {
50 StorageProof { trie_nodes: BTreeSet::from_iter(trie_nodes) }
51 }
52
53 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 pub fn empty() -> Self {
74 StorageProof { trie_nodes: BTreeSet::new() }
75 }
76
77 pub fn is_empty(&self) -> bool {
79 self.trie_nodes.is_empty()
80 }
81
82 pub fn len(&self) -> usize {
84 self.trie_nodes.len()
85 }
86
87 pub fn into_iter_nodes(self) -> impl Sized + DoubleEndedIterator<Item = Vec<u8>> {
90 self.trie_nodes.into_iter()
91 }
92
93 pub fn iter_nodes(&self) -> impl Sized + DoubleEndedIterator<Item = &Vec<u8>> {
96 self.trie_nodes.iter()
97 }
98
99 pub fn into_nodes(self) -> BTreeSet<Vec<u8>> {
101 self.trie_nodes
102 }
103
104 pub fn into_memory_db<H: Hasher>(self) -> crate::MemoryDB<H> {
106 self.into()
107 }
108
109 pub fn to_memory_db<H: Hasher>(&self) -> crate::MemoryDB<H> {
111 self.into()
112 }
113
114 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 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 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 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#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
176pub struct CompactProof {
177 pub encoded_nodes: Vec<Vec<u8>>,
178}
179
180impl CompactProof {
181 pub fn iter_compact_encoded_nodes(&self) -> impl Iterator<Item = &[u8]> {
183 self.encoded_nodes.iter().map(Vec::as_slice)
184 }
185
186 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 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}