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.

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