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.

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