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.3 KiB

10 years ago
10 years ago
10 years ago
  1. package core
  2. import (
  3. "fmt"
  4. acm "github.com/tendermint/tendermint/account"
  5. . "github.com/tendermint/tendermint/common"
  6. ptypes "github.com/tendermint/tendermint/permission/types"
  7. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  8. )
  9. func GenPrivAccount() (*acm.PrivAccount, error) {
  10. return acm.GenPrivAccount(), nil
  11. }
  12. func GetAccount(address []byte) (*acm.Account, error) {
  13. cache := mempoolReactor.Mempool.GetCache()
  14. account := cache.GetAccount(address)
  15. if account == nil {
  16. // XXX: shouldn't we return "account not found"?
  17. account = &acm.Account{
  18. Address: address,
  19. PubKey: nil,
  20. Sequence: 0,
  21. Balance: 0,
  22. Code: nil,
  23. StorageRoot: nil,
  24. Permissions: cache.GetAccount(ptypes.GlobalPermissionsAddress).Permissions,
  25. }
  26. }
  27. return account, nil
  28. }
  29. func GetStorage(address, key []byte) (*ctypes.ResponseGetStorage, error) {
  30. state := consensusState.GetState()
  31. account := state.GetAccount(address)
  32. if account == nil {
  33. return nil, fmt.Errorf("Unknown address: %X", address)
  34. }
  35. storageRoot := account.StorageRoot
  36. storageTree := state.LoadStorage(storageRoot)
  37. _, value := storageTree.Get(LeftPadWord256(key).Bytes())
  38. if value == nil {
  39. return &ctypes.ResponseGetStorage{key, nil}, nil
  40. }
  41. return &ctypes.ResponseGetStorage{key, value.([]byte)}, nil
  42. }
  43. func ListAccounts() (*ctypes.ResponseListAccounts, error) {
  44. var blockHeight int
  45. var accounts []*acm.Account
  46. state := consensusState.GetState()
  47. blockHeight = state.LastBlockHeight
  48. state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
  49. accounts = append(accounts, value.(*acm.Account))
  50. return false
  51. })
  52. return &ctypes.ResponseListAccounts{blockHeight, accounts}, nil
  53. }
  54. func DumpStorage(address []byte) (*ctypes.ResponseDumpStorage, error) {
  55. state := consensusState.GetState()
  56. account := state.GetAccount(address)
  57. if account == nil {
  58. return nil, fmt.Errorf("Unknown address: %X", address)
  59. }
  60. storageRoot := account.StorageRoot
  61. storageTree := state.LoadStorage(storageRoot)
  62. storageItems := []ctypes.StorageItem{}
  63. storageTree.Iterate(func(key interface{}, value interface{}) bool {
  64. storageItems = append(storageItems, ctypes.StorageItem{
  65. key.([]byte), value.([]byte)})
  66. return false
  67. })
  68. return &ctypes.ResponseDumpStorage{storageRoot, storageItems}, nil
  69. }