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.

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