Browse Source

Remove unnecessary layer of indirection / unnecessary allocation of hashes (#2620)

pull/2528/merge
Dev Ojha 6 years ago
committed by Ethan Buchman
parent
commit
1b51cf3f46
1 changed files with 9 additions and 22 deletions
  1. +9
    -22
      crypto/merkle/simple_tree.go

+ 9
- 22
crypto/merkle/simple_tree.go View File

@ -21,12 +21,16 @@ func SimpleHashFromTwoHashes(left, right []byte) []byte {
// SimpleHashFromByteSlices computes a Merkle tree where the leaves are the byte slice,
// in the provided order.
func SimpleHashFromByteSlices(items [][]byte) []byte {
hashes := make([][]byte, len(items))
for i, item := range items {
hash := tmhash.Sum(item)
hashes[i] = hash
switch len(items) {
case 0:
return nil
case 1:
return tmhash.Sum(items[0])
default:
left := SimpleHashFromByteSlices(items[:(len(items)+1)/2])
right := SimpleHashFromByteSlices(items[(len(items)+1)/2:])
return SimpleHashFromTwoHashes(left, right)
}
return simpleHashFromHashes(hashes)
}
// SimpleHashFromMap computes a Merkle tree from sorted map.
@ -40,20 +44,3 @@ func SimpleHashFromMap(m map[string][]byte) []byte {
}
return sm.Hash()
}
//----------------------------------------------------------------
// Expects hashes!
func simpleHashFromHashes(hashes [][]byte) []byte {
// Recursive impl.
switch len(hashes) {
case 0:
return nil
case 1:
return hashes[0]
default:
left := simpleHashFromHashes(hashes[:(len(hashes)+1)/2])
right := simpleHashFromHashes(hashes[(len(hashes)+1)/2:])
return SimpleHashFromTwoHashes(left, right)
}
}

Loading…
Cancel
Save