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.

67 lines
2.0 KiB

10 years ago
  1. package core
  2. import (
  3. "fmt"
  4. acm "github.com/tendermint/tendermint/account"
  5. . "github.com/tendermint/tendermint/common"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. )
  8. func GenPrivAccount() (*ctypes.ResultGenPrivAccount, error) {
  9. return &ctypes.ResultGenPrivAccount{acm.GenPrivAccount()}, nil
  10. }
  11. // If the account is not known, returns nil, nil.
  12. func GetAccount(address []byte) (*ctypes.ResultGetAccount, error) {
  13. cache := mempoolReactor.Mempool.GetCache()
  14. account := cache.GetAccount(address)
  15. if account == nil {
  16. return nil, nil
  17. }
  18. return &ctypes.ResultGetAccount{account}, nil
  19. }
  20. func GetStorage(address, key []byte) (*ctypes.ResultGetStorage, error) {
  21. state := consensusState.GetState()
  22. account := state.GetAccount(address)
  23. if account == nil {
  24. return nil, fmt.Errorf("UnknownAddress: %X", address)
  25. }
  26. storageRoot := account.StorageRoot
  27. storageTree := state.LoadStorage(storageRoot)
  28. _, value := storageTree.Get(LeftPadWord256(key).Bytes())
  29. if value == nil {
  30. return &ctypes.ResultGetStorage{key, nil}, nil
  31. }
  32. return &ctypes.ResultGetStorage{key, value.([]byte)}, nil
  33. }
  34. func ListAccounts() (*ctypes.ResultListAccounts, error) {
  35. var blockHeight int
  36. var accounts []*acm.Account
  37. state := consensusState.GetState()
  38. blockHeight = state.LastBlockHeight
  39. state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
  40. accounts = append(accounts, value.(*acm.Account))
  41. return false
  42. })
  43. return &ctypes.ResultListAccounts{blockHeight, accounts}, nil
  44. }
  45. func DumpStorage(address []byte) (*ctypes.ResultDumpStorage, error) {
  46. state := consensusState.GetState()
  47. account := state.GetAccount(address)
  48. if account == nil {
  49. return nil, fmt.Errorf("UnknownAddress: %X", address)
  50. }
  51. storageRoot := account.StorageRoot
  52. storageTree := state.LoadStorage(storageRoot)
  53. storageItems := []ctypes.StorageItem{}
  54. storageTree.Iterate(func(key interface{}, value interface{}) bool {
  55. storageItems = append(storageItems, ctypes.StorageItem{
  56. key.([]byte), value.([]byte)})
  57. return false
  58. })
  59. return &ctypes.ResultDumpStorage{storageRoot, storageItems}, nil
  60. }