1818use codec:: { Decode , Encode } ;
1919use hash_db:: { HashDB , Hasher } ;
2020use scale_info:: TypeInfo ;
21- use sp_std:: { collections:: btree_set:: BTreeSet , iter:: IntoIterator , vec:: Vec } ;
21+ use sp_std:: {
22+ collections:: btree_set:: BTreeSet ,
23+ iter:: { DoubleEndedIterator , IntoIterator } ,
24+ vec:: Vec ,
25+ } ;
2226// Note that `LayoutV1` usage here (proof compaction) is compatible
2327// with `LayoutV0`.
2428use crate :: LayoutV1 as Layout ;
@@ -54,10 +58,16 @@ impl StorageProof {
5458 self . trie_nodes . is_empty ( )
5559 }
5660
61+ /// Convert into an iterator over encoded trie nodes in lexicographical order constructed
62+ /// from the proof.
63+ pub fn into_iter_nodes ( self ) -> impl Sized + DoubleEndedIterator < Item = Vec < u8 > > {
64+ self . trie_nodes . into_iter ( )
65+ }
66+
5767 /// Create an iterator over encoded trie nodes in lexicographical order constructed
5868 /// from the proof.
59- pub fn iter_nodes ( self ) -> StorageProofNodeIterator {
60- StorageProofNodeIterator :: new ( self )
69+ pub fn iter_nodes ( & self ) -> impl Sized + DoubleEndedIterator < Item = & Vec < u8 > > {
70+ self . trie_nodes . iter ( )
6171 }
6272
6373 /// Convert into plain node vector.
@@ -70,14 +80,19 @@ impl StorageProof {
7080 self . into ( )
7181 }
7282
83+ /// Creates a [`MemoryDB`](crate::MemoryDB) from `Self` reference.
84+ pub fn to_memory_db < H : Hasher > ( & self ) -> crate :: MemoryDB < H > {
85+ self . into ( )
86+ }
87+
7388 /// Merges multiple storage proofs covering potentially different sets of keys into one proof
7489 /// covering all keys. The merged proof output may be smaller than the aggregate size of the
7590 /// input proofs due to deduplication of trie nodes.
7691 pub fn merge ( proofs : impl IntoIterator < Item = Self > ) -> Self {
7792 let trie_nodes = proofs
7893 . into_iter ( )
79- . flat_map ( |proof| proof. iter_nodes ( ) )
80- . collect :: < sp_std :: collections :: btree_set :: BTreeSet < _ > > ( )
94+ . flat_map ( |proof| proof. into_iter_nodes ( ) )
95+ . collect :: < BTreeSet < _ > > ( )
8196 . into_iter ( )
8297 . collect ( ) ;
8398
@@ -89,7 +104,17 @@ impl StorageProof {
89104 self ,
90105 root : H :: Out ,
91106 ) -> Result < CompactProof , crate :: CompactProofError < H :: Out , crate :: Error < H :: Out > > > {
92- crate :: encode_compact :: < Layout < H > > ( self , root)
107+ let db = self . into_memory_db ( ) ;
108+ crate :: encode_compact :: < Layout < H > , crate :: MemoryDB < H > > ( & db, & root)
109+ }
110+
111+ /// Encode as a compact proof with default trie layout.
112+ pub fn to_compact_proof < H : Hasher > (
113+ & self ,
114+ root : H :: Out ,
115+ ) -> Result < CompactProof , crate :: CompactProofError < H :: Out , crate :: Error < H :: Out > > > {
116+ let db = self . to_memory_db ( ) ;
117+ crate :: encode_compact :: < Layout < H > , crate :: MemoryDB < H > > ( & db, & root)
93118 }
94119
95120 /// Returns the estimated encoded size of the compact proof.
@@ -106,6 +131,12 @@ impl StorageProof {
106131
107132impl < H : Hasher > From < StorageProof > for crate :: MemoryDB < H > {
108133 fn from ( proof : StorageProof ) -> Self {
134+ From :: from ( & proof)
135+ }
136+ }
137+
138+ impl < H : Hasher > From < & StorageProof > for crate :: MemoryDB < H > {
139+ fn from ( proof : & StorageProof ) -> Self {
109140 let mut db = crate :: MemoryDB :: default ( ) ;
110141 proof. iter_nodes ( ) . for_each ( |n| {
111142 db. insert ( crate :: EMPTY_PREFIX , & n) ;
@@ -169,23 +200,3 @@ impl CompactProof {
169200 Ok ( ( db, root) )
170201 }
171202}
172-
173- /// An iterator over trie nodes constructed from a storage proof. The nodes are not guaranteed to
174- /// be traversed in any particular order.
175- pub struct StorageProofNodeIterator {
176- inner : <BTreeSet < Vec < u8 > > as IntoIterator >:: IntoIter ,
177- }
178-
179- impl StorageProofNodeIterator {
180- fn new ( proof : StorageProof ) -> Self {
181- StorageProofNodeIterator { inner : proof. trie_nodes . into_iter ( ) }
182- }
183- }
184-
185- impl Iterator for StorageProofNodeIterator {
186- type Item = Vec < u8 > ;
187-
188- fn next ( & mut self ) -> Option < Self :: Item > {
189- self . inner . next ( )
190- }
191- }
0 commit comments