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.

113 lines
3.2 KiB

  1. package types
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "time"
  6. "github.com/tendermint/tendermint/crypto"
  7. cmn "github.com/tendermint/tendermint/libs/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. AppState json.RawMessage `json:"app_state,omitempty"`
  25. }
  26. // SaveAs is a utility method for saving GenensisDoc as a JSON file.
  27. func (genDoc *GenesisDoc) SaveAs(file string) error {
  28. genDocBytes, err := cdc.MarshalJSONIndent(genDoc, "", " ")
  29. if err != nil {
  30. return err
  31. }
  32. return cmn.WriteFile(file, genDocBytes, 0644)
  33. }
  34. // ValidatorHash returns the hash of the validator set contained in the GenesisDoc
  35. func (genDoc *GenesisDoc) ValidatorHash() []byte {
  36. vals := make([]*Validator, len(genDoc.Validators))
  37. for i, v := range genDoc.Validators {
  38. vals[i] = NewValidator(v.PubKey, v.Power)
  39. }
  40. vset := NewValidatorSet(vals)
  41. return vset.Hash()
  42. }
  43. // ValidateAndComplete checks that all necessary fields are present
  44. // and fills in defaults for optional fields left empty
  45. func (genDoc *GenesisDoc) ValidateAndComplete() error {
  46. if genDoc.ChainID == "" {
  47. return cmn.NewError("Genesis doc must include non-empty chain_id")
  48. }
  49. if genDoc.ConsensusParams == nil {
  50. genDoc.ConsensusParams = DefaultConsensusParams()
  51. } else {
  52. if err := genDoc.ConsensusParams.Validate(); err != nil {
  53. return err
  54. }
  55. }
  56. if len(genDoc.Validators) == 0 {
  57. return cmn.NewError("The genesis file must have at least one validator")
  58. }
  59. for _, v := range genDoc.Validators {
  60. if v.Power == 0 {
  61. return cmn.NewError("The genesis file cannot contain validators with no voting power: %v", v)
  62. }
  63. }
  64. if genDoc.GenesisTime.IsZero() {
  65. genDoc.GenesisTime = time.Now()
  66. }
  67. return nil
  68. }
  69. //------------------------------------------------------------
  70. // Make genesis state from file
  71. // GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
  72. func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
  73. genDoc := GenesisDoc{}
  74. err := cdc.UnmarshalJSON(jsonBlob, &genDoc)
  75. if err != nil {
  76. return nil, err
  77. }
  78. if err := genDoc.ValidateAndComplete(); err != nil {
  79. return nil, err
  80. }
  81. return &genDoc, err
  82. }
  83. // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
  84. func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
  85. jsonBlob, err := ioutil.ReadFile(genDocFile)
  86. if err != nil {
  87. return nil, cmn.ErrorWrap(err, "Couldn't read GenesisDoc file")
  88. }
  89. genDoc, err := GenesisDocFromJSON(jsonBlob)
  90. if err != nil {
  91. return nil, cmn.ErrorWrap(err, cmn.Fmt("Error reading GenesisDoc at %v", genDocFile))
  92. }
  93. return genDoc, nil
  94. }