tendermint/merkle/util.go

70 lines
1.3 KiB
Go
Raw Normal View History

package merkle
import (
2014-07-01 14:50:24 -07:00
"crypto/sha256"
"fmt"
2014-07-01 14:50:24 -07:00
. "github.com/tendermint/tendermint/binary"
)
2014-06-04 01:39:50 -07:00
/*
Compute a deterministic merkle hash from a list of byteslices.
*/
func HashFromBinarySlice(items []Binary) ByteSlice {
2014-07-01 14:50:24 -07:00
switch len(items) {
case 0:
panic("Cannot compute hash of empty slice")
case 1:
hasher := sha256.New()
_, err := items[0].WriteTo(hasher)
if err != nil {
panic(err)
}
return ByteSlice(hasher.Sum(nil))
default:
hasher := sha256.New()
_, err := HashFromBinarySlice(items[0 : len(items)/2]).WriteTo(hasher)
if err != nil {
panic(err)
}
_, err = HashFromBinarySlice(items[len(items)/2:]).WriteTo(hasher)
if err != nil {
panic(err)
}
return ByteSlice(hasher.Sum(nil))
}
2014-06-04 01:39:50 -07:00
}
func PrintIAVLNode(node *IAVLNode) {
2014-07-01 14:50:24 -07:00
fmt.Println("==== NODE")
if node != nil {
printIAVLNode(node, 0)
}
fmt.Println("==== END")
}
func printIAVLNode(node *IAVLNode, indent int) {
2014-07-01 14:50:24 -07:00
indentPrefix := ""
for i := 0; i < indent; i++ {
indentPrefix += " "
}
2014-07-01 14:50:24 -07:00
if node.right != nil {
printIAVLNode(node.rightFilled(nil), indent+1)
}
2014-07-01 14:50:24 -07:00
fmt.Printf("%s%v:%v\n", indentPrefix, node.key, node.height)
2014-07-01 14:50:24 -07:00
if node.left != nil {
printIAVLNode(node.leftFilled(nil), indent+1)
}
}
2014-05-23 17:49:28 -07:00
2014-05-23 23:11:22 -07:00
func maxUint8(a, b uint8) uint8 {
2014-07-01 14:50:24 -07:00
if a > b {
return a
}
return b
2014-05-23 23:11:22 -07:00
}