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.

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