2018-06-20 17:27:32 +00:00
|
|
|
# Nimbus
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2018-06-20 17:27:32 +00:00
|
|
|
# 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
|
2023-12-08 09:35:50 +00:00
|
|
|
eth/common,
|
2022-08-26 09:36:04 +00:00
|
|
|
stint, json_rpc/server, json_rpc/errors,
|
|
|
|
eth/p2p, eth/p2p/enode,
|
2023-12-08 09:35:50 +00:00
|
|
|
../config,
|
|
|
|
../beacon/web3_eth_conv,
|
|
|
|
web3/conversions
|
2021-05-19 09:35:13 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-05-19 09:35:13 +00:00
|
|
|
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
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
NodePorts.useDefaultSerializationIn JrpcConv
|
|
|
|
NodeInfo.useDefaultSerializationIn JrpcConv
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc setupCommonRpc*(node: EthereumNode, conf: NimbusConf, server: RpcServer) =
|
2018-06-20 17:27:32 +00:00
|
|
|
server.rpc("web3_clientVersion") do() -> string:
|
2021-09-11 14:58:01 +00:00
|
|
|
result = conf.agentString
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
server.rpc("web3_sha3") do(data: seq[byte]) -> Web3Hash:
|
|
|
|
result = w3Hash(keccakHash(data))
|
2020-07-22 16:51:26 +00:00
|
|
|
|
|
|
|
server.rpc("net_version") do() -> string:
|
2021-09-16 15:59:46 +00:00
|
|
|
result = $conf.networkId
|
2020-07-22 16:51:26 +00:00
|
|
|
|
|
|
|
server.rpc("net_listening") do() -> bool:
|
2022-08-26 09:36:04 +00:00
|
|
|
let numPeers = node.numPeers
|
2021-09-11 14:58:01 +00:00
|
|
|
result = numPeers < conf.maxPeers
|
2020-07-22 16:51:26 +00:00
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
server.rpc("net_peerCount") do() -> Web3Quantity:
|
2022-08-26 09:36:04 +00:00
|
|
|
let peerCount = uint node.numPeers
|
2023-12-08 09:35:50 +00:00
|
|
|
result = w3Qty(peerCount)
|
2021-05-19 09:35:13 +00:00
|
|
|
|
2023-10-03 12:44:08 +00:00
|
|
|
server.rpc("admin_nodeInfo") do() -> NodeInfo:
|
|
|
|
let
|
|
|
|
enode = toENode(node)
|
|
|
|
nodeId = toNodeId(node.keys.pubkey)
|
|
|
|
nodeInfo = NodeInfo(
|
|
|
|
id: nodeId.toHex,
|
|
|
|
name: conf.agentString,
|
|
|
|
enode: $enode,
|
|
|
|
ip: $enode.address.ip,
|
|
|
|
ports: NodePorts(
|
|
|
|
discovery: $enode.address.udpPort,
|
|
|
|
listener: $enode.address.tcpPort
|
|
|
|
)
|
2021-05-19 09:35:13 +00:00
|
|
|
)
|
2022-06-07 10:20:02 +00:00
|
|
|
|
2023-10-03 12:44:08 +00:00
|
|
|
return nodeInfo
|
|
|
|
|
|
|
|
server.rpc("admin_addPeer") do(enode: string) -> bool:
|
2022-06-07 10:20:02 +00:00
|
|
|
var res = ENode.fromString(enode)
|
|
|
|
if res.isOk:
|
2022-08-26 09:36:04 +00:00
|
|
|
asyncSpawn node.connectToNode(res.get())
|
2022-06-07 10:20:02 +00:00
|
|
|
return true
|
|
|
|
raise (ref InvalidRequest)(code: -32602, msg: "Invalid ENode")
|