2018-06-20 17:27:32 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2018-07-26 21:48:01 +00:00
|
|
|
import
|
2021-05-19 09:35:13 +00:00
|
|
|
std/[strutils, tables],
|
2019-02-05 19:15:50 +00:00
|
|
|
nimcrypto, eth/common as eth_common, stint, json_rpc/server,
|
2021-05-19 09:35:13 +00:00
|
|
|
eth/p2p, eth/p2p/enode,
|
|
|
|
../config, ./hexstrings
|
|
|
|
|
|
|
|
type
|
|
|
|
NodePorts = object
|
|
|
|
discovery: string
|
|
|
|
listener : string
|
|
|
|
|
|
|
|
NodeInfo = object
|
|
|
|
id : string # UInt256 hex
|
|
|
|
name : string
|
|
|
|
enode : string # Enode string
|
|
|
|
ip : string # address string
|
|
|
|
ports : NodePorts
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
proc setupCommonRPC*(node: EthereumNode, server: RpcServer) =
|
2018-06-20 17:27:32 +00:00
|
|
|
server.rpc("web3_clientVersion") do() -> string:
|
2019-01-15 15:25:14 +00:00
|
|
|
result = NimbusIdent
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2018-08-22 21:40:05 +00:00
|
|
|
server.rpc("web3_sha3") do(data: HexDataStr) -> string:
|
2019-04-24 13:35:29 +00:00
|
|
|
var rawdata = nimcrypto.fromHex(data.string[2 .. ^1])
|
2018-06-20 17:27:32 +00:00
|
|
|
result = "0x" & $keccak_256.digest(rawdata)
|
2020-07-22 16:51:26 +00:00
|
|
|
|
|
|
|
server.rpc("net_version") do() -> string:
|
|
|
|
let conf = getConfiguration()
|
|
|
|
result = $conf.net.networkId
|
|
|
|
|
|
|
|
server.rpc("net_listening") do() -> bool:
|
|
|
|
let conf = getConfiguration()
|
|
|
|
let numPeers = node.peerPool.connectedNodes.len
|
|
|
|
result = numPeers < conf.net.maxPeers
|
|
|
|
|
|
|
|
server.rpc("net_peerCount") do() -> HexQuantityStr:
|
|
|
|
let peerCount = uint node.peerPool.connectedNodes.len
|
|
|
|
result = encodeQuantity(peerCount)
|
2021-05-19 09:35:13 +00:00
|
|
|
|
|
|
|
server.rpc("net_nodeInfo") do() -> NodeInfo:
|
|
|
|
let enode = toEnode(node)
|
|
|
|
result = NodeInfo(
|
|
|
|
id: node.discovery.thisNode.id.toHex,
|
|
|
|
name: NimbusIdent,
|
|
|
|
enode: $enode,
|
|
|
|
ip: $enode.address.ip,
|
|
|
|
ports: NodePorts(
|
|
|
|
discovery: $enode.address.udpPort,
|
|
|
|
listener: $enode.address.tcpPort
|
|
|
|
)
|
|
|
|
)
|