Have one generic installPortalApiHandlers to be used for different Portal networks (#893)

This commit is contained in:
Kim De Mey 2021-11-24 14:53:01 +01:00 committed by GitHub
parent 77cc9b6215
commit 5f4ea7822c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 16 deletions

View File

@ -103,8 +103,8 @@ proc run(config: PortalConf) {.raises: [CatchableError, Defect].} =
var rpcHttpServerWithProxy = RpcProxy.new([ta], config.proxyUri)
rpcHttpServerWithProxy.installEthApiHandlers()
rpcHttpServerWithProxy.installDiscoveryApiHandlers(d)
rpcHttpServerWithProxy.installPortalStateApiHandlers(stateNetwork.portalProtocol)
rpcHttpServerWithProxy.installPortalHistoryApiHandlers(historyNetwork.portalProtocol)
rpcHttpServerWithProxy.installPortalApiHandlers(stateNetwork.portalProtocol, "state")
rpcHttpServerWithProxy.installPortalApiHandlers(stateNetwork.portalProtocol, "history")
# 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()

View File

@ -12,26 +12,23 @@ import
../network/wire/portal_protocol,
./rpc_types
# TODO:
# Trying to make this dynamic by passing in a network sub string results in:
export rpcserver
# Note:
# Using a string for the network parameter will give an error in the rpc macro:
# Error: Invalid node kind nnkInfix for macros.`$`
proc installPortalStateApiHandlers*(rpcServerWithProxy: var RpcProxy, p: PortalProtocol)
# Using a static string works but some sandwich problem seems to be happening,
# as the proc becomes generic, where the rpc macro from router.nim can no longer
# be found, which is why we export rpcserver which should export router.
proc installPortalApiHandlers*(
rpcServerWithProxy: var RpcProxy, p: PortalProtocol, network: static string)
{.raises: [Defect, CatchableError].} =
## Portal routing table and portal wire json-rpc API is not yet defined but
## will look something similar as what exists here now:
## https://github.com/ethereum/portal-network-specs/pull/88
rpcServerWithProxy.rpc("portal_state_nodeInfo") do() -> NodeInfo:
rpcServerWithProxy.rpc("portal_" & network & "_nodeInfo") do() -> NodeInfo:
return p.routingTable.getNodeInfo()
rpcServerWithProxy.rpc("portal_state_routingTableInfo") do() -> RoutingTableInfo:
return getRoutingTableInfo(p.routingTable)
proc installPortalHistoryApiHandlers*(rpcServerWithProxy: var RpcProxy, p: PortalProtocol)
{.raises: [Defect, CatchableError].} =
rpcServerWithProxy.rpc("portal_history_nodeInfo") do() -> NodeInfo:
return p.routingTable.getNodeInfo()
rpcServerWithProxy.rpc("portal_history_routingTableInfo") do() -> RoutingTableInfo:
rpcServerWithProxy.rpc("portal_" & network & "_routingTableInfo") do() -> RoutingTableInfo:
return getRoutingTableInfo(p.routingTable)