2021-11-24 07:45:55 +00:00
|
|
|
# Nimbus
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2021-2023 Status Research & Development GmbH
|
2021-11-24 07:45:55 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-11-24 07:45:55 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/sequtils,
|
2021-11-29 09:39:37 +00:00
|
|
|
json_rpc/[rpcproxy, rpcserver], stew/byteutils,
|
2021-11-24 07:45:55 +00:00
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
|
|
|
./rpc_types
|
|
|
|
|
2021-11-29 09:39:37 +00:00
|
|
|
export rpc_types # tasty sandwich
|
|
|
|
|
|
|
|
type
|
2021-12-03 08:51:25 +00:00
|
|
|
PongResponse* = object
|
2021-11-29 09:39:37 +00:00
|
|
|
enrSeq: uint64
|
|
|
|
recipientIP: string
|
|
|
|
recipientPort: uint16
|
|
|
|
|
|
|
|
proc installDiscoveryApiHandlers*(rpcServer: RpcServer|RpcProxy,
|
2023-01-31 12:38:08 +00:00
|
|
|
d: discv5_protocol.Protocol) =
|
2021-11-24 07:45:55 +00:00
|
|
|
## Discovery v5 JSON-RPC API such as defined here:
|
2022-12-15 15:24:23 +00:00
|
|
|
## https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
|
2021-11-24 07:45:55 +00:00
|
|
|
|
2021-11-29 09:39:37 +00:00
|
|
|
rpcServer.rpc("discv5_nodeInfo") do() -> NodeInfo:
|
2021-11-24 07:45:55 +00:00
|
|
|
return d.routingTable.getNodeInfo()
|
|
|
|
|
2021-11-29 09:39:37 +00:00
|
|
|
rpcServer.rpc("discv5_updateNodeInfo") do(
|
|
|
|
kvPairs: seq[(string, string)]) -> NodeInfo:
|
2022-12-15 15:24:23 +00:00
|
|
|
# TODO: Not according to spec, as spec parameters are weird.
|
|
|
|
# It is currently as in
|
|
|
|
# https://ddht.readthedocs.io/en/latest/jsonrpc.html#discv5-updatenodeinfo
|
2021-11-29 09:39:37 +00:00
|
|
|
let enrFields = kvPairs.map(
|
2023-01-26 12:37:19 +00:00
|
|
|
proc(n: (string, string)): (string, seq[byte]) {.raises: [ValueError].} =
|
2021-11-29 09:39:37 +00:00
|
|
|
(n[0], hexToSeqByte(n[1]))
|
|
|
|
)
|
|
|
|
let updated = d.updateRecord(enrFields)
|
|
|
|
if updated.isErr():
|
|
|
|
raise newException(ValueError, $updated.error)
|
|
|
|
|
|
|
|
return d.routingTable.getNodeInfo()
|
|
|
|
|
2022-12-15 15:24:23 +00:00
|
|
|
rpcServer.rpc("discv5_routingTableInfo") do() -> RoutingTableInfo:
|
|
|
|
return getRoutingTableInfo(d.routingTable)
|
|
|
|
|
|
|
|
rpcServer.rpc("discv5_addEnr") do(enr: Record) -> bool:
|
|
|
|
let nodeRes = newNode(enr)
|
|
|
|
if nodeRes.isOk():
|
|
|
|
let node = nodeRes.get()
|
|
|
|
let res = d.addNode(node)
|
|
|
|
d.routingTable.setJustSeen(node)
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Failed creating Node from ENR")
|
|
|
|
|
2022-02-02 21:48:33 +00:00
|
|
|
rpcServer.rpc("discv5_addEnrs") do(enrs: seq[Record]) -> bool:
|
2022-12-15 15:24:23 +00:00
|
|
|
# Note: unspecified RPC, but useful for our local testnet test
|
2022-02-02 21:48:33 +00:00
|
|
|
# TODO: We could also adjust the API of addNode & newNode to accept a seen
|
|
|
|
# parameter, but perhaps only if that makes sense on other locations in
|
|
|
|
# discv5/portal that are not testing/debug related.
|
|
|
|
for enr in enrs:
|
|
|
|
let nodeRes = newNode(enr)
|
|
|
|
if nodeRes.isOk():
|
|
|
|
let node = nodeRes.get()
|
|
|
|
discard d.addNode(node)
|
|
|
|
d.routingTable.setJustSeen(node)
|
|
|
|
|
|
|
|
return true
|
2021-11-29 09:39:37 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("discv5_getEnr") do(nodeId: NodeId) -> Record:
|
|
|
|
let node = d.getNode(nodeId)
|
|
|
|
if node.isSome():
|
|
|
|
return node.get().record
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not in local routing table.")
|
|
|
|
|
|
|
|
rpcServer.rpc("discv5_deleteEnr") do(nodeId: NodeId) -> bool:
|
|
|
|
# TODO: Adjust `removeNode` to accept NodeId as param and to return bool.
|
|
|
|
let node = d.getNode(nodeId)
|
|
|
|
if node.isSome():
|
|
|
|
d.routingTable.removeNode(node.get())
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not in local routing table.")
|
|
|
|
|
2022-12-15 15:24:23 +00:00
|
|
|
rpcServer.rpc("discv5_lookupEnr") do(
|
|
|
|
nodeId: NodeId) -> Record:
|
2022-12-16 07:49:18 +00:00
|
|
|
# TODO: Not according to spec, missing optional enrSeq
|
|
|
|
# Can add `enrSeq: Option[uint64]` as parameter but Option appears to be
|
2022-12-15 15:24:23 +00:00
|
|
|
# not implemented as an option parameter in nim-json-rpc?
|
2021-11-29 09:39:37 +00:00
|
|
|
let lookup = await d.resolve(nodeId)
|
|
|
|
if lookup.isSome():
|
|
|
|
return lookup.get().record
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Record not found in DHT lookup.")
|
|
|
|
|
|
|
|
rpcServer.rpc("discv5_ping") do(enr: Record) -> PongResponse:
|
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
|
|
|
pong = await d.ping(node)
|
|
|
|
|
|
|
|
if pong.isErr():
|
|
|
|
raise newException(ValueError, $pong.error)
|
|
|
|
else:
|
|
|
|
let p = pong.get()
|
|
|
|
return PongResponse(
|
|
|
|
enrSeq: p.enrSeq,
|
|
|
|
recipientIP: $p.ip,
|
|
|
|
recipientPort: p.port
|
|
|
|
)
|
|
|
|
|
2021-12-13 13:12:51 +00:00
|
|
|
rpcServer.rpc("discv5_findNode") do(
|
2021-11-29 09:39:37 +00:00
|
|
|
enr: Record, distances: seq[uint16]) -> seq[Record]:
|
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
|
|
|
nodes = await d.findNode(node, distances)
|
|
|
|
if nodes.isErr():
|
|
|
|
raise newException(ValueError, $nodes.error)
|
|
|
|
else:
|
|
|
|
return nodes.get().map(proc(n: Node): Record = n.record)
|
|
|
|
|
2022-12-15 15:24:23 +00:00
|
|
|
rpcServer.rpc("discv5_talkReq") do(
|
|
|
|
enr: Record, protocol, payload: string) -> string:
|
2021-11-29 09:39:37 +00:00
|
|
|
let
|
|
|
|
node = toNodeWithAddress(enr)
|
2022-09-10 19:00:27 +00:00
|
|
|
talkresp = await d.talkReq(
|
2021-11-29 09:39:37 +00:00
|
|
|
node, hexToSeqByte(protocol), hexToSeqByte(payload))
|
|
|
|
if talkresp.isErr():
|
|
|
|
raise newException(ValueError, $talkresp.error)
|
|
|
|
else:
|
|
|
|
return talkresp.get().toHex()
|
2021-11-24 07:45:55 +00:00
|
|
|
|
2023-04-13 16:39:46 +00:00
|
|
|
rpcServer.rpc("discv5_recursiveFindNodes") do(nodeId: NodeId) -> seq[Record]:
|
2022-12-15 15:24:23 +00:00
|
|
|
let discovered = await d.lookup(nodeId)
|
2021-11-29 09:39:37 +00:00
|
|
|
return discovered.map(proc(n: Node): Record = n.record)
|