From 0f1e28bc4f9cb8e5bb3b0c85b9ae1d2a5a33fdd7 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 19 Sep 2024 17:46:23 -0400 Subject: [PATCH] implement trait for base2 --- .../runtime/src/proving_trie/base16.rs | 7 +--- .../runtime/src/proving_trie/base2.rs | 39 +++++++++++-------- .../runtime/src/proving_trie/mod.rs | 4 +- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/substrate/primitives/runtime/src/proving_trie/base16.rs b/substrate/primitives/runtime/src/proving_trie/base16.rs index a3f2d49dc636..c1379ff4949a 100644 --- a/substrate/primitives/runtime/src/proving_trie/base16.rs +++ b/substrate/primitives/runtime/src/proving_trie/base16.rs @@ -73,7 +73,7 @@ impl ProvingTrie for BasicProvingTrie< where Hashing: sp_core::Hasher, Key: Encode, - Value: Encode, + Value: Encode + Decode, { /// Create a new instance of a `ProvingTrie` using an iterator of key/value pairs. fn generate_for(items: I) -> Result @@ -101,10 +101,7 @@ where /// Query a value contained within the current trie. Returns `None` if the /// nodes within the current `MemoryDB` are insufficient to query the item. - fn query(&self, key: Key) -> Option - where - Value: Decode, - { + fn query(&self, key: Key) -> Option { let trie = TrieDBBuilder::new(&self.db, &self.root).build(); key.using_encoded(|s| trie.get(s)) .ok()? diff --git a/substrate/primitives/runtime/src/proving_trie/base2.rs b/substrate/primitives/runtime/src/proving_trie/base2.rs index 216910f2c7d0..51086fa9b073 100644 --- a/substrate/primitives/runtime/src/proving_trie/base2.rs +++ b/substrate/primitives/runtime/src/proving_trie/base2.rs @@ -20,9 +20,9 @@ //! this library is designed to work more easily with runtime native types, which simply need to //! implement `Encode`/`Decode`. -use super::TrieError; +use super::{ProvingTrie, TrieError}; use crate::{Decode, DispatchError, Encode}; -use binary_merkle_tree::{merkle_proof, merkle_root, verify_proof, MerkleProof}; +use binary_merkle_tree::{merkle_proof, merkle_root, MerkleProof}; use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; /// A helper structure for building a basic base-2 merkle trie and creating compact proofs for that @@ -37,14 +37,15 @@ where _phantom: core::marker::PhantomData<(Key, Value)>, } -impl BasicProvingTrie +impl ProvingTrie for BasicProvingTrie where Hashing: sp_core::Hasher, - Key: Encode + Ord, - Value: Encode, + Hashing::Out: Encode + Decode, + Key: Encode + Decode + Ord, + Value: Encode + Decode + Clone, { /// Create a new instance of a `ProvingTrie` using an iterator of key/value pairs. - pub fn generate_for(items: I) -> Result + fn generate_for(items: I) -> Result where I: IntoIterator, { @@ -57,26 +58,20 @@ where } /// Access the underlying trie root. - pub fn root(&self) -> &Hashing::Out { + fn root(&self) -> &Hashing::Out { &self.root } /// Query a value contained within the current trie. Returns `None` if the /// nodes within the current `db` are insufficient to query the item. - pub fn query(&self, key: Key) -> Option - where - Value: Decode + Clone, - { + fn query(&self, key: Key) -> Option { self.db.get(&key).cloned() } /// Create a compact merkle proof needed to prove a single key and its value are in the trie. /// Returns `None` if the nodes within the current `db` are insufficient to create a /// proof. - pub fn create_single_value_proof(&self, key: Key) -> Result, DispatchError> - where - Hashing::Out: Encode, - { + fn create_proof(&self, key: Key) -> Result, DispatchError> { let mut encoded = Vec::with_capacity(self.db.len()); let mut found_index = None; @@ -94,10 +89,20 @@ where let proof = merkle_proof::>, Vec>(encoded, index as u32); Ok(proof.encode()) } + + /// Verify the existence of `key` and `value` in a given trie root and proof. + fn verify_proof( + root: Hashing::Out, + proof: &[u8], + key: Key, + value: Value, + ) -> Result<(), DispatchError> { + verify_proof::(root, proof, key, value) + } } /// Verify the existence of `key` and `value` in a given trie root and proof. -pub fn verify_single_value_proof( +pub fn verify_proof( root: Hashing::Out, proof: &[u8], key: Key, @@ -119,7 +124,7 @@ where return Err(TrieError::ValueMismatch.into()); } - if verify_proof::( + if binary_merkle_tree::verify_proof::( &decoded_proof.root, decoded_proof.proof, decoded_proof.number_of_leaves, diff --git a/substrate/primitives/runtime/src/proving_trie/mod.rs b/substrate/primitives/runtime/src/proving_trie/mod.rs index e1764bb851b9..267796546e44 100644 --- a/substrate/primitives/runtime/src/proving_trie/mod.rs +++ b/substrate/primitives/runtime/src/proving_trie/mod.rs @@ -127,9 +127,7 @@ where fn root(&self) -> &Hashing::Out; /// Query a value contained within the current trie. Returns `None` if the /// the value does not exist in the trie. - fn query(&self, key: Key) -> Option - where - Value: Decode; + fn query(&self, key: Key) -> Option; /// Create a proof that can be used to verify a key and its value are in the trie. fn create_proof(&self, key: Key) -> Result, DispatchError>; /// Verify the existence of `key` and `value` in a given trie root and proof.