sp-trie: Switch to thiserror and some other small cleanups (#10954)

* sp-trie: Switch to thiserror and some other small cleanups

* Add some extra method for converting a compact proof to a memory db
This commit is contained in:
Bastian Köcher
2022-03-01 23:33:47 +01:00
committed by GitHub
parent 6006968199
commit fe5a50129a
6 changed files with 74 additions and 126 deletions
+40 -28
View File
@@ -35,12 +35,6 @@ pub struct StorageProof {
trie_nodes: Vec<Vec<u8>>,
}
/// Storage proof in compact form.
#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
pub struct CompactProof {
pub encoded_nodes: Vec<Vec<u8>>,
}
impl StorageProof {
/// Constructs a storage proof from a subset of encoded trie nodes in a storage backend.
pub fn new(trie_nodes: Vec<Vec<u8>>) -> Self {
@@ -71,7 +65,7 @@ impl StorageProof {
self.trie_nodes
}
/// Creates a `MemoryDB` from `Self`.
/// Creates a [`MemoryDB`](crate::MemoryDB) from `Self`.
pub fn into_memory_db<H: Hasher>(self) -> crate::MemoryDB<H> {
self.into()
}
@@ -79,10 +73,7 @@ impl StorageProof {
/// 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.
pub fn merge<I>(proofs: I) -> Self
where
I: IntoIterator<Item = Self>,
{
pub fn merge(proofs: impl IntoIterator<Item = Self>) -> Self {
let trie_nodes = proofs
.into_iter()
.flat_map(|proof| proof.iter_nodes())
@@ -93,12 +84,11 @@ impl StorageProof {
Self { trie_nodes }
}
/// Encode as a compact proof with default
/// trie layout.
/// Encode as a compact proof with default trie layout.
pub fn into_compact_proof<H: Hasher>(
self,
root: H::Out,
) -> Result<CompactProof, crate::CompactProofError<Layout<H>>> {
) -> Result<CompactProof, crate::CompactProofError<H::Out, crate::Error>> {
crate::encode_compact::<Layout<H>>(self, root)
}
@@ -114,6 +104,22 @@ impl StorageProof {
}
}
impl<H: Hasher> From<StorageProof> for crate::MemoryDB<H> {
fn from(proof: StorageProof) -> Self {
let mut db = crate::MemoryDB::default();
proof.iter_nodes().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 {
pub encoded_nodes: Vec<Vec<u8>>,
}
impl CompactProof {
/// Return an iterator on the compact encoded nodes.
pub fn iter_compact_encoded_nodes(&self) -> impl Iterator<Item = &[u8]> {
@@ -121,13 +127,10 @@ impl CompactProof {
}
/// Decode to a full storage_proof.
///
/// Method use a temporary `HashDB`, and `sp_trie::decode_compact`
/// is often better.
pub fn to_storage_proof<H: Hasher>(
&self,
expected_root: Option<&H::Out>,
) -> Result<(StorageProof, H::Out), crate::CompactProofError<Layout<H>>> {
) -> Result<(StorageProof, H::Out), crate::CompactProofError<H::Out, crate::Error>> {
let mut db = crate::MemoryDB::<H>::new(&[]);
let root = crate::decode_compact::<Layout<H>, _, _>(
&mut db,
@@ -144,6 +147,25 @@ impl CompactProof {
root,
))
}
/// Convert self into a [`MemoryDB`](crate::MemoryDB).
///
/// `expected_root` is the expected root of this compact proof.
///
/// Returns the memory db and the root of the trie.
pub fn to_memory_db<H: Hasher>(
&self,
expected_root: Option<&H::Out>,
) -> Result<(crate::MemoryDB<H>, H::Out), crate::CompactProofError<H::Out, crate::Error>> {
let mut db = crate::MemoryDB::<H>::new(&[]);
let root = crate::decode_compact::<Layout<H>, _, _>(
&mut db,
self.iter_compact_encoded_nodes(),
expected_root,
)?;
Ok((db, root))
}
}
/// An iterator over trie nodes constructed from a storage proof. The nodes are not guaranteed to
@@ -165,13 +187,3 @@ impl Iterator for StorageProofNodeIterator {
self.inner.next()
}
}
impl<H: Hasher> From<StorageProof> for crate::MemoryDB<H> {
fn from(proof: StorageProof) -> Self {
let mut db = crate::MemoryDB::default();
for item in proof.iter_nodes() {
db.insert(crate::EMPTY_PREFIX, &item);
}
db
}
}