You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.3 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package merkle
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. . "github.com/tendermint/tendermint/binary"
  6. )
  7. /*
  8. Compute a deterministic merkle hash from a list of byteslices.
  9. */
  10. func HashFromBinarySlice(items []Binary) ByteSlice {
  11. switch len(items) {
  12. case 0:
  13. panic("Cannot compute hash of empty slice")
  14. case 1:
  15. hasher := sha256.New()
  16. _, err := items[0].WriteTo(hasher)
  17. if err != nil {
  18. panic(err)
  19. }
  20. return ByteSlice(hasher.Sum(nil))
  21. default:
  22. hasher := sha256.New()
  23. _, err := HashFromBinarySlice(items[0 : len(items)/2]).WriteTo(hasher)
  24. if err != nil {
  25. panic(err)
  26. }
  27. _, err = HashFromBinarySlice(items[len(items)/2:]).WriteTo(hasher)
  28. if err != nil {
  29. panic(err)
  30. }
  31. return ByteSlice(hasher.Sum(nil))
  32. }
  33. }
  34. func PrintIAVLNode(node *IAVLNode) {
  35. fmt.Println("==== NODE")
  36. if node != nil {
  37. printIAVLNode(node, 0)
  38. }
  39. fmt.Println("==== END")
  40. }
  41. func printIAVLNode(node *IAVLNode, indent int) {
  42. indentPrefix := ""
  43. for i := 0; i < indent; i++ {
  44. indentPrefix += " "
  45. }
  46. if node.right != nil {
  47. printIAVLNode(node.rightFilled(nil), indent+1)
  48. }
  49. fmt.Printf("%s%v:%v\n", indentPrefix, node.key, node.height)
  50. if node.left != nil {
  51. printIAVLNode(node.leftFilled(nil), indent+1)
  52. }
  53. }
  54. func maxUint8(a, b uint8) uint8 {
  55. if a > b {
  56. return a
  57. }
  58. return b
  59. }