From 4ce9b5883c0707bc061d67f1c9679cc7dc7e26d5 Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Fri, 18 Jun 2021 19:20:48 +0200 Subject: [PATCH] Add Portal eth json-rpc stubs and Portal content file (#720) * Bump nim-json-rpc module * Add eth json rpc stubs * Add portal content file --- nlpn/content.nim | 48 +++++++++++++++++++++ nlpn/nlpn.nim | 11 ++++- nlpn/rpc/eth_api.nim | 99 ++++++++++++++++++++++++++++++++++++++++++++ vendor/nim-json-rpc | 2 +- 4 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 nlpn/content.nim create mode 100644 nlpn/rpc/eth_api.nim diff --git a/nlpn/content.nim b/nlpn/content.nim new file mode 100644 index 000000000..e502fc68d --- /dev/null +++ b/nlpn/content.nim @@ -0,0 +1,48 @@ +# Nimbus +# Copyright (c) 2021 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +# https://github.com/ethereum/stateless-ethereum-specs/blob/master/state-network.md#content + +{.push raises: [Defect].} + +import + nimcrypto/[sha2, hash], eth/ssz/ssz_serialization + +type + ByteList* = List[byte, 2048] + + ContentType* = enum + Account = 0x01 + ContractStorage = 0x02 + ContractBytecode = 0x03 + + NetworkId* = uint16 + + NodeHash* = List[byte, 32] # MDigest[32 * 8] - sha256 + + CodeHash* = List[byte, 32] # MDigest[32 * 8] - keccak256 + + Address* = List[byte, 20] + + ContentKey* = object + # TODO: How shall we deal with the different ContentKey structures? + networkId: NetworkId + contentType: ContentType + address: Address + triePath: ByteList + nodeHash: NodeHash + + ContentId* = MDigest[32 * 8] + +template toSszType*(x: auto): auto = + mixin toSszType + + when x is ContentType: uint8(x) + else: x + +func toContentId*(contentKey: ContentKey): ContentId = + sha2.sha_256.digest(SSZ.encode(contentKey)) diff --git a/nlpn/nlpn.nim b/nlpn/nlpn.nim index 473344599..91798fd78 100644 --- a/nlpn/nlpn.nim +++ b/nlpn/nlpn.nim @@ -9,11 +9,11 @@ import confutils, confutils/std/net, chronicles, chronicles/topics_registry, - chronos, metrics, metrics/chronos_httpserver, + chronos, metrics, metrics/chronos_httpserver, json_rpc/servers/httpserver, eth/keys, eth/net/nat, eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/portal/protocol as portal_protocol, - ./conf + ./conf, ./rpc/eth_api proc run(config: PortalConf) {.raises: [CatchableError, Defect].} = let @@ -51,6 +51,13 @@ proc run(config: PortalConf) {.raises: [CatchableError, Defect].} = # TODO: Ideally we don't have the Exception here except Exception as exc: raiseAssert exc.msg + if config.rpcEnabled: + let + ta = initTAddress(config.rpcAddress, config.rpcPort) + rpcHttpServer = newRpcHttpServer([ta]) + rpcHttpServer.installEthApiHandlers() + rpcHttpServer.start() + d.start() runForever() diff --git a/nlpn/rpc/eth_api.nim b/nlpn/rpc/eth_api.nim new file mode 100644 index 000000000..67839e2af --- /dev/null +++ b/nlpn/rpc/eth_api.nim @@ -0,0 +1,99 @@ +# Nimbus +# Copyright (c) 2021 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +{.push raises: [Defect].} + +import + json_rpc/rpcserver + +# Subset of Eth JSON-RPC API: https://eth.wiki/json-rpc/API +# Supported subset will eventually be found here: +# https://github.com/ethereum/stateless-ethereum-specs/blob/master/portal-network.md#json-rpc-api +# +# In order to already support these calls before every part of the Portal +# Network is up, one plan is to get the data directly from an external client +# through RPC calls. Practically just playing a proxy to that client. +# 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) + {.raises: [Defect, CatchableError].} = + + # Supported API + rpcServer.rpc("eth_blockNumber") do (): discard + + rpcServer.rpc("eth_call") do (): discard + + rpcServer.rpc("eth_chainId") do (): discard + + rpcServer.rpc("eth_estimateGas") do (): discard + + rpcServer.rpc("eth_feeHistory") do (): discard + + rpcServer.rpc("eth_getBalance") do (): discard + + rpcServer.rpc("eth_getBlockByHash") do (): discard + + rpcServer.rpc("eth_getBlockByNumber") do (): discard + + rpcServer.rpc("eth_getBlockTransactionCountByHash") do (): discard + + rpcServer.rpc("eth_getBlockTransactionCountByNumber") do (): discard + + rpcServer.rpc("eth_getCode") do (): discard + + rpcServer.rpc("eth_getRawTransactionByHash") do (): discard + + rpcServer.rpc("eth_getRawTransactionByBlockHashAndIndex") do (): discard + + rpcServer.rpc("eth_getRawTransactionByBlockNumberAndIndex") do (): discard + + rpcServer.rpc("eth_getStorageAt") do (): discard + + rpcServer.rpc("eth_getTransactionByBlockHashAndIndex") do (): discard + + rpcServer.rpc("eth_getTransactionByBlockNumberAndIndex") do (): discard + + rpcServer.rpc("eth_getTransactionByHash") do (): discard + + rpcServer.rpc("eth_getTransactionCount") do (): discard + + rpcServer.rpc("eth_getTransactionReceipt") do (): discard + + rpcServer.rpc("eth_getUncleByBlockHashAndIndex") do (): discard + + rpcServer.rpc("eth_getUncleByBlockNumberAndIndex") do (): discard + + rpcServer.rpc("eth_getUncleCountByBlockHash") do (): discard + + rpcServer.rpc("eth_getUncleCountByBlockNumber") do (): discard + + rpcServer.rpc("eth_getProof") do (): discard + + rpcServer.rpc("eth_sendRawTransaction") do (): discard + + # Optional API + + rpcServer.rpc("eth_gasPrice") do (): discard + + rpcServer.rpc("eth_getFilterChanges") do (): discard + + rpcServer.rpc("eth_getFilterLogs") do (): discard + + rpcServer.rpc("eth_getLogs") do (): discard + + rpcServer.rpc("eth_newBlockFilter") do (): discard + + rpcServer.rpc("eth_newFilter") do (): discard + + rpcServer.rpc("eth_newPendingTransactionFilter") do (): discard + + rpcServer.rpc("eth_pendingTransactions") do (): discard + + rpcServer.rpc("eth_syncing") do (): discard + + rpcServer.rpc("eth_uninstallFilter") do (): discard diff --git a/vendor/nim-json-rpc b/vendor/nim-json-rpc index 4eb39203e..22c342bcc 160000 --- a/vendor/nim-json-rpc +++ b/vendor/nim-json-rpc @@ -1 +1 @@ -Subproject commit 4eb39203ebd391c77d16a1c387dc8a6b7d90bc69 +Subproject commit 22c342bcc11515c69d59b53819bd38ab813d9b93