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.

69 lines
2.1 KiB

10 years ago
  1. package core
  2. import (
  3. "fmt"
  4. "github.com/tendermint/tendermint2/account"
  5. . "github.com/tendermint/tendermint2/common"
  6. )
  7. func GenPrivAccount() (*ResponseGenPrivAccount, error) {
  8. return &ResponseGenPrivAccount{account.GenPrivAccount()}, nil
  9. }
  10. func GetAccount(addr []byte) (*ResponseGetAccount, error) {
  11. cache := mempoolReactor.Mempool.GetCache()
  12. return &ResponseGetAccount{cache.GetAccount(addr)}, nil
  13. }
  14. func GetStorage(address, slot []byte) (*ResponseGetStorage, error) {
  15. state := consensusState.GetState()
  16. account := state.GetAccount(address)
  17. if account == nil {
  18. return nil, fmt.Errorf("Unknown address: %X", address)
  19. }
  20. storageRoot := account.StorageRoot
  21. storage := state.LoadStorage(storageRoot)
  22. _, value := storage.Get(RightPadWord256(slot).Bytes())
  23. if value == nil {
  24. return &ResponseGetStorage{slot, nil}, nil
  25. }
  26. return &ResponseGetStorage{slot, value.([]byte)}, nil
  27. }
  28. func ListAccounts() (*ResponseListAccounts, error) {
  29. var blockHeight uint
  30. var accounts []*account.Account
  31. state := consensusState.GetState()
  32. blockHeight = state.LastBlockHeight
  33. state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
  34. accounts = append(accounts, value.(*account.Account))
  35. return false
  36. })
  37. return &ResponseListAccounts{blockHeight, accounts}, nil
  38. }
  39. func GetStorage(address, storage []byte) (*ResponseGetStorage, error) {
  40. cache := mempoolReactor.Mempool.GetCache()
  41. addr, slot := RightPadWord256(address), RightPadWord256(storage)
  42. value := cache.GetStorage(addr, slot)
  43. fmt.Printf("STORAGE: %x, %x, %x\n", addr, slot, value)
  44. return &ResponseGetStorage{storage, value.Bytes()}, nil
  45. }
  46. func DumpStorage(addr []byte) (*ResponseDumpStorage, error) {
  47. state := consensusState.GetState()
  48. account := state.GetAccount(addr)
  49. if account == nil {
  50. return nil, fmt.Errorf("Unknown address: %X", addr)
  51. }
  52. storageRoot := account.StorageRoot
  53. storage := state.LoadStorage(storageRoot)
  54. storageItems := []StorageItem{}
  55. storage.Iterate(func(key interface{}, value interface{}) bool {
  56. storageItems = append(storageItems, StorageItem{
  57. key.([]byte), value.([]byte)})
  58. return false
  59. })
  60. return &ResponseDumpStorage{storageRoot, storageItems}, nil
  61. }