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.

121 lines
3.6 KiB

  1. package types
  2. import (
  3. "sort"
  4. "time"
  5. acm "github.com/tendermint/tendermint/account"
  6. . "github.com/tendermint/tendermint/common"
  7. ptypes "github.com/tendermint/tendermint/permission/types"
  8. "github.com/tendermint/tendermint/types"
  9. "github.com/tendermint/tendermint/wire"
  10. )
  11. //------------------------------------------------------------
  12. // we store the gendoc in the db
  13. var GenDocKey = []byte("GenDocKey")
  14. //------------------------------------------------------------
  15. // core types for a genesis definition
  16. type BasicAccount struct {
  17. Address []byte `json:"address"`
  18. Amount int64 `json:"amount"`
  19. }
  20. type GenesisAccount struct {
  21. Address []byte `json:"address"`
  22. Amount int64 `json:"amount"`
  23. Name string `json:"name"`
  24. Permissions *ptypes.AccountPermissions `json:"permissions"`
  25. }
  26. type GenesisValidator struct {
  27. PubKey acm.PubKeyEd25519 `json:"pub_key"`
  28. Amount int64 `json:"amount"`
  29. Name string `json:"name"`
  30. UnbondTo []BasicAccount `json:"unbond_to"`
  31. }
  32. type GenesisParams struct {
  33. GlobalPermissions *ptypes.AccountPermissions `json:"global_permissions"`
  34. }
  35. type GenesisDoc struct {
  36. GenesisTime time.Time `json:"genesis_time"`
  37. ChainID string `json:"chain_id"`
  38. Params *GenesisParams `json:"params"`
  39. Accounts []GenesisAccount `json:"accounts"`
  40. Validators []GenesisValidator `json:"validators"`
  41. }
  42. //------------------------------------------------------------
  43. // Make genesis state from file
  44. func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
  45. var err error
  46. wire.ReadJSONPtr(&genState, jsonBlob, &err)
  47. if err != nil {
  48. Exit(Fmt("Couldn't read GenesisDoc: %v", err))
  49. }
  50. return
  51. }
  52. //------------------------------------------------------------
  53. // Make random genesis state
  54. func RandAccount(randBalance bool, minBalance int64) (*acm.Account, *acm.PrivAccount) {
  55. privAccount := acm.GenPrivAccount()
  56. perms := ptypes.DefaultAccountPermissions
  57. acc := &acm.Account{
  58. Address: privAccount.PubKey.Address(),
  59. PubKey: privAccount.PubKey,
  60. Sequence: RandInt(),
  61. Balance: minBalance,
  62. Permissions: perms,
  63. }
  64. if randBalance {
  65. acc.Balance += int64(RandUint32())
  66. }
  67. return acc, privAccount
  68. }
  69. func RandGenesisDoc(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*GenesisDoc, []*acm.PrivAccount, []*types.PrivValidator) {
  70. accounts := make([]GenesisAccount, numAccounts)
  71. privAccounts := make([]*acm.PrivAccount, numAccounts)
  72. defaultPerms := ptypes.DefaultAccountPermissions
  73. for i := 0; i < numAccounts; i++ {
  74. account, privAccount := RandAccount(randBalance, minBalance)
  75. accounts[i] = GenesisAccount{
  76. Address: account.Address,
  77. Amount: account.Balance,
  78. Permissions: &defaultPerms, // This will get copied into each state.Account.
  79. }
  80. privAccounts[i] = privAccount
  81. }
  82. validators := make([]GenesisValidator, numValidators)
  83. privValidators := make([]*types.PrivValidator, numValidators)
  84. for i := 0; i < numValidators; i++ {
  85. valInfo, _, privVal := types.RandValidator(randBonded, minBonded)
  86. validators[i] = GenesisValidator{
  87. PubKey: valInfo.PubKey,
  88. Amount: valInfo.FirstBondAmount,
  89. UnbondTo: []BasicAccount{
  90. {
  91. Address: valInfo.PubKey.Address(),
  92. Amount: valInfo.FirstBondAmount,
  93. },
  94. },
  95. }
  96. privValidators[i] = privVal
  97. }
  98. sort.Sort(types.PrivValidatorsByAddress(privValidators))
  99. return &GenesisDoc{
  100. GenesisTime: time.Now(),
  101. ChainID: "tendermint_test",
  102. Accounts: accounts,
  103. Validators: validators,
  104. }, privAccounts, privValidators
  105. }