mirror of
https://github.com/mii443/merkle.rs.git
synced 2025-08-22 16:05:30 +00:00
Test the Protobuf serialization.
This commit is contained in:
@ -9,7 +9,7 @@ use proof::{ Proof, Lemma };
|
||||
/// A Merkle tree is a binary tree, with values of type `T` at the leafs,
|
||||
/// and where every node holds the hash of the concatenation of the hashes of
|
||||
/// its children nodes.
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MerkleTree<D, T> {
|
||||
/// The hashing function used by this Merkle tree
|
||||
digest: D,
|
||||
|
@ -6,7 +6,7 @@ use merkledigest::MerkleDigest;
|
||||
|
||||
/// An inclusion proof represent the fact that a `value` is a member
|
||||
/// of a `MerkleTree` with root hash `root_hash`, and hash function `digest`.
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Proof<D, T> {
|
||||
|
||||
/// The hash function used in the original `MerkleTree`
|
||||
@ -75,7 +75,7 @@ impl <D, T> Proof<D, T> {
|
||||
/// A `Lemma` holds the hash of a node, the hash of its sibling node,
|
||||
/// and a sub lemma, whose `node_hash`, when combined with this `sibling_hash`
|
||||
/// must be equal to this `node_hash`.
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Lemma {
|
||||
pub node_hash: Vec<u8>,
|
||||
pub sibling_hash: Option<Positioned<Vec<u8>>>,
|
||||
@ -134,7 +134,7 @@ impl Lemma {
|
||||
}
|
||||
|
||||
/// Tags a value so that we know from which branch of a `Tree` (if any) it was found.
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Positioned<T> {
|
||||
|
||||
/// The value was found in the left branch
|
||||
|
@ -9,7 +9,7 @@ pub use proof::{
|
||||
};
|
||||
|
||||
/// Binary Tree where leaves hold a stand-alone value.
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Tree<T> {
|
||||
Leaf {
|
||||
hash: Vec<u8>,
|
||||
|
28
tests/proto.rs
Normal file
28
tests/proto.rs
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
#![cfg(feature="serialization-protobuf")]
|
||||
|
||||
extern crate crypto;
|
||||
extern crate merkle;
|
||||
extern crate protobuf;
|
||||
|
||||
use crypto::sha3::Sha3;
|
||||
use merkle::{ MerkleTree, Proof };
|
||||
|
||||
#[test]
|
||||
fn test_protobuf_inverse() {
|
||||
let digest = Sha3::sha3_256();
|
||||
let values = (1..10).map(|x| vec![x]).collect::<Vec<_>>();
|
||||
|
||||
let tree = MerkleTree::from_vec_unsafe(digest.clone(), values.clone());
|
||||
|
||||
for value in values {
|
||||
let proof = tree.gen_proof(value).unwrap();
|
||||
let bytes = proof.clone().write_to_bytes().unwrap();
|
||||
let res = Proof::<Sha3, Vec<u8>>::parse_from_bytes(&bytes, digest).unwrap().unwrap();
|
||||
|
||||
assert_eq!(proof.root_hash, res.root_hash);
|
||||
assert_eq!(proof.value, res.value);
|
||||
assert_eq!(proof.lemma, res.lemma);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user