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.

129 lines
3.8 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "time"
  8. "github.com/tendermint/tendermint/crypto"
  9. cmn "github.com/tendermint/tendermint/libs/common"
  10. tmtime "github.com/tendermint/tendermint/types/time"
  11. )
  12. const (
  13. // MaxChainIDLen is a maximum length of the chain ID.
  14. MaxChainIDLen = 50
  15. )
  16. //------------------------------------------------------------
  17. // core types for a genesis definition
  18. // NOTE: any changes to the genesis definition should
  19. // be reflected in the documentation:
  20. // docs/tendermint-core/using-tendermint.md
  21. // GenesisValidator is an initial validator.
  22. type GenesisValidator struct {
  23. Address Address `json:"address"`
  24. PubKey crypto.PubKey `json:"pub_key"`
  25. Power int64 `json:"power"`
  26. Name string `json:"name"`
  27. }
  28. // GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
  29. type GenesisDoc struct {
  30. GenesisTime time.Time `json:"genesis_time"`
  31. ChainID string `json:"chain_id"`
  32. ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
  33. Validators []GenesisValidator `json:"validators,omitempty"`
  34. AppHash cmn.HexBytes `json:"app_hash"`
  35. AppState json.RawMessage `json:"app_state,omitempty"`
  36. }
  37. // SaveAs is a utility method for saving GenensisDoc as a JSON file.
  38. func (genDoc *GenesisDoc) SaveAs(file string) error {
  39. genDocBytes, err := cdc.MarshalJSONIndent(genDoc, "", " ")
  40. if err != nil {
  41. return err
  42. }
  43. return cmn.WriteFile(file, genDocBytes, 0644)
  44. }
  45. // ValidatorHash returns the hash of the validator set contained in the GenesisDoc
  46. func (genDoc *GenesisDoc) ValidatorHash() []byte {
  47. vals := make([]*Validator, len(genDoc.Validators))
  48. for i, v := range genDoc.Validators {
  49. vals[i] = NewValidator(v.PubKey, v.Power)
  50. }
  51. vset := NewValidatorSet(vals)
  52. return vset.Hash()
  53. }
  54. // ValidateAndComplete checks that all necessary fields are present
  55. // and fills in defaults for optional fields left empty
  56. func (genDoc *GenesisDoc) ValidateAndComplete() error {
  57. if genDoc.ChainID == "" {
  58. return cmn.NewError("Genesis doc must include non-empty chain_id")
  59. }
  60. if len(genDoc.ChainID) > MaxChainIDLen {
  61. return cmn.NewError("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen)
  62. }
  63. if genDoc.ConsensusParams == nil {
  64. genDoc.ConsensusParams = DefaultConsensusParams()
  65. } else {
  66. if err := genDoc.ConsensusParams.Validate(); err != nil {
  67. return err
  68. }
  69. }
  70. for i, v := range genDoc.Validators {
  71. if v.Power == 0 {
  72. return cmn.NewError("The genesis file cannot contain validators with no voting power: %v", v)
  73. }
  74. if len(v.Address) > 0 && !bytes.Equal(v.PubKey.Address(), v.Address) {
  75. return cmn.NewError("Incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address())
  76. }
  77. if len(v.Address) == 0 {
  78. genDoc.Validators[i].Address = v.PubKey.Address()
  79. }
  80. }
  81. if genDoc.GenesisTime.IsZero() {
  82. genDoc.GenesisTime = tmtime.Now()
  83. }
  84. return nil
  85. }
  86. //------------------------------------------------------------
  87. // Make genesis state from file
  88. // GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
  89. func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
  90. genDoc := GenesisDoc{}
  91. err := cdc.UnmarshalJSON(jsonBlob, &genDoc)
  92. if err != nil {
  93. return nil, err
  94. }
  95. if err := genDoc.ValidateAndComplete(); err != nil {
  96. return nil, err
  97. }
  98. return &genDoc, err
  99. }
  100. // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
  101. func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
  102. jsonBlob, err := ioutil.ReadFile(genDocFile)
  103. if err != nil {
  104. return nil, cmn.ErrorWrap(err, "Couldn't read GenesisDoc file")
  105. }
  106. genDoc, err := GenesisDocFromJSON(jsonBlob)
  107. if err != nil {
  108. return nil, cmn.ErrorWrap(err, fmt.Sprintf("Error reading GenesisDoc at %v", genDocFile))
  109. }
  110. return genDoc, nil
  111. }