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.

78 lines
1.7 KiB

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