Skip to content

Commit

Permalink
implement trait for base2
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi committed Sep 19, 2024
1 parent 240a777 commit 0f1e28b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
7 changes: 2 additions & 5 deletions substrate/primitives/runtime/src/proving_trie/base16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<Hashing, Key, Value> ProvingTrie<Hashing, Key, Value> 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<I>(items: I) -> Result<Self, DispatchError>
Expand Down Expand Up @@ -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<Value>
where
Value: Decode,
{
fn query(&self, key: Key) -> Option<Value> {
let trie = TrieDBBuilder::new(&self.db, &self.root).build();
key.using_encoded(|s| trie.get(s))
.ok()?
Expand Down
39 changes: 22 additions & 17 deletions substrate/primitives/runtime/src/proving_trie/base2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,14 +37,15 @@ where
_phantom: core::marker::PhantomData<(Key, Value)>,
}

impl<Hashing, Key, Value> BasicProvingTrie<Hashing, Key, Value>
impl<Hashing, Key, Value> ProvingTrie<Hashing, Key, Value> for BasicProvingTrie<Hashing, Key, Value>
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<I>(items: I) -> Result<Self, DispatchError>
fn generate_for<I>(items: I) -> Result<Self, DispatchError>
where
I: IntoIterator<Item = (Key, Value)>,
{
Expand All @@ -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<Value>
where
Value: Decode + Clone,
{
fn query(&self, key: Key) -> Option<Value> {
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<Vec<u8>, DispatchError>
where
Hashing::Out: Encode,
{
fn create_proof(&self, key: Key) -> Result<Vec<u8>, DispatchError> {
let mut encoded = Vec::with_capacity(self.db.len());
let mut found_index = None;

Expand All @@ -94,10 +89,20 @@ where
let proof = merkle_proof::<Hashing, Vec<Vec<u8>>, Vec<u8>>(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::<Hashing, Key, Value>(root, proof, key, value)
}
}

/// Verify the existence of `key` and `value` in a given trie root and proof.
pub fn verify_single_value_proof<Hashing, Key, Value>(
pub fn verify_proof<Hashing, Key, Value>(
root: Hashing::Out,
proof: &[u8],
key: Key,
Expand All @@ -119,7 +124,7 @@ where
return Err(TrieError::ValueMismatch.into());
}

if verify_proof::<Hashing, _, _>(
if binary_merkle_tree::verify_proof::<Hashing, _, _>(
&decoded_proof.root,
decoded_proof.proof,
decoded_proof.number_of_leaves,
Expand Down
4 changes: 1 addition & 3 deletions substrate/primitives/runtime/src/proving_trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>
where
Value: Decode;
fn query(&self, key: Key) -> Option<Value>;
/// 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<Vec<u8>, DispatchError>;
/// Verify the existence of `key` and `value` in a given trie root and proof.
Expand Down

0 comments on commit 0f1e28b

Please sign in to comment.