2017-04-21 12:33:25 -07:00
|
|
|
/*
|
|
|
|
Computes a deterministic minimal height merkle tree hash.
|
|
|
|
If the number of items is not a power of two, some leaves
|
|
|
|
will be at different levels. Tries to keep both sides of
|
|
|
|
the tree the same size, but the left may be one greater.
|
|
|
|
|
|
|
|
Use this for short deterministic trees, such as the validator list.
|
|
|
|
For larger datasets, use IAVLTree.
|
|
|
|
|
|
|
|
*
|
|
|
|
/ \
|
|
|
|
/ \
|
|
|
|
/ \
|
|
|
|
/ \
|
|
|
|
* *
|
|
|
|
/ \ / \
|
|
|
|
/ \ / \
|
|
|
|
/ \ / \
|
|
|
|
* * * h6
|
|
|
|
/ \ / \ / \
|
|
|
|
h0 h1 h2 h3 h4 h5
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
package merkle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/crypto/ripemd160"
|
|
|
|
|
|
|
|
"github.com/tendermint/go-wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SimpleHashFromTwoHashes(left []byte, right []byte) []byte {
|
|
|
|
var hasher = ripemd160.New()
|
2018-01-14 14:35:57 -08:00
|
|
|
err := wire.EncodeByteSlice(hasher, left)
|
|
|
|
err = wire.EncodeByteSlice(hasher, right)
|
2017-04-21 12:33:25 -07:00
|
|
|
if err != nil {
|
2018-01-14 14:35:57 -08:00
|
|
|
panic(err)
|
2017-04-21 12:33:25 -07:00
|
|
|
}
|
|
|
|
return hasher.Sum(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:05:23 -08:00
|
|
|
// NOTE: Do not implement this, use SimpleHashFromByteslices instead.
|
|
|
|
// type Byteser interface { Bytes() []byte }
|
|
|
|
// func SimpleHashFromBytesers(items []Byteser) []byte { ... }
|
|
|
|
|
|
|
|
func SimpleHashFromByteslices(bzs [][]byte) []byte {
|
|
|
|
hashes := make([][]byte, len(bzs))
|
|
|
|
for i, bz := range bzs {
|
|
|
|
hashes[i] = SimpleHashFromBytes(bz)
|
2017-04-21 12:33:25 -07:00
|
|
|
}
|
|
|
|
return SimpleHashFromHashes(hashes)
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:05:23 -08:00
|
|
|
func SimpleHashFromBytes(bz []byte) []byte {
|
2018-01-14 14:35:57 -08:00
|
|
|
hasher := ripemd160.New()
|
|
|
|
hasher.Write(bz)
|
2017-04-21 12:33:25 -07:00
|
|
|
return hasher.Sum(nil)
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:05:23 -08:00
|
|
|
func SimpleHashFromHashers(items []Hasher) []byte {
|
2017-04-21 12:33:25 -07:00
|
|
|
hashes := make([][]byte, len(items))
|
|
|
|
for i, item := range items {
|
|
|
|
hash := item.Hash()
|
|
|
|
hashes[i] = hash
|
|
|
|
}
|
|
|
|
return SimpleHashFromHashes(hashes)
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:05:23 -08:00
|
|
|
func SimpleHashFromMap(m map[string]Hasher) []byte {
|
2017-12-17 13:04:15 -08:00
|
|
|
sm := NewSimpleMap()
|
|
|
|
for k, v := range m {
|
|
|
|
sm.Set(k, v)
|
|
|
|
}
|
|
|
|
return sm.Hash()
|
2017-04-21 12:33:25 -07:00
|
|
|
}
|