json.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // Spec at https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC
  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. args, err := req.ToGetCodeAtArgs()
  65. if err != nil {
  66. return err
  67. }
  68. return xeth.GetCodeAt(args, reply)
  69. case "eth_balanceAt":
  70. args, err := req.ToGetBalanceArgs()
  71. if err != nil {
  72. return err
  73. }
  74. return xeth.GetBalanceAt(args, reply)
  75. case "eth_stateAt":
  76. args, err := req.ToGetStorageArgs()
  77. if err != nil {
  78. return err
  79. }
  80. return xeth.GetStorageAt(args, reply)
  81. case "eth_blockByNumber", "eth_blockByHash":
  82. args, err := req.ToGetBlockArgs()
  83. if err != nil {
  84. return err
  85. }
  86. return xeth.GetBlock(args, reply)
  87. default:
  88. return NewErrorResponse(ErrorNotImplemented)
  89. }
  90. jsonlogger.DebugDetailf("Reply: %T %s", reply, reply)
  91. return nil
  92. }
  93. var JSON jsonWrapper