mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 06:42:16 +00:00
Remove unnecessary layer of indirection / unnecessary allocation of hashes (#2620)
This commit is contained in:
parent
2363d88979
commit
1b51cf3f46
@ -21,12 +21,16 @@ func SimpleHashFromTwoHashes(left, right []byte) []byte {
|
|||||||
// SimpleHashFromByteSlices computes a Merkle tree where the leaves are the byte slice,
|
// SimpleHashFromByteSlices computes a Merkle tree where the leaves are the byte slice,
|
||||||
// in the provided order.
|
// in the provided order.
|
||||||
func SimpleHashFromByteSlices(items [][]byte) []byte {
|
func SimpleHashFromByteSlices(items [][]byte) []byte {
|
||||||
hashes := make([][]byte, len(items))
|
switch len(items) {
|
||||||
for i, item := range items {
|
case 0:
|
||||||
hash := tmhash.Sum(item)
|
return nil
|
||||||
hashes[i] = hash
|
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.
|
// SimpleHashFromMap computes a Merkle tree from sorted map.
|
||||||
@ -40,20 +44,3 @@ func SimpleHashFromMap(m map[string][]byte) []byte {
|
|||||||
}
|
}
|
||||||
return sm.Hash()
|
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…
x
Reference in New Issue
Block a user