message.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "bytes"
  17. "encoding/json"
  18. "errors"
  19. )
  20. const (
  21. ErrorArguments = "Error: Insufficient arguments"
  22. ErrorNotImplemented = "Error: Method not implemented"
  23. ErrorUnknown = "Error: Unknown error"
  24. ErrorParseRequest = "Error: Could not parse request"
  25. ErrorDecodeArgs = "Error: Could not decode arguments"
  26. )
  27. type ErrorResponse struct {
  28. Error bool `json:"error"`
  29. ErrorText string `json:"errorText"`
  30. }
  31. type RpcSuccessResponse struct {
  32. ID int `json:"id"`
  33. JsonRpc string `json:"jsonrpc"`
  34. Error bool `json:"error"`
  35. Result interface{} `json:"result"`
  36. }
  37. type RpcErrorResponse struct {
  38. ID int `json:"id"`
  39. JsonRpc string `json:"jsonrpc"`
  40. Error bool `json:"error"`
  41. ErrorText string `json:"errortext"`
  42. }
  43. type RpcRequest struct {
  44. JsonRpc string `json:"jsonrpc"`
  45. ID int `json:"id"`
  46. Method string `json:"method"`
  47. Params []json.RawMessage `json:"params"`
  48. }
  49. func (req *RpcRequest) ToGetBlockArgs() (*GetBlockArgs, error) {
  50. if len(req.Params) < 1 {
  51. return nil, NewErrorResponse(ErrorArguments)
  52. }
  53. args := new(GetBlockArgs)
  54. r := bytes.NewReader(req.Params[0])
  55. err := json.NewDecoder(r).Decode(args)
  56. if err != nil {
  57. return nil, NewErrorResponse(ErrorDecodeArgs)
  58. }
  59. jsonlogger.DebugDetailf("%T %v", args, args)
  60. return args, nil
  61. }
  62. func (req *RpcRequest) ToNewTxArgs() (*NewTxArgs, error) {
  63. if len(req.Params) < 7 {
  64. return nil, NewErrorResponse(ErrorArguments)
  65. }
  66. args := new(NewTxArgs)
  67. r := bytes.NewReader(req.Params[0])
  68. err := json.NewDecoder(r).Decode(args)
  69. if err != nil {
  70. return nil, NewErrorResponse(ErrorDecodeArgs)
  71. }
  72. jsonlogger.DebugDetailf("%T %v", args, args)
  73. return args, nil
  74. }
  75. func (req *RpcRequest) ToPushTxArgs() (*PushTxArgs, error) {
  76. if len(req.Params) < 1 {
  77. return nil, NewErrorResponse(ErrorArguments)
  78. }
  79. args := new(PushTxArgs)
  80. r := bytes.NewReader(req.Params[0])
  81. err := json.NewDecoder(r).Decode(args)
  82. if err != nil {
  83. return nil, NewErrorResponse(ErrorDecodeArgs)
  84. }
  85. jsonlogger.DebugDetailf("%T %v", args, args)
  86. return args, nil
  87. }
  88. func (req *RpcRequest) ToGetStorageArgs() (*GetStorageArgs, error) {
  89. if len(req.Params) < 2 {
  90. return nil, NewErrorResponse(ErrorArguments)
  91. }
  92. args := new(GetStorageArgs)
  93. // TODO need to pass both arguments
  94. r := bytes.NewReader(req.Params[0])
  95. err := json.NewDecoder(r).Decode(args)
  96. if err != nil {
  97. return nil, NewErrorResponse(ErrorDecodeArgs)
  98. }
  99. jsonlogger.DebugDetailf("%T %v", args, args)
  100. return args, nil
  101. }
  102. func (req *RpcRequest) ToGetTxCountArgs() (*GetTxCountArgs, error) {
  103. if len(req.Params) < 1 {
  104. return nil, NewErrorResponse(ErrorArguments)
  105. }
  106. args := new(GetTxCountArgs)
  107. r := bytes.NewReader(req.Params[0])
  108. err := json.NewDecoder(r).Decode(args)
  109. if err != nil {
  110. return nil, NewErrorResponse(ErrorDecodeArgs)
  111. }
  112. jsonlogger.DebugDetailf("%T %v", args, args)
  113. return args, nil
  114. }
  115. func (req *RpcRequest) ToGetBalanceArgs() (*GetBalanceArgs, error) {
  116. if len(req.Params) < 1 {
  117. return nil, NewErrorResponse(ErrorArguments)
  118. }
  119. args := new(GetBalanceArgs)
  120. r := bytes.NewReader(req.Params[0])
  121. err := json.NewDecoder(r).Decode(args)
  122. if err != nil {
  123. return nil, NewErrorResponse(ErrorDecodeArgs)
  124. }
  125. jsonlogger.DebugDetailf("%T %v", args, args)
  126. return args, nil
  127. }
  128. func (req *RpcRequest) ToGetCodeAtArgs() (*GetCodeAtArgs, error) {
  129. if len(req.Params) < 1 {
  130. return nil, NewErrorResponse(ErrorArguments)
  131. }
  132. args := new(GetCodeAtArgs)
  133. r := bytes.NewReader(req.Params[0])
  134. err := json.NewDecoder(r).Decode(args)
  135. if err != nil {
  136. return nil, NewErrorResponse(ErrorDecodeArgs)
  137. }
  138. jsonlogger.DebugDetailf("%T %v", args, args)
  139. return args, nil
  140. }
  141. func NewErrorResponse(msg string) error {
  142. return errors.New(msg)
  143. }