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.

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