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.

127 lines
3.8 KiB

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