rootmultistore.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package lightclient
  2. import (
  3. "fmt"
  4. "github.com/tendermint/tendermint/crypto/merkle"
  5. "github.com/tendermint/tendermint/crypto/tmhash"
  6. )
  7. //----------------------------------------
  8. // CommitID
  9. // CommitID contains the tree version number and its merkle root.
  10. type CommitID struct {
  11. Version int64
  12. Hash []byte
  13. }
  14. func (cid CommitID) IsZero() bool { //nolint
  15. return cid.Version == 0 && len(cid.Hash) == 0
  16. }
  17. func (cid CommitID) String() string {
  18. return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version)
  19. }
  20. //----------------------------------------
  21. // CommitInfo
  22. // NOTE: Keep CommitInfo a simple immutable struct.
  23. type CommitInfo struct {
  24. // Version
  25. Version int64
  26. // Store info for
  27. StoreInfos []StoreInfo
  28. }
  29. // Hash returns the simple merkle root hash of the stores sorted by name.
  30. func (ci CommitInfo) Hash() []byte {
  31. // TODO cache to ci.hash []byte
  32. m := make(map[string][]byte, len(ci.StoreInfos))
  33. for _, storeInfo := range ci.StoreInfos {
  34. m[storeInfo.Name] = storeInfo.Hash()
  35. }
  36. return merkle.SimpleHashFromMap(m)
  37. }
  38. func (ci CommitInfo) CommitID() CommitID {
  39. return CommitID{
  40. Version: ci.Version,
  41. Hash: ci.Hash(),
  42. }
  43. }
  44. //----------------------------------------
  45. // StoreInfo
  46. // StoreInfo contains the name and core reference for an
  47. // underlying store. It is the leaf of the rootMultiStores top
  48. // level simple merkle tree.
  49. type StoreInfo struct {
  50. Name string
  51. Core StoreCore
  52. }
  53. type StoreCore struct {
  54. // StoreType StoreType
  55. CommitID CommitID
  56. // ... maybe add more state
  57. }
  58. // Implements merkle.Hasher.
  59. func (si StoreInfo) Hash() []byte {
  60. // Doesn't write Name, since merkle.SimpleHashFromMap() will
  61. // include them via the keys.
  62. bz, _ := Cdc.MarshalBinaryLengthPrefixed(si.Core) // Does not error
  63. hasher := tmhash.New()
  64. _, err := hasher.Write(bz)
  65. if err != nil {
  66. // TODO: Handle with #870
  67. panic(err)
  68. }
  69. return hasher.Sum(nil)
  70. }