github.com/status-im/status-go@v1.1.0/rpc/route.go (about)

     1  package rpc
     2  
     3  // router implements logic for routing
     4  // JSON-RPC requests either to Upstream or
     5  // Local node.
     6  type router struct {
     7  	methods         map[string]bool
     8  	blockedMethods  map[string]struct{}
     9  	upstreamEnabled bool
    10  }
    11  
    12  // newRouter inits new router.
    13  func newRouter(upstreamEnabled bool) *router {
    14  	r := &router{
    15  		methods:         make(map[string]bool),
    16  		blockedMethods:  make(map[string]struct{}),
    17  		upstreamEnabled: upstreamEnabled,
    18  	}
    19  
    20  	for _, m := range remoteMethods {
    21  		r.methods[m] = true
    22  	}
    23  
    24  	for _, m := range blockedMethods {
    25  		r.blockedMethods[m] = struct{}{}
    26  	}
    27  
    28  	return r
    29  }
    30  
    31  // routeRemote returns true if given method should be routed to the remote node
    32  func (r *router) routeRemote(method string) bool {
    33  	if !r.upstreamEnabled {
    34  		return false
    35  	}
    36  
    37  	// else check route using the methods list
    38  	return r.methods[method]
    39  }
    40  
    41  func (r *router) routeBlocked(method string) bool {
    42  	_, ok := r.blockedMethods[method]
    43  	return ok
    44  }
    45  
    46  // blockedMethods is a list of dangerous or having security implications JSON-RPC methods
    47  // that are not allowed to be called.
    48  var blockedMethods = [...]string{
    49  	"shh_getPrivateKey",
    50  }
    51  
    52  // BlockedMethods returns a list of methods that are not allowed to be called.
    53  // A copy of a slice is returned in order to prevent from changing it from outside.
    54  func BlockedMethods() []string {
    55  	return append([]string(nil), blockedMethods[:]...)
    56  }
    57  
    58  // remoteMethods contains methods that should be routed to
    59  // the upstream node; the rest is considered to be routed to
    60  // the local node.
    61  // A list of supported methods:
    62  //
    63  //	curl --include \
    64  //	   --header "Content-Type: application/json" \
    65  //	   --header "Accept: application/json" 'https://api.infura.io/v1/jsonrpc/ropsten/methods'
    66  //
    67  // Although it's tempting to only list methods coming to the local node as there're fewer of them
    68  // but it's deceptive: we want to ensure that only known requests leave our zone of responsibility.
    69  // Also, we want new requests in newer Geth versions not to be accidentally routed to the upstream.
    70  // The list of methods: https://github.com/ethereum/wiki/wiki/JSON-RPC
    71  var remoteMethods = [...]string{
    72  	"eth_protocolVersion",
    73  	"eth_syncing",
    74  	"eth_coinbase",
    75  	"eth_mining",
    76  	"eth_hashrate",
    77  	"eth_gasPrice",
    78  	"eth_maxPriorityFeePerGas",
    79  	"eth_feeHistory",
    80  	//"eth_accounts", // due to sub-accounts handling
    81  	"eth_blockNumber",
    82  	"eth_getBalance",
    83  	"eth_getStorageAt",
    84  	"eth_getTransactionCount",
    85  	"eth_getBlockTransactionCountByHash",
    86  	"eth_getBlockTransactionCountByNumber",
    87  	"eth_getUncleCountByBlockHash",
    88  	"eth_getUncleCountByBlockNumber",
    89  	"eth_getCode",
    90  	//"eth_sign", // only the local node has an injected account to sign the payload with
    91  	//"eth_sendTransaction", // we handle this specially calling eth_estimateGas, signing it locally and sending eth_sendRawTransaction afterwards
    92  	"eth_sendRawTransaction",
    93  	"eth_call",
    94  	"eth_estimateGas",
    95  	"eth_getBlockByHash",
    96  	"eth_getBlockByNumber",
    97  	"eth_getTransactionByHash",
    98  	"eth_getTransactionByBlockHashAndIndex",
    99  	"eth_getTransactionByBlockNumberAndIndex",
   100  	"eth_getTransactionReceipt",
   101  	"eth_getUncleByBlockHashAndIndex",
   102  	"eth_getUncleByBlockNumberAndIndex",
   103  	//"eth_getCompilers",    // goes to the local because there's no need to send it anywhere
   104  	//"eth_compileLLL",      // goes to the local because there's no need to send it anywhere
   105  	//"eth_compileSolidity", // goes to the local because there's no need to send it anywhere
   106  	//"eth_compileSerpent",  // goes to the local because there's no need to send it anywhere
   107  
   108  	"eth_getLogs",
   109  	"eth_getWork",
   110  	"eth_submitWork",
   111  	"eth_submitHashrate",
   112  	"net_version",
   113  	"net_peerCount",
   114  	"net_listening",
   115  }