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.0 KiB

  1. package types
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/tendermint/go-crypto"
  8. "github.com/tendermint/go-wire/data"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. )
  11. //------------------------------------------------------------
  12. // core types for a genesis definition
  13. // GenesisValidator is an initial validator.
  14. type GenesisValidator struct {
  15. PubKey crypto.PubKey `json:"pub_key"`
  16. Amount int64 `json:"amount"`
  17. Name string `json:"name"`
  18. }
  19. // GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
  20. type GenesisDoc struct {
  21. GenesisTime time.Time `json:"genesis_time"`
  22. ChainID string `json:"chain_id"`
  23. ConsensusParams ConsensusParams `json:"consensus_params"`
  24. Validators []GenesisValidator `json:"validators"`
  25. AppHash data.Bytes `json:"app_hash"`
  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 := json.Marshal(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.Amount)
  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 errors.Errorf("Genesis doc must include non-empty chain_id")
  49. }
  50. var emptyParams ConsensusParams
  51. if genDoc.ConsensusParams == emptyParams {
  52. genDoc.ConsensusParams = DefaultConsensusParams()
  53. } else {
  54. if err := genDoc.ConsensusParams.Validate(); err != nil {
  55. return err
  56. }
  57. }
  58. if len(genDoc.Validators) == 0 {
  59. return errors.Errorf("The genesis file must have at least one validator")
  60. }
  61. if genDoc.GenesisTime.IsZero() {
  62. genDoc.GenesisTime = time.Now()
  63. }
  64. return nil
  65. }
  66. //------------------------------------------------------------
  67. // Make genesis state from file
  68. // GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
  69. func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
  70. genDoc := GenesisDoc{}
  71. err := json.Unmarshal(jsonBlob, &genDoc)
  72. if err != nil {
  73. return nil, err
  74. }
  75. if err := genDoc.ValidateAndComplete(); err != nil {
  76. return nil, err
  77. }
  78. return &genDoc, err
  79. }
  80. // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
  81. func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
  82. jsonBlob, err := ioutil.ReadFile(genDocFile)
  83. if err != nil {
  84. return nil, errors.Wrap(err, "Couldn't read GenesisDoc file")
  85. }
  86. genDoc, err := GenesisDocFromJSON(jsonBlob)
  87. if err != nil {
  88. return nil, errors.Wrap(err, cmn.Fmt("Error reading GenesisDoc at %v", genDocFile))
  89. }
  90. return genDoc, nil
  91. }