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.

73 lines
2.2 KiB

9 years ago
9 years ago
9 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.ResponseGenPrivAccount, error) {
  9. return &ctypes.ResponseGenPrivAccount{acm.GenPrivAccount()}, nil
  10. }
  11. func GetAccount(address []byte) (*ctypes.ResponseGetAccount, error) {
  12. cache := mempoolReactor.Mempool.GetCache()
  13. account := cache.GetAccount(address)
  14. if account == nil {
  15. account = &acm.Account{
  16. Address: address,
  17. PubKey: nil,
  18. Sequence: 0,
  19. Balance: 0,
  20. Code: nil,
  21. StorageRoot: nil,
  22. }
  23. }
  24. return &ctypes.ResponseGetAccount{account}, nil
  25. }
  26. func GetStorage(address, key []byte) (*ctypes.ResponseGetStorage, error) {
  27. state := consensusState.GetState()
  28. account := state.GetAccount(address)
  29. if account == nil {
  30. return nil, fmt.Errorf("Unknown address: %X", address)
  31. }
  32. storageRoot := account.StorageRoot
  33. storageTree := state.LoadStorage(storageRoot)
  34. _, value := storageTree.Get(LeftPadWord256(key).Bytes())
  35. if value == nil {
  36. return &ctypes.ResponseGetStorage{key, nil}, nil
  37. }
  38. return &ctypes.ResponseGetStorage{key, value.([]byte)}, nil
  39. }
  40. func ListAccounts() (*ctypes.ResponseListAccounts, error) {
  41. var blockHeight uint
  42. var accounts []*acm.Account
  43. state := consensusState.GetState()
  44. blockHeight = state.LastBlockHeight
  45. state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
  46. accounts = append(accounts, value.(*acm.Account))
  47. return false
  48. })
  49. return &ctypes.ResponseListAccounts{blockHeight, accounts}, nil
  50. }
  51. func DumpStorage(address []byte) (*ctypes.ResponseDumpStorage, error) {
  52. state := consensusState.GetState()
  53. account := state.GetAccount(address)
  54. if account == nil {
  55. return nil, fmt.Errorf("Unknown address: %X", address)
  56. }
  57. storageRoot := account.StorageRoot
  58. storageTree := state.LoadStorage(storageRoot)
  59. storageItems := []ctypes.StorageItem{}
  60. storageTree.Iterate(func(key interface{}, value interface{}) bool {
  61. storageItems = append(storageItems, ctypes.StorageItem{
  62. key.([]byte), value.([]byte)})
  63. return false
  64. })
  65. return &ctypes.ResponseDumpStorage{storageRoot, storageItems}, nil
  66. }