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.

113 lines
2.8 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
  1. package state
  2. import (
  3. "bytes"
  4. "sort"
  5. . "github.com/tendermint/tendermint/account"
  6. . "github.com/tendermint/tendermint/block"
  7. . "github.com/tendermint/tendermint/common"
  8. db_ "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, *PrivAccount) {
  21. privAccount := GenPrivAccount()
  22. account := &Account{
  23. Address: privAccount.PubKey.Address(),
  24. PubKey: privAccount.PubKey,
  25. Sequence: RandUint(),
  26. Balance: minBalance,
  27. }
  28. if randBalance {
  29. account.Balance += uint64(RandUint32())
  30. }
  31. return account, privAccount
  32. }
  33. func RandValidator(randBonded bool, minBonded uint64) (*ValidatorInfo, *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: []*TxOutput{&TxOutput{
  44. Amount: bonded,
  45. Address: privVal.Address,
  46. }},
  47. FirstBondHeight: 0,
  48. FirstBondAmount: bonded,
  49. }
  50. return valInfo, privVal
  51. }
  52. func RandGenesisState(numAccounts int, randBalance bool, minBalance uint64, numValidators int, randBonded bool, minBonded uint64) (*State, []*PrivAccount, []*PrivValidator) {
  53. db := db_.NewMemDB()
  54. accounts := make([]GenesisAccount, numAccounts)
  55. privAccounts := make([]*PrivAccount, numAccounts)
  56. for i := 0; i < numAccounts; i++ {
  57. account, privAccount := RandAccount(randBalance, minBalance)
  58. accounts[i] = GenesisAccount{
  59. Address: account.Address,
  60. Amount: account.Balance,
  61. }
  62. privAccounts[i] = privAccount
  63. }
  64. validators := make([]GenesisValidator, numValidators)
  65. privValidators := make([]*PrivValidator, numValidators)
  66. for i := 0; i < numValidators; i++ {
  67. valInfo, privVal := RandValidator(randBonded, minBonded)
  68. validators[i] = GenesisValidator{
  69. PubKey: valInfo.PubKey,
  70. Amount: valInfo.FirstBondAmount,
  71. UnbondTo: []GenesisAccount{
  72. {
  73. Address: valInfo.PubKey.Address(),
  74. Amount: valInfo.FirstBondAmount,
  75. },
  76. },
  77. }
  78. privValidators[i] = privVal
  79. }
  80. sort.Sort(PrivValidatorsByAddress(privValidators))
  81. s0 := MakeGenesisState(db, &GenesisDoc{
  82. GenesisTime: time.Now(),
  83. Accounts: accounts,
  84. Validators: validators,
  85. })
  86. s0.Save()
  87. return s0, privAccounts, privValidators
  88. }
  89. //-------------------------------------
  90. type PrivValidatorsByAddress []*PrivValidator
  91. func (pvs PrivValidatorsByAddress) Len() int {
  92. return len(pvs)
  93. }
  94. func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
  95. return bytes.Compare(pvs[i].Address, pvs[j].Address) == -1
  96. }
  97. func (pvs PrivValidatorsByAddress) Swap(i, j int) {
  98. it := pvs[i]
  99. pvs[i] = pvs[j]
  100. pvs[j] = it
  101. }