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