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.

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