Skip to content

Commit

Permalink
removed let binding and added a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor O'Hara authored and Connor O'Hara committed Apr 23, 2024
1 parent 1c76d89 commit 19f3e16
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ borsh = { version = "0.10.0", optional = true }
borsh = { version = "0.10.0" }
serde_json = "1.0.96"
postcard = { version = "1.0.4" }
tendermint = {version = "0.35.0"}

[features]
default = ["std"]
Expand Down
24 changes: 20 additions & 4 deletions src/tendermint_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ fn leaf_hash(bytes: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(LEAF_PREFIX);
hasher.update(bytes);
let result: [u8; 32] = hasher.finalize().into();
result
hasher.finalize().into()
}

fn inner_hash(left: &[u8], right: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(INNER_PREFIX);
hasher.update(left);
hasher.update(right);
let result: [u8; 32] = hasher.finalize().into();
result
hasher.finalize().into()
}

/// A sha256 hasher, compatible with [Tendermint merkle hash](https://github.com/informalsystems/tendermint-rs/blob/979456c9f33463944f97f7ea3900640e59f7ea6d/tendermint/src/merkle.rs)
Expand All @@ -44,4 +42,22 @@ impl MerkleHash for TmSha2Hasher {
fn hash_nodes(&self, left: &Self::Output, right: &Self::Output) -> Self::Output {
inner_hash(left, right)
}
}

#[cfg(test)]
mod tests {
use super::*;
use tendermint::merkle::simple_hash_from_byte_vectors;
use crate::{MerkleTree, MemDb};
#[test]
fn test_tm_hash_matches_upstream() {
let leaves: Vec<&[u8]> = vec![b"leaf_1", b"leaf_2", b"leaf_3", b"leaf_4"];
let hasher = TmSha2Hasher{};
let mut tree: MerkleTree<MemDb<[u8; 32]>, TmSha2Hasher> = MerkleTree::with_hasher(hasher);
leaves.iter().for_each(|leaf| {
tree.push_raw_leaf(leaf);
});
let hash_from_byte_slices = simple_hash_from_byte_vectors::<Sha256>(leaves.as_slice());
assert_eq!(tree.root().as_ref(), &hash_from_byte_slices);
}
}

0 comments on commit 19f3e16

Please sign in to comment.