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.

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