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
3.9 KiB

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