Remove leaf hashing in MerkleRoot function (#1653)

* Remove leaf hashing in MerkleRoot function

See https://github.com/ethereum/eth2.0-specs/pull/646 for clarification.

* Add comment clarifying leaf hashing removal
This commit is contained in:
Cayman 2019-02-20 11:21:35 -07:00 committed by Nishant Das
parent 9bee695a3b
commit e3ba3e181c
2 changed files with 6 additions and 10 deletions

View File

@ -3,6 +3,8 @@ package hashutil
// MerkleRoot derives the merkle root from a 2d byte array with each element
// in the outer array signifying the data that is to be represented in the
// merkle tree.
// Note: This function is only used to merklize a list of block root hashes.
// As such, we assume the input comes pre-hashed and do NOT hash the leaves.
// Spec:
// def merkle_root(values):
// o = [0] * len(values) + values
@ -12,12 +14,6 @@ package hashutil
func MerkleRoot(values [][]byte) []byte {
length := len(values)
// Data is hashed so as to be stored as leaves in the tree.
for i, v := range values {
hashedValue := Hash(v)
values[i] = hashedValue[:]
}
newSet := make([][]byte, length, length*2)
newSet = append(newSet, values...)

View File

@ -13,10 +13,10 @@ func TestMerkleRoot(t *testing.T) {
{'d'},
}
hashedV1 := Hash([]byte{'a'})
hashedV2 := Hash([]byte{'b'})
hashedV3 := Hash([]byte{'c'})
hashedV4 := Hash([]byte{'d'})
hashedV1 := []byte{'a'}
hashedV2 := []byte{'b'}
hashedV3 := []byte{'c'}
hashedV4 := []byte{'d'}
leftNode := Hash(append(hashedV1[:], hashedV2[:]...))
rightNode := Hash(append(hashedV3[:], hashedV4[:]...))