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.

126 lines
3.5 KiB

  1. package types
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "time"
  6. "github.com/pkg/errors"
  7. crypto "github.com/tendermint/go-crypto"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. )
  10. //------------------------------------------------------------
  11. // core types for a genesis definition
  12. // GenesisValidator is an initial validator.
  13. type GenesisValidator struct {
  14. PubKey crypto.PubKey `json:"pub_key"`
  15. Power int64 `json:"power"`
  16. Name string `json:"name"`
  17. }
  18. // GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
  19. type GenesisDoc struct {
  20. GenesisTime time.Time `json:"genesis_time"`
  21. ChainID string `json:"chain_id"`
  22. ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
  23. Validators []GenesisValidator `json:"validators"`
  24. AppHash cmn.HexBytes `json:"app_hash"`
  25. _AppState json.RawMessage `json:"app_state,omitempty"`
  26. AppOptions json.RawMessage `json:"app_options,omitempty"` // DEPRECATED
  27. }
  28. // AppState returns raw application state.
  29. // TODO: replace with AppState field during next breaking release (0.18)
  30. func (genDoc *GenesisDoc) AppState() json.RawMessage {
  31. if len(genDoc.AppOptions) > 0 {
  32. return genDoc.AppOptions
  33. } else {
  34. return genDoc._AppState
  35. }
  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 := json.Marshal(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 errors.Errorf("Genesis doc must include non-empty chain_id")
  59. }
  60. if genDoc.ConsensusParams == nil {
  61. genDoc.ConsensusParams = DefaultConsensusParams()
  62. } else {
  63. if err := genDoc.ConsensusParams.Validate(); err != nil {
  64. return err
  65. }
  66. }
  67. if len(genDoc.Validators) == 0 {
  68. return errors.Errorf("The genesis file must have at least one validator")
  69. }
  70. for _, v := range genDoc.Validators {
  71. if v.Power == 0 {
  72. return errors.Errorf("The genesis file cannot contain validators with no voting power: %v", v)
  73. }
  74. }
  75. if genDoc.GenesisTime.IsZero() {
  76. genDoc.GenesisTime = time.Now()
  77. }
  78. return nil
  79. }
  80. //------------------------------------------------------------
  81. // Make genesis state from file
  82. // GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
  83. func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
  84. genDoc := GenesisDoc{}
  85. err := json.Unmarshal(jsonBlob, &genDoc)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if err := genDoc.ValidateAndComplete(); err != nil {
  90. return nil, err
  91. }
  92. return &genDoc, err
  93. }
  94. // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
  95. func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
  96. jsonBlob, err := ioutil.ReadFile(genDocFile)
  97. if err != nil {
  98. return nil, errors.Wrap(err, "Couldn't read GenesisDoc file")
  99. }
  100. genDoc, err := GenesisDocFromJSON(jsonBlob)
  101. if err != nil {
  102. return nil, errors.Wrap(err, cmn.Fmt("Error reading GenesisDoc at %v", genDocFile))
  103. }
  104. return genDoc, nil
  105. }