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_getBlockByHash(data: EthHashStr, fullTransactions: bool): Option[BlockObject]
proc eth_getBlockByNumber(quantityTag: string, 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] proc eth_getLogs(filterOptions: FilterOptions): seq[FilterLog]

View File

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

View File

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

View File

@ -8,7 +8,7 @@
# those terms. # those terms.
import import
times, tables, std/[times, tables, typetraits],
json_rpc/rpcserver, hexstrings, stint, stew/byteutils, json_rpc/rpcserver, hexstrings, stint, stew/byteutils,
json_serialization, web3/conversions, json_serialization/std/options, json_serialization, web3/conversions, json_serialization/std/options,
eth/common/eth_types_json_serialization, eth/common/eth_types_json_serialization,
@ -30,7 +30,9 @@ import
type cast to avoid extra processing. 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 = proc getStateDB(header: BlockHeader): ReadOnlyStateDB =
## Retrieves the account db from canonical head ## 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 some($n.version)
return none(string) return none(string)
server.rpc("eth_chainId") do() -> HexQuantityStr:
return encodeQuantity(distinctBase(chain.config.chainId))
server.rpc("eth_syncing") do() -> JsonNode: server.rpc("eth_syncing") do() -> JsonNode:
## Returns SyncObject or false when not syncing. ## Returns SyncObject or false when not syncing.
# TODO: make sure we are 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_peerCount(): HexQuantityStr
proc net_listening(): bool proc net_listening(): bool
proc eth_protocolVersion(): string proc eth_protocolVersion(): string
proc eth_chainId(): HexQuantityStr
proc eth_syncing(): JsonNode proc eth_syncing(): JsonNode
proc eth_coinbase(): EthAddressStr proc eth_coinbase(): EthAddressStr
proc eth_mining(): bool proc eth_mining(): bool

View File

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