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.

135 lines
3.7 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
10 years ago
10 years ago
10 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "sort"
  5. acm "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) (*acm.Account, *acm.PrivAccount) {
  22. privAccount := acm.GenPrivAccount()
  23. perms := ptypes.DefaultAccountPermissions
  24. acc := &acm.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 RandGenesisDoc(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*GenesisDoc, []*acm.PrivAccount, []*PrivValidator) {
  66. accounts := make([]GenesisAccount, numAccounts)
  67. privAccounts := make([]*acm.PrivAccount, numAccounts)
  68. defaultPerms := ptypes.DefaultAccountPermissions
  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: &defaultPerms, // This will get copied into each state.Account.
  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: []BasicAccount{
  86. {
  87. Address: valInfo.PubKey.Address(),
  88. Amount: valInfo.FirstBondAmount,
  89. },
  90. },
  91. }
  92. privValidators[i] = privVal
  93. }
  94. sort.Sort(PrivValidatorsByAddress(privValidators))
  95. return &GenesisDoc{
  96. GenesisTime: time.Now(),
  97. ChainID: "tendermint_test",
  98. Accounts: accounts,
  99. Validators: validators,
  100. }, privAccounts, privValidators
  101. }
  102. func RandGenesisState(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*State, []*acm.PrivAccount, []*PrivValidator) {
  103. db := dbm.NewMemDB()
  104. genDoc, privAccounts, privValidators := RandGenesisDoc(numAccounts, randBalance, minBalance, numValidators, randBonded, minBonded)
  105. s0 := MakeGenesisState(db, genDoc)
  106. s0.Save()
  107. return s0, privAccounts, privValidators
  108. }
  109. //-------------------------------------
  110. type PrivValidatorsByAddress []*PrivValidator
  111. func (pvs PrivValidatorsByAddress) Len() int {
  112. return len(pvs)
  113. }
  114. func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
  115. return bytes.Compare(pvs[i].Address, pvs[j].Address) == -1
  116. }
  117. func (pvs PrivValidatorsByAddress) Swap(i, j int) {
  118. it := pvs[i]
  119. pvs[i] = pvs[j]
  120. pvs[j] = it
  121. }