-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: generalize some functions in sp-trie #12376
Changes from 5 commits
34bab80
88a9527
2401b0c
7d300d9
8e1dd21
990dbf3
91f8c01
aa0f469
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,11 @@ | |
| use codec::{Decode, Encode}; | ||
| use hash_db::{HashDB, Hasher}; | ||
| use scale_info::TypeInfo; | ||
| use sp_std::{collections::btree_set::BTreeSet, iter::IntoIterator, vec::Vec}; | ||
| use sp_std::{ | ||
| collections::btree_set::{BTreeSet, IntoIter, Iter}, | ||
| iter::IntoIterator, | ||
| vec::Vec, | ||
| }; | ||
| // Note that `LayoutV1` usage here (proof compaction) is compatible | ||
| // with `LayoutV0`. | ||
| use crate::LayoutV1 as Layout; | ||
|
|
@@ -54,10 +58,16 @@ impl StorageProof { | |
| self.trie_nodes.is_empty() | ||
| } | ||
|
|
||
| /// Convert into an iterator over encoded trie nodes in lexicographical order constructed | ||
| /// from the proof. | ||
| pub fn iter_nodes(self) -> IntoIter<Vec<u8>> { | ||
|
||
| self.trie_nodes.into_iter() | ||
| } | ||
|
|
||
| /// Create an iterator over encoded trie nodes in lexicographical order constructed | ||
| /// from the proof. | ||
| pub fn iter_nodes(self) -> StorageProofNodeIterator { | ||
| StorageProofNodeIterator::new(self) | ||
| pub fn iter(&self) -> Iter<'_, Vec<u8>> { | ||
|
||
| self.trie_nodes.iter() | ||
| } | ||
|
|
||
| /// Convert into plain node vector. | ||
|
|
@@ -70,6 +80,11 @@ impl StorageProof { | |
| self.into() | ||
| } | ||
|
|
||
| /// Creates a [`MemoryDB`](crate::MemoryDB) from `Self` reference. | ||
| pub fn to_memory_db<H: Hasher>(&self) -> crate::MemoryDB<H> { | ||
| self.into() | ||
| } | ||
|
|
||
| /// Merges multiple storage proofs covering potentially different sets of keys into one proof | ||
| /// covering all keys. The merged proof output may be smaller than the aggregate size of the | ||
| /// input proofs due to deduplication of trie nodes. | ||
|
|
@@ -89,7 +104,17 @@ impl StorageProof { | |
| self, | ||
| root: H::Out, | ||
| ) -> Result<CompactProof, crate::CompactProofError<H::Out, crate::Error<H::Out>>> { | ||
| crate::encode_compact::<Layout<H>>(self, root) | ||
| let db = self.into_memory_db(); | ||
| crate::encode_compact::<Layout<H>, crate::MemoryDB<H>>(db, root) | ||
| } | ||
|
|
||
| /// Encode as a compact proof with default trie layout. | ||
| pub fn to_compact_proof<H: Hasher>( | ||
| &self, | ||
| root: H::Out, | ||
| ) -> Result<CompactProof, crate::CompactProofError<H::Out, crate::Error<H::Out>>> { | ||
| let db = self.to_memory_db(); | ||
| crate::encode_compact::<Layout<H>, crate::MemoryDB<H>>(db, root) | ||
| } | ||
|
|
||
| /// Returns the estimated encoded size of the compact proof. | ||
|
|
@@ -114,6 +139,17 @@ impl<H: Hasher> From<StorageProof> for crate::MemoryDB<H> { | |
| } | ||
| } | ||
|
|
||
| impl<H: Hasher> From<&StorageProof> for crate::MemoryDB<H> { | ||
| fn from(proof: &StorageProof) -> Self { | ||
| let mut db = crate::MemoryDB::default(); | ||
yjhmelody marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| proof.iter().for_each(|n| { | ||
| db.insert(crate::EMPTY_PREFIX, &n); | ||
| }); | ||
|
|
||
| db | ||
| } | ||
| } | ||
|
|
||
| /// Storage proof in compact form. | ||
| #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode, TypeInfo)] | ||
| pub struct CompactProof { | ||
|
|
@@ -169,23 +205,3 @@ impl CompactProof { | |
| Ok((db, root)) | ||
| } | ||
| } | ||
|
|
||
| /// An iterator over trie nodes constructed from a storage proof. The nodes are not guaranteed to | ||
| /// be traversed in any particular order. | ||
| pub struct StorageProofNodeIterator { | ||
| inner: <BTreeSet<Vec<u8>> as IntoIterator>::IntoIter, | ||
| } | ||
|
|
||
| impl StorageProofNodeIterator { | ||
| fn new(proof: StorageProof) -> Self { | ||
| StorageProofNodeIterator { inner: proof.trie_nodes.into_iter() } | ||
| } | ||
| } | ||
|
|
||
| impl Iterator for StorageProofNodeIterator { | ||
| type Item = Vec<u8>; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| self.inner.next() | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.