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.

123 lines
3.5 KiB

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