forked from 0xPolygon/polygon-edge
/
jsonrpc.go
330 lines (274 loc) · 7.76 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package jsonrpc
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"sync"
"time"
"github.com/Gabulhas/polygon-external-consensus/versioning"
"github.com/gorilla/websocket"
"github.com/hashicorp/go-hclog"
)
type serverType int
const (
serverIPC serverType = iota
serverHTTP
serverWS
)
func (s serverType) String() string {
switch s {
case serverIPC:
return "ipc"
case serverHTTP:
return "http"
case serverWS:
return "ws"
default:
panic("BUG: Not expected")
}
}
// JSONRPC is an API consensus
type JSONRPC struct {
logger hclog.Logger
config *Config
dispatcher dispatcher
}
type dispatcher interface {
RemoveFilterByWs(conn wsConn)
HandleWs(reqBody []byte, conn wsConn) ([]byte, error)
Handle(reqBody []byte) ([]byte, error)
}
// JSONRPCStore defines all the methods required
// by all the JSON RPC endpoints
type JSONRPCStore interface {
ethStore
networkStore
txPoolStore
filterManagerStore
}
type Config struct {
Store JSONRPCStore
Addr *net.TCPAddr
ChainID uint64
ChainName string
AccessControlAllowOrigin []string
PriceLimit uint64
BatchLengthLimit uint64
BlockRangeLimit uint64
}
// NewJSONRPC returns the JSONRPC http server
func NewJSONRPC(logger hclog.Logger, config *Config) (*JSONRPC, error) {
srv := &JSONRPC{
logger: logger.Named("jsonrpc"),
config: config,
dispatcher: newDispatcher(
logger,
config.Store,
&dispatcherParams{
chainID: config.ChainID,
chainName: config.ChainName,
priceLimit: config.PriceLimit,
jsonRPCBatchLengthLimit: config.BatchLengthLimit,
blockRangeLimit: config.BlockRangeLimit,
},
),
}
// start http server
if err := srv.setupHTTP(); err != nil {
return nil, err
}
return srv, nil
}
func (j *JSONRPC) setupHTTP() error {
j.logger.Info("http server started", "addr", j.config.Addr.String())
lis, err := net.Listen("tcp", j.config.Addr.String())
if err != nil {
return err
}
// NewServeMux must be used, as it disables all debug features.
// For some strange reason, with DefaultServeMux debug/vars is always enabled (but not debug/pprof).
// If pprof need to be enabled, this should be DefaultServeMux
mux := http.NewServeMux()
// The middleware factory returns a handler, so we need to wrap the handler function properly.
jsonRPCHandler := http.HandlerFunc(j.handle)
mux.Handle("/", middlewareFactory(j.config)(jsonRPCHandler))
mux.HandleFunc("/ws", j.handleWs)
srv := http.Server{
Handler: mux,
ReadHeaderTimeout: 60 * time.Second,
}
go func() {
if err := srv.Serve(lis); err != nil {
j.logger.Error("closed http connection", "err", err)
}
}()
return nil
}
// The middlewareFactory builds a middleware which enables CORS using the provided config.
func middlewareFactory(config *Config) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
for _, allowedOrigin := range config.AccessControlAllowOrigin {
if allowedOrigin == "*" {
w.Header().Set("Access-Control-Allow-Origin", "*")
break
}
if allowedOrigin == origin {
w.Header().Set("Access-Control-Allow-Origin", origin)
break
}
}
next.ServeHTTP(w, r)
})
}
}
// wsUpgrader defines upgrade parameters for the WS connection
var wsUpgrader = websocket.Upgrader{
// Uses the default HTTP buffer sizes for Read / Write buffers.
// Documentation specifies that they are 4096B in size.
// There is no need to have them be 4x in size when requests / responses
// shouldn't exceed 1024B
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// wsWrapper is a wrapping object for the web socket connection and logger
type wsWrapper struct {
sync.Mutex
ws *websocket.Conn // the actual WS connection
logger hclog.Logger // module logger
filterID string // filter ID
}
func (w *wsWrapper) SetFilterID(filterID string) {
w.filterID = filterID
}
func (w *wsWrapper) GetFilterID() string {
return w.filterID
}
// WriteMessage writes out the message to the WS peer
func (w *wsWrapper) WriteMessage(messageType int, data []byte) error {
w.Lock()
defer w.Unlock()
writeErr := w.ws.WriteMessage(messageType, data)
if writeErr != nil {
w.logger.Error(
fmt.Sprintf("Unable to write WS message, %s", writeErr.Error()),
)
}
return writeErr
}
// isSupportedWSType returns a status indicating if the message type is supported
func isSupportedWSType(messageType int) bool {
return messageType == websocket.TextMessage ||
messageType == websocket.BinaryMessage
}
func (j *JSONRPC) handleWs(w http.ResponseWriter, req *http.Request) {
// CORS rule - Allow requests from anywhere
wsUpgrader.CheckOrigin = func(r *http.Request) bool { return true }
// Upgrade the connection to a WS one
ws, err := wsUpgrader.Upgrade(w, req, nil)
if err != nil {
j.logger.Error(fmt.Sprintf("Unable to upgrade to a WS connection, %s", err.Error()))
return
}
// Defer WS closure
defer func(ws *websocket.Conn) {
err = ws.Close()
if err != nil {
j.logger.Error(
fmt.Sprintf("Unable to gracefully close WS connection, %s", err.Error()),
)
}
}(ws)
wrapConn := &wsWrapper{ws: ws, logger: j.logger}
j.logger.Info("Websocket connection established")
// Run the listen loop
for {
// Read the incoming message
msgType, message, err := ws.ReadMessage()
if err != nil {
if websocket.IsCloseError(err,
websocket.CloseGoingAway,
websocket.CloseNormalClosure,
websocket.CloseAbnormalClosure,
) {
// Accepted close codes
j.logger.Info("Closing WS connection gracefully")
} else {
j.logger.Error(fmt.Sprintf("Unable to read WS message, %s", err.Error()))
j.logger.Info("Closing WS connection with error")
}
j.dispatcher.RemoveFilterByWs(wrapConn)
break
}
if isSupportedWSType(msgType) {
go func() {
resp, handleErr := j.dispatcher.HandleWs(message, wrapConn)
if handleErr != nil {
j.logger.Error(fmt.Sprintf("Unable to handle WS request, %s", handleErr.Error()))
_ = wrapConn.WriteMessage(
msgType,
[]byte(fmt.Sprintf("WS Handle error: %s", handleErr.Error())),
)
} else {
_ = wrapConn.WriteMessage(msgType, resp)
}
}()
}
}
}
func (j *JSONRPC) handle(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set(
"Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization",
)
switch req.Method {
case "POST":
j.handleJSONRPCRequest(w, req)
case "GET":
j.handleGetRequest(w)
case "OPTIONS":
// nothing to return
default:
_, _ = w.Write([]byte("method " + req.Method + " not allowed"))
}
}
func (j *JSONRPC) handleJSONRPCRequest(w http.ResponseWriter, req *http.Request) {
data, err := io.ReadAll(req.Body)
if err != nil {
_, _ = w.Write([]byte(err.Error()))
return
}
// log request
j.logger.Debug("handle", "request", string(data))
resp, err := j.dispatcher.Handle(data)
if err != nil {
_, _ = w.Write([]byte(err.Error()))
} else {
_, _ = w.Write(resp)
}
j.logger.Debug("handle", "response", string(resp))
}
type GetResponse struct {
Name string `json:"name"`
ChainID uint64 `json:"chain_id"`
Version string `json:"version"`
}
func (j *JSONRPC) handleGetRequest(writer io.Writer) {
data := &GetResponse{
Name: j.config.ChainName,
ChainID: j.config.ChainID,
Version: versioning.Version,
}
resp, err := json.Marshal(data)
if err != nil {
_, _ = writer.Write([]byte(err.Error()))
}
if _, err = writer.Write(resp); err != nil {
_, _ = writer.Write([]byte(err.Error()))
}
}