From 1c5c2cba8b8286f8362e5ac8177224cd9993348f Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Wed, 7 Jul 2021 14:13:27 +0200 Subject: [PATCH] [FEATURE] Use rpc proxy (#747) --- fluffy/conf.nim | 8 +++++ fluffy/fluffy.nim | 13 ++++---- fluffy/rpc/eth_api.nim | 76 +++++++++++++++++++++--------------------- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/fluffy/conf.nim b/fluffy/conf.nim index 4f85154eb..6858b5e6a 100644 --- a/fluffy/conf.nim +++ b/fluffy/conf.nim @@ -14,6 +14,7 @@ import const DefaultListenAddress* = (static ValidIpAddress.init("0.0.0.0")) DefaultAdminListenAddress* = (static ValidIpAddress.init("127.0.0.1")) + DefaultProxyAddress* = (static "http://127.0.0.1:8546") type PortalCmd* = enum @@ -92,6 +93,13 @@ type desc: "if provided, enables getting data from bridge node" name: "bridge-client-uri" .}: Option[string] + # it makes little sense to have default value here in final release, but until then + # it would be troublesome to add some fake uri param every time + proxyUri* {. + defaultValue: DefaultProxyAddress + desc: "uri of client to get data for unimplemented rpc methods" + name: "proxy-uri" .}: string + case cmd* {. command defaultValue: noCommand .}: PortalCmd diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index f829aad66..ca791331f 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -9,7 +9,7 @@ import confutils, confutils/std/net, chronicles, chronicles/topics_registry, - chronos, metrics, metrics/chronos_httpserver, json_rpc/clients/httpclient, json_rpc/servers/httpserver, + chronos, metrics, metrics/chronos_httpserver, json_rpc/clients/httpclient, json_rpc/rpcproxy, eth/keys, eth/net/nat, eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/portal/protocol as portal_protocol, @@ -68,11 +68,12 @@ proc run(config: PortalConf) {.raises: [CatchableError, Defect].} = except Exception as exc: raiseAssert exc.msg if config.rpcEnabled: - let - ta = initTAddress(config.rpcAddress, config.rpcPort) - rpcHttpServer = newRpcHttpServer([ta]) - rpcHttpServer.installEthApiHandlers() - rpcHttpServer.start() + let ta = initTAddress(config.rpcAddress, config.rpcPort) + var rpcHttpServerWithProxy = newRpcHttpProxy([$ta]) + rpcHttpServerWithProxy.installEthApiHandlers() + # TODO for now we can only proxy to local node (or remote one without ssl) to make it possible + # to call infura https://github.com/status-im/nim-json-rpc/pull/101 needs to get merged for http client to support https/ + waitFor rpcHttpServerWithProxy.start(config.proxyUri) let bridgeClient = initializeBridgeClient(config.bridgeUri) diff --git a/fluffy/rpc/eth_api.nim b/fluffy/rpc/eth_api.nim index 67839e2af..4032c0c22 100644 --- a/fluffy/rpc/eth_api.nim +++ b/fluffy/rpc/eth_api.nim @@ -8,7 +8,7 @@ {.push raises: [Defect].} import - json_rpc/rpcserver + json_rpc/rpcproxy # Subset of Eth JSON-RPC API: https://eth.wiki/json-rpc/API # Supported subset will eventually be found here: @@ -20,80 +20,80 @@ import # Can be done by just forwarding the rpc call, or by adding a call here, but # that would introduce a unnecessary serializing/deserializing step. -proc installEthApiHandlers*(rpcServer: RpcServer) +proc installEthApiHandlers*(rpcServerWithProxy: var RpcHttpProxy) {.raises: [Defect, CatchableError].} = # Supported API - rpcServer.rpc("eth_blockNumber") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_blockNumber") - rpcServer.rpc("eth_call") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_call") - rpcServer.rpc("eth_chainId") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_chainId") - rpcServer.rpc("eth_estimateGas") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_estimateGas") - rpcServer.rpc("eth_feeHistory") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_feeHistory") - rpcServer.rpc("eth_getBalance") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getBalance") - rpcServer.rpc("eth_getBlockByHash") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getBlockByHash") - rpcServer.rpc("eth_getBlockByNumber") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getBlockByNumber") - rpcServer.rpc("eth_getBlockTransactionCountByHash") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getBlockTransactionCountByHash") - rpcServer.rpc("eth_getBlockTransactionCountByNumber") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getBlockTransactionCountByNumber") - rpcServer.rpc("eth_getCode") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getCode") - rpcServer.rpc("eth_getRawTransactionByHash") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getRawTransactionByHash") - rpcServer.rpc("eth_getRawTransactionByBlockHashAndIndex") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getRawTransactionByBlockHashAndIndex") - rpcServer.rpc("eth_getRawTransactionByBlockNumberAndIndex") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getRawTransactionByBlockNumberAndIndex") - rpcServer.rpc("eth_getStorageAt") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getStorageAt") - rpcServer.rpc("eth_getTransactionByBlockHashAndIndex") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getTransactionByBlockHashAndIndex") - rpcServer.rpc("eth_getTransactionByBlockNumberAndIndex") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getTransactionByBlockNumberAndIndex") - rpcServer.rpc("eth_getTransactionByHash") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getTransactionByHash") - rpcServer.rpc("eth_getTransactionCount") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getTransactionCount") - rpcServer.rpc("eth_getTransactionReceipt") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getTransactionReceipt") - rpcServer.rpc("eth_getUncleByBlockHashAndIndex") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getUncleByBlockHashAndIndex") - rpcServer.rpc("eth_getUncleByBlockNumberAndIndex") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getUncleByBlockNumberAndIndex") - rpcServer.rpc("eth_getUncleCountByBlockHash") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getUncleCountByBlockHash") - rpcServer.rpc("eth_getUncleCountByBlockNumber") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getUncleCountByBlockNumber") - rpcServer.rpc("eth_getProof") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getProof") - rpcServer.rpc("eth_sendRawTransaction") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_sendRawTransaction") # Optional API - rpcServer.rpc("eth_gasPrice") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_gasPrice") - rpcServer.rpc("eth_getFilterChanges") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getFilterChanges") - rpcServer.rpc("eth_getFilterLogs") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getFilterLogs") - rpcServer.rpc("eth_getLogs") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_getLogs") - rpcServer.rpc("eth_newBlockFilter") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_newBlockFilter") - rpcServer.rpc("eth_newFilter") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_newFilter") - rpcServer.rpc("eth_newPendingTransactionFilter") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_newPendingTransactionFilter") - rpcServer.rpc("eth_pendingTransactions") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_pendingTransactions") - rpcServer.rpc("eth_syncing") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_syncing") - rpcServer.rpc("eth_uninstallFilter") do (): discard + rpcServerWithProxy.registerProxyMethod("eth_uninstallFilter")