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.

137 lines
4.1 KiB

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