-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
executable file
·275 lines (244 loc) · 7.95 KB
/
rpc.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
package rpc
import (
"fmt"
"net"
"strings"
"github.com/zennittians/intelchain/eth/rpc"
"github.com/zennittians/intelchain/internal/configs/intelchain"
nodeconfig "github.com/zennittians/intelchain/internal/configs/node"
"github.com/zennittians/intelchain/internal/utils"
"github.com/zennittians/intelchain/itc"
eth "github.com/zennittians/intelchain/rpc/eth"
v1 "github.com/zennittians/intelchain/rpc/v1"
v2 "github.com/zennittians/intelchain/rpc/v2"
)
// Version enum
const (
V1 Version = iota
V2
Eth
Debug
Trace
)
const (
// APIVersion used for DApp's, bumped after RPC refactor (7/2020)
APIVersion = "1.1"
// LogTag is the tag found in the log for all RPC logs
LogTag = "[RPC]"
// HTTPPortOffset ..
HTTPPortOffset = 500
// WSPortOffset ..
WSPortOffset = 800
netNamespace = "net"
netV1Namespace = "netv1"
netV2Namespace = "netv2"
web3Namespace = "web3"
)
var (
// HTTPModules ..
HTTPModules = []string{"itc", "itcv2", "eth", "debug", "trace", netNamespace, netV1Namespace, netV2Namespace, web3Namespace, "explorer", "preimages"}
// WSModules ..
WSModules = []string{"itc", "itcv2", "eth", "debug", "trace", netNamespace, netV1Namespace, netV2Namespace, web3Namespace, "web3"}
httpListener net.Listener
httpHandler *rpc.Server
wsListener net.Listener
wsHandler *rpc.Server
httpEndpoint = ""
httpAuthEndpoint = ""
wsEndpoint = ""
wsAuthEndpoint = ""
httpVirtualHosts = []string{"*"}
httpOrigins = []string{"*"}
wsOrigins = []string{"*"}
)
// Version of the RPC
type Version int
// Namespace of the RPC version
func (n Version) Namespace() string {
return HTTPModules[n]
}
// StartServers starts the http & ws servers
func StartServers(itc *itc.Intelchain, apis []rpc.API, config nodeconfig.RPCServerConfig, rpcOpt intelchain.RpcOptConfig) error {
apis = append(apis, getAPIs(itc, config)...)
authApis := append(apis, getAuthAPIs(itc, config.DebugEnabled, config.RateLimiterEnabled, config.RequestsPerSecond)...)
if rpcOpt.PreimagesEnabled {
authApis = append(authApis, NewPreimagesAPI(itc, "preimages"))
}
// load method filter from file (if exist)
var rmf rpc.RpcMethodFilter
rpcFilterFilePath := strings.TrimSpace(rpcOpt.RpcFilterFile)
if len(rpcFilterFilePath) > 0 {
if err := rmf.LoadRpcMethodFiltersFromFile(rpcFilterFilePath); err != nil {
return err
}
} else {
rmf.ExposeAll()
}
if config.HTTPEnabled {
timeouts := rpc.HTTPTimeouts{
ReadTimeout: config.HTTPTimeoutRead,
WriteTimeout: config.HTTPTimeoutWrite,
IdleTimeout: config.HTTPTimeoutIdle,
}
httpEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPPort)
if err := startHTTP(apis, &rmf, timeouts); err != nil {
return err
}
httpAuthEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPAuthPort)
if err := startAuthHTTP(authApis, &rmf, timeouts); err != nil {
return err
}
}
if config.WSEnabled {
wsEndpoint = fmt.Sprintf("%v:%v", config.WSIp, config.WSPort)
if err := startWS(apis, &rmf); err != nil {
return err
}
wsAuthEndpoint = fmt.Sprintf("%v:%v", config.WSIp, config.WSAuthPort)
if err := startAuthWS(authApis, &rmf); err != nil {
return err
}
}
return nil
}
// StopServers stops the http & ws servers
func StopServers() error {
if httpListener != nil {
if err := httpListener.Close(); err != nil {
return err
}
httpListener = nil
utils.Logger().Info().
Str("url", fmt.Sprintf("http://%s", httpEndpoint)).
Msg("HTTP endpoint closed")
}
if httpHandler != nil {
httpHandler.Stop()
httpHandler = nil
}
if wsListener != nil {
if err := wsListener.Close(); err != nil {
return err
}
wsListener = nil
utils.Logger().Info().
Str("url", fmt.Sprintf("http://%s", wsEndpoint)).
Msg("WS endpoint closed")
}
if wsHandler != nil {
wsHandler.Stop()
wsHandler = nil
}
return nil
}
func getAuthAPIs(itc *itc.Intelchain, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API {
return []rpc.API{
NewPublicTraceAPI(itc, Debug), // Debug version means geth trace rpc
NewPublicTraceAPI(itc, Trace), // Trace version means parity trace rpc
}
}
// getAPIs returns all the API methods for the RPC interface
func getAPIs(itc *itc.Intelchain, config nodeconfig.RPCServerConfig) []rpc.API {
publicAPIs := []rpc.API{
// Public methods
NewPublicIntelchainAPI(itc, V1),
NewPublicIntelchainAPI(itc, V2),
NewPublicBlockchainAPI(itc, V1, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicBlockchainAPI(itc, V2, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicContractAPI(itc, V1, config.RateLimiterEnabled, config.RequestsPerSecond, config.EvmCallTimeout),
NewPublicContractAPI(itc, V2, config.RateLimiterEnabled, config.RequestsPerSecond, config.EvmCallTimeout),
NewPublicTransactionAPI(itc, V1),
NewPublicTransactionAPI(itc, V2),
NewPublicPoolAPI(itc, V1, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicPoolAPI(itc, V2, config.RateLimiterEnabled, config.RequestsPerSecond),
}
// Legacy methods (subject to removal)
if config.LegacyRPCsEnabled {
publicAPIs = append(publicAPIs,
v1.NewPublicLegacyAPI(itc, "itc"),
v2.NewPublicLegacyAPI(itc, "itcv2"),
)
}
if config.StakingRPCsEnabled {
publicAPIs = append(publicAPIs,
NewPublicStakingAPI(itc, V1),
NewPublicStakingAPI(itc, V2),
)
}
if config.EthRPCsEnabled {
publicAPIs = append(publicAPIs,
NewPublicIntelchainAPI(itc, Eth),
NewPublicBlockchainAPI(itc, Eth, config.RateLimiterEnabled, config.RequestsPerSecond),
NewPublicContractAPI(itc, Eth, config.RateLimiterEnabled, config.RequestsPerSecond, config.EvmCallTimeout),
NewPublicTransactionAPI(itc, Eth),
NewPublicPoolAPI(itc, Eth, config.RateLimiterEnabled, config.RequestsPerSecond),
eth.NewPublicEthService(itc, "eth"),
)
}
publicDebugAPIs := []rpc.API{
//Public debug API
NewPublicDebugAPI(itc, V1),
NewPublicDebugAPI(itc, V2),
}
privateAPIs := []rpc.API{
NewPrivateDebugAPI(itc, V1),
NewPrivateDebugAPI(itc, V2),
}
if config.DebugEnabled {
apis := append(publicAPIs, publicDebugAPIs...)
return append(apis, privateAPIs...)
}
return publicAPIs
}
func startHTTP(apis []rpc.API, rmf *rpc.RpcMethodFilter, httpTimeouts rpc.HTTPTimeouts) (err error) {
httpListener, httpHandler, err = rpc.StartHTTPEndpoint(
httpEndpoint, apis, HTTPModules, rmf, httpOrigins, httpVirtualHosts, httpTimeouts,
)
if err != nil {
return err
}
utils.Logger().Info().
Str("url", fmt.Sprintf("http://%s", httpEndpoint)).
Str("cors", strings.Join(httpOrigins, ",")).
Str("vhosts", strings.Join(httpVirtualHosts, ",")).
Msg("HTTP endpoint opened")
fmt.Printf("Started RPC server at: %v\n", httpEndpoint)
return nil
}
func startAuthHTTP(apis []rpc.API, rmf *rpc.RpcMethodFilter, httpTimeouts rpc.HTTPTimeouts) (err error) {
httpListener, httpHandler, err = rpc.StartHTTPEndpoint(
httpAuthEndpoint, apis, HTTPModules, rmf, httpOrigins, httpVirtualHosts, httpTimeouts,
)
if err != nil {
return err
}
utils.Logger().Info().
Str("url", fmt.Sprintf("http://%s", httpAuthEndpoint)).
Str("cors", strings.Join(httpOrigins, ",")).
Str("vhosts", strings.Join(httpVirtualHosts, ",")).
Msg("HTTP endpoint opened")
fmt.Printf("Started Auth-RPC server at: %v\n", httpAuthEndpoint)
return nil
}
func startWS(apis []rpc.API, rmf *rpc.RpcMethodFilter) (err error) {
wsListener, wsHandler, err = rpc.StartWSEndpoint(wsEndpoint, apis, WSModules, rmf, wsOrigins, true)
if err != nil {
return err
}
utils.Logger().Info().
Str("url", fmt.Sprintf("ws://%s", wsListener.Addr())).
Msg("WebSocket endpoint opened")
fmt.Printf("Started WS server at: %v\n", wsEndpoint)
return nil
}
func startAuthWS(apis []rpc.API, rmf *rpc.RpcMethodFilter) (err error) {
wsListener, wsHandler, err = rpc.StartWSEndpoint(wsAuthEndpoint, apis, WSModules, rmf, wsOrigins, true)
if err != nil {
return err
}
utils.Logger().Info().
Str("url", fmt.Sprintf("ws://%s", wsListener.Addr())).
Msg("WebSocket endpoint opened")
fmt.Printf("Started Auth-WS server at: %v\n", wsAuthEndpoint)
return nil
}