Add eth_chainId rpc to Nimbus and Fluffy (#1219)

This commit is contained in:
Kim De Mey 2022-09-10 15:05:32 +02:00 committed by GitHub
parent 011f44abea
commit 4a396bdd25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 14 deletions

View File

@ -1,3 +1,5 @@
proc eth_chaindId(): HexQuantityStr
proc eth_getBlockByHash(data: EthHashStr, fullTransactions: bool): Option[BlockObject]
proc eth_getBlockByNumber(quantityTag: string, fullTransactions: bool): Option[BlockObject]
proc eth_getBlockTransactionCountByHash(data: EthHashStr): HexQuantityStr
proc eth_getLogs(filterOptions: FilterOptions): seq[FilterLog]

View File

@ -8,12 +8,12 @@
{.push raises: [Defect].}
import
std/[times, sequtils],
std/[times, sequtils, typetraits],
json_rpc/[rpcproxy, rpcserver], stew/byteutils,
web3/conversions, # sigh, for FixedBytes marshalling
eth/[common/eth_types, rlp],
../../nimbus/rpc/[rpc_types, hexstrings, filters],
../../nimbus/transaction,
../../nimbus/[transaction, chain_config],
../network/history/[history_network, history_content]
# Subset of Eth JSON-RPC API: https://eth.wiki/json-rpc/API
@ -116,7 +116,7 @@ proc installEthApiHandlers*(
rpcServerWithProxy.registerProxyMethod("eth_call")
rpcServerWithProxy.registerProxyMethod("eth_chainId")
# rpcServerWithProxy.registerProxyMethod("eth_chainId")
rpcServerWithProxy.registerProxyMethod("eth_estimateGas")
@ -188,6 +188,10 @@ proc installEthApiHandlers*(
# Supported API through the Portal Network
rpcServerWithProxy.rpc("eth_chainId") do() -> HexQuantityStr:
# The Portal Network can only support MainNet at the moment
return encodeQuantity(distinctBase(MainNet.ChainId))
rpcServerWithProxy.rpc("eth_getBlockByHash") do(
data: EthHashStr, fullTransactions: bool) -> Option[BlockObject]:
## Returns information about a block by hash.
@ -207,17 +211,18 @@ proc installEthApiHandlers*(
let (header, body) = blockRes.unsafeGet()
return some(BlockObject.init(header, body))
# TODO add test to local testnet, it requires addin proper handling accumulators
# in testnet
rpcServerWithProxy.rpc("eth_getBlockByNumber") do(quantityTag: string, fullTransactions: bool) -> Option[BlockObject]:
# TODO for now support only numeric queries, as it is not obvious how to retrieve
# pending or even latest block.
# TODO: add test to local testnet, it requires activating accumulator
# in testnet script
rpcServerWithProxy.rpc("eth_getBlockByNumber") do(
quantityTag: string, fullTransactions: bool) -> Option[BlockObject]:
# TODO: for now support only numeric queries, as it is not obvious how to
# retrieve pending or even latest block.
if not isValidHexQuantity(quantityTag):
raise newException(ValueError, "Provided tag should be valid hex number")
let blockNum = fromHex(UInt256, quantityTag)
let blockResult = await historyNetwork.getBlock(1'u16, blockNum)
let
blockNumber = fromHex(UInt256, quantityTag)
blockResult = await historyNetwork.getBlock(1'u16, blockNumber)
if blockResult.isOk():
let maybeBlock = blockResult.get()

View File

@ -368,7 +368,7 @@ proc toFork*(c: ChainConfig, number: BlockNumber): Fork =
elif number >= c.homesteadBlock: FkHomestead
else: FkFrontier
proc chainConfigForNetwork(id: NetworkId): ChainConfig =
proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
# For some public networks, NetworkId and ChainId value are identical
# but that is not always the case

View File

@ -8,7 +8,7 @@
# those terms.
import
times, tables,
std/[times, tables, typetraits],
json_rpc/rpcserver, hexstrings, stint, stew/byteutils,
json_serialization, web3/conversions, json_serialization/std/options,
eth/common/eth_types_json_serialization,
@ -30,7 +30,9 @@ import
type cast to avoid extra processing.
]#
proc setupEthRpc*(node: EthereumNode, ctx: EthContext, chain: BaseChainDB, txPool: TxPoolRef, server: RpcServer) =
proc setupEthRpc*(
node: EthereumNode, ctx: EthContext, chain: BaseChainDB,
txPool: TxPoolRef, server: RpcServer) =
proc getStateDB(header: BlockHeader): ReadOnlyStateDB =
## Retrieves the account db from canonical head
@ -53,6 +55,9 @@ proc setupEthRpc*(node: EthereumNode, ctx: EthContext, chain: BaseChainDB, txPoo
return some($n.version)
return none(string)
server.rpc("eth_chainId") do() -> HexQuantityStr:
return encodeQuantity(distinctBase(chain.config.chainId))
server.rpc("eth_syncing") do() -> JsonNode:
## Returns SyncObject or false when not syncing.
# TODO: make sure we are not syncing

View File

@ -19,6 +19,7 @@ proc net_version(): string
proc net_peerCount(): HexQuantityStr
proc net_listening(): bool
proc eth_protocolVersion(): string
proc eth_chainId(): HexQuantityStr
proc eth_syncing(): JsonNode
proc eth_coinbase(): EthAddressStr
proc eth_mining(): bool

View File

@ -216,6 +216,10 @@ proc rpcMain*() =
# obvious. Just change this number.
check res == $ethVersion
test "eth_chainId":
let res = await client.eth_chainId()
check res == encodeQuantity(distinctBase(chain.config.chainId))
test "eth_syncing":
let res = await client.eth_syncing()
if res.kind == JBool: