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.

131 lines
4.0 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/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. 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 tmos.WriteFile(file, genDocBytes, 0644)
  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.ConsensusParams == nil {
  68. genDoc.ConsensusParams = DefaultConsensusParams()
  69. } else if err := ValidateConsensusParams(*genDoc.ConsensusParams); err != nil {
  70. return err
  71. }
  72. for i, v := range genDoc.Validators {
  73. if v.Power == 0 {
  74. return fmt.Errorf("the genesis file cannot contain validators with no voting power: %v", v)
  75. }
  76. if len(v.Address) > 0 && !bytes.Equal(v.PubKey.Address(), v.Address) {
  77. return fmt.Errorf("incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address())
  78. }
  79. if len(v.Address) == 0 {
  80. genDoc.Validators[i].Address = v.PubKey.Address()
  81. }
  82. }
  83. if genDoc.GenesisTime.IsZero() {
  84. genDoc.GenesisTime = tmtime.Now()
  85. }
  86. return nil
  87. }
  88. //------------------------------------------------------------
  89. // Make genesis state from file
  90. // GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
  91. func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
  92. genDoc := GenesisDoc{}
  93. err := tmjson.Unmarshal(jsonBlob, &genDoc)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if err := genDoc.ValidateAndComplete(); err != nil {
  98. return nil, err
  99. }
  100. return &genDoc, err
  101. }
  102. // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
  103. func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
  104. jsonBlob, err := ioutil.ReadFile(genDocFile)
  105. if err != nil {
  106. return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
  107. }
  108. genDoc, err := GenesisDocFromJSON(jsonBlob)
  109. if err != nil {
  110. return nil, fmt.Errorf("error reading GenesisDoc at %s: %w", genDocFile, err)
  111. }
  112. return genDoc, nil
  113. }