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.

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