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.

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