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.

110 lines
3.1 KiB

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