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.

151 lines
4.6 KiB

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