-
Notifications
You must be signed in to change notification settings - Fork 249
/
route.go
111 lines (99 loc) · 3.5 KB
/
route.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
package rpc
// router implements logic for routing
// JSON-RPC requests either to Upstream or
// Local node.
type router struct {
methods map[string]bool
blockedMethods map[string]struct{}
upstreamEnabled bool
}
// newRouter inits new router.
func newRouter(upstreamEnabled bool) *router {
r := &router{
methods: make(map[string]bool),
blockedMethods: make(map[string]struct{}),
upstreamEnabled: upstreamEnabled,
}
for _, m := range remoteMethods {
r.methods[m] = true
}
for _, m := range blockedMethods {
r.blockedMethods[m] = struct{}{}
}
return r
}
// routeRemote returns true if given method should be routed to the remote node
func (r *router) routeRemote(method string) bool {
if !r.upstreamEnabled {
return false
}
// else check route using the methods list
return r.methods[method]
}
func (r *router) routeBlocked(method string) bool {
_, ok := r.blockedMethods[method]
return ok
}
// blockedMethods is a list of dangerous or having security implications JSON-RPC methods
// that are not allowed to be called.
var blockedMethods = [...]string{
"shh_getPrivateKey",
}
// BlockedMethods returns a list of methods that are not allowed to be called.
// A copy of a slice is returned in order to prevent from changing it from outside.
func BlockedMethods() []string {
return append([]string(nil), blockedMethods[:]...)
}
// remoteMethods contains methods that should be routed to
// the upstream node; the rest is considered to be routed to
// the local node.
// A list of supported methods:
// curl --include \
// --header "Content-Type: application/json" \
// --header "Accept: application/json" 'https://api.infura.io/v1/jsonrpc/ropsten/methods'
// Although it's tempting to only list methods coming to the local node as there're fewer of them
// but it's deceptive: we want to ensure that only known requests leave our zone of responsibility.
// Also, we want new requests in newer Geth versions not to be accidentally routed to the upstream.
// The list of methods: https://github.com/ethereum/wiki/wiki/JSON-RPC
var remoteMethods = [...]string{
"eth_protocolVersion",
"eth_syncing",
"eth_coinbase",
"eth_mining",
"eth_hashrate",
"eth_gasPrice",
//"eth_accounts", // due to sub-accounts handling
"eth_blockNumber",
"eth_getBalance",
"eth_getStorageAt",
"eth_getTransactionCount",
"eth_getBlockTransactionCountByHash",
"eth_getBlockTransactionCountByNumber",
"eth_getUncleCountByBlockHash",
"eth_getUncleCountByBlockNumber",
"eth_getCode",
//"eth_sign", // only the local node has an injected account to sign the payload with
//"eth_sendTransaction", // we handle this specially calling eth_estimateGas, signing it locally and sending eth_sendRawTransaction afterwards
"eth_sendRawTransaction",
"eth_call",
"eth_estimateGas",
"eth_getBlockByHash",
"eth_getBlockByNumber",
"eth_getTransactionByHash",
"eth_getTransactionByBlockHashAndIndex",
"eth_getTransactionByBlockNumberAndIndex",
"eth_getTransactionReceipt",
"eth_getUncleByBlockHashAndIndex",
"eth_getUncleByBlockNumberAndIndex",
//"eth_getCompilers", // goes to the local because there's no need to send it anywhere
//"eth_compileLLL", // goes to the local because there's no need to send it anywhere
//"eth_compileSolidity", // goes to the local because there's no need to send it anywhere
//"eth_compileSerpent", // goes to the local because there's no need to send it anywhere
"eth_getLogs",
"eth_getWork",
"eth_submitWork",
"eth_submitHashrate",
"net_version",
"net_peerCount",
"net_listening",
}