2017-09-14 20:14:31 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
// router implements logic for routing
|
|
|
|
// JSON-RPC requests either to Upstream or
|
|
|
|
// Local node.
|
|
|
|
type router struct {
|
|
|
|
methods map[string]bool
|
2018-08-02 07:07:55 +00:00
|
|
|
blockedMethods map[string]struct{}
|
2017-09-14 20:14:31 +00:00
|
|
|
upstreamEnabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRouter inits new router.
|
|
|
|
func newRouter(upstreamEnabled bool) *router {
|
|
|
|
r := &router{
|
|
|
|
methods: make(map[string]bool),
|
2018-08-02 07:07:55 +00:00
|
|
|
blockedMethods: make(map[string]struct{}),
|
2017-09-14 20:14:31 +00:00
|
|
|
upstreamEnabled: upstreamEnabled,
|
|
|
|
}
|
|
|
|
|
2017-09-19 09:52:38 +00:00
|
|
|
for _, m := range remoteMethods {
|
2017-09-14 20:14:31 +00:00
|
|
|
r.methods[m] = true
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:07:55 +00:00
|
|
|
for _, m := range blockedMethods {
|
|
|
|
r.blockedMethods[m] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:14:31 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-09-19 09:52:38 +00:00
|
|
|
// routeRemote returns true if given method should be routed to the remote node
|
|
|
|
func (r *router) routeRemote(method string) bool {
|
2017-09-14 20:14:31 +00:00
|
|
|
if !r.upstreamEnabled {
|
2017-09-19 09:52:38 +00:00
|
|
|
return false
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// else check route using the methods list
|
|
|
|
return r.methods[method]
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:07:55 +00:00
|
|
|
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[:]...)
|
|
|
|
}
|
|
|
|
|
2017-09-19 09:52:38 +00:00
|
|
|
// remoteMethods contains methods that should be routed to
|
|
|
|
// the upstream node; the rest is considered to be routed to
|
|
|
|
// the local node.
|
2018-05-04 10:53:41 +00:00
|
|
|
// A list of supported methods:
|
|
|
|
// curl --include \
|
|
|
|
// --header "Content-Type: application/json" \
|
|
|
|
// --header "Accept: application/json" 'https://api.infura.io/v1/jsonrpc/ropsten/methods'
|
2017-09-19 09:52:38 +00:00
|
|
|
// 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",
|
2017-09-25 16:04:40 +00:00
|
|
|
//"eth_accounts", // due to sub-accounts handling
|
2017-09-19 09:52:38 +00:00
|
|
|
"eth_blockNumber",
|
|
|
|
"eth_getBalance",
|
|
|
|
"eth_getStorageAt",
|
|
|
|
"eth_getTransactionCount",
|
|
|
|
"eth_getBlockTransactionCountByHash",
|
|
|
|
"eth_getBlockTransactionCountByNumber",
|
|
|
|
"eth_getUncleCountByBlockHash",
|
|
|
|
"eth_getUncleCountByBlockNumber",
|
|
|
|
"eth_getCode",
|
2017-09-25 16:04:40 +00:00
|
|
|
//"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
|
2017-09-19 09:52:38 +00:00
|
|
|
"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
|
2018-05-04 10:53:41 +00:00
|
|
|
|
2017-09-19 09:52:38 +00:00
|
|
|
"eth_getLogs",
|
|
|
|
"eth_getWork",
|
|
|
|
"eth_submitWork",
|
|
|
|
"eth_submitHashrate",
|
2017-09-29 15:09:41 +00:00
|
|
|
"net_version",
|
|
|
|
"net_peerCount",
|
|
|
|
"net_listening",
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|