Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty Merkle tree hash fix #557

Merged
merged 2 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Unreleased

- Add spec for the light client attack evidence handling ([#544])
- return rfc6962 hash for empty merkle tree ([#498])

[#544]: https://github.com/informalsystems/tendermint-rs/pull/544
[#498]: https://github.com/informalsystems/tendermint-rs/issues/498

## v0.16.0

Expand Down
18 changes: 16 additions & 2 deletions tendermint/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn simple_hash_from_byte_vectors(byte_vecs: Vec<Vec<u8>>) -> Hash {
fn simple_hash_from_byte_slices_inner(byte_slices: &[Vec<u8>]) -> Hash {
let length = byte_slices.len();
match length {
0 => [0; HASH_SIZE],
0 => empty_hash(),
1 => leaf_hash(byte_slices[0].as_slice()),
_ => {
let k = get_split_point(length);
Expand All @@ -42,6 +42,20 @@ fn get_split_point(length: usize) -> usize {
}
}

// tmhash({})
fn empty_hash() -> Hash {
// the empty string / byte slice
let empty = Vec::with_capacity(0);

// hash it !
let digest = Sha256::digest(&empty);

// copy the GenericArray out
let mut hash_bytes = [0u8; HASH_SIZE];
hash_bytes.copy_from_slice(&digest);
hash_bytes
}

// tmhash(0x00 || leaf)
fn leaf_hash(bytes: &[u8]) -> Hash {
// make a new array starting with 0 and copy in the bytes
Expand Down Expand Up @@ -97,7 +111,7 @@ mod tests {
#[test]
fn test_rfc6962_empty_tree() {
let empty_tree_root_hex =
"0000000000000000000000000000000000000000000000000000000000000000";
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
let empty_tree_root = &hex::decode(empty_tree_root_hex).unwrap();
let empty_tree: Vec<Vec<u8>> = vec![vec![]; 0];

Expand Down