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.

131 lines
3.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "sort"
  5. "github.com/tendermint/tendermint/account"
  6. . "github.com/tendermint/tendermint/common"
  7. dbm "github.com/tendermint/tendermint/db"
  8. ptypes "github.com/tendermint/tendermint/permission/types"
  9. "github.com/tendermint/tendermint/types"
  10. "io/ioutil"
  11. "os"
  12. "time"
  13. )
  14. func Tempfile(prefix string) (*os.File, string) {
  15. file, err := ioutil.TempFile("", prefix)
  16. if err != nil {
  17. panic(err)
  18. }
  19. return file, file.Name()
  20. }
  21. func RandAccount(randBalance bool, minBalance int64) (*account.Account, *account.PrivAccount) {
  22. privAccount := account.GenPrivAccount()
  23. perms := ptypes.NewDefaultAccountPermissions()
  24. acc := &account.Account{
  25. Address: privAccount.PubKey.Address(),
  26. PubKey: privAccount.PubKey,
  27. Sequence: RandInt(),
  28. Balance: minBalance,
  29. Permissions: perms,
  30. }
  31. if randBalance {
  32. acc.Balance += int64(RandUint32())
  33. }
  34. return acc, privAccount
  35. }
  36. func RandValidator(randBonded bool, minBonded int64) (*ValidatorInfo, *Validator, *PrivValidator) {
  37. privVal := GenPrivValidator()
  38. _, tempFilePath := Tempfile("priv_validator_")
  39. privVal.SetFile(tempFilePath)
  40. bonded := minBonded
  41. if randBonded {
  42. bonded += int64(RandUint32())
  43. }
  44. valInfo := &ValidatorInfo{
  45. Address: privVal.Address,
  46. PubKey: privVal.PubKey,
  47. UnbondTo: []*types.TxOutput{&types.TxOutput{
  48. Amount: bonded,
  49. Address: privVal.Address,
  50. }},
  51. FirstBondHeight: 0,
  52. FirstBondAmount: bonded,
  53. }
  54. val := &Validator{
  55. Address: valInfo.Address,
  56. PubKey: valInfo.PubKey,
  57. BondHeight: 0,
  58. UnbondHeight: 0,
  59. LastCommitHeight: 0,
  60. VotingPower: valInfo.FirstBondAmount,
  61. Accum: 0,
  62. }
  63. return valInfo, val, privVal
  64. }
  65. func RandGenesisState(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*State, []*account.PrivAccount, []*PrivValidator) {
  66. db := dbm.NewMemDB()
  67. accounts := make([]GenesisAccount, numAccounts)
  68. privAccounts := make([]*account.PrivAccount, numAccounts)
  69. for i := 0; i < numAccounts; i++ {
  70. account, privAccount := RandAccount(randBalance, minBalance)
  71. accounts[i] = GenesisAccount{
  72. Address: account.Address,
  73. Amount: account.Balance,
  74. Permissions: ptypes.NewDefaultAccountPermissions(),
  75. }
  76. privAccounts[i] = privAccount
  77. }
  78. validators := make([]GenesisValidator, numValidators)
  79. privValidators := make([]*PrivValidator, numValidators)
  80. for i := 0; i < numValidators; i++ {
  81. valInfo, _, privVal := RandValidator(randBonded, minBonded)
  82. validators[i] = GenesisValidator{
  83. PubKey: valInfo.PubKey,
  84. Amount: valInfo.FirstBondAmount,
  85. UnbondTo: []GenesisAccount{
  86. {
  87. Address: valInfo.PubKey.Address(),
  88. Amount: valInfo.FirstBondAmount,
  89. },
  90. },
  91. }
  92. privValidators[i] = privVal
  93. }
  94. sort.Sort(PrivValidatorsByAddress(privValidators))
  95. s0 := MakeGenesisState(db, &GenesisDoc{
  96. GenesisTime: time.Now(),
  97. ChainID: "tendermint_test",
  98. Accounts: accounts,
  99. Validators: validators,
  100. Params: &GenesisParams{
  101. GlobalPermissions: ptypes.NewDefaultAccountPermissions(),
  102. },
  103. })
  104. s0.Save()
  105. return s0, privAccounts, privValidators
  106. }
  107. //-------------------------------------
  108. type PrivValidatorsByAddress []*PrivValidator
  109. func (pvs PrivValidatorsByAddress) Len() int {
  110. return len(pvs)
  111. }
  112. func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
  113. return bytes.Compare(pvs[i].Address, pvs[j].Address) == -1
  114. }
  115. func (pvs PrivValidatorsByAddress) Swap(i, j int) {
  116. it := pvs[i]
  117. pvs[i] = pvs[j]
  118. pvs[j] = it
  119. }