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.

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