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.

129 lines
3.8 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "time"
  8. "github.com/pkg/errors"
  9. "github.com/tendermint/tendermint/crypto"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  11. tmtime "github.com/tendermint/tendermint/types/time"
  12. )
  13. const (
  14. // MaxChainIDLen is a maximum length of the chain ID.
  15. MaxChainIDLen = 50
  16. )
  17. //------------------------------------------------------------
  18. // core types for a genesis definition
  19. // NOTE: any changes to the genesis definition should
  20. // be reflected in the documentation:
  21. // docs/tendermint-core/using-tendermint.md
  22. // GenesisValidator is an initial validator.
  23. type GenesisValidator struct {
  24. Address Address `json:"address"`
  25. PubKey crypto.PubKey `json:"pub_key"`
  26. Power int64 `json:"power"`
  27. Name string `json:"name"`
  28. }
  29. // GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
  30. type GenesisDoc struct {
  31. GenesisTime time.Time `json:"genesis_time"`
  32. ChainID string `json:"chain_id"`
  33. ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
  34. Validators []GenesisValidator `json:"validators,omitempty"`
  35. AppHash cmn.HexBytes `json:"app_hash"`
  36. AppState json.RawMessage `json:"app_state,omitempty"`
  37. }
  38. // SaveAs is a utility method for saving GenensisDoc as a JSON file.
  39. func (genDoc *GenesisDoc) SaveAs(file string) error {
  40. genDocBytes, err := cdc.MarshalJSONIndent(genDoc, "", " ")
  41. if err != nil {
  42. return err
  43. }
  44. return cmn.WriteFile(file, genDocBytes, 0644)
  45. }
  46. // ValidatorHash returns the hash of the validator set contained in the GenesisDoc
  47. func (genDoc *GenesisDoc) ValidatorHash() []byte {
  48. vals := make([]*Validator, len(genDoc.Validators))
  49. for i, v := range genDoc.Validators {
  50. vals[i] = NewValidator(v.PubKey, v.Power)
  51. }
  52. vset := NewValidatorSet(vals)
  53. return vset.Hash()
  54. }
  55. // ValidateAndComplete checks that all necessary fields are present
  56. // and fills in defaults for optional fields left empty
  57. func (genDoc *GenesisDoc) ValidateAndComplete() error {
  58. if genDoc.ChainID == "" {
  59. return errors.New("Genesis doc must include non-empty chain_id")
  60. }
  61. if len(genDoc.ChainID) > MaxChainIDLen {
  62. return errors.Errorf("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen)
  63. }
  64. if genDoc.ConsensusParams == nil {
  65. genDoc.ConsensusParams = DefaultConsensusParams()
  66. } else if err := genDoc.ConsensusParams.Validate(); err != nil {
  67. return err
  68. }
  69. for i, 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. if len(v.Address) > 0 && !bytes.Equal(v.PubKey.Address(), v.Address) {
  74. return errors.Errorf("Incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address())
  75. }
  76. if len(v.Address) == 0 {
  77. genDoc.Validators[i].Address = v.PubKey.Address()
  78. }
  79. }
  80. if genDoc.GenesisTime.IsZero() {
  81. genDoc.GenesisTime = tmtime.Now()
  82. }
  83. return nil
  84. }
  85. //------------------------------------------------------------
  86. // Make genesis state from file
  87. // GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
  88. func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
  89. genDoc := GenesisDoc{}
  90. err := cdc.UnmarshalJSON(jsonBlob, &genDoc)
  91. if err != nil {
  92. return nil, err
  93. }
  94. if err := genDoc.ValidateAndComplete(); err != nil {
  95. return nil, err
  96. }
  97. return &genDoc, err
  98. }
  99. // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
  100. func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
  101. jsonBlob, err := ioutil.ReadFile(genDocFile)
  102. if err != nil {
  103. return nil, errors.Wrap(err, "Couldn't read GenesisDoc file")
  104. }
  105. genDoc, err := GenesisDocFromJSON(jsonBlob)
  106. if err != nil {
  107. return nil, errors.Wrap(err, fmt.Sprintf("Error reading GenesisDoc at %v", genDocFile))
  108. }
  109. return genDoc, nil
  110. }