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.

76 lines
2.5 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
  1. package state
  2. import (
  3. "sort"
  4. "time"
  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/state/types"
  10. "github.com/tendermint/tendermint/types"
  11. )
  12. func RandAccount(randBalance bool, minBalance int64) (*acm.Account, *acm.PrivAccount) {
  13. privAccount := acm.GenPrivAccount()
  14. perms := ptypes.DefaultAccountPermissions
  15. acc := &acm.Account{
  16. Address: privAccount.PubKey.Address(),
  17. PubKey: privAccount.PubKey,
  18. Sequence: RandInt(),
  19. Balance: minBalance,
  20. Permissions: perms,
  21. }
  22. if randBalance {
  23. acc.Balance += int64(RandUint32())
  24. }
  25. return acc, privAccount
  26. }
  27. func RandGenesisDoc(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*GenesisDoc, []*acm.PrivAccount, []*types.PrivValidator) {
  28. accounts := make([]GenesisAccount, numAccounts)
  29. privAccounts := make([]*acm.PrivAccount, numAccounts)
  30. defaultPerms := ptypes.DefaultAccountPermissions
  31. for i := 0; i < numAccounts; i++ {
  32. account, privAccount := RandAccount(randBalance, minBalance)
  33. accounts[i] = GenesisAccount{
  34. Address: account.Address,
  35. Amount: account.Balance,
  36. Permissions: &defaultPerms, // This will get copied into each state.Account.
  37. }
  38. privAccounts[i] = privAccount
  39. }
  40. validators := make([]GenesisValidator, numValidators)
  41. privValidators := make([]*types.PrivValidator, numValidators)
  42. for i := 0; i < numValidators; i++ {
  43. valInfo, _, privVal := types.RandValidator(randBonded, minBonded)
  44. validators[i] = GenesisValidator{
  45. PubKey: valInfo.PubKey,
  46. Amount: valInfo.FirstBondAmount,
  47. UnbondTo: []BasicAccount{
  48. {
  49. Address: valInfo.PubKey.Address(),
  50. Amount: valInfo.FirstBondAmount,
  51. },
  52. },
  53. }
  54. privValidators[i] = privVal
  55. }
  56. sort.Sort(types.PrivValidatorsByAddress(privValidators))
  57. return &GenesisDoc{
  58. GenesisTime: time.Now(),
  59. ChainID: "tendermint_test",
  60. Accounts: accounts,
  61. Validators: validators,
  62. }, privAccounts, privValidators
  63. }
  64. func RandGenesisState(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*State, []*acm.PrivAccount, []*types.PrivValidator) {
  65. db := dbm.NewMemDB()
  66. genDoc, privAccounts, privValidators := RandGenesisDoc(numAccounts, randBalance, minBalance, numValidators, randBonded, minBonded)
  67. s0 := MakeGenesisState(db, genDoc)
  68. s0.Save()
  69. return s0, privAccounts, privValidators
  70. }