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.

93 lines
2.3 KiB

  1. /*
  2. Computes a deterministic minimal height merkle tree hash.
  3. If the number of items is not a power of two, some leaves
  4. will be at different levels. Tries to keep both sides of
  5. the tree the same size, but the left may be one greater.
  6. Use this for short deterministic trees, such as the validator list.
  7. For larger datasets, use IAVLTree.
  8. *
  9. / \
  10. / \
  11. / \
  12. / \
  13. * *
  14. / \ / \
  15. / \ / \
  16. / \ / \
  17. * * * h6
  18. / \ / \ / \
  19. h0 h1 h2 h3 h4 h5
  20. */
  21. package merkle
  22. import (
  23. "golang.org/x/crypto/ripemd160"
  24. "github.com/tendermint/go-wire"
  25. . "github.com/tendermint/tmlibs/common"
  26. )
  27. func SimpleHashFromTwoHashes(left []byte, right []byte) []byte {
  28. var n int
  29. var err error
  30. var hasher = ripemd160.New()
  31. wire.WriteByteSlice(left, hasher, &n, &err)
  32. wire.WriteByteSlice(right, hasher, &n, &err)
  33. if err != nil {
  34. PanicCrisis(err)
  35. }
  36. return hasher.Sum(nil)
  37. }
  38. func SimpleHashFromHashes(hashes [][]byte) []byte {
  39. // Recursive impl.
  40. switch len(hashes) {
  41. case 0:
  42. return nil
  43. case 1:
  44. return hashes[0]
  45. default:
  46. left := SimpleHashFromHashes(hashes[:(len(hashes)+1)/2])
  47. right := SimpleHashFromHashes(hashes[(len(hashes)+1)/2:])
  48. return SimpleHashFromTwoHashes(left, right)
  49. }
  50. }
  51. // Convenience for SimpleHashFromHashes.
  52. func SimpleHashFromBinaries(items []interface{}) []byte {
  53. hashes := make([][]byte, len(items))
  54. for i, item := range items {
  55. hashes[i] = SimpleHashFromBinary(item)
  56. }
  57. return SimpleHashFromHashes(hashes)
  58. }
  59. // General Convenience
  60. func SimpleHashFromBinary(item interface{}) []byte {
  61. hasher, n, err := ripemd160.New(), new(int), new(error)
  62. wire.WriteBinary(item, hasher, n, err)
  63. if *err != nil {
  64. PanicCrisis(err)
  65. }
  66. return hasher.Sum(nil)
  67. }
  68. // Convenience for SimpleHashFromHashes.
  69. func SimpleHashFromHashables(items []Hashable) []byte {
  70. hashes := make([][]byte, len(items))
  71. for i, item := range items {
  72. hash := item.Hash()
  73. hashes[i] = hash
  74. }
  75. return SimpleHashFromHashes(hashes)
  76. }
  77. // Convenience for SimpleHashFromHashes.
  78. func SimpleHashFromMap(m map[string]interface{}) []byte {
  79. kpPairsH := MakeSortedKVPairs(m)
  80. return SimpleHashFromHashables(kpPairsH)
  81. }