json.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package rpc
  15. import (
  16. "encoding/json"
  17. "io"
  18. "net/http"
  19. )
  20. type jsonWrapper struct{}
  21. func (self jsonWrapper) Send(writer io.Writer, v interface{}) (n int, err error) {
  22. var payload []byte
  23. payload, err = json.Marshal(v)
  24. if err != nil {
  25. jsonlogger.Fatalln("Error marshalling JSON", err)
  26. return 0, err
  27. }
  28. jsonlogger.Infof("Sending payload: %s", payload)
  29. return writer.Write(payload)
  30. }
  31. func (self jsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) {
  32. var reqParsed RpcRequest
  33. // Convert JSON to native types
  34. d := json.NewDecoder(req.Body)
  35. // d.UseNumber()
  36. defer req.Body.Close()
  37. err := d.Decode(&reqParsed)
  38. if err != nil {
  39. jsonlogger.Errorln("Error decoding JSON: ", err)
  40. return reqParsed, err
  41. }
  42. jsonlogger.DebugDetailf("Parsed request: %s", reqParsed)
  43. return reqParsed, nil
  44. }
  45. func (self jsonWrapper) GetRequestReply(xeth *EthereumApi, req *RpcRequest, reply *interface{}) error {
  46. // call function for request method
  47. jsonlogger.DebugDetailf("%T %s", req.Params, req.Params)
  48. switch req.Method {
  49. case "eth_coinbase":
  50. return xeth.GetCoinbase(reply)
  51. case "eth_listening":
  52. return xeth.GetIsListening(reply)
  53. case "eth_mining":
  54. return xeth.GetIsMining(reply)
  55. case "eth_peerCount":
  56. return xeth.GetPeerCount(reply)
  57. case "eth_countAt":
  58. args, err := req.ToGetTxCountArgs()
  59. if err != nil {
  60. return err
  61. }
  62. return xeth.GetTxCountAt(args, reply)
  63. // case "eth_codeAt":
  64. // return nil
  65. case "eth_balanceAt":
  66. args, err := req.ToGetBalanceArgs()
  67. if err != nil {
  68. return err
  69. }
  70. return xeth.GetBalanceAt(args, reply)
  71. case "eth_stateAt":
  72. args, err := req.ToGetStorageArgs()
  73. if err != nil {
  74. return err
  75. }
  76. return xeth.GetStorageAt(args, reply)
  77. case "eth_blockByNumber", "eth_blockByHash":
  78. args, err := req.ToGetBlockArgs()
  79. if err != nil {
  80. return err
  81. }
  82. return xeth.GetBlock(args, reply)
  83. default:
  84. return NewErrorResponse(ErrorNotImplemented)
  85. }
  86. jsonlogger.DebugDetailf("Reply: %T %s", reply, reply)
  87. return nil
  88. }
  89. var JSON jsonWrapper