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.

65 lines
1.7 KiB

  1. package types
  2. import (
  3. "sort"
  4. "time"
  5. . "github.com/tendermint/go-common"
  6. "github.com/tendermint/go-crypto"
  7. "github.com/tendermint/go-wire"
  8. )
  9. //------------------------------------------------------------
  10. // we store the gendoc in the db
  11. var GenDocKey = []byte("GenDocKey")
  12. //------------------------------------------------------------
  13. // core types for a genesis definition
  14. type GenesisValidator struct {
  15. PubKey crypto.PubKeyEd25519 `json:"pub_key"`
  16. Amount int64 `json:"amount"`
  17. Name string `json:"name"`
  18. }
  19. type GenesisDoc struct {
  20. GenesisTime time.Time `json:"genesis_time"`
  21. ChainID string `json:"chain_id"`
  22. Validators []GenesisValidator `json:"validators"`
  23. }
  24. //------------------------------------------------------------
  25. // Make genesis state from file
  26. func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
  27. var err error
  28. wire.ReadJSONPtr(&genState, jsonBlob, &err)
  29. if err != nil {
  30. Exit(Fmt("Couldn't read GenesisDoc: %v", err))
  31. }
  32. return
  33. }
  34. //------------------------------------------------------------
  35. // Make random genesis state
  36. func RandGenesisDoc(numValidators int, randPower bool, minPower int64) (*GenesisDoc, []*PrivValidator) {
  37. validators := make([]GenesisValidator, numValidators)
  38. privValidators := make([]*PrivValidator, numValidators)
  39. for i := 0; i < numValidators; i++ {
  40. val, privVal := RandValidator(randPower, minPower)
  41. validators[i] = GenesisValidator{
  42. PubKey: val.PubKey,
  43. Amount: val.VotingPower,
  44. }
  45. privValidators[i] = privVal
  46. }
  47. sort.Sort(PrivValidatorsByAddress(privValidators))
  48. return &GenesisDoc{
  49. GenesisTime: time.Now(),
  50. ChainID: "tendermint_test",
  51. Validators: validators,
  52. }, privValidators
  53. }