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.

125 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. AppStateJSON 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. }
  34. return genDoc.AppStateJSON
  35. }
  36. // SaveAs is a utility method for saving GenensisDoc as a JSON file.
  37. func (genDoc *GenesisDoc) SaveAs(file string) error {
  38. genDocBytes, err := json.Marshal(genDoc)
  39. if err != nil {
  40. return err
  41. }
  42. return cmn.WriteFile(file, genDocBytes, 0644)
  43. }
  44. // ValidatorHash returns the hash of the validator set contained in the GenesisDoc
  45. func (genDoc *GenesisDoc) ValidatorHash() []byte {
  46. vals := make([]*Validator, len(genDoc.Validators))
  47. for i, v := range genDoc.Validators {
  48. vals[i] = NewValidator(v.PubKey, v.Power)
  49. }
  50. vset := NewValidatorSet(vals)
  51. return vset.Hash()
  52. }
  53. // ValidateAndComplete checks that all necessary fields are present
  54. // and fills in defaults for optional fields left empty
  55. func (genDoc *GenesisDoc) ValidateAndComplete() error {
  56. if genDoc.ChainID == "" {
  57. return errors.Errorf("Genesis doc must include non-empty chain_id")
  58. }
  59. if genDoc.ConsensusParams == nil {
  60. genDoc.ConsensusParams = DefaultConsensusParams()
  61. } else {
  62. if err := genDoc.ConsensusParams.Validate(); err != nil {
  63. return err
  64. }
  65. }
  66. if len(genDoc.Validators) == 0 {
  67. return errors.Errorf("The genesis file must have at least one validator")
  68. }
  69. for _, v := range genDoc.Validators {
  70. if v.Power == 0 {
  71. return errors.Errorf("The genesis file cannot contain validators with no voting power: %v", v)
  72. }
  73. }
  74. if genDoc.GenesisTime.IsZero() {
  75. genDoc.GenesisTime = time.Now()
  76. }
  77. return nil
  78. }
  79. //------------------------------------------------------------
  80. // Make genesis state from file
  81. // GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
  82. func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
  83. genDoc := GenesisDoc{}
  84. err := json.Unmarshal(jsonBlob, &genDoc)
  85. if err != nil {
  86. return nil, err
  87. }
  88. if err := genDoc.ValidateAndComplete(); err != nil {
  89. return nil, err
  90. }
  91. return &genDoc, err
  92. }
  93. // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
  94. func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
  95. jsonBlob, err := ioutil.ReadFile(genDocFile)
  96. if err != nil {
  97. return nil, errors.Wrap(err, "Couldn't read GenesisDoc file")
  98. }
  99. genDoc, err := GenesisDocFromJSON(jsonBlob)
  100. if err != nil {
  101. return nil, errors.Wrap(err, cmn.Fmt("Error reading GenesisDoc at %v", genDocFile))
  102. }
  103. return genDoc, nil
  104. }