This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
jsonrpc.go
175 lines (154 loc) · 3.77 KB
/
jsonrpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (c) 2017-2020 The qitmeer developers
*/
package cmds
import (
"encoding/json"
"fmt"
)
// These are all service namespace in node
const (
DefaultServiceNameSpace = "qitmeer"
MinerNameSpace = "miner"
TestNameSpace = "test"
LogNameSpace = "log"
NotifyNameSpace = ""
)
type RPCErrorCode int
type RPCError struct {
Code RPCErrorCode `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
var _, _ error = RPCError{}, (*RPCError)(nil)
func (e RPCError) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}
func NewRPCError(code RPCErrorCode, message string) *RPCError {
return &RPCError{
Code: code,
Message: message,
}
}
func IsValidIDType(id interface{}) bool {
switch id.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64,
string,
nil:
return true
default:
return false
}
}
type Request struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []json.RawMessage `json:"params"`
ID interface{} `json:"id"`
}
func NewRequest(id interface{}, method string, params []interface{}) (*Request, error) {
if !IsValidIDType(id) {
return nil, fmt.Errorf("the id of type '%T' is invalid", id)
}
rawParams := make([]json.RawMessage, 0, len(params))
for _, param := range params {
marshalledParam, err := json.Marshal(param)
if err != nil {
return nil, err
}
rawMessage := json.RawMessage(marshalledParam)
rawParams = append(rawParams, rawMessage)
}
return &Request{
Jsonrpc: "1.0",
ID: id,
Method: method,
Params: rawParams,
}, nil
}
type Response struct {
Result json.RawMessage `json:"result"`
Error *RPCError `json:"error"`
ID *interface{} `json:"id"`
}
func NewResponse(id interface{}, marshalledResult []byte, rpcErr *RPCError) (*Response, error) {
if !IsValidIDType(id) {
return nil, fmt.Errorf("the id of type '%T' is invalid", id)
}
pid := &id
return &Response{
Result: marshalledResult,
Error: rpcErr,
ID: pid,
}, nil
}
func MarshalResponse(id interface{}, result interface{}, rpcErr *RPCError) ([]byte, error) {
marshalledResult, err := json.Marshal(result)
if err != nil {
return nil, err
}
response, err := NewResponse(id, marshalledResult, rpcErr)
if err != nil {
return nil, err
}
return json.Marshal(&response)
}
var (
ErrRPCInvalidRequest = &RPCError{
Code: -32600,
Message: "Invalid request",
}
ErrRPCMethodNotFound = &RPCError{
Code: -32601,
Message: "Method not found",
}
ErrRPCInvalidParams = &RPCError{
Code: -32602,
Message: "Invalid parameters",
}
ErrRPCInternal = &RPCError{
Code: -32603,
Message: "Internal error",
}
// ErrRPCDatabase indicates a database error.
ErrRPCDatabase = &RPCError{
Code: -32001,
Message: "Database error",
}
ErrRPCBlockNotFound = &RPCError{
Code: -32002,
Message: "Block Not Found error",
}
ErrInvalidNode = &RPCError{
Code: -32003,
Message: "Invalid Node",
}
ErrRPCParse = &RPCError{
Code: -32700,
Message: "Parse error",
}
ErrRPCDecodeHexString = &RPCError{
Code: -32701,
Message: "Hex decode error",
}
)
func InternalRPCError(errStr, context string) *RPCError {
logStr := errStr
if context != "" {
logStr = context + ": " + errStr
}
log.Error(logStr)
return NewRPCError(ErrRPCInternal.Code, errStr)
}
type JsonRequestStatus struct {
Name string `json:"name"`
TotalCalls int `json:"totalcalls"`
TotalTime string `json:"totaltime"`
AverageTime string `json:"averagetime"`
MaxTime string `json:"maxtime"`
MinTime string `json:"mintime"`
MaxTimeReqID string `json:"maxtimereqid"`
MinTimeReqID string `json:"mintimereqid"`
RunningNum int `json:"runningnum"`
}